1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace slavkluev\Ip2Location; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
9
|
|
|
use ZipArchive; |
10
|
|
|
|
11
|
|
|
class Uploader |
12
|
|
|
{ |
13
|
|
|
const BASE_URI = 'https://www.ip2location.com/download/'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Stores the HTTP Client. |
17
|
|
|
* @var Client |
18
|
|
|
*/ |
19
|
|
|
private $httpClient; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Uploader constructor. |
23
|
|
|
* @param Client|null $client |
24
|
|
|
*/ |
25
|
18 |
|
public function __construct(?Client $client = null) |
26
|
|
|
{ |
27
|
18 |
|
if ($client) { |
28
|
18 |
|
$this->httpClient = $client; |
29
|
|
|
} else { |
30
|
3 |
|
$this->httpClient = new Client([ |
31
|
3 |
|
'base_uri' => self::BASE_URI, |
32
|
|
|
]); |
33
|
|
|
} |
34
|
18 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $token |
38
|
|
|
* @param string $code |
39
|
|
|
* @param string $filePath |
40
|
|
|
* @throws GuzzleException |
41
|
|
|
* @throws \Exception |
42
|
|
|
*/ |
43
|
15 |
|
public function update(string $token, string $code, string $filePath) |
44
|
|
|
{ |
45
|
15 |
|
$tmpFile = $this->uploadUrl($token, $code); |
46
|
15 |
|
$this->extractBinFile((string)$tmpFile, $filePath); |
47
|
6 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return Client |
51
|
|
|
*/ |
52
|
3 |
|
public function getHttpClient(): Client |
53
|
|
|
{ |
54
|
3 |
|
return $this->httpClient; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $filename |
59
|
|
|
* @param string $filePath |
60
|
|
|
* @throws \Exception |
61
|
|
|
*/ |
62
|
15 |
|
protected function extractBinFile(string $filename, string $filePath) |
63
|
|
|
{ |
64
|
15 |
|
$zip = new ZipArchive(); |
65
|
15 |
|
$zip->open($filename); |
66
|
15 |
|
$binFilename = $this->findBinFile($zip); |
67
|
12 |
|
$file = $zip->getFromName($binFilename); |
68
|
|
|
|
69
|
12 |
|
if (false === @file_put_contents($filePath, $file)) { |
70
|
6 |
|
throw new \Exception('Failure while storing: ' . error_get_last()['message']); |
71
|
|
|
} |
72
|
|
|
|
73
|
6 |
|
$zip->close(); |
74
|
6 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param ZipArchive $archive |
78
|
|
|
* @return string |
79
|
|
|
* @throws \Exception |
80
|
|
|
*/ |
81
|
15 |
|
protected function findBinFile(ZipArchive $archive): string |
82
|
|
|
{ |
83
|
15 |
|
for ($i = 0; $i < $archive->numFiles; $i++) { |
84
|
15 |
|
$filename = $archive->getNameIndex($i); |
85
|
15 |
|
if (substr(strtolower($filename), -4, 4) == '.bin') { |
86
|
12 |
|
return $filename; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
3 |
|
throw new \Exception("bin file has not found"); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $token |
95
|
|
|
* @param string $code |
96
|
|
|
* @return TmpFile |
97
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
98
|
|
|
*/ |
99
|
15 |
|
protected function uploadUrl(string $token, string $code): TmpFile |
100
|
|
|
{ |
101
|
15 |
|
$tmpFile = new TmpFile(); |
102
|
15 |
|
$resource = fopen((string)$tmpFile, 'w'); |
103
|
15 |
|
$this->httpClient->request( |
104
|
15 |
|
'GET', |
105
|
15 |
|
'/', |
106
|
|
|
[ |
107
|
|
|
'query' => [ |
108
|
15 |
|
'token' => $token, |
109
|
15 |
|
'file' => $code, |
110
|
|
|
], |
111
|
15 |
|
'sink' => $resource, |
112
|
|
|
] |
113
|
|
|
); |
114
|
15 |
|
fclose($resource); |
|
|
|
|
115
|
|
|
|
116
|
15 |
|
return $tmpFile; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|