forked from JiSoft/yii2-sypexgeo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSxUpdate.php
153 lines (138 loc) · 3.63 KB
/
SxUpdate.php
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* Class update data
* User: den67rus
* Date: 22.07.2017
* Time: 3:19
*/
namespace jisoft\sypexgeo;
/**
* Class SxUpdate
* @package jisoft\sypexgeo
*/
class SxUpdate
{
/**
* Custom dir dat files
* by default Yii::getAlias('@runtime'); or __DIR__ if not Yii Framework
* @var string
*/
public $updateDir;
/**
* @var string Custom url update
*/
public $countryUrl = 'https://sypexgeo.net/files/SxGeoCountry.zip';
/**
* @var string Custom url update
*/
public $cityUrl = 'https://sypexgeo.net/files/SxGeoCity_utf8.zip';
/**
* @var string Custom url update
*/
public $maxUrl;
/**
* SxUpdate constructor.
*/
public function __construct()
{
set_time_limit(600);
if (class_exists('\Yii')) {
$this->updateDir = \Yii::getAlias('@runtime');
} else {
$this->updateDir = __DIR__;
}
}
/**
* Update Sypex Geo Country file
* @return bool
*/
public function updateCountry()
{
return $this->update($this->countryUrl, 'SxGeo');
}
/**
* Update Sypex Geo City file
* @return bool
*/
public function updateCity()
{
return $this->update($this->cityUrl, 'SxGeoCity');
}
/**
* Update Sypex Geo Max file
* @return bool
*/
public function updateMax()
{
return $this->update($this->maxUrl, 'SxGeoMax');
}
/**
* Update Sypex Geo all file
* @return object
*/
public function updateAll()
{
return (object) [
'country' => $this->updateCountry(),
'city' => $this->updateCity(),
'max' => $this->updateMax(),
];
}
/**
* Download and update file
* @param string $url
* @param string $type
* @return bool
* @throws \Exception
*/
protected function update($url, $type)
{
$dir = $this->getDir();
$dat_file = $dir . '/' . $type . '.dat';
$upd_file = $dir . '/' .$type . '.upd';
$fp = fopen($dir .'/SxGeoTmp.zip', 'wb');
$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_FILE => $fp,
CURLOPT_HTTPHEADER => file_exists($upd_file) ? array("If-Modified-Since: " .file_get_contents($upd_file)) : [],
));
if(!curl_exec($ch)) {
throw new \Exception('Failed to download the update file Sypexgeo. Url: ' . $url);
}
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
fclose($fp);
if ($code == 304) {
@unlink($dir . '/SxGeoTmp.zip');
return false;
}
$fp = fopen('zip://' . $dir . '/SxGeoTmp.zip#' . $type . '.dat', 'rb');
$fw = fopen($dat_file, 'wb');
if (!$fp) {
throw new \Exception('Failed to open the downloaded update file Sypexgeo.');
}
stream_copy_to_stream($fp, $fw);
fclose($fp);
fclose($fw);
if(filesize($dat_file) == 0) {
throw new \Exception('Failed to unzipping the downloaded update file Sypexgeo.');
}
@unlink($dir . '/SxGeoTmp.zip');
file_put_contents($upd_file, gmdate('D, d M Y H:i:s') . ' GMT');
return true;
}
/**
* @return string
* @throws \Exception
*/
protected function getDir()
{
$dir = $this->updateDir . '/sx_data';
if (!file_exists($dir)) {
if (!mkdir($dir, 0777, true)) {
throw new \Exception('Failed to create directory Sypexgeo update. Dir: ' . $dir);
}
}
return $dir;
}
}