Code Duplication    Length = 413-413 lines in 2 locations

php5/TraitForFileSystem.inc 1 location

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

php7/TraitForFileSystem.inc 1 location

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