Code Duplication    Length = 582-582 lines in 2 locations

php5/TraitForSharedMethods.inc 1 location

@@ 30-611 (lines=582) @@
27
use \RecursiveIteratorIterator;
28
use \DateTime;
29
30
trait TraitForSharedMethods
31
{
32
33
    /**
34
     *
35
     * {@inheritDoc}
36
     *
37
     */
38
    protected function setCwd($path = false, $validate_dir_name = false)
39
    {
40
        if (empty($path))
41
            $current_working_directory = $this->getCwd();
42
        else
43
            $current_working_directory = $this->makeAbsolute($path);
44
        
45
        if (! empty($validate_dir_name) && (basename($current_working_directory) !== $validate_dir_name))
46
            return false;
47
        
48
        if (! is_dir($current_working_directory) || ! is_readable($current_working_directory) ||
49
             ! chdir($current_working_directory)) {
50
            $this->alert(200, false, $current_working_directory);
51
            return false;
52
        }
53
        $this->cwd = $current_working_directory;
54
        return true;
55
    }
56
57
    /**
58
     *
59
     * {@inheritDoc}
60
     *
61
     */
62
    public function getCwd()
63
    {
64
        if (empty($this->cwd))
65
            $this->cwd = getcwd();
66
        
67
        return $this->cwd;
68
    }
69
70
    /**
71
     *
72
     * {@inheritDoc}
73
     *
74
     */
75
    public function getSize($convert = false, $filename = false)
76
    {
77
        $filename = ! empty($filename) ? $this->makeAbsolute($filename) : false;
78
        $bytestotal = empty($filename) ? ((method_exists(get_parent_class($this), 'getSize')) ? parent::getSize() : false) : filesize(
79
            $filename);
80
        
81
        if (! $this->isDir($filename) && empty($convert))
82
            return $bytestotal;
83
        
84
        $suffixes = array(
85
            'B',
86
            'kB',
87
            'MB',
88
            'GB',
89
            'TB',
90
            'PB',
91
            'EB',
92
            'ZB',
93
            'YB'
94
        );
95
        if ($this->isDir($filename)) {
96
            $path = ((method_exists(get_class($this), 'getPathname')) ? $this->getPathname() : $filename);
97
            if ($path === '/home/nitrotrigger/tmp/libhowi-filesystem/log/phpunit_test.log')
98
                die('ss');
99
            
100
            foreach (new RecursiveIteratorIterator(
101
                new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS)) as $object) {
102
                $bytestotal += $object->getSize();
103
            }
104
            if (! $convert)
105
                return $bytestotal;
106
        }
107
        $base = log($bytestotal, 1024);
108
        
109
        $result = [
110
            0,
111
            'B'
112
        ];
113
        if ($bytestotal > 0) {
114
            
115
            $result = array(
116
                round(pow(1024, $base - floor($base)), 2),
117
                $suffixes[floor($base)]
118
            );
119
        }
120
        return ! empty($convert) ? $result : $result[0];
121
    }
122
123
    /**
124
     *
125
     * {@inheritDoc}
126
     *
127
     */
128
    public function getATime($timeformat = false, $pathname = false)
129
    {
130
        $atime = empty($pathname) ? parent::getATime() : fileatime($this->makeAbsolute($pathname));
131
        if (! empty($timeformat) && is_int($atime)) {
132
            switch ($timeformat) {
133
                case 'ago':
134
                    $atime = $this->ct_ago($atime, 'y,m,d,h,i,s', false, true);
135
                    break;
136
                case 'ago.single':
137
                    $atime = $this->ct_ago($atime, 'y,m,d,h,i,s', false, true, true);
138
                    break;
139
                default:
140
                    $atime = date($timeformat, $atime);
141
                    break;
142
            }
143
        }
144
        return $atime;
145
    }
146
147
    /**
148
     *
149
     * {@inheritDoc}
150
     *
151
     */
152
    public function getCTime($timeformat = false, $pathname = false)
153
    {
154
        $ctime = empty($pathname) ? parent::getCTime() : filectime($this->makeAbsolute($pathname));
155
        if (! empty($timeformat) && is_int($ctime)) {
156
            switch ($timeformat) {
157
                case 'ago':
158
                    $ctime = $this->ct_ago($ctime, 'y,m,d,h,i,s', false, true);
159
                    break;
160
                case 'ago.single':
161
                    $ctime = $this->ct_ago($ctime, 'y,m,d,h,i,s', false, true, true);
162
                    break;
163
                default:
164
                    $ctime = date($timeformat, $ctime);
165
                    break;
166
            }
167
        }
168
        return $ctime;
169
    }
170
171
    /**
172
     *
173
     * {@inheritDoc}
174
     *
175
     */
176
    public function getMTime($timeformat = false, $pathname = false)
