Advertisement
aridho

onetimeacces

Mar 29th, 2018
1,525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.32 KB | None | 0 0
  1. <?php
  2. class IpList {
  3.     private $iplist = array();
  4.     private $ipfile = "";
  5.     public function __construct( $list ) {
  6.         $contents = array();
  7.         $this->ipfile = $list;
  8.         $lines = $this->read( $list );
  9.         foreach( $lines as $line ) {
  10.             $line = trim( $line );
  11.             # remove comment and blank lines
  12.             if ( empty($line ) ) {
  13.                 continue;
  14.             }
  15.             if ( $line[0] == '#' ) {
  16.                 continue;
  17.             }
  18.             # remove on line comments
  19.             $temp = explode( "#", $line );
  20.             $line = trim( $temp[0] );
  21.             # create content array
  22.             $contents[] = $this->normal($line);
  23.         }      
  24.         $this->iplist = $contents;
  25.     }
  26.    
  27.     public function __destruct() {
  28.     }
  29.    
  30.     public function __toString() {
  31.         return implode(' ',$this->iplist);
  32.     }
  33.    
  34.     public function is_inlist( $ip ) {
  35.         $retval = false;       
  36.         foreach( $this->iplist as $ipf ) {
  37.             $retval = $this->ip_in_range( $ip, $ipf );
  38.             if ($retval === true ) {
  39.                 $this->range = $ipf;
  40.                 break;
  41.             }
  42.         }
  43.         return $retval;
  44.     }
  45.    
  46.     /*
  47.     ** public function that returns the ip list array
  48.     */
  49.     public function iplist() {
  50.         return $this->iplist;
  51.     }
  52.    
  53.     /*
  54.     */
  55.     public function message() {
  56.         return $this->range;
  57.     }
  58.    
  59.     public function append( $ip, $comment ) {
  60.         return file_put_contents( $this->ipfile, $ip, $comment );
  61.     }
  62.    
  63.     public function listname() {
  64.         return $this->ipfile;
  65.     }
  66.    
  67.     /*
  68.     ** private function that reads the file into  array
  69.     */
  70.     private function read( $fname ) {
  71.         try {
  72.             $file = file( $fname, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES  );
  73.         }
  74.         catch( Exception $e ) {
  75.             throw new Exception( $fname.': '.$e->getmessage() . '\n');
  76.         }
  77.         return $file;
  78.     }
  79.    
  80.     private function ip_in_range( $ip, $range ) {
  81.    
  82.         // return ip_in_range( $ip, $range );
  83.        
  84.         if ( strpos($range, '/') !== false ) {
  85.             // IP/NETMASK format
  86.             list( $range, $netmask ) = explode( '/', $range, 2 );
  87.             if ( strpos( $netmask, '.' ) !== false ) {
  88.                 // 255.255.255.0 format w/ wildcards
  89.                 $netmask = str_replace('*', '0', $netmask );
  90.                 $dnetmask = ip2long( $netmask );
  91.                 return ((ip2long( $ip ) & $dnetmask) == (ip2long($range) & $dnetmask ));
  92.             }
  93.             else {
  94.                 // IP/CIDR format
  95.                 // insure $range format 0.0.0.0
  96.                 $r = explode( '.', $range );
  97.                 while( count( $r ) < 4 ) {
  98.                     $r[] = '0';
  99.                 }
  100.                 for($i = 0; $i < 4; $i++) {
  101.                     $r[$i] = empty($r[$i]) ? '0': $r[$i];
  102.                 }
  103.                 $range = implode( '.', $r );
  104.                 // build netmask
  105.                 $dnetmask = ~(pow( 2, ( 32 - $netmask)) - 1);
  106.                 return ((ip2long($ip) & $dnetmask)==(ip2long($range) & $dnetmask));
  107.             }
  108.         }
  109.         else {
  110.             if ( strpos( $range, '*' ) !== false ) {
  111.                 // 255.255.*.* format
  112.                 $low = str_replace( '*', '0', $range );
  113.                 $high = str_replace( '*', '255', $range );
  114.                 $range = $low.'-'.$high;
  115.             }
  116.             if ( strpos( $range, '-') !== false ) {
  117.                 // 128.255.255.0-128.255.255.255 format
  118.                 list( $low, $high ) = explode( '-', $range, 2 );
  119.                
  120.                 $dlow  = $this->toLongs( $low );
  121.                 $dhigh = $this->toLongs( $high );
  122.                 $dip   = $this->toLongs( $ip );
  123.                 return (($this->compare($dip,$dlow) != -1) && ($this->compare($dip,$dhigh) != 1));
  124.             }
  125.         }
  126.         return ( $ip == $range );
  127.     }
  128.     private function normal( $range ) {
  129.         if ( strpbrk( "*-/", $range ) === False ) {
  130.             $range .= "/32";
  131.         }
  132.         return str_replace( ' ', '', $range );
  133.     }
  134.     private function toLongs( $ip ) {
  135.         # $Ip = $this->expand();
  136.         # $Parts = explode(':', $Ip);
  137.         $Parts = explode('.', $ip );
  138.         $Ip = array('', '');
  139.         # for ($i = 0; $i < 4; $i++) {
  140.         for ($i = 0; $i < 2; $i++){
  141.             $Ip[0] .= str_pad(base_convert($Parts[$i], 16, 2), 16, 0, STR_PAD_LEFT);
  142.         }
  143.         # for ($i = 4; $i < 8; $i++) {
  144.         for ($i = 2; $i < 4; $i++) {
  145.             $Ip[1] .= str_pad(base_convert($Parts[$i], 16, 2), 16, 0, STR_PAD_LEFT);
  146.         }
  147.         return array(base_convert($Ip[0], 2, 10), base_convert($Ip[1], 2, 10));
  148.     }
  149.    
  150.     private function compare( $ipdec1, $ipdec2 ) {
  151.         if( $ipdec1[0] < $ipdec2[0] ) {
  152.             return -1;
  153.         }
  154.         elseif ( $ipdec1[0] > $ipdec2[0] ) {
  155.             return 1;
  156.         }
  157.         elseif ( $ipdec1[1] < $ipdec2[1] ) {
  158.             return -1;
  159.         }
  160.         elseif ( $ipdec1[1] > $ipdec2[1] ) {
  161.             return 1;
  162.         }
  163.         return 0;
  164.     }
  165. }
  166. class IpBlockList {
  167.  
  168.     private $statusid = array( 'negative' => -1, 'neutral' => 0, 'positive' => 1 );
  169.  
  170.     private $whitelist = array();
  171.     private $blacklist = array();
  172.     private $message   = NULL;
  173.     private $status    = NULL;
  174.  
  175.     public function __construct(
  176.         $whitelistfile = 'assets/includes/whitelist.dat',
  177.         $blacklistfile = 'assets/includes/blacklist.dat' ) {
  178.         $this->whitelistfile = $whitelistfile;
  179.         $this->blacklistfile = $blacklistfile;
  180.        
  181.         $this->whitelist = new IpList( $whitelistfile );
  182.         $this->blacklist = new IpList( $blacklistfile );
  183.     }
  184.    
  185.     public  function    __destruct() {
  186.     }
  187.    
  188.     public function ipPass( $ip ) {
  189.         $retval = False;
  190.        
  191.         if ( !filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ) {
  192.             throw new Exception( 'Requires valid IPv4 address' );
  193.         }
  194.        
  195.         if ( $this->whitelist->is_inlist( $ip ) ) {
  196.             // Ip is white listed, so it passes
  197.             $retval = True;
  198.             $this->message = $ip . " is whitelisted by ".$this->whitelist->message().".";
  199.             $this->status = $this->statusid['positive'];
  200.         }
  201.         else if ( $this->blacklist->is_inlist( $ip ) ) {
  202.             $retval = False;
  203.             $this->message = $ip . " is blacklisted by ".$this->blacklist->message().".";
  204.             $this->status = $this->statusid['negative'];
  205.         }
  206.         else {
  207.             $retval = True;
  208.             $this->message = $ip . " is unlisted.";
  209.             $this->status = $this->statusid['neutral'];
  210.         }
  211.         return $retval;
  212.     }
  213.    
  214.     public function message() {
  215.         return $this->message;
  216.     }
  217.  
  218.          public function status() {
  219.              return $this->status;
  220.          }
  221.  
  222.     public function append( $type, $ip, $comment = "" ) {
  223.         if ($type == 'WHITELIST' ) {
  224.             $retval = $this->whitelistfile->append( $ip, $comment );
  225.         }
  226.         elseif( $type == 'BLACKLIST' ) {
  227.             $retval = $this->blacklistfile->append( $ip, $comment );
  228.         }
  229.         else {
  230.             $retval = false;
  231.         }
  232.     }
  233.  
  234.     public function filename( $type, $ip, $comment = "" ) {
  235.         if ($type == 'WHITELIST' ) {
  236.             $retval = $this->whitelistfile->filename( $ip, $comment );
  237.         }
  238.         elseif( $type == 'BLACKLIST' ) {
  239.             $retval = $this->blacklistfile->filename( $ip, $comment );
  240.         }
  241.         else {
  242.             $retval = false;
  243.         }
  244.     }
  245. }
  246. $ips = array(   $_SERVER['REMOTE_ADDR'],
  247.                  );
  248. $checklist = new IpBlockList( );
  249. foreach ($ips as $ip ) {
  250. $result = $checklist->ipPass( $ip );
  251.     if ( $result ) {
  252.         // Continue with page
  253.     }
  254.     else {
  255.         header("Location: https://www.google.ca/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwi_yey8kvzJAhWwj4MKHVp5ALcQFggcMAA&url=https%3A%2F%2Fappleid.apple.com%2F&usg=AFQjCNF7841Jq5PLrYJwYDN8RkcZjuNVww");
  256.         die();
  257.     }  
  258. }
  259. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement