|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PierInfor\GeoLite; |
|
4
|
|
|
|
|
5
|
|
|
use PierInfor\GeoLite\Downloader; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* FileManager class to manage files |
|
9
|
|
|
*/ |
|
10
|
|
|
class FileManager implements Interfaces\FileManagerInterface |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Downloader instance |
|
15
|
|
|
* |
|
16
|
|
|
* @var Downloader |
|
17
|
|
|
*/ |
|
18
|
|
|
private $downloader; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Instanciate |
|
22
|
|
|
*/ |
|
23
|
15 |
|
public function __construct() |
|
24
|
|
|
{ |
|
25
|
15 |
|
$this->downloader = new Downloader(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* returns downloader instance |
|
30
|
|
|
* |
|
31
|
|
|
* @return Downloader |
|
32
|
|
|
*/ |
|
33
|
1 |
|
public function getDownloader(): Downloader |
|
34
|
|
|
{ |
|
35
|
1 |
|
return $this->downloader; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* download a file for a given url and filename |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $url |
|
42
|
|
|
* @param string $toFilename |
|
43
|
|
|
* @return FileManager |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function download(string $url, string $toFilename): FileManager |
|
46
|
|
|
{ |
|
47
|
1 |
|
$this->downloader->download($url, $toFilename); |
|
48
|
1 |
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* uncompress a tar gz file to a tar archive and return true if ok |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $tgzFilename |
|
55
|
|
|
* @return boolean |
|
56
|
|
|
* @throws \Exception |
|
57
|
|
|
*/ |
|
58
|
4 |
|
public function ungz(string $tgzFilename): bool |
|
59
|
|
|
{ |
|
60
|
4 |
|
$result = false; |
|
61
|
4 |
|
if (empty($tgzFilename) || !file_exists($tgzFilename)) { |
|
62
|
1 |
|
return $result; |
|
63
|
|
|
} |
|
64
|
|
|
try { |
|
65
|
3 |
|
(new \PharData($tgzFilename))->decompress(); |
|
66
|
1 |
|
} catch (\Exception $e) { |
|
67
|
1 |
|
if ($e instanceof \UnexpectedValueException) { |
|
68
|
1 |
|
throw new \Exception('Malformed gzip'); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
2 |
|
return true; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* extract a tar archive to a target folder |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $tarFilename |
|
78
|
|
|
* @param string $targetFolder |
|
79
|
|
|
* @return boolean |
|
80
|
|
|
*/ |
|
81
|
1 |
|
public function untar(string $tarFilename, string $targetFolder): bool |
|
82
|
|
|
{ |
|
83
|
1 |
|
if (empty($tarFilename) |
|
84
|
1 |
|
|| empty($targetFolder) |
|
85
|
1 |
|
|| !file_exists($tarFilename) |
|
86
|
|
|
) { |
|
87
|
1 |
|
return false; |
|
88
|
|
|
} |
|
89
|
1 |
|
(new \PharData($tarFilename))->extractTo($targetFolder); |
|
90
|
1 |
|
return true; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* copy file from to and returns operation status |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $from |
|
97
|
|
|
* @param string $to |
|
98
|
|
|
* @return boolean |
|
99
|
|
|
*/ |
|
100
|
5 |
|
public function copyFile(string $from, string $to): bool |
|
101
|
|
|
{ |
|
102
|
5 |
|
return @copy($from, $to); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* returns folders list for a given path |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $path |
|
109
|
|
|
* @return array |
|
110
|
|
|
*/ |
|
111
|
15 |
|
public function folderList(string $path): array |
|
112
|
|
|
{ |
|
113
|
15 |
|
if (false === is_dir($path)) { |
|
114
|
15 |
|
return []; |
|
115
|
|
|
} |
|
116
|
2 |
|
$list = @glob($path . '*', GLOB_ONLYDIR); |
|
117
|
2 |
|
return (false === $list) ? [] : $list; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* returns file list for a given path |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $path |
|
124
|
|
|
* @return array |
|
125
|
|
|
*/ |
|
126
|
1 |
|
public function fileList(string $path): array |
|
127
|
|
|
{ |
|
128
|
1 |
|
return @glob(rtrim($path, '/') . '/*.{*}', GLOB_BRACE); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* returns Y-m-d modification file date for a $path |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $path |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
2 |
|
public function fileDate(string $path): string |
|
138
|
|
|
{ |
|
139
|
2 |
|
clearstatcache(); |
|
140
|
2 |
|
if (!file_exists($path)) { |
|
141
|
1 |
|
return '1971-01-14'; |
|
142
|
|
|
} |
|
143
|
2 |
|
return date('Y-m-d', filemtime($path)); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* returns true if modified file date is today |
|
148
|
|
|
* |
|
149
|
|
|
* @param string $path |
|
150
|
|
|
* @return string |
|
151
|
|
|
*/ |
|
152
|
1 |
|
public function isFileDateToday(string $path): bool |
|
153
|
|
|
{ |
|
154
|
1 |
|
return (date('Y-m-d') == $this->fileDate($path)); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* unlink multiples files from a path and a mask |
|
159
|
|
|
* |
|
160
|
|
|
* @param string $mask |
|
161
|
|
|
* @return boolean |
|
162
|
|
|
*/ |
|
163
|
15 |
|
public function unlinkFiles(string $mask): bool |
|
164
|
|
|
{ |
|
165
|
15 |
|
if (empty($mask)) { |
|
166
|
1 |
|
return false; |
|
167
|
|
|
} |
|
168
|
15 |
|
if (false === is_dir(dirname($mask))) { |
|
169
|
1 |
|
return false; |
|
170
|
|
|
} |
|
171
|
15 |
|
$toDelete = @glob($mask); |
|
172
|
15 |
|
if (false === $toDelete) { |
|
173
|
|
|
return false; |
|
174
|
|
|
} |
|
175
|
15 |
|
if ($toDelete == []) { |
|
176
|
9 |
|
return true; |
|
177
|
|
|
} |
|
178
|
7 |
|
$toDeleteCount = count($toDelete); |
|
179
|
7 |
|
$errors = 0; |
|
180
|
7 |
|
for ($c = 0; $c < $toDeleteCount; $c++) { |
|
181
|
7 |
|
$errors += (@unlink($toDelete[$c])) ? 0 : 1; |
|
182
|
|
|
} |
|
183
|
7 |
|
return ($errors === 0); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* unlink multiples folders even if not empty from a path |
|
188
|
|
|
* |
|
189
|
|
|
* @param string $mask |
|
190
|
|
|
* @return boolean |
|
191
|
|
|
*/ |
|
192
|
15 |
|
public function unlinkFolders(string $path): bool |
|
193
|
|
|
{ |
|
194
|
15 |
|
$toDelete = $this->folderList($path); |
|
195
|
15 |
|
$toDeleteCount = count($toDelete); |
|
196
|
15 |
|
$errors = 0; |
|
197
|
15 |
|
for ($c = 0; $c < $toDeleteCount; $c++) { |
|
198
|
1 |
|
$errors += ($this->deleteFolder($toDelete[$c])) ? 0 : 1; |
|
199
|
|
|
} |
|
200
|
15 |
|
return ($errors === 0); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* delete a not empty folder |
|
205
|
|
|
* |
|
206
|
|
|
* @param string $path |
|
207
|
|
|
* @return boolean |
|
208
|
|
|
*/ |
|
209
|
2 |
|
public function deleteFolder(string $path): bool |
|
210
|
|
|
{ |
|
211
|
2 |
|
$errors = 0; |
|
212
|
2 |
|
$fsItNoDots = \FilesystemIterator::SKIP_DOTS; |
|
213
|
2 |
|
$rdiT = new \RecursiveDirectoryIterator($path, $fsItNoDots); |
|
214
|
2 |
|
$riiFirstChild = \RecursiveIteratorIterator::CHILD_FIRST; |
|
215
|
2 |
|
$rii = new \RecursiveIteratorIterator($rdiT, $riiFirstChild); |
|
216
|
2 |
|
foreach ($rii as $file) { |
|
217
|
2 |
|
if ($file->isDir()) { |
|
218
|
2 |
|
$errors += (@rmdir($file->getPathname())) ? 0 : 1; |
|
219
|
|
|
} else { |
|
220
|
2 |
|
$errors += (@unlink($file->getPathname())) ? 0 : 1; |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
2 |
|
$errors += (@rmdir($path)) ? 0 : 1; |
|
224
|
2 |
|
return ($errors === 0); |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|