177
    {
178
        $mtime = empty($pathname) ? parent::getMTime() : filemtime($this->makeAbsolute($pathname));
179
        if (! empty($timeformat) && is_int($mtime)) {
180
            switch ($timeformat) {
181
                case 'ago':
182
                    $mtime = $this->ct_ago($mtime, 'y,m,d,h,i,s', false, true);
183
                    break;
184
                case 'ago.single':
185
                    $mtime = $this->ct_ago($mtime, 'y,m,d,h,i,s', false, true, true);
186
                    break;
187
                default:
188
                    $mtime = date($timeformat, $mtime);
189
                    break;
190
            }
191
        }
192
        return $mtime;
193
    }
194
195
    /**
196
     *
197
     * {@inheritDoc}
198
     *
199
     */
200
    public function ct_ago($timestamp, $date_format = 'y,m,d,h,i,s', $sfx = true, $ommit_zero = true, $ago_single = true, $clocale = [])
201
    {
202
        $locale = array_merge(
203
            array(
204
                'y' => array(
205
                    'y',
206
                    'year',
207
                    'years'
208
                ),
209
                'm' => array(
210
                    'm',
211
                    'month',
212
                    'months'
213
                ),
214
                'd' => $d = array(
215
                    'd',
216
                    'day',
217
                    'days'
218
                ),
219
                'h' => array(
220
                    'h',
221
                    'hour',
222
                    'hours'
223
                ),
224
                'i' => array(
225
                    'm',
226
                    'minute',
227
                    'minutes'
228
                ),
229
                's' => array(
230
                    's',
231
                    'second',
232
                    'seconds'
233
                ),
234
                'days' => $d,
235
                'ago' => 'ago',
236
                'now' => 'just now'
237
            ), $clocale);
238
        
239
        $date = new DateTime();
240
        $date->setTimestamp($timestamp);
241
        
242
        $interval = $date->diff(new DateTime('now'));
243
        
244
        for ($df = strtok($date_format, ','); $df !== false; $df = strtok(",")) {
245
            $lkey = ($df !== 'a') ? strtolower($df) : 'days';
246
            
247
            if ($ommit_zero && $interval->$lkey === 0)
248
                continue;
249
            
250
            $fparam = $df . (empty($sfx) ? $locale[$lkey][0] : ' ' .
251
                 (($interval->$lkey === 1) ? $locale[$lkey][1] : $locale[$lkey][2]));
252
            
253
            if (! empty($ago_single) && ! empty($dfs))
254
                break;
255
            else
256
                $dfs = ! empty($dfs) ? $dfs . "%$fparam " : "%$fparam ";
257
        }
258
        if (empty($dfs))
259
            $ret = $locale['now'];
260
        else
261
            $ret = $interval->format($dfs) . $locale['ago'];
262
        return $ret;
263
    }
264
265
    /**
266
     *
267
     * {@inheritDoc}
268
     *
269
     */
270
    public function openFile($open_mode = "r", $use_include_path = false, $context = null)
271
    {
272
        if (! method_exists(get_parent_class($this), 'openFile'))
273
            return false;
274
        
275
        return ! $this->isDir() ? (! empty($context) ? parent::openFile($open_mode, $use_include_path, 
276
            $context) : parent::openFile($open_mode, $use_include_path)) : false;
277
    }
278
279
    /**
280
     *
281
     * {@inheritDoc}
282
     *
283
     */
284
    public function setFileClass($class_name = "\HOWI3\libhowi\Filesystem\php5\Objects\FileObject")
285
    {
286
        if (! method_exists(get_parent_class($this), 'setFileClass'))
287
            return false;
288
        
289
        if (is_subclass_of($class_name, 'SplFileObject') || $class_name === 'SplFileObject') {
290
            parent::setFileClass($class_name);
291
            return true;
292
        }
293
        return false;
294
    }
295
296
    /**
297
     *
298
     * {@inheritDoc}
299
     *
300
     */
301
    public function setInfoClass($class_name = "\HOWI3\libhowi\Filesystem\php5\Objects\InfoObject")
302
    {
303
        if (! method_exists(get_parent_class($this), 'setInfoClass'))
304
            return false;
305
        
306
        if (is_subclass_of($class_name, 'SplFileInfo') || $class_name === 'SplFileInfo') {
307
            parent::setInfoClass($class_name);
308
            return true;
309
        }
310
        return false;
311
    }
312
313
    /**
314
     *
315
     * {@inheritDoc}
316
     *
317
     */
318
    public function getOwnerName($filename = false)
319
    {
320
        $userID = empty($filename) && method_exists(get_parent_class($this), 'getOwner') ? parent::getOwner() : (! empty(
321
            $filename) ? fileowner($this->makeAbsolute($filename)) : false);
322
        $user = ! empty($userID) ? posix_getpwuid($userID) : false;
323
        return is_array($user) && array_key_exists('name', $user) ? $user['name'] : false;
324
    }
