Files::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * This file is part of the alphaz Framework.
5
 *
6
 * @author Muhammad Umer Farooq (Malik) <[email protected]>
7
 *
8
 * @link https://github.com/alphazframework/framework
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 * @since 1.0.0
13
 *
14
 * @license MIT
15
 */
16
17
namespace alphaz\Files;
18
19
class Files
20
{
21
    /**
22
     * Mine Types of files.
23
     *
24
     * @since 1.0.0
25
     *
26
     * @var array
27
     */
28
    private $mineTypes = [];
29
30
    /**
31
     * Types.
32
     *
33
     * @since 1.0.0
34
     *
35
     * @var array
36
     */
37
    private $types = [];
38
39
    /**
40
     * The default value for recursive create dirs.
41
     *
42
     * @since 1.0.0
43
     *
44
     * @var bool
45
     */
46
    private $recursiveDirectories = true;
47
48
    /**
49
     * Default chmod.
50
     *
51
     * @since 1.0.0
52
     *
53
     * @var int
54
     */
55
    private $defCHMOD = 0755;
56
57
    /**
58
     * __construct.
59
     *
60
     * @param array $mines
61
     * @param array $extensions
62
     *
63
     * @since 1.0.0
64
     */
65
    public function __construct($mines, $extensions)
66
    {
67
        $this->mineTypes = $mines;
68
        $this->types = $extensions;
69
    }
70
71
    /**
72
     * Create alphaz system dir.
73
     *
74
     * @since 1.0.0
75
     * @deprecated 3.0.0
76
     *
77
     * @return bool
78
     */
79
    public function systemDirs()
80
    {
81
        $this->mkDir('../Storage');
82
        $this->mkDir('../Storage/Data');
83
        $this->mkDir('../Storage/Logs');
84
        $this->mkDir('../Storage/Session');
85
        $this->mkDir('../Storage/Backup');
86
        $this->mkDIr('../Storage/Cache');
87
    }
88
89
    /**
90
     * Change the default chmod.
91
     *
92
     * @param mixed $chomd Valid chmod
93
     *
94
     * @since 1.0.0
95
     *
96
     * @return int
97
     */
98
    public function changeDefaultChmod($chmod)
99
    {
100
        return ($chmod === null) ? $this->defCHMOD : $this->defCHMOD = $chmod;
101
    }
102
103
    /**
104
     * Create recurisve dir.
105
     *
106
     * @param mixed $value recursive status true|false.
107
     *
108
     * @since 1.0.0
109
     *
110
     * @return bool
111
     */
112
    public function recursiveCreateDir($value = null)
113
    {
114
        return ($value === null) ? $this->recursiveDirectories : $this->recursiveDirectories = $value;
115
    }
116
117
    /**
118
     * Add the mine type.
119
     *
120
     * @param (string) $type Correct mine type.
121
     *
122
     * @since 1.0.0
123
     *
124
     * @return void
125
     */
126
    public function addMineTypes($type)
127
    {
128
        array_push($this->mineTypes, $type);
129
    }
130
131
    /**
132
     * Add the extemsio.
133
     *
134
     * @param (string) $type Correct type.
135
     * @param (string) $sub  Extensions/
136
     *
137
     * @since 1.0.0
138
     *
139
     * @return void
140
     */
141
    public function addExt($type, $ext)
142
    {
143
        array_push($this->types[$type], $ext);
144
    }
145
146
    /**
147
     * Make the dir.
148
     *
149
     * @param (string) $name      Name of dir with path.
150
     * @param (string) $recursive Recursive mode create: null|true|false.
151
     * @param (string) $chmod     Directory permission on create: default 0755.
152
     *
153
     * @since 1.0.0
154
     *
155
     * @return bool
156
     */
157
    public function mkDir($name, $recursive = null, $chmod = null)
158
    {
159
        // test the recursive mode with default value
160
        $recursive = ($recursive === null) ? $this->recursiveDirectories : $recursive;
0 ignored issues
show
Unused Code introduced by
The assignment to $recursive is dead and can be removed.
Loading history...
161
        // test the chmod with default value
162
        $chmod = ($chmod === null) ? $this->defCHMOD : $chmod;
0 ignored issues
show
Unused Code introduced by
The assignment to $chmod is dead and can be removed.
Loading history...
163
        if (!is_dir($name)) {
164
            return (mkdir($name)) ? true : false;
165
        }
166
167
        return false;
168
    }
169
170
    /**
171
     * Change the permission.
172
     *
173
     * @param (string) $source Name of file or directory with path.
174
     * @param (mixed)  $pre    Valid permission.
175
     *
176
     * @since 1.0.0
177
     *
178
     * @return bool
179
     */
180
    public function permission($source, $pre)
181
    {
182
        if (!is_dir($source)) {
183
            return (file_exists($source)) ? chmod($source, $pre) : false;
184
        }
185
186
        return false;
187
    }
188
189
    /**
190
     * Change the owner of an array of files.
191
     *
192
     * @param (string) $source Name of file or directory with path.
193
     * @param (array)  $files  Files to be copy.
194
     * @param (mixed)  $user   The new owner user name.
195
     *
196
     * @since 1.0.0
197
     *
198
     * @return bool
199
     */
200
    public function chown($source, $files, $user)
201
    {
202
        foreach ($files as $file => $value) {
203
            if (file_exists($source.$value)) {
204
                if (@chown($file, $user) === false) {
205
                    throw new \RuntimeException("The ownership of {$file} can not change", 500);
206
                }
207
            }
208
        }
209
    }
210
211
    /**
212
     * Sets access and modification time of file.
213
     *
214
     * @param (string) $source Name of file or directory with path.
215
     * @param (array)  $files  Files to be copy.
216
     * @param (int)    $time   The touch time as a Unix timestamp.
217
     * @param (int)    $atime  The access time as a Unix timestamp.
218
     *
219
     * @since 1.0.0
220
     *
221
     * @return bool
222
     */
223
    public function touch($source, $files, $time = null, $atime = null)
224
    {
225
        foreach ($files as $file => $value) {
226
            if (file_exists($source.$value)) {
227
                $touch = $time ? @touch($file, $time, $atime) : @touch($file);
228
                if ($touch !== true) {
229
                    return false;
230
                }
231
            }
232
        }
233
    }
234
235
    /**
236
     * Copy file.
237
     *
238
     * @param (string) $source Name of file or directory with path.
239
     * @param (string) $target Target directory.
240
     * @param (string) $files  Files to be copy.
241
     *
242
     * @since 1.0.0
243
     *
244
     * @return void
245
     */
246
    public function copyFile($source, $target, $file)
247
    {
248
        $this->mkDir($target);
249
        if (file_exists($source.$file)) {
250
            copy($source.$file, $target.$file);
251
        }
252
    }
253
254
    /**
255
     * Copy files.
256
     *
257
     * @param (string) $source Name of file or directory with path.
258
     * @param (string) $target Target directory.
259
     * @param (array)  $files  Files to be copy.
260
     *
261
     * @since 1.0.0
262
     *
263
     * @return void
264
     */
265
    public function copyFiles($source, $target, $files)
266
    {
267
        foreach ($files as $file) {
268
            $this->copyFile($source, $target, $file);
269
        }
270
    }
271
272
    /**
273
     * Move file.
274
     *
275
     * @param (string) $source Name of file or directory with path.
276
     * @param (string) $target Target directory.
277
     * @param (string) $file   Files to be moved.
278
     *
279
     * @since 1.0.0
280
     *
281
     * @return void
282
     */
283
    public function moveFile($source, $target, $file)
284
    {
285
        if (file_exists($source.$file)) {
286
            rename($source.$file, $target.$file);
287
        }
288
    }
289
290
    /**
291
     * Move files.
292
     *
293
     * @param (string)       $source Name of file or directory with path.
294
     * @param (string)       $target Target directory.
295
     * @param (array|string) $files  Files to be moved.
296
     *
297
     * @since 1.0.0
298
     *
299
     * @return void
300
     */
301
    public function moveFiles($source, $target, $files)
302
    {
303
        foreach ($files as $file) {
304
            $this->moveFile($source, $target, $files);
0 ignored issues
show
Bug introduced by
$files of type array is incompatible with the type string expected by parameter $file of alphaz\Files\Files::moveFile(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

304
            $this->moveFile($source, $target, /** @scrutinizer ignore-type */ $files);
Loading history...
305
        }
306
    }
307
308
    /**
309
     * Delete file.
310
     *
311
     * @param (string) $file Name of file with path.
312
     *
313
     * @since 1.0.0
314
     *
315
     * @return void
316
     */
317
    public function deleteFile($file)
318
    {
319
        if (file_exists($file)) {
320
            unlink($file);
321
        }
322
    }
323
324
    /**
325
     * Delete files.
326
     *
327
     * @param (array) $file Name of file with path.
328
     *
329
     * @since 1.0.0
330
     *
331
     * @return void
332
     */
333
    public function deleteFiles($files)
334
    {
335
        foreach ($files as $file) {
336
            $this->deleteFile($file);
337
        }
338
    }
339
340
    /**
341
     * Copy dir.
342
     *
343
     * @param (string) $source Directory with path.
344
     * @param (string) $target Target directory.
345
     * @param (string) $dir    Directory to be copied.
346
     *
347
     * @since 1.0.0
348
     *
349
     * @return void
350
     */
351
    public function copyDir($source, $target, $dir)
352
    {
353
        $this->mkDir($target);
354
        $serverOs = (new \alphaz\Common\OperatingSystem())->get();
355
        $command = ($serverOs === 'Windows') ? 'xcopy ' : 'cp -r ';
356
        if (is_dir($source.$dir)) {
357
            shell_exec($command.$source.$dir.' '.$target);
358
        }
359
    }
360
361
    /**
362
     * Copy dirs.
363
     *
364
     * @param (string) $source Directory with path.
365
     * @param (string) $target Target directory.
366
     * @param (array)  $dirs   Directories to be copied.
367
     *
368
     * @since 1.0.0
369
     *
370
     * @return void
371
     */
372
    public function copyDirs($source, $target, $dirs)
373
    {
374
        foreach ($dirs as $dir) {
375
            $this->copyDir($source, $target, $dir);
376
        }
377
    }
378
379
    /**
380
     * Move dir.
381
     *
382
     * @param (string) $source Directory with path.
383
     * @param (string) $target Target directory.
384
     * @param (string) $dir    Directory to be copied.
385
     *
386
     * @since 1.0.0
387
     *
388
     * @return void
389
     */
390
    public function moveDir($source, $target, $dir)
391
    {
392
        $this->mkDir($target);
393
        $serverOs = (new \alphaz\Common\OperatingSystem())->get();
394
        $command = ($serverOs === 'Windows') ? 'move ' : 'mv ';
395
        if (is_dir($source.$dir)) {
396
            shell_exec($command.$source.$dir.' '.$target);
397
        }
398
    }
399
400
    /**
401
     * Move dirs.
402
     *
403
     * @param (string) $source Directory with path.
404
     * @param (string) $target Target directory.
405
     * @param (array)  $dirs   Dirs to be copy.
406
     *
407
     * @since 1.0.0
408
     *
409
     * @return void
410
     */
411
    public function moveDirs($source, $target, $dirs)
412
    {
413
        foreach ($dirs as $dir) {
414
            $this->moveDir($source, $target, $dir);
415
        }
416
    }
417
418
    /**
419
     * Delete dir.
420
     *
421
     * @param (string) $dir Directory with path.
422
     *
423
     * @since 1.0.0
424
     *
425
     * @return bool
426
     */
427
    public function deleteDir($dir)
428
    {
429
        if (!file_exists($dir)) {
430
            return true;
431
        }
432
433
        if (!is_dir($dir)) {
434
            return unlink($dir);
435
        }
436
        foreach (scandir($dir) as $item) {
437
            if ($item == '.' || $item == '..') {
438
                continue;
439
            }
440
            $this->deleteDir($dir.DIRECTORY_SEPARATOR.$item);
441
442
            return true;
443
        }
444
445
        return rmdir($dir);
446
    }
447
448
    /**
449
     * Delete dirs.
450
     *
451
     * @param (array) $dirs Directory with path.
452
     *
453
     * @since 1.0.0
454
     *
455
     * @return void
456
     */
457
    public function deleteDirs($dirs)
458
    {
459
        foreach ($dirs as $dir) {
460
            $this->deleteDir($dir);
461
        }
462
    }
463
464
    /**
465
     * Upload file.
466
     *
467
     * @param (string) $file     File to be uploaded.
468
     * @param (string) $target   Target where file should be uploaded.
469
     * @param (string) $fileType Supported => image,media,docs,zip.
470
     * @param (int)    $maxSize  File size to be allowed.
471
     *
472
     * @since 1.0.0
473
     *
474
     * @return mixed
475
     */
476
    public function fileUpload($file, $target, $fileType, $maxSize = 7992000000)
477
    {
478
        $exactName = basename($file['name']);
479
        $fileTmp = $file['tmp_name'];
480
        $fileSize = $file['size'];
481
        $error = $file['error'];
482
        $type = $file['type'];
483
        $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
484
        $newName = \alphaz\Site\Site::salts(30);
485
        $fileNewName = $newName.'.'.$ext;
0 ignored issues
show
Bug introduced by
Are you sure $ext of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

485
        $fileNewName = $newName.'.'./** @scrutinizer ignore-type */ $ext;
Loading history...
486
        $allowerd_ext = $this->types[$fileType];
487
        if (in_array($type, $this->mineTypes) === false) {
488
            return [
489
                'status'  => 'error',
490
                'code'    => 'mineType',
491
                'message' => sprintf(printl('z:files:error:mine:type'), $type),
492
            ];
493
        }
494
        if (in_array($ext, $allowerd_ext) === true) {
495
            if ($error === 0) {
496
                if ($fileSize <= $maxSize) {
497
                    $this->mkdir($target);
498
                    $fileRoot = $target.$fileNewName;
499
                    if (is_uploaded_file($fileTmp)) {
500
                        if (move_uploaded_file($fileTmp, $fileRoot)) {
501
                            return [
502
                                'status'  => 'success',
503
                                'code'    => $fileNewName,
504
                                'message' => printl('z:files:success'),
505
                                'name'    => $exactName,
506
                            ];
507
                        } else {
508
                            return [
509
                                'status'  => 'error',
510
                                'code'    => 'somethingwrong',
511
                                'message' => printl('z:files:error:somethingwrong'),
512
                            ];
513
                        }
514
                    } else {
515
                        return [
516
                            'status'  => 'error',
517
                            'code'    => 'invilidfile',
518
                            'message' => printl('z:files:error:invilid:file'),
519
                        ];
520
                    }
521
                } else {
522
                    return [
523
                        'status'  => 'error',
524
                        'code'    => 'exceedlimit',
525
                        'message' => sprintf(printl('z:files:error:exceed:limit'), $maxSize),
526
                    ];
527
                }
528
            } else {
529
                return [
530
                    'status'  => 'error',
531
                    'code'    => $error,
532
                    'message' => $error,
533
                ];
534
            }
535
        } else {
536
            return [
537
                'status'  => 'error',
538
                'code'    => 'extension',
539
                'message' => sprintf(printl('z:files:error:extension'), $ext),
0 ignored issues
show
Bug introduced by
It seems like $ext can also be of type array; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

539
                'message' => sprintf(printl('z:files:error:extension'), /** @scrutinizer ignore-type */ $ext),
Loading history...
540
            ];
541
        }
542
    }
543
544
    /**
545
     * Upload files.
546
     *
547
     * @param (array)  $files    Files to be uploaded.
548
     * @param (string) $target   Target where file should be upload.
549
     * @param (string) $fileType Supported => image,media,docs,zip.
550
     * @param (int)    $count    Number of file count.
551
     * @param (int)    $maxSize  File size to be allowed.
552
     *
553
     * @since 1.0.0
554
     *
555
     * @return void
556
     */
557
    public function filesUpload($files, $target, $fileType, $count, $maxSize = 7992000000)
558
    {
559
        $status = [];
560
        for ($i = 0; $i <= $count; $i++) {
561
            $exactName = basename($files['name'][$i]);
562
            $fileTmp = $files['tmp_name'][$i];
563
            $fileSize = $files['size'][$i];
564
            $error = $files['error'][$i];
565
            $type = $files['type'][$i];
566
            $ext = pathinfo($files['name'][$i], PATHINFO_EXTENSION);
567
            $newName = \alphaz\Site\Site::salts(30);
568
            $fileNewName = $newName.'.'.$ext;
0 ignored issues
show
Bug introduced by
Are you sure $ext of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

568
            $fileNewName = $newName.'.'./** @scrutinizer ignore-type */ $ext;
Loading history...
569
            $allowerd_ext = $this->types[$fileType];
570
            if (in_array($type, $this->mineTypes) === false) {
571
                $status[$i] = [
572
                    'status'  => 'error',
573
                    'code'    => 'mineType',
574
                    'message' => sprintf(printl('z:files:error:mine:type'), $type),
575
                ];
576
            }
577
            if (in_array($ext, $allowerd_ext) === true) {
578
                if ($error === 0) {
579
                    if ($fileSize <= $maxSize) {
580
                        $this->mkdir($target);
581
                        $fileRoot = $target.$fileNewName;
582
                        if (is_uploaded_file($fileTmp)) {
583
                            if (move_uploaded_file($fileTmp, $fileRoot)) {
584
                                $status[$i] = [
585
                                    'status'  => 'success',
586
                                    'code'    => $fileNewName,
587
                                    'message' => printl('z:files:success'),
588
                                    'name'    => $exactName,
589
                                ];
590
                            } else {
591
                                $status[$i] = [
592
                                    'status'  => 'error',
593
                                    'code'    => 'somethingwrong',
594
                                    'message' => printl('z:files:error:somethingwrong'),
595
                                ];
596
                            }
597
                        } else {
598
                            return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('status' =>...s:error:invilid:file')) returns the type array<string,string> which is incompatible with the documented return type void.
Loading history...
599
                                'status'  => 'error',
600
                                'code'    => 'invilidfile',
601
                                'message' => printl('z:files:error:invilid:file'),
602
                            ];
603
                        }
604
                    } else {
605
                        $status[$i] = [
606
                            'status'  => 'error',
607
                            'code'    => 'exceedlimit',
608
                            'message' => sprintf(printl('z:files:error:exceed:limit'), $maxSize),
609
                        ];
610
                    }
611
                } else {
612
                    $status[$i] = [
613
                        'status'  => 'error',
614
                        'code'    => $error,
615
                        'message' => $error,
616
                    ];
617
                }
618
            } else {
619
                $status[$i] = [
620
                    'status'  => 'error',
621
                    'code'    => 'extension',
622
                    'message' => sprintf(printl('z:files:error:extension'), $ext),
0 ignored issues
show
Bug introduced by
It seems like $ext can also be of type array; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

622
                    'message' => sprintf(printl('z:files:error:extension'), /** @scrutinizer ignore-type */ $ext),
Loading history...
623
                ];
624
            }
625
        }
626
627
        return $status;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $status returns the type array which is incompatible with the documented return type void.
Loading history...
628
    }
629
}
630