We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Intranet Plugin

Development and Programming

Chris Jennings's avatar
Chris Jennings
87 posts
17 years ago
Chris Jennings's avatar Chris Jennings

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'}"}
You can see this content
{if:else}
You are not allowed to see this content
{/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?

       
J.B.'s avatar
J.B.
79 posts
17 years ago
J.B.'s avatar J.B.

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}
                   This is a restricted article.  You must be <a href="https://support.dalton.missouri.edu/index.php/login">logged in</a> before you can view it.
                {/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 ('
Our ip is: ' . $our_ip );
        echo ('
allowed ranges are: ' . $allowed_ips);
        echo ('
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('
preg_replace1:' . $ipregex . ' from ' . $IP[$i]);
        $iptmp = $ipregex;
        $ipregex = preg_replace('/\*/', '.*', $ipregex);
        echo('
preg_replace2: ' . $ipregex . ' == ' . $iptmp);
        echo('
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
       
Chris Jennings's avatar
Chris Jennings
87 posts
17 years ago
Chris Jennings's avatar Chris Jennings

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

       
J.B.'s avatar
J.B.
79 posts
17 years ago
J.B.'s avatar J.B.

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!

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.