325
326
    /**
327
     *
328
     * {@inheritDoc}
329
     *
330
     */
331
    public function getGroupName($filename = false)
332
    {
333
        $groupID = empty($filename) ? ((method_exists(get_parent_class($this), 'getGroup')) ? parent::getGroup() : false) : filegroup(
334
            $this->makeAbsolute($filename));
335
        $group = ! empty($groupID) ? posix_getgrgid($groupID) : false;
336
        return ! empty($group) && array_key_exists('name', $group) ? $group['name'] : false;
337
    }
338
339
    /**
340
     *
341
     * {@inheritDoc}
342
     *
343
     */
344
    public function getRealpath($filename = false)
345
    {
346
        return ! empty($filename) ? realpath($this->makeAbsolute($filename)) : ((method_exists(
347
            get_parent_class($this), 'getRealpath')) ? parent::getRealpath() : false);
348
    }
349
350
    /**
351
     *
352
     * {@inheritDoc}
353
     *
354
     */
355
    public function getPathInfo($filename = false, $flags = false, 
356
        $class_name = '\HOWI3\libhowi\Filesystem\php5\Objects\InfoObject')
357
    {
358
        return ! empty($filename) ? empty($flags) ? pathinfo($this->makeAbsolute($filename)) : pathinfo(
359
            $this->makeAbsolute($filename), $flags) : (method_exists(get_parent_class($this), 'getPathInfo') ? parent::getPathInfo(
360
            $class_name) : false);
361
    }
362
363
    /**
364
     *
365
     * {@inheritDoc}
366
     *
367
     */
368
    public function getPath()
369
    {
370
        return method_exists(get_parent_class($this), 'getPath') ? parent::getPath() : (property_exists($this, 
371
            'path') ? $this->path : dirname($this->getPathname()));
372
    }
373
374
    /**
375
     *
376
     * {@inheritDoc}
377
     *
378
     */
379
    public function getPathname()
380
    {
381
        return method_exists(get_parent_class($this), 'getPathname') ? parent::getPathname() : $this->getCwd();
382
    }
383
384
    /**
385
     *
386
     * {@inheritDoc}
387
     *
388
     */
389
    public function getGroup($filename = false)
390
    {
391
        return empty($filename) ? ((method_exists(get_parent_class($this), 'getGroup')) ? parent::getGroup() : false) : filegroup(
392
            $this->makeAbsolute($filename));
393
    }
394
395
    /**
396
     *
397
     * {@inheritDoc}
398
     *
399
     */
400
    public function getOwner($filename = false)
401
    {
402
        return empty($filename) ? ((method_exists(get_parent_class($this), 'getOwner')) ? parent::getOwner() : false) : fileowner(
403
            $this->makeAbsolute($filename));
404
    }
405
406
    /**
407
     *
408
     * {@inheritDoc}
409
     *
410
     */
411
    public function getInode($filename = false)
412
    {
413
        return empty($filename) ? ((method_exists(get_parent_class($this), 'getInode')) ? parent::getInode() : false) : fileinode(
414
            $this->makeAbsolute($filename));
415
    }
416
417
    /**
418
     *
419
     * {@inheritDoc}
420
     *
421
     */
422
    public function getBasename($suffix = false, $pathname = false)
423
    {
424
        $pathname = ! empty($pathname) ? $pathname : ($this->isDot() ? $this->getPath() : false);
425
        $suffix = ! empty($suffix) ? $suffix : '';
426
        return ! empty($pathname) || ! method_exists(get_parent_class($this), 'getBasename') ? basename(
427
            $pathname, $suffix) : parent::getBasename($suffix);
428
    }
429
430
    /**
431
     *
432
     * {@inheritDoc}
433
     *
434
     */
435
    public function getPerms($filename = false)
436
    {
437
        $perms = empty($filename) ? ((method_exists(get_parent_class($this), 'getPerms')) ? parent::getPerms() : false) : fileperms(
438
            $this->makeAbsolute($filename));
439
        return ! empty($perms) ? (int) ltrim(substr(sprintf('%o', $perms), - 4), '0') : false;
440
    }
441
442
    /**
443
     *
444
     * {@inheritDoc}
445
     *
446
     */
447
    public function getType($filename = false)
448
    {
449
        return ! empty($filename) ? filetype($this->makeAbsolute($filename)) : ((method_exists(
450
            get_parent_class($this), 'getType')) ? parent::getType() : false);
451
    }
452
453
    /**
454
     *
455
     * {@inheritDoc}
456
     *
457
     */
458
    public function isDot()
459
    {
460
        return ((method_exists(get_parent_class($this), 'isDot')) ? parent::isDot() : false);
461
    }
