Example #1
0
 function test_getIPAddressType()
 {
     $ips = array('0.0.0.0' => 'external', '10.0.0.1' => 'internal', '127.0.0.1' => 'loopback', '172.16.0.1' => 'internal', '192.168.0.1' => 'internal', '255.0.0.1' => 'external');
     foreach ($ips as $ip => $expected) {
         $rv = fbHTTP::getIPAddressType($ip);
         $this->assertEquals($expected, $rv);
     }
 }
Example #2
0
 function getRemoteAddress()
 {
     global $_SERVER;
     // < 4.1.0
     static $rv = null;
     while (is_null($rv)) {
         if (isset($_SERVER['HTTP_CLIENT_IP'])) {
             $ip = trim($_SERVER['HTTP_CLIENT_IP']);
             if (strcasecmp($ip, 'unknown')) {
                 $ip2 = explode('.', $ip);
                 $rv = $ip2[3] . '.' . $ip2[2] . '.' . $ip2[1] . '.' . $ip2[0];
                 if (fbHTTP::getIPAddressType($rv) == 'external') {
                     break;
                 }
             }
         }
         if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
             $ips = trim($_SERVER['HTTP_X_FORWARDED_FOR']);
             while (preg_match('/([^, ]+)[, ]+(.*)/', $ips, $matches)) {
                 $rv = trim($matches[1]);
                 if (strcasecmp($ip, 'unknown')) {
                     if (fbHTTP::getIPAddressType($rv) == 'external') {
                         break 2;
                     }
                 }
                 $ips = @$matches[2];
             }
         }
         if (isset($_SERVER['HTTP_FORWARDED'])) {
             $ips = trim($_SERVER['HTTP_FORWARDED']);
             while (preg_match('/([^, ]+)[, ]+(.*)/', $ips, $matches)) {
                 $rv = trim($matches[1]);
                 if (strcasecmp($ip, 'unknown')) {
                     if (fbHTTP::getIPAddressType($rv) == 'external') {
                         break 2;
                     }
                 }
                 $ips = @$matches[2];
             }
         }
         if (isset($_SERVER['REMOTE_ADDR'])) {
             $rv = trim($_SERVER['REMOTE_ADDR']);
             break;
         }
         $rv = false;
         break;
     }
     return $rv;
 }