/** * @return array or boolean false */ public function findLocationByIp() { $ip = $this->findIp(); if (empty($ip)) { return false; } if (Validation::ip($ip)) { App::import('Vendor', 'geoip', ['file' => 'geoip' . DS . 'geoip.php']); $gi = Net_GeoIP::getInstance(APP . 'vendors' . DS . 'geoip' . DS . 'GeoLiteCity.dat'); $record = $gi->lookupLocation($ip); $gi->close(); } else { $this->log('Invalid IP \'' . h($ip) . '\'', LOG_WARNING); } return !empty($record) ? $this->findLocationByCoordinates($record->latitude, $record->longitude, 1) : false; }
/** * testIp v6 * * @return void */ public function testIpv6() { $this->assertTrue(Validation::ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334', 'IPv6')); $this->assertTrue(Validation::ip('2001:db8:85a3:0:0:8a2e:370:7334', 'IPv6')); $this->assertTrue(Validation::ip('2001:db8:85a3::8a2e:370:7334', 'IPv6')); $this->assertTrue(Validation::ip('2001:0db8:0000:0000:0000:0000:1428:57ab', 'IPv6')); $this->assertTrue(Validation::ip('2001:0db8:0000:0000:0000::1428:57ab', 'IPv6')); $this->assertTrue(Validation::ip('2001:0db8:0:0:0:0:1428:57ab', 'IPv6')); $this->assertTrue(Validation::ip('2001:0db8:0:0::1428:57ab', 'IPv6')); $this->assertTrue(Validation::ip('2001:0db8::1428:57ab', 'IPv6')); $this->assertTrue(Validation::ip('2001:db8::1428:57ab', 'IPv6')); $this->assertTrue(Validation::ip('0000:0000:0000:0000:0000:0000:0000:0001', 'IPv6')); $this->assertTrue(Validation::ip('::1', 'IPv6')); $this->assertTrue(Validation::ip('::ffff:12.34.56.78', 'IPv6')); $this->assertTrue(Validation::ip('::ffff:0c22:384e', 'IPv6')); $this->assertTrue(Validation::ip('2001:0db8:1234:0000:0000:0000:0000:0000', 'IPv6')); $this->assertTrue(Validation::ip('2001:0db8:1234:ffff:ffff:ffff:ffff:ffff', 'IPv6')); $this->assertTrue(Validation::ip('2001:db8:a::123', 'IPv6')); $this->assertTrue(Validation::ip('fe80::', 'IPv6')); $this->assertTrue(Validation::ip('::ffff:192.0.2.128', 'IPv6')); $this->assertTrue(Validation::ip('::ffff:c000:280', 'IPv6')); $this->assertFalse(Validation::ip('123', 'IPv6')); $this->assertFalse(Validation::ip('ldkfj', 'IPv6')); $this->assertFalse(Validation::ip('2001::FFD3::57ab', 'IPv6')); $this->assertFalse(Validation::ip('2001:db8:85a3::8a2e:37023:7334', 'IPv6')); $this->assertFalse(Validation::ip('2001:db8:85a3::8a2e:370k:7334', 'IPv6')); $this->assertFalse(Validation::ip('1:2:3:4:5:6:7:8:9', 'IPv6')); $this->assertFalse(Validation::ip('1::2::3', 'IPv6')); $this->assertFalse(Validation::ip('1:::3:4:5', 'IPv6')); $this->assertFalse(Validation::ip('1:2:3::4:5:6:7:8:9', 'IPv6')); $this->assertFalse(Validation::ip('::ffff:2.3.4', 'IPv6')); $this->assertFalse(Validation::ip('::ffff:257.1.2.3', 'IPv6')); $this->assertFalse(Validation::ip('255.255.255.255', 'ipv6'), 'IPv4 is not valid IPv6'); }
/** * Get all IP addresses associated with the current connection. * * @return array IP addresses */ public function addresses() { if (Validation::ip($this->config['host'])) { return array($this->config['host']); } return gethostbynamel($this->config['host']); }
/** * testIpV6 method * * @access public * @return void */ function testIpV6() { $this->assertFalse(Validation::ip('', '6')); $this->assertFalse(Validation::ip(':', '6')); $this->assertTrue(Validation::ip('::', '6')); $this->assertFalse(Validation::ip(':::', '6')); $this->assertFalse(Validation::ip('::::', '6')); $this->assertTrue(Validation::ip('::1', '6')); $this->assertTrue(Validation::ip('4:4:4:4:4:4:4:4:4', '6')); $this->assertTrue(Validation::ip('0:123:af00:fff:0C67:0:0:8787', '6')); $this->assertTrue(Validation::ip('0::af00:ffff:0C67:0:0:8787', '6')); $this->assertTrue(Validation::ip('0af4:0af4:0af4:0af4:0af4:0af4:0af4:0af4', '6')); $this->assertTrue(Validation::ip('af4:af4:af4:af4:af4:af4:af4:af4', '6')); $this->assertTrue(Validation::ip('f4:f4:f4:f4:f4:f4:f4:f4', '6')); $this->assertTrue(Validation::ip('4:4:4:4:4:4:4:4', '6')); $this->assertTrue(Validation::ip('4::4:4:4:4:4:4', '6')); $this->assertTrue(Validation::ip('4::4:4:4:4:4', '6')); $this->assertTrue(Validation::ip('4::4:4:4:4', '6')); $this->assertTrue(Validation::ip('4::4:4:4', '6')); $this->assertTrue(Validation::ip('4::4:4', '6')); $this->assertTrue(Validation::ip('4::4', '6')); $this->assertTrue(Validation::ip('4::', '6')); $this->assertTrue(Validation::ip('::ffff:12.34.56.78', '6')); $this->assertFalse(Validation::ip('::01.02.03.04', '6')); $this->assertFalse(Validation::ip('0:0:0:255.255.255.255', '6')); $this->assertFalse(Validation::ip('1fff::a88:85a3::172.31.128.1', '6')); }
/** * testIpBoth method * * @return void * @access public */ function testIpBoth() { $this->assertTrue(Validation::ip('0.0.0.0')); $this->assertTrue(Validation::ip('192.168.1.156')); $this->assertTrue(Validation::ip('255.255.255.255')); $this->assertFalse(Validation::ip('127.0.0')); $this->assertFalse(Validation::ip('127.0.0.a')); $this->assertFalse(Validation::ip('127.0.0.256')); $this->assertTrue(Validation::ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334')); $this->assertTrue(Validation::ip('2001:db8:85a3:0:0:8a2e:370:7334')); $this->assertTrue(Validation::ip('2001:db8:85a3::8a2e:370:7334')); $this->assertFalse(Validation::ip('2001:db8:85a3::8a2e:37023:7334')); $this->assertFalse(Validation::ip('2001:db8:85a3::8a2e:370k:7334')); $this->assertFalse(Validation::ip('1:2:3:4:5:6:7:8:9')); }
/** * testIp method * * @access public * @return void */ function testIp() { $this->assertTrue(Validation::ip('0.0.0.0')); $this->assertTrue(Validation::ip('192.168.1.156')); $this->assertTrue(Validation::ip('255.255.255.255')); $this->assertFalse(Validation::ip('127.0.0')); $this->assertFalse(Validation::ip('127.0.0.a')); $this->assertFalse(Validation::ip('127.0.0.256')); }
/** * @testdox ip should return false to malformed IPs */ public function testInvalidIpAsMalformed() { $value = '123456789'; $this->assertFalse(Validation::ip($value)); }
/** * @deprecated - use IpLip instead! * @return string v4/v6 or FALSE on failure */ public function ipFormat($ip) { if (Validation::ip($ip, 'ipv4')) { return 'ipv4'; } if (Validation::ip($ip, 'ipv6')) { return 'ipv6'; } return false; }