462
463
    /**
464
     *
465
     * {@inheritDoc}
466
     *
467
     */
468
    public function isAbsolute($path = false)
469
    {
470
        return ((strspn($path, DIRECTORY_SEPARATOR, 0, 1) + (ctype_alnum($path) ? 0 : 1) +
471
             (strpos($path, ':') === false) + (strpos($path, "/../") === false ? 1 : 0) +
472
             (filter_var($path, FILTER_VALIDATE_URL) === false ? 1 : 0) === 5) ? true : false);
473
    }
474
475
    /**
476
     *
477
     * {@inheritDoc}
478
     *
479
     */
480
    public function isRelative($path = false)
481
    {
482
        // ^(?=.*?(.))((?!(^\/)).)*$
483
        return (preg_match('/^[^\/].*$/', $path) === 1) ? true : false;
484
    }
485
486
    /**
487
     *
488
     * {@inheritDoc}
489
     *
490
     */
491
    public function isWritable($path = false)
492
    {
493
        return empty($path) ? ((method_exists(get_parent_class($this), 'isWritable')) ? parent::isWritable() : false) : is_writable(
494
            $this->makeAbsolute($path));
495
    }
496
497
    /**
498
     *
499
     * {@inheritDoc}
500
     *
501
     */
502
    public function isReadable($path = false)
503
    {
504
        return empty($path) ? ((method_exists(get_parent_class($this), 'isReadable')) ? parent::isReadable() : false) : is_readable(
505
            $this->makeAbsolute($path));
506
    }
507
508
    /**
509
     *
510
     * {@inheritDoc}
511
     *
512
     */
513
    public function isDir($filename = false)
514
    {
515
        return empty($filename) ? ((method_exists(get_parent_class($this), 'isDir')) ? parent::isDir() : false) : is_dir(
516
            $this->makeAbsolute($filename));
517
    }
518
519
    /**
520
     *
521
     * {@inheritDoc}
522
     *
523
     */
524
    public function isExecutable($filename = false)
525
    {
526
        return ! empty($filename) ? is_executable($this->makeAbsolute($filename)) : ((method_exists(
527
            get_parent_class($this), 'isExecutable')) ? parent::isExecutable() : false);
528
    }
529
530
    /**
531
     *
532
     * {@inheritDoc}
533
     *
534
     */
535
    public function isFile($filename = false)
536
    {
537
        return ! empty($filename) ? is_file($this->makeAbsolute($filename)) : ((method_exists(
538
            get_parent_class($this), 'isFile')) ? parent::isFile() : false);
539
    }
540
541
    /**
542
     *
543
     * {@inheritDoc}
544
     *
545
     */
546
    public function isLink($filename = false)
547
    {
548
        return ! empty($filename) ? is_link($this->makeAbsolute($filename)) : ((method_exists(
549
            get_parent_class($this), 'isLink')) ? parent::isLink() : false);
550
    }
551
552
    /**
553
     *
554
     * {@inheritDoc}
555
     *
556
     */
557
    public function makeAbsolute($path = false)
558
    {
559
        if (! empty($path) && $this->isAbsolute($path))
560
            $absolute_path = $path;
561
        elseif (! empty($path) && $this->isRelative($path)) {
562
            
563
            if (preg_match('/^(~\/)/', $path) === 1) {
564
                $absolute_path = getenv("HOME") . substr($path, 1);
565
            } elseif (preg_match('/^(.\/|..\/)/', $path) === 1) {
566
                $absolute_path = realpath($path);
567
            } else
568
                $absolute_path = $this->getCwd() . ($path !== '.' ? DIRECTORY_SEPARATOR . $path : '');
569
        } else {
570
            $absolute_path = $path;
571
        }
572
        return $absolute_path;
573
    }
574
575
    /**
576
     *
577
     * {@inheritDoc}
578
     *
579
     */
580
    public function exists($pathname = false)
581
    {
582
        if (empty($pathname))
583
            return false;
584
        
585
        $pathname = $this->makeAbsolute($pathname);
586
        
587
        return ! empty($pathname) ? file_exists($pathname) : false;
588
    }
589
590
    /**
591
     *
592
     * {@inheritDoc}
593
     *
594
     */
595
    public function getFilename($filename = false)
596
    {
597
        return ! empty($filename) ? basename($this->makeAbsolute($filename)) : ((method_exists(
598
            get_parent_class($this), 'getFilename')) ? parent::getFilename() : false);
599
    }
600
601
    /**
602
     *
603
     * {@inheritDoc}
604
     *
605
     */
606
    public function getExtension($filename = false)
607
    {
608
        return ! empty($filename) ? pathinfo($this->makeAbsolute($filename), PATHINFO_EXTENSION) : ((method_exists(
609
            get_parent_class($this), 'getExtension')) ? parent::getExtension() : false);
610
    }
