Testing Posters IP Address

Discuss news styles, css code, and other Coranto customizations in here...the possibilities are endless, so have at it!

Moderator: Spunkmeyer

Testing Posters IP Address

Postby Astradyne » Sat Jun 24, 2006 1:52 am

Hi

I'm using Guestpost to allow items to be posted on the website to a "non-approved" category and wanted to put together some code that would check the IP address of the poster, and if it was within my network address range then automatically change the Category to it's approved equivalent and build the news.

Although I'm not a Perl programmer, I have been programming in business for over 20 years and figured I could work something out by creating an Addon and hooking in to the SaveNews_Pre hook.

The code I came up with for the addon, or at least the current incarnation of it, is:


Code: Select all
#! CRADDON 1
#! NAME UserExits
#! DESCRIPTION Exit point processing for various hooks
#! VERSION 1

# Identify the Addon to Coranto...

  my $addon = new Addon('UsrExits');

# Hook into the various exit points...

  $addon->hook('SaveNews_Pre', 'UsrExits_AutoPromote');

###############
# SUBROUTINES #
###############

sub UsrExits_AutoPromote {

  # If a guest posting has been made from inside the local network, then
  # automatically promote (approve) the post...

  if ($in{'uname'} eq 'guest_links') {

    $_ = $ENV{'REMOTE_ADDR'};
    if (/192\.168\.0/) {

      if ($in{'Category'} eq "guest_kids") {
        $in{'Category'} = "kids_links";
      }
      elsif ($in{'Category'} eq "guest_link") {
        $in{'Category'} = "Links";
      }
      elsif ($in{'Category'} eq "guest_download") {
        $in{'Category'} = "downloads";
      }
    }
  }

}


# End of Addon source...
1;


I'm posting from a PC with an IP address of 192.168.0.122 which I would expect to be picked up by the regular expression check, and then the $Category variable would get changed.

For some reason it appears that either the Regular Expression check or the appropriate Category code check isn't working and I can't for the life of me see why.

I've tried putting some Print statements in to see if I could see the value of $_ but that didn't help and I don't know where to begin to use the debugger.

Any help or advice would be most gratefully welcome.

Thanks

Jonathan
Astradyne
 
Posts: 36
Joined: Wed Jan 10, 2007 4:17 pm
Location: Cheshire, UK

Re: Testing Posters IP Address

Postby Parahead » Sat Jun 24, 2006 1:38 pm

astradyne wrote:For some reason it appears that either the Regular Expression check or the appropriate Category code check isn't working and I can't for the life of me see why.
What happens? Do you get an errormessage or "nothing happens"...

astradyne wrote:I've tried putting some Print statements in to see if I could see the value of $_ but that didn't help and I don't know where to begin to use the debugger.
In what sense did it not help? Didn't you get a print out or?

Maybe a insulting question, but have you actually enabled the addon once you have uploaded it to your Coranto installation? If "nothing happens" is the answer to both of my questions above that may be one reason why?
Yes, I am still around...
www.parahead.com/coranto/
User avatar
Parahead
 
Posts: 4837
Joined: Fri Jan 12, 2007 8:54 pm
Location: Stockholm - Sweden

Re: Testing Posters IP Address

Postby Astradyne » Sun Jun 25, 2006 12:39 am

Hi Parahead,

Thanks for the reply, I've managed to fumble through it and it seems to be working now.

It's not an insulting question - the number of times the solution is the obvious answer is unbelievable. The Addon was enabled, originally it kept falling over with compiler errors because of various syntax errors. Once they were ironed out and I'd changed my "if" statements from using "==" to using "eq" nothing appeared to be happening.

When I started putting in various Print statements to check the value of the variables, nothing was being displayed on the screen because of the speed with which Guestpost threw up the Thank You page.

After much trawling around various Perl sites, I managed to come across the "die" command and stuck that in at the end of my subroutine to prevent the actual post from happening and to allow me to see the Print results.

Basically, the $uname variable wasn't populated or had been initialised earlier on, and I was able to change the condition to $CurrentUser instead. This then worked and the items were posted with the non-guest categories that I wanted.

The next problem was that the news didn't rebuild (because I had the auto build turned off in the Guestpost config) so I've had to add an extra option into the Auto Build drop down list for "Local IP":
Code: Select all
      if ($CConfig{'guestpost_guestauto'}) { $guestpost_selval =
 "<select name=\"guestpost_guestauto\" size=\"1\"> <option value=\"1\">Yes (On)</option> <option value=\"0\">No (Off)</option> </option> <option value=\"2\" selected>Local IP</option></select>" ;}
   elsif (!$CConfig{'guestpost_guestauto'}) { $guestpost_selval = "<select name=\"guestpost_guestauto\" size=\"1\"> <option value=\"0\" selected>No (Off)</option> <option value=\"1\">Yes (On)</option><option value=\"2\">Local IP</option> </select>" ;}

And then added the following immediately after the check for whether or not BuildNews was to be run:
Code: Select all
if ($CO_guest_build==2) {              
  $_ = $ENV{'REMOTE_ADDR'};              
  if (/192\.168\.0/) {                 
      BuildNews();                 
  }               
}

I know I shouldn't hard code the IP range, but I didn't want to mod the main routine too much. I figured that this was an easy way of allowing automatic approval with some level of security.

All the best

Jonathan
Astradyne
 
Posts: 36
Joined: Wed Jan 10, 2007 4:17 pm
Location: Cheshire, UK

Re: Testing Posters IP Address

Postby Parahead » Mon Jun 26, 2006 6:31 pm

astradyne wrote:It's not an insulting question - the number of times the solution is the obvious answer is unbelievable.
Yeah, we both know that, but surprisingly often I get a snide comment from the person seeking help after such questions so I have reverted to the better safe than sorry approach... ;-) Good you got things working and thanks for the report about how you did it.

astradyne wrote:I know I shouldn't hard code the IP range, but I didn't want to mod the main routine too much. I figured that this was an easy way of allowing automatic approval with some level of security.
Maybe you could improve the check slightly by changing the check to the below to make sure the pattern is matched at the start and also that the last zero is followed by a dot (somewhat redundant though):
Code: Select all
 if (/^192\.168\.0\./) {
Yes, I am still around...
www.parahead.com/coranto/
User avatar
Parahead
 
Posts: 4837
Joined: Fri Jan 12, 2007 8:54 pm
Location: Stockholm - Sweden

Re: Testing Posters IP Address

Postby Astradyne » Mon Jun 26, 2006 8:10 pm

Parahead wrote:[Maybe you could improve the check slightly by changing the check to the below to make sure the pattern is matched at the start and also that the last zero is followed by a dot (somewhat redundant though):
Code: Select all
 if (/^192\.168\.0\./) {


I thought about the ^ character in the regular expression and the ending dot, but dropped them when I was trying to find out what was going wrong and nothing seemed to be working. I even tried a pattern with '000' in case that was affecting it :oops: I will change the pattern though for the sake of clarity and completeness.

I've been to the library today and taken out 5 books on Perl programming to help me get a better start on the language, so there'll probably be more posts in the near future :wink:

Thanks again

Jonathan
Astradyne
 
Posts: 36
Joined: Wed Jan 10, 2007 4:17 pm
Location: Cheshire, UK


Return to Customization

Who is online

Users browsing this forum: No registered users and 2 guests

cron