|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Simple S3 package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Mauro Cassani<https://github.com/mauretto78> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Matecat\SimpleS3\Helpers; |
|
13
|
|
|
|
|
14
|
|
|
use FilesystemIterator; |
|
15
|
|
|
use RecursiveDirectoryIterator; |
|
16
|
|
|
use RecursiveIteratorIterator; |
|
17
|
|
|
|
|
18
|
|
|
class File |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @param string $path |
|
22
|
|
|
* |
|
23
|
|
|
* @return bool |
|
24
|
|
|
*/ |
|
25
|
10 |
|
public static function checkIfIsADir(string $path): bool |
|
26
|
|
|
{ |
|
27
|
10 |
|
if (str_contains($path, DIRECTORY_SEPARATOR)) { |
|
28
|
6 |
|
return true; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
6 |
|
return false; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param string $string |
|
36
|
|
|
* @param string $separator |
|
37
|
|
|
* |
|
38
|
|
|
* @return bool |
|
39
|
|
|
*/ |
|
40
|
26 |
|
public static function endsWith(string $string, string $separator): bool |
|
41
|
|
|
{ |
|
42
|
26 |
|
return substr($string, -1) === $separator; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $path |
|
47
|
|
|
* |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
10 |
|
public static function getBaseName(string $path): string |
|
51
|
|
|
{ |
|
52
|
10 |
|
if (!self::checkIfIsADir($path)) { |
|
53
|
6 |
|
return $path; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
6 |
|
return self::getPathInfo($path)[ 'basename' ]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $filename |
|
61
|
|
|
* |
|
62
|
|
|
* @return string|null |
|
63
|
|
|
*/ |
|
64
|
|
|
public static function getExtension(string $filename): ?string |
|
65
|
|
|
{ |
|
66
|
|
|
return self::getPathInfo($filename)[ 'extension' ]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $filename |
|
71
|
|
|
* @param int $mode |
|
72
|
|
|
* |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function getMimeType(string $filename, int $mode = 0): string |
|
76
|
|
|
{ |
|
77
|
|
|
// mode 0 = full check |
|
78
|
|
|
// mode 1 = extension check only |
|
79
|
|
|
|
|
80
|
|
|
$mimetype = ''; |
|
81
|
|
|
|
|
82
|
|
|
$mime_types = [ |
|
83
|
|
|
'txt' => 'text/plain', |
|
84
|
|
|
'htm' => 'text/html', |
|
85
|
|
|
'html' => 'text/html', |
|
86
|
|
|
'php' => 'text/html', |
|
87
|
|
|
'css' => 'text/css', |
|
88
|
|
|
'js' => 'application/javascript', |
|
89
|
|
|
'json' => 'application/json', |
|
90
|
|
|
'xml' => 'application/xml', |
|
91
|
|
|
'swf' => 'application/x-shockwave-flash', |
|
92
|
|
|
'flv' => 'video/x-flv', |
|
93
|
|
|
|
|
94
|
|
|
// images |
|
95
|
|
|
'png' => 'image/png', |
|
96
|
|
|
'jpe' => 'image/jpeg', |
|
97
|
|
|
'jpeg' => 'image/jpeg', |
|
98
|
|
|
'jpg' => 'image/jpeg', |
|
99
|
|
|
'gif' => 'image/gif', |
|
100
|
|
|
'bmp' => 'image/bmp', |
|
101
|
|
|
'ico' => 'image/vnd.microsoft.icon', |
|
102
|
|
|
'tiff' => 'image/tiff', |
|
103
|
|
|
'tif' => 'image/tiff', |
|
104
|
|
|
'svg' => 'image/svg+xml', |
|
105
|
|
|
'svgz' => 'image/svg+xml', |
|
106
|
|
|
|
|
107
|
|
|
// archives |
|
108
|
|
|
'zip' => 'application/zip', |
|
109
|
|
|
'rar' => 'application/x-rar-compressed', |
|
110
|
|
|
'exe' => 'application/x-msdownload', |
|
111
|
|
|
'msi' => 'application/x-msdownload', |
|
112
|
|
|
'cab' => 'application/vnd.ms-cab-compressed', |
|
113
|
|
|
|
|
114
|
|
|
// audio/video |
|
115
|
|
|
'mp3' => 'audio/mpeg', |
|
116
|
|
|
'qt' => 'video/quicktime', |
|
117
|
|
|
'mov' => 'video/quicktime', |
|
118
|
|
|
|
|
119
|
|
|
// adobe |
|
120
|
|
|
'pdf' => 'application/pdf', |
|
121
|
|
|
'psd' => 'image/vnd.adobe.photoshop', |
|
122
|
|
|
'ai' => 'application/postscript', |
|
123
|
|
|
'eps' => 'application/postscript', |
|
124
|
|
|
'ps' => 'application/postscript', |
|
125
|
|
|
|
|
126
|
|
|
// ms office |
|
127
|
|
|
'doc' => 'application/msword', |
|
128
|
|
|
'rtf' => 'application/rtf', |
|
129
|
|
|
'xls' => 'application/vnd.ms-excel', |
|
130
|
|
|
'ppt' => 'application/vnd.ms-powerpoint', |
|
131
|
|
|
'docx' => 'application/msword', |
|
132
|
|
|
'xlsx' => 'application/vnd.ms-excel', |
|
133
|
|
|
'pptx' => 'application/vnd.ms-powerpoint', |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
// open office |
|
137
|
|
|
'odt' => 'application/vnd.oasis.opendocument.text', |
|
138
|
|
|
'ods' => 'application/vnd.oasis.opendocument.spreadsheet', |
|
139
|
|
|
]; |
|
140
|
|
|
|
|
141
|
|
|
if (function_exists('mime_content_type') and $mode === 0) { |
|
142
|
|
|
return mime_content_type($filename); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
if (function_exists('finfo_open') and $mode === 0) { |
|
146
|
|
|
$finfo = finfo_open(FILEINFO_MIME); |
|
147
|
|
|
|
|
148
|
|
|
if (false !== $finfo) { |
|
149
|
|
|
$mimetype = finfo_file($finfo, $filename); |
|
150
|
|
|
finfo_close($finfo); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return $mimetype; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
$ext = self::getExtension($filename); |
|
157
|
|
|
|
|
158
|
|
|
if (null !== $ext and array_key_exists($ext, $mime_types)) { |
|
159
|
|
|
return $mime_types[ $ext ]; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return 'application/octet-stream'; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param string $path |
|
167
|
|
|
* |
|
168
|
|
|
* @return array |
|
169
|
|
|
*/ |
|
170
|
23 |
|
public static function getPathInfo(string $path): array |
|
171
|
|
|
{ |
|
172
|
23 |
|
return pathinfo($path); |
|
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @param string $filename |
|
177
|
|
|
* |
|
178
|
|
|
* @return false|int |
|
179
|
|
|
*/ |
|
180
|
9 |
|
public static function getSize(string $filename): false|int |
|
181
|
|
|
{ |
|
182
|
9 |
|
return filesize($filename); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param string $url |
|
187
|
|
|
* @param bool $sslVerify |
|
188
|
|
|
* |
|
189
|
|
|
* @return bool|string |
|
190
|
|
|
*/ |
|
191
|
3 |
|
public static function loadFile(string $url, bool $sslVerify = true): bool|string |
|
192
|
|
|
{ |
|
193
|
3 |
|
if (function_exists('curl_version')) { |
|
194
|
3 |
|
$ch = curl_init(); |
|
195
|
|
|
|
|
196
|
3 |
|
$verifyPeer = $sslVerify ? 1 : 0; |
|
197
|
3 |
|
$verifyHost = $sslVerify ? 2 : 0; |
|
198
|
|
|
|
|
199
|
3 |
|
curl_setopt($ch, CURLOPT_HEADER, 0); |
|
200
|
3 |
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
201
|
3 |
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $verifyHost); |
|
202
|
3 |
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verifyPeer); |
|
203
|
3 |
|
curl_setopt($ch, CURLOPT_URL, $url); |
|
204
|
|
|
|
|
205
|
3 |
|
$data = curl_exec($ch); |
|
206
|
3 |
|
curl_close($ch); |
|
207
|
|
|
|
|
208
|
3 |
|
return $data; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
$context = stream_context_create([ |
|
212
|
|
|
'ssl' => [ |
|
213
|
|
|
'verify_peer' => $sslVerify, |
|
214
|
|
|
'verify_peer_name' => $sslVerify, |
|
215
|
|
|
] |
|
216
|
|
|
]); |
|
217
|
|
|
|
|
218
|
|
|
return file_get_contents($url, false, $context); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @param string $filename |
|
223
|
|
|
* @param bool $sslVerify |
|
224
|
|
|
* |
|
225
|
|
|
* @return bool|resource |
|
226
|
|
|
*/ |
|
227
|
9 |
|
public static function open(string $filename, bool $sslVerify = true) |
|
228
|
|
|
{ |
|
229
|
9 |
|
$context = stream_context_create([ |
|
230
|
9 |
|
'ssl' => [ |
|
231
|
9 |
|
'verify_peer' => $sslVerify, |
|
232
|
9 |
|
'verify_peer_name' => $sslVerify, |
|
233
|
9 |
|
] |
|
234
|
9 |
|
]); |
|
235
|
|
|
|
|
236
|
9 |
|
return fopen($filename, 'r', false, $context); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @param string $dir |
|
241
|
|
|
* @param bool $removeItself |
|
242
|
|
|
*/ |
|
243
|
1 |
|
public static function cleanDir(string $dir, bool $removeItself = false): void |
|
244
|
|
|
{ |
|
245
|
1 |
|
$files = new RecursiveIteratorIterator( |
|
246
|
1 |
|
new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS), |
|
247
|
1 |
|
RecursiveIteratorIterator::CHILD_FIRST |
|
248
|
1 |
|
); |
|
249
|
|
|
|
|
250
|
1 |
|
foreach ($files as $fileInfo) { |
|
251
|
1 |
|
if ($fileInfo->getFilename() == '.keep') { |
|
252
|
1 |
|
$removeItself = false; |
|
253
|
1 |
|
continue; |
|
254
|
|
|
} |
|
255
|
1 |
|
$todo = ($fileInfo->isDir() ? 'rmdir' : 'unlink'); |
|
256
|
1 |
|
$todo($fileInfo->getRealPath()); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
1 |
|
if ($removeItself) { |
|
260
|
|
|
rmdir($dir); |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|