611
}
612

php7/TraitForSharedMethods.inc 1 location

@@ 30-611 (lines=582) @@
27
use \RecursiveIteratorIterator;
28
use \DateTime;
29
30
trait TraitForSharedMethods
31
{
32
33
    /**
34
     *
35
     * {@inheritDoc}
36
     *
37
     */
38
    protected function setCwd($path = false, $validate_dir_name = false)
39
    {
40
        if (empty($path))
41
            $current_working_directory = $this->getCwd();
42
        else
43
            $current_working_directory = $this->makeAbsolute($path);
44
        
45
        if (! empty($validate_dir_name) && (basename($current_working_directory) !== $validate_dir_name))
46
            return false;
47
        
48
        if (! is_dir($current_working_directory) || ! is_readable($current_working_directory) ||
49
             ! chdir($current_working_directory)) {
50
            $this->alert(200, false, $current_working_directory);
51
            return false;
52
        }
53
        $this->cwd = $current_working_directory;
54
        return true;
55
    }
56
57
    /**
58
     *
59
     * {@inheritDoc}
60
     *
61
     */
62
    public function getCwd()
63
    {
64
        if (empty($this->cwd))
65
            $this->cwd = getcwd();
66
        
67
        return $this->cwd;
68
    }
69
70
    /**
71
     *
72
     * {@inheritDoc}
73
     *
74
     */
75
    public function getSize($convert = false, $filename = false)
76
    {
77
        $filename = ! empty($filename) ? $this->makeAbsolute($filename) : false;
78
        $bytestotal = empty($filename) ? ((method_exists(get_parent_class($this), 'getSize')) ? parent::getSize() : false) : filesize(
79
            $filename);
80
        
81
        if (! $this->isDir($filename) && empty($convert))
82
            return $bytestotal;
83
        
84
        $suffixes = array(
85
            'B',
86
            'kB',
87
            'MB',
88
            'GB',
89
            'TB',
90
            'PB',
91
            'EB',
92
            'ZB',
93
            'YB'
94
        );
95
        if ($this->isDir($filename)) {
96
            $path = ((method_exists(get_class($this), 'getPathname')) ? $this->getPathname() : $filename);
97
            if ($path === '/home/nitrotrigger/tmp/libhowi-filesystem/log/phpunit_test.log')
98
                die('ss');
99
            
100
            foreach (new RecursiveIteratorIterator(
101
                new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS)) as $object) {
102
                $bytestotal += $object->getSize();
103
            }
104
            if (! $convert)
105
                return $bytestotal;
106
        }
107
        $base = log($bytestotal, 1024);
108
        
109
        $result = [
110
            0,
111
            'B'
112
        ];
113
        if ($bytestotal > 0) {
114
            
115
            $result = array(
116
                round(pow(1024, $base - floor($base)), 2),
117
                $suffixes[floor($base)]
118
            );
119
        }
120
        return ! empty($convert) ? $result : $result[0];
121
    }
122
123
    /**
124
     *
125
     * {@inheritDoc}
126
     *
127
     */
128
    public function getATime($timeformat = false, $pathname = false)
129
    {
130
        $atime = empty($pathname) ? parent::getATime() : fileatime($this->makeAbsolute($pathname));
131
        if (! empty($timeformat) && is_int($atime)) {
132
            switch ($timeformat) {
133
                case 'ago':
134
                    $atime = $this->ct_ago($atime, 'y,m,d,h,i,s', false, true);
135
                    break;
136
                case 'ago.single':
137
                    $atime = $this->ct_ago($atime, 'y,m,d,h,i,s', false, true, true);
138
                    break;
139
                default:
140
                    $atime = date($timeformat, $atime);
141
                    break;
142
            }
143
        }
144
        return $atime;
145
    }
146
147
    /**
148
     *
149
     * {@inheritDoc}
150
     *
151
     */
152
    public function getCTime($timeformat = false, $pathname = false)
153
    {
154
        $ctime = empty($pathname) ? parent::getCTime() : filectime($this->makeAbsolute($pathname));
155
        if (! empty($timeformat) && is_int($ctime)) {
156
            switch ($timeformat) {
157
                case 'ago':
158
                    $ctime = $this->ct_ago($ctime, 'y,m,d,h,i,s', false, true);
159
                    break;
160
                case 'ago.single':
161
                    $ctime = $this->ct_ago($ctime, 'y,m,d,h,i,s', false, true, true);
162
                    break;
163
                default:
164
                    $ctime = date($timeformat, $ctime);
165
                    break;
166
            }
167
        }
168
        return $ctime;
169
    }
170
171
    /**
172
     *
173
     * {@inheritDoc}
174
     *
175
     */
176
    public function getMTime($timeformat = false, $pathname = false)
