Campustream 1.0
A social network MQP for WPI
application/lib/iprules.php
Go to the documentation of this file.
00001 <?php
00002 
00007 class iprules {
00008         
00009         // array of specific IP's to whitelist if it is blocked
00010         // using the ip ranges below
00011         private static $whitelist = array(
00012                 
00013         );
00014         
00024         private static $ip_ranges = array(
00025                 
00026         );
00027         
00028         public static function allowed() {
00029                 if(in_array($_SERVER['REMOTE_ADDR'], self::$whitelist)) {
00030                         return true;
00031                 }
00032                 
00033                 foreach(self::$ip_ranges as $ip=>$mask) {
00034                         if(self::ip_in_network($_SERVER['REMOTE_ADDR'], $ip, $mask)) { 
00035                                 return false;
00036                         }
00037                 }
00038                 
00039                 return true;
00040         }
00041 
00042         private static function ip_in_network($ip, $net_addr, $net_mask) {
00043                 if ($net_mask <= 0) { return false; }
00044                 $ip_binary_string = sprintf("%032b", ip2long($ip));
00045                 $net_binary_string = sprintf("%032b", ip2long($net_addr));
00046                 return substr_compare($ip_binary_string, $net_binary_string, 0, $net_mask) === 0;
00047         }
00048 }
00049 ?>