Exemplo n.º 1
0
 public function test_ipv6()
 {
     $result = iplookup_find_location('2a01:8900:2:3:8c6c:c0db:3d33:9ce6');
     $this->assertEquals('array', gettype($result));
     $this->assertEquals('Lancaster', $result['city']);
     $this->assertEquals(-2.7997, $result['longitude'], 'Coordinates are out of accepted tolerance', 0.01);
     $this->assertEquals(54.0465, $result['latitude'], 'Coordinates are out of accepted tolerance', 0.01);
     $this->assertNull($result['error']);
     $this->assertEquals('array', gettype($result['title']));
     $this->assertEquals('Lancaster', $result['title'][0]);
     $this->assertEquals('United Kingdom', $result['title'][1]);
 }
Exemplo n.º 2
0
 public function test_ipv6()
 {
     // NOTE: these tests can be altered by the geoip dataset, there has been an attempt to get
     // a 'reliable' result.
     $result = iplookup_find_location('2607:f010:3fe:fff1::ff:fe00:25');
     $this->assertEquals('array', gettype($result));
     $this->assertEquals('Los Angeles', $result['city']);
     $this->assertEquals(-118.2987, $result['longitude'], 'Coordinates are out of accepted tolerance', 0.01);
     $this->assertEquals(33.7866, $result['latitude'], 'Coordinates are out of accepted tolerance', 0.01);
     $this->assertNull($result['error']);
     $this->assertEquals('array', gettype($result['title']));
     $this->assertEquals('Los Angeles', $result['title'][0]);
     $this->assertEquals('United States', $result['title'][1]);
 }
Exemplo n.º 3
0
 public function test_geoip()
 {
     global $CFG;
     require_once "{$CFG->libdir}/filelib.php";
     require_once "{$CFG->dirroot}/iplookup/lib.php";
     if (!PHPUNIT_LONGTEST) {
         // this may take a long time
         return;
     }
     $this->resetAfterTest();
     // let's store the file somewhere
     $gzfile = "{$CFG->dataroot}/phpunit/geoip/GeoLiteCity.dat.gz";
     check_dir_exists(dirname($gzfile));
     if (file_exists($gzfile) and filemtime($gzfile) < time() - 60 * 60 * 24 * 30) {
         // delete file if older than 1 month
         unlink($gzfile);
     }
     if (!file_exists($gzfile)) {
         download_file_content('http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz', null, null, false, 300, 20, false, $gzfile);
     }
     $this->assertTrue(file_exists($gzfile));
     $zd = gzopen($gzfile, "r");
     $contents = gzread($zd, 50000000);
     gzclose($zd);
     $geoipfile = "{$CFG->dataroot}/geoip/GeoLiteCity.dat";
     check_dir_exists(dirname($geoipfile));
     $fp = fopen($geoipfile, 'w');
     fwrite($fp, $contents);
     fclose($fp);
     $this->assertTrue(file_exists($geoipfile));
     $CFG->geoipfile = $geoipfile;
     $result = iplookup_find_location('147.230.16.1');
     $this->assertEquals('array', gettype($result));
     $this->assertEquals('Liberec', $result['city']);
     $this->assertEquals(15.0653, $result['longitude'], '', 0.001);
     $this->assertEquals(50.7639, $result['latitude'], '', 0.001);
     $this->assertNull($result['error']);
     $this->assertEquals('array', gettype($result['title']));
     $this->assertEquals('Liberec', $result['title'][0]);
     $this->assertEquals('Czech Republic', $result['title'][1]);
 }
Exemplo n.º 4
0
 public function test_geoip()
 {
     global $CFG;
     require_once "{$CFG->libdir}/filelib.php";
     require_once "{$CFG->dirroot}/iplookup/lib.php";
     if (!PHPUNIT_LONGTEST) {
         // we do not want to DDOS their server, right?
         return;
     }
     $this->resetAfterTest();
     $CFG->geoipfile = '';
     $result = iplookup_find_location('147.230.16.1');
     $this->assertEquals('array', gettype($result));
     $this->assertEquals('Liberec', $result['city']);
     $this->assertEquals(15.0653, $result['longitude'], '', 0.001);
     $this->assertEquals(50.7639, $result['latitude'], '', 0.001);
     $this->assertNull($result['error']);
     $this->assertEquals('array', gettype($result['title']));
     $this->assertEquals('Liberec', $result['title'][0]);
     $this->assertEquals('Czech Republic', $result['title'][1]);
 }
Exemplo n.º 5
0
}
$PAGE->set_url('/iplookup/index.php', array('id' => $ip, 'user' => $user));
$PAGE->set_pagelayout('popup');
$PAGE->set_context(context_system::instance());
$info = array($ip);
$note = array();
if (!preg_match('/(^\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$/', $ip, $match)) {
    print_error('invalidipformat', 'error');
}
if ($match[1] > 255 or $match[2] > 255 or $match[3] > 255 or $match[4] > 255) {
    print_error('invalidipformat', 'error');
}
if ($match[1] == '127' or $match[1] == '10' or $match[1] == '172' and $match[2] >= '16' and $match[2] <= '31' or $match[1] == '192' and $match[2] == '168') {
    print_error('iplookupprivate', 'error');
}
$info = iplookup_find_location($ip);
if ($info['error']) {
    // Can not display.
    notice($info['error']);
}
if ($user) {
    if ($user = $DB->get_record('user', array('id' => $user, 'deleted' => 0))) {
        // note: better not show full names to everybody
        if (has_capability('moodle/user:viewdetails', context_user::instance($user->id))) {
            array_unshift($info['title'], fullname($user));
        }
    }
}
array_unshift($info['title'], $ip);
$title = implode(' - ', $info['title']);
$PAGE->set_title(get_string('iplookup', 'admin') . ': ' . $title);
Exemplo n.º 6
0
 public function test_geoip_ipv6()
 {
     $result = iplookup_find_location('2a01:8900:2:3:8c6c:c0db:3d33:9ce6');
     $this->assertNotNull($result['error']);
     $this->assertEquals($result['error'], get_string('invalidipformat', 'error'));
 }