177
    {
178
        $mtime = empty($pathname) ? parent::getMTime() : filemtime($this->makeAbsolute($pathname));
179
        if (! empty($timeformat) && is_int($mtime)) {
180
            switch ($timeformat) {
181
                case 'ago':
182
                    $mtime = $this->ct_ago($mtime, 'y,m,d,h,i,s', false, true);
183
                    break;
184
                case 'ago.single':
185
                    $mtime = $this->ct_ago($mtime, 'y,m,d,h,i,s', false, true, true);
186
                    break;
187
                default:
188
                    $mtime = date($timeformat, $mtime);
189
                    break;
190
            }
191
        }
192
        return $mtime;
193
    }
194
195
    /**
196
     *
197
     * {@inheritDoc}
198
     *
199
     */
200
    public function ct_ago($timestamp, $date_format = 'y,m,d,h,i,s', $sfx = true, $ommit_zero = true, $ago_single = true, $clocale = [])
201
    {
202
        $locale = array_merge(
203
            array(
204
                'y' => array(
205
                    'y',
206
                    'year',
207
                    'years'
208
                ),
209
                'm' => array(
210
                    'm',
211
                    'month',
212
                    'months'
213
                ),
214
                'd' => $d = array(
215
                    'd',
216
                    'day',
217
                    'days'
218
                ),
219
                'h' => array(
220
                    'h',
221
                    'hour',
222
                    'hours'
223
                ),
224
                'i' => array(
225
                    'm',
226
                    'minute',
227
                    'minutes'
228
                ),
229
                's' => array(
230
                    's',
231
                    'second',
232
                    'seconds'
233
                ),
234
                'days' => $d,
235
                'ago' => 'ago',
236
                'now' => 'just now'
237
            ), $clocale);
238
        
239
        $date = new DateTime();
240
        $date->setTimestamp($timestamp);
241
        
242
        $interval = $date->diff(new DateTime('now'));
243
        
244
        for ($df = strtok($date_format, ','); $df !== false; $df = strtok(",")) {
245
            $lkey = ($df !== 'a') ? strtolower($df) : 'days';
246
            
247
            if ($ommit_zero && $interval->$lkey === 0)
248
                continue;
249
            
250
            $fparam = $df . (empty($sfx) ? $locale[$lkey][0] : ' ' .
251
                 (($interval->$lkey === 1) ? $locale[$lkey][1] : $locale[$lkey][2]));
252
            
253
            if (! empty($ago_single) && ! empty($dfs))
254
                break;
255
            else
256
                $dfs = ! empty($dfs) ? $dfs . "%$fparam " : "%$fparam ";
257
        }
258
        if (empty($dfs))
259
            $ret = $locale['now'];
260
        else
261
            $ret = $interval->format($dfs) . $locale['ago'];
262
        return $ret;
263
    }
264
265
    /**
266
     *
267
     * {@inheritDoc}
268
     *
269
     */
270
    public function openFile($open_mode = "r", $use_include_path = false, $context = null)
271
    {
272
        if (! method_exists(get_parent_class($this), 'openFile'))
273
            return false;
274
        
275
        return ! $this->isDir() ? (! empty($context) ? parent::openFile($open_mode, $use_include_path, 
276
            $context) : parent::openFile($open_mode, $use_include_path)) : false;
277
    }
278
279
    /**
280
     *
281
     * {@inheritDoc}
282
     *
283
     */
284
    public function setFileClass($class_name = "\HOWI3\libhowi\Filesystem\php7\Objects\FileObject")
285
    {
286
        if (! method_exists(get_parent_class($this), 'setFileClass'))
287
            return false;
288
        
289
        if (is_subclass_of($class_name, 'SplFileObject') || $class_name === 'SplFileObject') {
290
            parent::setFileClass($class_name);
291
            return true;
292
        }
293
        return false;
294
    }
295
296
    /**
297
     *
298
     * {@inheritDoc}
299
     *
300
     */
301
    public function setInfoClass($class_name = "\HOWI3\libhowi\Filesystem\php7\Objects\InfoObject")
302
    {
303
        if (! method_exists(get_parent_class($this), 'setInfoClass'))
304
            return false;
305
        
306
        if (is_subclass_of($class_name, 'SplFileInfo') || $class_name === 'SplFileInfo') {
307
            parent::setInfoClass($class_name);
308
            return true;
309
        }
310
        return false;
311
    }
312
313
    /**
314
     *
315
     * {@inheritDoc}
316
     *
317
     */
318
    public function getOwnerName($filename = false)
319
    {
320
        $userID = empty($filename) && method_exists(get_parent_class($this), 'getOwner') ? parent::getOwner() : (! empty(
321
            $filename) ? fileowner($this->makeAbsolute($filename)) : false);
322
        $user = ! empty($userID) ? posix_getpwuid($userID) : false;
323
        return is_array($user) && array_key_exists('name', $user) ? $user['name'] : false;
324
    }
