Intranet Plugin
Posted: 27 December 2007 10:22 AM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  217
Joined  11-12-2002

Here is a little problem that others may have encountered.

You have a site built with EE and it is within an enterprise, or, in my case a University. A good part of the site is for public view and some is only visible to logged in users, However, they also have an Intranet where access is limited to those within the local network. Now that they see the wonderful INTERNET site built with ExpressionEngine, they now want the same features and functionality for an INTRANET site only visible within the network. They don’t want Intranet users to have to login. They just want it limited by IP address. Now, you could achieve this with .htaccess, but there are problems with this method, so I have created a plugin to achieve the required functionality.

I am not really sure yet if I have done this the right way, so I post here for your perusal and hopefully there may be someone who could check it out? Maybe there is an easier way even!

Here is the way to use it:

{if "{exp:intranet ips='129.84.*.*|211.109.238.74'}"}
<p>You can see this content</p>
{if:else}
<p>You are not allowed to see this content</p>
{/if}

As you may observe, the plugin simply returns true or false depending on whether the user’s IP fits within the give IPs given as a parameter with the plugin. There are some other examples given in the Plugin when you download it.

I have not been able to test fully yet and I do have one little problem:
On one server that is set up by colleagues, I cannot get the server’s IP address. Is there a PHP setting that turns off super globals?

File Attachments
pi.intranet.php.zip  (File Size: 3KB - Downloads: 41)
 Signature 

PageToScreen

Profile
 
 
Posted: 02 July 2008 05:35 PM   [ Ignore ]   [ # 1 ]  
Grad Student
Avatar
Rank
Total Posts:  87
Joined  04-12-2008

Chris, thanks for this plugin, this makes my life a lot easier!

However, I’m having some issues when using 2 different masks to allow, but maybe I’m missing something system.  My call that works is:

{if namespace == "Private"}
               {if logged_in}
                   {wiki
:page}
               {if
:elseif "{exp:intranet ips='10.0.*.*'}"}
                   {wiki
:page}
               {if
:else}
                   
<p>This is a restricted article.  You must be <a href="https://support.dalton.missouri.edu/index.php/login" alt="Log In">logged in</a> before you can view it.</p>
                
{/if}
            {if
:else}
                {wiki
:page}
            {
/if}

but if I add a second mask so the one line reads:

{if:elseif "{exp:intranet ips='10.0.*.*|192.168.*.*'}"}

it no longer works if viewing from a either class B’s.  I modified the plugin function with the following debug statements and have put the output for each.  Maybe you can point me to what I’m doing wrong.  I’m afraid I suck at Regex stuff.

function Intranet()
   
{
        
global $TMPL;
        
$IP = array();
        
$allowed_ips = "";
        
$your_ip = $_SERVER['REMOTE_ADDR'];
        
$our_ip = $_SERVER['SERVER_ADDR'];
        
$allowed_ips = $TMPL->fetch_param('ips');
        if(
$allowed_ips=="") {
        $allowed_ips
= $our_ip;
        
// change the server ip address into a subnet address
        
$allowed_ips = preg_replace('/([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/', '$1.$2.$3.*', $allowed_ips);
        
}
        
// Now split it into an array using a pad
        
$IP=explode('|',$allowed_ips);
        echo (
'Your IP address is: ' . $your_ip );
        echo (
'<br />Our ip is: ' . $our_ip );
        echo (
'<br />allowed ranges are: ' . $allowed_ips);
        echo (
'<br />explode IP are: ' . $IP[0] . ' and ' . $IP[1]);
        
//testing that correct IP address used in order
        //to access admin area...
        
for($i=0, $cnt=count($IP); $i<$cnt; $i++)
        
{
        $ipregex
= preg_replace('/\./', '\.', $IP[$i]);
        echo(
'<br />preg_replace1:' . $ipregex . ' from ' . $IP[$i]);
        
$iptmp = $ipregex;
        
$ipregex = preg_replace('/\*/', '.*', $ipregex);
        echo(
'<br />preg_replace2: ' . $ipregex . ' == ' . $iptmp);
        echo(
'<br />now matching ' . $ipregex . ' to ' . $your_ip);
        if(
preg_match('/'.$ipregex.'/', $your_ip)) {    
        $this
->return_data = 'true';
        
} else {
        $this
->return_data = '';
        
}
        }
    }

Output with 1 mask that succeeds:

Your IP address is: 10.0.0.185
Our ip is:
allowed ranges are: 10.0.*.*
explode IP are: 10.0.*.* and
preg_replace1:10\.0\.*\.* from 10.0.*.*
preg_replace2: 10\.0\..*\..* == 10\.0\.*\.*
now matching 10\.0\..*\..* to 10.0.0.185

Now output with 2 masks that fails (i.e. doesn’t show the page if from either range):

Your IP address is: 10.0.0.185
Our ip is:
allowed ranges are: 10.0.*.*|192.168.*.*
explode IP are: 10.0.*.* and 192.168.*.*
preg_replace1:10\.0\.*\.* from 10.0.*.*
preg_replace2: 10\.0\..*\..* == 10\.0\.*\.*
now matching 10\.0\..*\..* to 10.0.0.185
preg_replace1:192\.168\.*\.* from 192.168.*.*
preg_replace2: 192\.168\..*\..* == 192\.168\.*\.*
now matching 192\.168\..*\..* to 10.0.0.185

 Signature 

“You can’t fall off the floor” - Paul’s Law
“Brawndo, It’s what plants crave!” - Idiocracy

Profile
 
 
Posted: 03 July 2008 12:08 PM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  217
Joined  11-12-2002

Hi leejb

there was a bug in that first version…. I have replaced the file at the top of this thread with a new one.

I hope that solves the problem.

Chris J

 Signature 

PageToScreen

Profile
 
 
Posted: 04 July 2008 12:10 AM   [ Ignore ]   [ # 3 ]  
Grad Student
Avatar
Rank
Total Posts:  87
Joined  04-12-2008

I thought I made sure to download the latest, but obviously I goofed.  That was the problem.  Again, thanks for the plug, it’s saving me lots!

 Signature 

“You can’t fall off the floor” - Paul’s Law
“Brawndo, It’s what plants crave!” - Idiocracy

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 64916 Total Logged-in Users: 25
Total Topics: 81870 Total Anonymous Users: 22
Total Replies: 440151 Total Guests: 177
Total Posts: 522021    
Members ( View Memberlist )