1
|
|
|
<?php |
2
|
|
|
/******************************************************************* |
3
|
|
|
* Created by: Marko Kungla @ OkramLabs on Aug 6, 2012 - 9:36:42 |
4
|
|
|
* Contact: [email protected] - https://okramlabs.com |
5
|
|
|
* @copyright 2015 OkramLabs - https://okramlabs.com |
6
|
|
|
* @license MIT |
7
|
|
|
* |
8
|
|
|
* Package name: libhowi-filesystem |
9
|
|
|
* @category HOWI3 |
10
|
|
|
* @package libhowi |
11
|
|
|
* @subpackage filesystem |
12
|
|
|
* |
13
|
|
|
* Lang: PHP |
14
|
|
|
* Encoding: UTF-8 |
15
|
|
|
* File: TraitForFileSystem.inc |
16
|
|
|
* @link https:// |
17
|
|
|
******************************************************************** |
18
|
|
|
* Contributors: |
19
|
|
|
* @author Marko Kungla <[email protected]> |
20
|
|
|
* Github: https://github.com/mkungla |
21
|
|
|
******************************************************************** |
22
|
|
|
* Comments: |
23
|
|
|
*/ |
24
|
|
|
namespace HOWI3\libhowi\Filesystem\php7; |
25
|
|
|
|
26
|
|
|
use \RecursiveIteratorIterator; |
27
|
|
|
use \HOWI3\libhowi\Filesystem\php7\Objects\DirectoryTreeObject; |
28
|
|
|
|
29
|
|
View Code Duplication |
trait TraitForFileSystem |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
* {@inheritDoc} |
35
|
|
|
* |
36
|
|
|
*/ |
37
|
6 |
|
public function cd($path = false, $validate_dir_name = false) |
|
|
|
|
38
|
|
|
{ |
39
|
6 |
|
return $this->setCwd($path, $validate_dir_name); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* rmdir |
44
|
|
|
* |
45
|
|
|
* {@inheritDoc} |
46
|
|
|
* |
47
|
|
|
*/ |
48
|
2 |
|
public function rm($pathname, $shred = false) |
49
|
|
|
{ |
50
|
2 |
|
$pathname = $this->makeAbsolute($pathname); |
|
|
|
|
51
|
2 |
|
$response = false; |
52
|
2 |
|
if (is_dir($pathname)) { |
53
|
|
|
|
54
|
2 |
|
$files = array_diff(scandir($pathname), |
55
|
|
|
array( |
56
|
2 |
|
'.', |
57
|
|
|
'..' |
58
|
|
|
)); |
59
|
2 |
|
foreach ($files as $file) { |
60
|
|
|
|
61
|
1 |
|
(is_dir("$pathname/$file") ? $this->rm("$pathname/$file", $shred) : (! empty($shred) && |
62
|
1 |
|
! $this->isLink($pathname . DIRECTORY_SEPARATOR . $file) ? exec( |
|
|
|
|
63
|
1 |
|
"shred -fzu $pathname" . DIRECTORY_SEPARATOR . "$file") : $this->rm( |
64
|
1 |
|
$pathname . DIRECTORY_SEPARATOR . $file, $shred))); |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
$response = rmdir($pathname); |
68
|
2 |
|
$this->info(702, $pathname); |
|
|
|
|
69
|
2 |
|
$this->response()->setStatus($response); |
|
|
|
|
70
|
|
|
} else { |
71
|
2 |
|
$this->info(702, $pathname); |
|
|
|
|
72
|
2 |
|
$this->response()->setStatus($response); |
|
|
|
|
73
|
2 |
|
(! empty($shred) && ! $this->isLink($pathname) ? exec("shred -fzu $pathname") : unlink($pathname)); |
|
|
|
|
74
|
|
|
} |
75
|
2 |
|
return $response; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* |
80
|
|
|
* {@inheritDoc} |
81
|
|
|
* |
82
|
|
|
*/ |
83
|
1 |
|
public function scandir($path = false) |
84
|
|
|
{ |
85
|
1 |
|
return ! empty($path) && is_dir($this->makeAbsolute($path)) ? scandir($this->makeAbsolute($path)) : false; |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* |
90
|
|
|
* {@inheritDoc} |
91
|
|
|
* |
92
|
|
|
*/ |
93
|
1 |
|
public function stat($filename = false) |
94
|
|
|
{ |
95
|
1 |
|
return ! empty($filename) && file_exists($this->makeAbsolute($filename)) ? stat( |
|
|
|
|
96
|
1 |
|
$this->makeAbsolute($filename)) : false; |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* |
101
|
|
|
* {@inheritDoc} |
102
|
|
|
* |
103
|
|
|
*/ |
104
|
1 |
|
public function getDiskTotalSpace($partition_location = false, $convert = false) |
105
|
|
|
{ |
106
|
1 |
|
if (empty($partition_location)) |
107
|
1 |
|
$partition_location = $this->getCwd(); |
|
|
|
|
108
|
|
|
|
109
|
1 |
|
if (empty($convert)) |
110
|
1 |
|
return disk_total_space($partition_location); |
111
|
|
|
|
112
|
1 |
|
$bytestotal = 0; |
|
|
|
|
113
|
|
|
$suffixes = array( |
114
|
1 |
|
'B', |
115
|
|
|
'kB', |
116
|
|
|
'MB', |
117
|
|
|
'GB', |
118
|
|
|
'TB', |
119
|
|
|
'PB', |
120
|
|
|
'EB', |
121
|
|
|
'ZB', |
122
|
|
|
'YB' |
123
|
|
|
); |
124
|
1 |
|
$bytestotal = disk_total_space($partition_location); |
125
|
|
|
|
126
|
1 |
|
$base = log($bytestotal, 1024); |
127
|
|
|
return array( |
128
|
1 |
|
round(pow(1024, $base - floor($base)), 2), |
129
|
1 |
|
$suffixes[floor($base)] |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* |
135
|
|
|
* {@inheritDoc} |
136
|
|
|
* |
137
|
|
|
*/ |
138
|
1 |
|
public function getDiskFreeSpace($partition_location = false, $convert = false) |
139
|
|
|
{ |
140
|
1 |
|
if (empty($partition_location)) |
141
|
1 |
|
$partition_location = $this->getCwd(); |
|
|
|
|
142
|
|
|
|
143
|
1 |
|
if (empty($convert)) |
144
|
1 |
|
return disk_free_space($partition_location); |
145
|
|
|
|
146
|
|
|
$result = [ |
147
|
1 |
|
0, |
148
|
|
|
'B' |
149
|
|
|
]; |
150
|
1 |
|
$bytestotal = 0; |
|
|
|
|
151
|
|
|
$suffixes = array( |
152
|
1 |
|
'B', |
153
|
|
|
'kB', |
154
|
|
|
'MB', |
155
|
|
|
'GB', |
156
|
|
|
'TB', |
157
|
|
|
'PB', |
158
|
|
|
'EB', |
159
|
|
|
'ZB', |
160
|
|
|
'YB' |
161
|
|
|
); |
162
|
1 |
|
$bytestotal = disk_free_space($partition_location); |
163
|
|
|
|
164
|
1 |
|
$base = log($bytestotal, 1024); |
165
|
|
|
|
166
|
1 |
|
if ($bytestotal > 0) { |
167
|
|
|
$result = array( |
168
|
1 |
|
round(pow(1024, $base - floor($base)), 2), |
169
|
1 |
|
$suffixes[floor($base)] |
170
|
|
|
); |
171
|
|
|
} |
172
|
1 |
|
return $result; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* |
177
|
|
|
* {@inheritDoc} |
178
|
|
|
* |
179
|
|
|
*/ |
180
|
1 |
|
public function chgrp($filename = false, $group = false) |
181
|
|
|
{ |
182
|
1 |
|
return ! empty($filename) && $this->exists($this->makeAbsolute($filename)) ? chgrp( |
|
|
|
|
183
|
1 |
|
$this->makeAbsolute($filename), $group) : false; |
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* |
188
|
|
|
* {@inheritDoc} |
189
|
|
|
* |
190
|
|
|
*/ |
191
|
1 |
|
public function chmod($filename = false, $mode = false) |
192
|
|
|
{ |
193
|
1 |
|
$filename = $this->makeAbsolute($filename); |
|
|
|
|
194
|
1 |
|
if (file_exists($filename) && ! empty($mode) && is_int($mode)) |
195
|
1 |
|
return chmod($filename, octdec(str_pad($mode, 4, 0, STR_PAD_LEFT))); |
196
|
|
|
else |
197
|
1 |
|
return false; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* |
202
|
|
|
* {@inheritDoc} |
203
|
|
|
* |
204
|
|
|
*/ |
205
|
1 |
|
public function chown($filename = false, $user = false) |
206
|
|
|
{ |
207
|
1 |
|
$filename = $this->makeAbsolute($filename); |
|
|
|
|
208
|
1 |
|
if (file_exists($filename) && ! empty($user)) |
209
|
1 |
|
return chown($filename, $user); |
210
|
|
|
else |
211
|
1 |
|
return false; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* |
216
|
|
|
* {@inheritDoc} |
217
|
|
|
* |
218
|
|
|
*/ |
219
|
2 |
|
public function clearstatcache($clear_realpath_cache = false, $filename = false) |
220
|
|
|
{ |
221
|
2 |
|
if (! empty($filename) && ! empty($clear_realpath_cache)) |
222
|
1 |
|
clearstatcache($clear_realpath_cache, $this->makeAbsolute($filename)); |
|
|
|
|
223
|
|
|
else |
224
|
2 |
|
clearstatcache(); |
225
|
2 |
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* |
229
|
|
|
* {@inheritDoc} |
230
|
|
|
* |
231
|
|
|
*/ |
232
|
1 |
|
public function copy($source = false, $dest = false, $context = false) |
233
|
|
|
{ |
234
|
1 |
|
$source = $this->makeAbsolute($source); |
|
|
|
|
235
|
1 |
|
$dest = $this->makeAbsolute($dest); |
|
|
|
|
236
|
|
|
|
237
|
1 |
|
if (! empty($source) && ! empty($dest) && ! is_dir($source)) { |
238
|
|
|
|
239
|
1 |
|
if (empty($context)) |
240
|
1 |
|
return copy($source, $dest); |
241
|
|
|
else |
242
|
1 |
|
return copy($source, $dest, $context); |
243
|
1 |
|
} elseif ((! empty($source) && ! empty($dest)) && is_dir($source) && ! file_exists($dest)) { |
244
|
|
|
|
245
|
1 |
|
$tmp_key = basename($source); |
246
|
1 |
|
$tmp_key2 = basename($dest); |
247
|
1 |
|
$tmp_src_key = $this->dir($tmp_key, dirname($source)); |
|
|
|
|
248
|
|
|
|
249
|
1 |
|
$this->dir($tmp_key2, dirname($dest), false, $tmp_src_key->getPerms()); |
|
|
|
|
250
|
1 |
|
$this->close($tmp_key); |
|
|
|
|
251
|
1 |
|
$this->close($tmp_key2); |
|
|
|
|
252
|
|
|
|
253
|
1 |
|
foreach ($iterator = new RecursiveIteratorIterator( |
254
|
1 |
|
new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS), // DirectoryTreeObject::CURRENT_AS_FILEINFO |
255
|
1 |
|
\RecursiveIteratorIterator::SELF_FIRST) as $item) { |
256
|
1 |
|
if ($item->isDir()) { |
257
|
1 |
|
if (empty($context)) { |
258
|
1 |
|
mkdir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName(), |
259
|
1 |
|
octdec(str_pad($iterator->getPerms(), 4, 0, STR_PAD_LEFT))); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
else |
263
|
1 |
|
mkdir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName(), |
264
|
1 |
|
octdec(str_pad($iterator->getPerms(), 4, 0, STR_PAD_LEFT)), false, $context); |
265
|
|
|
} else { |
266
|
1 |
|
if (empty($context)) |
267
|
1 |
|
$this->copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); |
|
|
|
|
268
|
|
|
else |
269
|
1 |
|
$this->copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName(), |
|
|
|
|
270
|
|
|
$context); |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
274
|
1 |
|
return true; |
275
|
|
|
} else |
276
|
1 |
|
return false; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* |
281
|
|
|
* {@inheritDoc} |
282
|
|
|
* |
283
|
|
|
*/ |
284
|
1 |
|
public function mv($oldname = false, $newname = false, $context = false) |
285
|
|
|
{ |
286
|
1 |
|
if (empty($oldname) || empty($newname)) |
287
|
1 |
|
return false; |
288
|
|
|
|
289
|
1 |
|
$oldname = $this->makeAbsolute($oldname); |
|
|
|
|
290
|
1 |
|
$newname = $this->makeAbsolute($newname); |
|
|
|
|
291
|
|
|
|
292
|
1 |
|
if (! empty($context)) |
293
|
|
|
return rename($oldname, $newname, $context); |
294
|
|
|
|
295
|
1 |
|
return rename($oldname, $newname); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* |
300
|
|
|
* {@inheritDoc} |
301
|
|
|
* |
302
|
|
|
*/ |
303
|
1 |
|
public function namePatternMatch($pattern = false, $string = false, $flags = false) |
304
|
|
|
{ |
305
|
1 |
|
if (empty($pattern)) |
306
|
1 |
|
return false; |
307
|
|
|
|
308
|
1 |
|
if (! empty($flags)) |
309
|
|
|
return fnmatch($pattern, $string, $flags); |
310
|
|
|
else |
311
|
1 |
|
return fnmatch($pattern, $string); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* |
316
|
|
|
* {@inheritDoc} |
317
|
|
|
* |
318
|
|
|
*/ |
319
|
1 |
|
public function getGlob($pattern = false, $flags = 0) |
320
|
|
|
{ |
321
|
1 |
|
return glob($pattern, $flags); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* |
326
|
|
|
* {@inheritDoc} |
327
|
|
|
* |
328
|
|
|
*/ |
329
|
1 |
|
public function isUploadedFile($filename = false) |
330
|
|
|
{ |
331
|
1 |
|
$filename = $this->makeAbsolute($filename); |
|
|
|
|
332
|
1 |
|
return is_uploaded_file($filename); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* |
337
|
|
|
* {@inheritDoc} |
338
|
|
|
* |
339
|
|
|
*/ |
340
|
1 |
|
public function getRealpathCache() |
341
|
|
|
{ |
342
|
1 |
|
return realpath_cache_get(); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* |
347
|
|
|
* {@inheritDoc} |
348
|
|
|
* |
349
|
|
|
*/ |
350
|
1 |
|
public function getRealpathCacheSize() |
351
|
|
|
{ |
352
|
1 |
|
return realpath_cache_size(); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* |
357
|
|
|
* {@inheritDoc} |
358
|
|
|
* |
359
|
|
|
*/ |
360
|
2 |
|
public function close($keyword = false) |
361
|
|
|
{ |
362
|
2 |
|
if (!empty($keyword) && array_key_exists($keyword, $this->dirkeys) && |
|
|
|
|
363
|
2 |
|
array_key_exists($this->dirkeys[$keyword], $this->dirs)) { |
|
|
|
|
364
|
2 |
|
unset($this->dirs[$this->dirkeys[$keyword]]); |
365
|
2 |
|
unset($this->dirkeys[$keyword]); |
366
|
2 |
|
return true; |
367
|
1 |
|
} elseif(!empty($keyword) && array_key_exists($keyword,$this->files)) { |
|
|
|
|
368
|
1 |
|
$this->files[$keyword] = null; |
369
|
1 |
|
unset($this->files[$keyword]); |
370
|
1 |
|
return true; |
371
|
|
|
} else { |
372
|
1 |
|
return false; |
373
|
|
|
} |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* |
378
|
|
|
* {@inheritDoc} |
379
|
|
|
* |
380
|
|
|
*/ |
381
|
1 |
|
public function touch($filename = false, $time = false, $atime = false) |
382
|
|
|
{ |
383
|
1 |
|
if (empty($filename)) |
384
|
|
|
return false; |
385
|
|
|
|
386
|
1 |
|
$filename = $this->makeAbsolute($filename); |
|
|
|
|
387
|
|
|
|
388
|
1 |
|
$time = ! empty($time) ? $time : time(); |
389
|
1 |
|
$atime = ! empty($atime) ? $atime : time(); |
390
|
|
|
|
391
|
1 |
|
return touch($filename, $time, $atime); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* |
396
|
|
|
* {@inheritDoc} |
397
|
|
|
* |
398
|
|
|
*/ |
399
|
1 |
|
public function getDirKeys() |
|
|
|
|
400
|
|
|
{ |
401
|
1 |
|
return $this->dirkeys; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* |
406
|
|
|
* {@inheritDoc} |
407
|
|
|
* |
408
|
|
|
*/ |
409
|
1 |
|
public function getFileKeys() |
|
|
|
|
410
|
|
|
{ |
411
|
1 |
|
return $this->files; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* |
416
|
|
|
* {@inheritDoc} |
417
|
|
|
* |
418
|
|
|
*/ |
419
|
2 |
|
public function createStructure($rootpath, $data_array, $skip_debug = false) |
420
|
|
|
{ |
421
|
2 |
|
$absolute_path = $this->makeAbsolute($rootpath); |
|
|
|
|
422
|
2 |
|
if (empty($skip_debug)) |
423
|
2 |
|
$this->debug(810); |
|
|
|
|
424
|
2 |
|
if (empty($rootpath) || ! is_array($data_array) || |
425
|
2 |
|
(! $this->isWritable($absolute_path) && ! $this->isWritable(dirname($absolute_path)))) |
|
|
|
|
426
|
2 |
|
return $this->warning(506) && false; |
|
|
|
|
427
|
|
|
|
428
|
2 |
|
if (! $this->isDir($absolute_path)) |
|
|
|
|
429
|
2 |
|
mkdir($absolute_path, 0755, true); |
430
|
|
|
|
431
|
2 |
|
foreach ($data_array as $key => $val) { |
432
|
2 |
|
if (is_array($val)) |
433
|
2 |
|
! $this->createStructure($absolute_path . DIRECTORY_SEPARATOR . $key, $val, true); |
434
|
|
|
else |
435
|
2 |
|
touch($absolute_path . DIRECTORY_SEPARATOR . $val); |
436
|
|
|
} |
437
|
2 |
|
if (empty($skip_debug)) |
438
|
2 |
|
$this->info(704, $rootpath); |
|
|
|
|
439
|
2 |
|
return true; |
440
|
|
|
} |
441
|
|
|
} |
442
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.