-
Notifications
You must be signed in to change notification settings - Fork 1
/
FilesystemUtils.php
306 lines (274 loc) · 8.26 KB
/
FilesystemUtils.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @copyright 2010 onwards James McQuillan (http://pdyn.net)
* @author James McQuillan <james@pdyn.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace pdyn\filesystem;
/**
* A collection of filesystem utilities.
*/
class FilesystemUtils {
/**
* Determine the mime-type of a file.
*
* @param string $filename The full, absolute path to a file.
* @return string The mime type.
*/
public static function get_mime_type($filename) {
if (extension_loaded('fileinfo')) {
try {
$finfo = new \finfo(FILEINFO_MIME_TYPE);
return $finfo->file($filename);
} catch (\Exception $e) {
// Try next option...
}
}
if (function_exists('mime_content_type')) {
try {
return mime_content_type($filename);
} catch (\Exception $e) {
// Try next option...
}
}
$mime = \pdyn\filesystem\Mimetype::ext2mime(static::get_ext($filename));
// Strip out encoding, if present.
if (mb_strpos($mime, ';') !== false) {
$mime = explode(';', $mime);
$mime = $mime[0];
}
return $mime;
}
/**
* This helps us create normalized filenames.
*
* As the point of this function is to alter the input, the result should not be used to refer to the original, but instead
* used as a normalized name for storing new files.
*
* @param string $input The input filename.
* @return string Normalized filename.
*/
public static function normalize_filename($input) {
if (empty($input)) {
return false;
}
$pathinfo = pathinfo($input);
// Normalize extension.
$ext = mb_strtolower($pathinfo['extension']);
if ($ext === 'jpeg') {
$ext = 'jpg';
}
// Rebuild filename.
$filename = $pathinfo['filename'].'.'.$ext;
// Sanitize.
$filename = static::sanitize_filename($filename);
return $filename;
}
/**
* Sanitize a filename.
*
* Specifically, this removes directory traversal via ./ and ../
*
* @param string $i The input value.
* @param boolean $allow_subdirs Whether to allow forward directory traversal (i.e. subdirectories).
* @return string The sanitized string.
*/
public static function sanitize_filename($i, $allow_subdirs = false, $allowabsolute = false) {
$i = trim($i);
$replacements = ['../', './'];
if ($allow_subdirs === false) {
$replacements[] = '/';
}
if ($allowabsolute !== true && $i{0} === '/') {
$i = substr($i, 1);
}
return str_replace($replacements, '', $i);
}
/**
* Create a complete directory structure in a given path.
*
* @param array $folders An array representing the structure to be created. Keys are folder names, values are arrays of
* subfolders (with keys being names and values being subfolders, etc)
* @param string $base_dir The directory to create the structure in. Must include trailing slash.
*/
public static function create_directory_structure($folders, $base_dir) {
if (!empty($folders) && is_array($folders)) {
foreach ($folders as $name => $subfolders) {
if (!file_exists($base_dir.$name.'/')) {
mkdir($base_dir.$name.'/');
}
touch($base_dir.$name.'/index.html');
if (!empty($subfolders) && is_array($subfolders)) {
static::create_directory_structure($subfolders, $base_dir.$name.'/');
}
}
}
}
/**
* Delete a folder and all subfolders.
*
* @param string $path The absolute path to the folder to delete.
* @return bool Success/Failure.
*/
public static function rrmdir($path) {
if (is_file($path)) {
return unlink($path);
} elseif (is_dir($path)) {
$dir_members = scandir($path);
foreach ($dir_members as $member) {
if ($member !== '.' && $member !== '..') {
static::rrmdir($path.'/'.$member);
}
}
return @rmdir($path);
}
}
/**
* Unzip a zip file into a directory.
*
* @param string $file The absolute path to a zip file.
* @param string $destination The absolute path to the directory to unzip to file into.
* @return bool Success/Failure.
*/
public static function unzip($file, $destination) {
if (!file_exists($file)) {
return false;
}
if (!file_exists($destination)) {
mkdir($destination);
}
if (class_exists('ZipArchive')) {
$result = static::unzip_using_ziparchive($file, $destination);
if ($result === true) {
return true;
}
} else {
throw new \Exception('No zip support available', 500);
}
}
/**
* Unzip a file into a directory using the ZipArchive class.
*
* @param string $file The absolute path to a zip file.
* @param string $destination The absolute path to the directory to unzip to file into.
* @return bool Success/Failure.
*/
public static function unzip_using_ziparchive($file, $destination) {
$za = new \ZipArchive;
$zares = $za->open($file);
if ($zares === true) {
$za->extractTo($destination);
$za->close();
return true;
} else {
return false;
}
}
/**
* Get the general file type from a mime type.
*
* This returns the beginning part (before the slash) of a mime type, giving a more general category of the file.
*
* @param string $mime The mime type.
* @return string The general file type.
*/
public static function get_file_type_from_mime($mime) {
$validtypes = ['application', 'audio', 'image', 'message', 'multipart', 'text', 'video'];
if (mb_stripos($mime, '/') === false) {
// Malformed mime type.
return false;
}
$iparts = explode('/', $mime);
if (count($iparts) !== 2) {
// Malformed mime type.
return false;
}
return (in_array($iparts[0], $validtypes, true)) ? $iparts[0] : false;
}
/**
* Get the mime type of a file using only the filename.
*
* This will return the mime type of a file using the files extension. It will not check if the file exists, open the file,
* analyze the file, etc. Therefore, understand that the returned mime type may not reflect what is actually in the file.
*
* @param string $fn The filename.
* @return string The mime type.
*/
public static function get_mime_from_filename($fn) {
return \pdyn\filesystem\Mimetype::ext2mime(static::get_ext($fn));
}
/**
* Get the file extension of a filename.
*
* @param string $filename The filename.
* @return string The extension.
*/
public static function get_ext($filename) {
return mb_substr(mb_strrchr($filename, '.'), 1);
}
/**
* Convert bytes into a human-readable format.
*
* For example: $bytes = 1024 would return 1KB, $bytes = 1048576 would return 1MB, etc.
*
* @param int $bytes The number of bytes to convert.
* @return string The human readable representation of the bytes.
*/
public static function make_human_readable_filesize($bytes) {
$units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
$num_units = count($units);
$size = $bytes;
foreach ($units as $i => $unit) {
if ($size < 1024 || (($i + 1) === $num_units)) {
return round($size, 2).$unit;
}
$size = $size / 1024;
}
return $size;
}
/**
* Get the disk usage of a given directory.
*
* @param string $dir The absolute path to the directory to get the disk usage of.
* @return int The disk usage of the directory in bytes.
*/
public static function dirsize($dir, $createifmissing = false) {
$dirsize = 0;
if (!file_exists($dir)) {
if ($createifmissing === true) {
mkdir($dir);
return 0;
}
throw new \Exception('Directory does not exist!', 400);
}
$dir_info = scandir($dir);
if ($dir{(mb_strlen($dir) - 1)} !== '/') {
$dir .= '/';
}
clearstatcache();
foreach ($dir_info as $i => $member) {
if ($member === '.' || $member === '..') {
continue;
}
$abs_member = $dir.$member;
if (is_file($abs_member)) {
$dirsize += filesize($abs_member);
} elseif (is_dir($abs_member)) {
$dirsize += static::dirsize($abs_member.'/');
}
}
return $dirsize;
}
}