Area51 @ 4seven.de

Mod Base and Testarea
It is currently 23 Feb 2025, 19:32

All times are UTC + 1 hour




Post new topic TOPIC_LOCKED  [ 1 post ] 
Author Message
PostPosted: 10 Jun 2012, 22:23 
Offline
Tiptop
Tiptop
User avatar

Joined: 11 Feb 2008, 11:49
Posts: 241
Some forums allowed for given subnets only

With this snippet you can allow to view given forums for given subnets only

Requested by: Knopper
Request topic: http://www.phpbb.com/community/viewtopi ... &t=2157326

Screens:

User IP not in subnet

Attachment:
forum_id_4_forbidden_false_ip_subnet.png
forum_id_4_forbidden_false_ip_subnet.png [ 34.41 KiB | Viewed 1758 times ]

User IP in subnet

Attachment:
forum_id_4_forbidden_right_ip_subnet.png
forum_id_4_forbidden_right_ip_subnet.png [ 53.69 KiB | Viewed 1757 times ]

Instruction

open

viewtopic.php

find

Code:
// Output the page           


before add

Code:
// Some forums allowed for given subnets only / 4seven / 2012

### CONFIG
// Put here the forum_ids, which will 
// be restricted for given ip subnets,
// eg. array(16,35,12,6);

   $forum_id_arr = array(4,5);

### CONFIG

// no forum, no action
if (isset($forum_id) && ($forum_id !== 0)){

// only, if forum is in array
if (in_array($forum_id, $forum_id_arr)){

// Thanks to Bill Grady
// and zend.framework
function ip_in_subnet($ip) {

### CONFIG
   // put in the subnet range (cidr),
   // eg. array("24.256.0.0/20");
 
   
// enter more ranges in this way 
   /*    array("24.45.0.0/08",
         "46.73.0.0/24",
         "54.25.0.0/20");
   */
 
 
   $source 
= array("56.41.144.0/20");

### CONFIG

    // if array or not, pass through
    foreach ($source as $line) {

        // Get the base and the bits from the CIDR
        list($base, $bits) = explode('/', $line);
       
        
// Now split it up into it's classes
        list($a, $b, $c, $d) = explode('.', $base);
       
        
// Now do some bit shifting/switching to convert to ints
        $i    = ($a << 24) + ($b << 16) + ( $c << 8 ) + $d;
        $mask = $bits == 0 ? 0: (~<< (32 - $bits));
       
        
// Here's our lowest int
        $low = $i & $mask;
       
        
// Here's our highest int
        $high = $i | (~$mask & 0xFFFFFFFF);
       
        
// Now split the ip we're checking against up into classes
        list($a, $b, $c, $d) = explode('.', $ip);
       
        
// Now convert the ip we're checking against to an int
        $check = ($a << 24) + ($b << 16) + ( $c << 8 ) + $d;
       
        
// If the ip is within the range, including highest/lowest values,
        // then it's witin the CIDR range
        if ($check >= $low && $check <= $high) {
            return true;
        }

    }

    return false;

}

// getting current ip address
if(!isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$client_ip = $_SERVER['REMOTE_ADDR'];
}
else
{
$client_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}

// check, if ip is in subnet
if(!ip_in_subnet($client_ip))
{
// trigger error / no access
trigger_error('Sorry, no access to this forum from your network.
<br /><a href="javascript:history.back()">Back to last site</a>'
);
}

}}

// Some forums allowed for given subnets only / 4seven / 2012           


  • Make settings in both ### CONFIG Sections
  • Read comments

_________________
Mod-Bot / Service-Team


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic TOPIC_LOCKED  [ 1 post ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group
[ Time : 0.058s | 24 Queries | GZIP : Off ]