325
326
    /**
327
     *
328
     * {@inheritDoc}
329
     *
330
     */
331
    public function getGroupName($filename = false)
332
    {
333
        $groupID = empty($filename) ? ((method_exists(get_parent_class($this), 'getGroup')) ? parent::getGroup() : false) : filegroup(
334
            $this->makeAbsolute($filename));
335
        $group = ! empty($groupID) ? posix_getgrgid($groupID) : false;
336
        return ! empty($group) && array_key_exists('name', $group) ? $group['name'] : false;
337
    }
338
339
    /**
340
     *
341
     * {@inheritDoc}
342
     *
343
     */
344
    public function getRealpath($filename = false)
345
    {
346
        return ! empty($filename) ? realpath($this->makeAbsolute($filename)) : ((method_exists(
347
            get_parent_class($this), 'getRealpath')) ? parent::getRealpath() : false);
348
    }
349
350
    /**
351
     *
352
     * {@inheritDoc}
353
     *
354
     */
355
    public function getPathInfo($filename = false, $flags = false, 
356
        $class_name = '\HOWI3\libhowi\Filesystem\php7\Objects\InfoObject')
357
    {
358
        return ! empty($filename) ? empty($flags) ? pathinfo($this->makeAbsolute($filename)) : pathinfo(
359
            $this->makeAbsolute($filename), $flags) : (method_exists(get_parent_class($this), 'getPathInfo') ? parent::getPathInfo(
360
            $class_name) : false);
361
    }
362
363
    /**
364
     *
365
     * {@inheritDoc}
366
     *
367
     */
368
    public function getPath()
369
    {
370
        return method_exists(get_parent_class($this), 'getPath') ? parent::getPath() : (property_exists($this, 
371
            'path') ? $this->path : dirname($this->getPathname()));
372
    }
373
374
    /**
375
     *
376
     * {@inheritDoc}
377
     *
378
     */
379
    public function getPathname()
380
    {
381
        return method_exists(get_parent_class($this), 'getPathname') ? parent::getPathname() : $this->getCwd();
382
    }
383
384
    /**
385
     *
386
     * {@inheritDoc}
387
     *
388
     */
389
    public function getGroup($filename = false)
390
    {
391
        return empty($filename) ? ((method_exists(get_parent_class($this), 'getGroup')) ? parent::getGroup() : false) : filegroup(
392
            $this->makeAbsolute($filename));
393
    }
394
395
    /**
396
     *
397
     * {@inheritDoc}
398
     *
399
     */
400
    public function getOwner($filename = false)
401
    {
402
        return empty($filename) ? ((method_exists(get_parent_class($this), 'getOwner')) ? parent::getOwner() : false) : fileowner(
403
            $this->makeAbsolute($filename));
404
    }
405
406
    /**
407
     *
408
     * {@inheritDoc}
409
     *
410
     */
411
    public function getInode($filename = false)
412
    {
413
        return empty($filename) ? ((method_exists(get_parent_class($this), 'getInode')) ? parent::getInode() : false) : fileinode(
414
            $this->makeAbsolute($filename));
415
    }
416
417
    /**
418
     *
419
     * {@inheritDoc}
420
     *
421
     */
422
    public function getBasename($suffix = false, $pathname = false)
423
    {
424
        $pathname = ! empty($pathname) ? $pathname : ($this->isDot() ? $this->getPath() : false);
425
        $suffix = ! empty($suffix) ? $suffix : '';
426
        return ! empty($pathname) || ! method_exists(get_parent_class($this), 'getBasename') ? basename(
427
            $pathname, $suffix) : parent::getBasename($suffix);
428
    }
429
430
    /**
431
     *
432
     * {@inheritDoc}
433
     *
434
     */
435
    public function getPerms($filename = false)
436
    {
437
        $perms = empty($filename) ? ((method_exists(get_parent_class($this), 'getPerms')) ? parent::getPerms() : false) : fileperms(
438
            $this->makeAbsolute($filename));
439
        return ! empty($perms) ? (int) ltrim(substr(sprintf('%o', $perms), - 4), '0') : false;
440
    }
441
442
    /**
443
     *
444
     * {@inheritDoc}
445
     *
446
     */
447
    public function getType($filename = false)
448
    {
449
        return ! empty($filename) ? filetype($this->makeAbsolute($filename)) : ((method_exists(
450
            get_parent_class($this), 'getType')) ? parent::getType() : false);
451
    }
452
453
    /**
454
     *
455
     * {@inheritDoc}
456
     *
457
     */
458
    public function isDot()
459
    {
460
        return ((method_exists(get_parent_class($this), 'isDot')) ? parent::isDot() : false);
461
    }
