1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Geography module |
5
|
|
|
* |
6
|
|
|
* @author Alexey Krupskiy <[email protected]> |
7
|
|
|
* @link http://inji.ru/ |
8
|
|
|
* @copyright 2016 Alexey Krupskiy |
9
|
|
|
* @license https://github.com/injitools/cms-Inji/blob/master/LICENSE |
10
|
|
|
*/ |
11
|
|
|
class Geography extends Module { |
12
|
|
|
|
13
|
|
|
public $geographyDbDir = '/tmp/Geography'; |
14
|
|
|
|
15
|
|
|
public function init() { |
16
|
|
|
if (!empty(App::$primary->config['site']['domain'])) { |
17
|
|
|
$domain = App::$primary->config['site']['domain']; |
18
|
|
|
} else { |
19
|
|
|
$domain = implode('.', array_slice(explode('.', idn_to_utf8(INJI_DOMAIN_NAME)), -2)); |
20
|
|
|
} |
21
|
|
|
$alias = str_replace($domain, '', idn_to_utf8(INJI_DOMAIN_NAME)); |
22
|
|
|
$city = null; |
23
|
|
|
if ($alias) { |
24
|
|
|
$alias = str_replace('.', '', $alias); |
25
|
|
|
$city = Geography\City::get($alias, 'alias'); |
26
|
|
|
} |
27
|
|
|
if (!$city) { |
28
|
|
|
if (!file_exists(App::$primary->path . $this->geographyDbDir . '/SxGeoCity.dat')) { |
29
|
|
|
$this->updateDb(); |
30
|
|
|
} |
31
|
|
|
if (file_exists(App::$primary->path . $this->geographyDbDir . '/SxGeoCity.dat')) { |
32
|
|
|
$SxGeo = new Geography\SxGeo(App::$primary->path . $this->geographyDbDir . '/SxGeoCity.dat'); |
33
|
|
|
$cityIp = $SxGeo->getCity($_SERVER['REMOTE_ADDR']); |
34
|
|
|
if (!empty($cityIp['city']['name_ru'])) { |
35
|
|
|
$city = Geography\City::get($cityIp['city']['name_ru'], 'name'); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
if (!empty($_COOKIE['curcity'])) { |
40
|
|
|
$city = \Geography\City::get((int) $_COOKIE['curcity']); |
41
|
|
|
} |
42
|
|
|
if (!$city) { |
43
|
|
|
$city = Geography\City::get(1, 'default'); |
44
|
|
|
} |
45
|
|
|
if (!empty($this->config['aliasRedirect']) && $city && $city->alias && !$city->default && !$alias && Module::$cur->moduleName !== 'Exchange1c') { |
46
|
|
|
Tools::redirect('//' . $city->alias . '.' . $domain . $_SERVER['REQUEST_URI']); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
Geography\City::$cur = $city; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function updateDb() { |
52
|
|
|
// Обновление файла базы данных Sypex Geo |
53
|
|
|
// Настройки |
54
|
|
|
$url = 'https://sypexgeo.net/files/SxGeoCity_utf8.zip'; // Путь к скачиваемому файлу |
55
|
|
|
$dat_file_dir = App::$primary->path . $this->geographyDbDir; // Каталог в который сохранять dat-файл |
56
|
|
|
$last_updated_file = $dat_file_dir . '/SxGeo.upd'; // Файл в котором хранится дата последнего обновления |
57
|
|
|
$info = false; // Вывод сообщений о работе, true заменить на false после установки в cron |
58
|
|
|
|
59
|
|
|
// Конец настроек |
60
|
|
|
|
61
|
|
|
set_time_limit(600); |
62
|
|
|
//error_reporting(E_ALL); |
63
|
|
|
//header('Content-type: text/plain; charset=utf8'); |
64
|
|
|
|
65
|
|
|
$t = microtime(1); |
|
|
|
|
66
|
|
|
Tools::createDir($dat_file_dir); |
67
|
|
|
$types = array( |
68
|
|
|
'Country' => 'SxGeo.dat', |
69
|
|
|
'City' => 'SxGeoCity.dat', |
70
|
|
|
'Max' => 'SxGeoMax.dat', |
71
|
|
|
); |
72
|
|
|
// Скачиваем архив |
73
|
|
|
preg_match("/(Country|City|Max)/", pathinfo($url, PATHINFO_BASENAME), $m); |
74
|
|
|
$type = $m[1]; |
75
|
|
|
$dat_file = $types[$type]; |
76
|
|
|
if ($info) echo "Скачиваем архив с сервера\n"; |
77
|
|
|
|
78
|
|
|
$fp = fopen($dat_file_dir . '/SxGeoTmp.zip', 'wb'); |
79
|
|
|
$ch = curl_init($url); |
80
|
|
|
curl_setopt_array($ch, array( |
81
|
|
|
CURLOPT_FILE => $fp, |
82
|
|
|
CURLOPT_HTTPHEADER => file_exists($last_updated_file) ? array("If-Modified-Since: " . file_get_contents($last_updated_file)) : array(), |
83
|
|
|
)); |
84
|
|
|
if (!curl_exec($ch)) { |
85
|
|
|
if ($info) echo 'Ошибка при скачивании архива'; |
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
89
|
|
|
curl_close($ch); |
90
|
|
|
fclose($fp); |
|
|
|
|
91
|
|
|
if ($code == 304) { |
92
|
|
|
@unlink($dat_file_dir . '/SxGeoTmp.zip'); |
|
|
|
|
93
|
|
|
if ($info) echo "Архив не обновился, с момента предыдущего скачивания\n"; |
94
|
|
|
return; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if ($info) echo "Архив с сервера скачан\n"; |
98
|
|
|
// Распаковываем архив |
99
|
|
|
$fp = fopen('zip://' . $dat_file_dir . '/SxGeoTmp.zip#' . $dat_file, 'rb'); |
100
|
|
|
$fw = fopen($dat_file_dir . '/' . $dat_file, 'wb'); |
101
|
|
|
if (!$fp) { |
102
|
|
|
if ($info) |
103
|
|
|
echo "Не получается открыть\n"; |
104
|
|
|
return; |
105
|
|
|
} |
106
|
|
|
if ($info) echo "Распаковываем архив\n"; |
107
|
|
|
stream_copy_to_stream($fp, $fw); |
108
|
|
|
fclose($fp); |
109
|
|
|
fclose($fw); |
110
|
|
|
if (filesize($dat_file) == 0) { |
111
|
|
|
if ($info) echo 'Ошибка при распаковке архива'; |
112
|
|
|
} |
113
|
|
|
@unlink($dat_file_dir . '/SxGeoTmp.zip'); |
114
|
|
|
file_put_contents($last_updated_file, gmdate('D, d M Y H:i:s') . ' GMT'); |
115
|
|
|
if ($info) echo "Перемещен файл в {$dat_file_dir}/{$dat_file}\n"; |
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths