Geolocation.class1.php
1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace SH;
class Geolocation
{
static $names = array(
'country' => 'Страна',
'district' => 'Округ',
'region' => 'Регион',
'city' => 'Город',
'ip' => 'IP-адрес'
);
static function toString()
{
$data = self::getData();
$data['ip'] = $_SERVER['REMOTE_ADDR'];
$result = '';
foreach($data as $key => $val) {
if($val) {
if($result) {
$result .= "\n";
}
$result .= (isset(self::$names[$key]) ? self::$names[$key] : $key) . ': ' . $val;
}
}
return $result;
}
static function getData()
{
static $data;
if (!isset($data)) {
// if (!empty($_SESSION['geolocation-data'])) {
// $data = json_decode($_SESSION['geolocation-data'], true);
// if (!is_array($data) || !isset($data['city'])) $data = false;
// }
if (!$data) {
$data = array();
try {
$geolocator = new \Cpeople\Classes\Geolocation\LocatorIpgeobase();
if ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') {
$geolocator->setIP('109.110.70.1');
// $geolocator->setIP('127.0.0.1');
}
$result = $geolocator->locate();
if ($result->isError()) throw new \Exception();
$data['country'] = $result->getCountry();
$data['district'] = $result->getDestrict();
$data['region'] = $result->getRegion();
$data['city'] = $result->getCity();
} catch (\Exception $e) {
}
$_SESSION['geolocation-data'] = $data;
}
}
return $data;
}
}