462
463
    /**
464
     *
465
     * {@inheritDoc}
466
     *
467
     */
468
    public function isAbsolute($path = false)
469
    {
470
        return ((strspn($path, DIRECTORY_SEPARATOR, 0, 1) + (ctype_alnum($path) ? 0 : 1) +
471
             (strpos($path, ':') === false) + (strpos($path, "/../") === false ? 1 : 0) +
472
             (filter_var($path, FILTER_VALIDATE_URL) === false ? 1 : 0) === 5) ? true : false);
473
    }
474
475
    /**
476
     *
477
     * {@inheritDoc}
478
     *
479
     */
480
    public function isRelative($path = false)
481
    {
482
        // ^(?=.*?(.))((?!(^\/)).)*$
483
        return (preg_match('/^[^\/].*$/', $path) === 1) ? true : false;
484
    }
485
486
    /**
487
     *
488
     * {@inheritDoc}
489
     *
490
     */
491
    public function isWritable($path = false)
492
    {
493
        return empty($path) ? ((method_exists(get_parent_class($this), 'isWritable')) ? parent::isWritable() : false) : is_writable(
494
            $this->makeAbsolute($path));
495
    }
496
497
    /**
498
     *
499
     * {@inheritDoc}
500
     *
501
     */
502
    public function isReadable($path = false)
503
    {
504
        return empty($path) ? ((method_exists(get_parent_class($this), 'isReadable')) ? parent::isReadable() : false) : is_readable(
505
            $this->makeAbsolute($path));
506
    }
507
508
    /**
509
     *
510
     * {@inheritDoc}
511
     *
512
     */
513
    public function isDir($filename = false)
514
    {
515
        return empty($filename) ? ((method_exists(get_parent_class($this), 'isDir')) ? parent::isDir() : false) : is_dir(
516
            $this->makeAbsolute($filename));
517
    }
518
519
    /**
520
     *
521
     * {@inheritDoc}
522
     *
523
     */
524
    public function isExecutable($filename = false)
525
    {
526
        return ! empty($filename) ? is_executable($this->makeAbsolute($filename)) : ((method_exists(
527
            get_parent_class($this), 'isExecutable')) ? parent::isExecutable() : false);
528
    }
529
530
    /**
531
     *
532
     * {@inheritDoc}
533
     *
534
     */
535
    public function isFile($filename = false)
536
    {
537
        return ! empty($filename) ? is_file($this->makeAbsolute($filename)) : ((method_exists(
538
            get_parent_class($this), 'isFile')) ? parent::isFile() : false);
539
    }
540
541
    /**
542
     *
543
     * {@inheritDoc}
544
     *
545
     */
546
    public function isLink($filename = false)
547
    {
548
        return ! empty($filename) ? is_link($this->makeAbsolute($filename)) : ((method_exists(
549
            get_parent_class($this), 'isLink')) ? parent::isLink() : false);
550
    }
551
552
    /**
553
     *
554
     * {@inheritDoc}
555
     *
556
     */
557
    public function makeAbsolute($path = false)
558
    {
559
        if (! empty($path) && $this->isAbsolute($path))
560
            $absolute_path = $path;
561
        elseif (! empty($path) && $this->isRelative($path)) {
562
            
563
            if (preg_match('/^(~\/)/', $path) === 1) {
564
                $absolute_path = getenv("HOME") . substr($path, 1);
565
            } elseif (preg_match('/^(.\/|..\/)/', $path) === 1) {
566
                $absolute_path = realpath($path);
567
            } else
568
                $absolute_path = $this->getCwd() . ($path !== '.' ? DIRECTORY_SEPARATOR . $path : '');
569
        } else {
570
            $absolute_path = $path;
571
        }
572
        return $absolute_path;
573
    }
574
575
    /**
576
     *
577
     * {@inheritDoc}
578
     *
579
     */
580
    public function exists($pathname = false)
581
    {
582
        if (empty($pathname))
583
            return false;
584
        
585
        $pathname = $this->makeAbsolute($pathname);
586
        
587
        return ! empty($pathname) ? file_exists($pathname) : false;
588
    }
589
590
    /**
591
     *
592
     * {@inheritDoc}
593
     *
594
     */
595
    public function getFilename($filename = false)
596
    {
597
        return ! empty($filename) ? basename($this->makeAbsolute($filename)) : ((method_exists(
598
            get_parent_class($this), 'getFilename')) ? parent::getFilename() : false);
599
    }
600
601
    /**
602
     *
603
     * {@inheritDoc}
604
     *
605
     */
606
    public function getExtension($filename = false)
607
    {
608
        return ! empty($filename) ? pathinfo($this->makeAbsolute($filename), PATHINFO_EXTENSION) : ((method_exists(
609
            get_parent_class($this), 'getExtension')) ? parent::getExtension() : false);
610
    }
611
}
612