Issues (17)

src/TraitUpload.php (12 issues)

1
<?php
2
3
namespace Erykai\Upload;
4
5
use finfo;
6
use RuntimeException;
7
use stdClass;
8
9
/**
10
 * Class Trait Upload
11
 */
12
trait TraitUpload
13
{
14
    /**
15
     * @param string $path
16
     */
17
    protected function createDir(string $path): void
18
    {
19
        $folders = explode("/", $path);
20
        $dir = dirname(__DIR__, 4);
21
        foreach ($folders as $folder) {
22
            $dir .= "/" . $folder;
23
            if (!file_exists($dir) && !mkdir($dir) && !is_dir($dir)) {
24
                throw new RuntimeException(sprintf('Directory "%s" was not created', $dir));
25
            }
26
        }
27
    }
28
29
    /**
30
     * @param string $name
31
     * @return string
32
     */
33
    protected function slug(string $name): string
34
    {
35
        $characters = array(
36
            'Š' => 'S', 'š' => 's', 'Đ' => 'Dj', 'đ' => 'dj', 'Ž' => 'Z', 'ž' => 'z', 'Č' => 'C', 'č' => 'c', 'Ć' => 'C', 'ć' => 'c',
37
            'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'A', 'Ç' => 'C', 'È' => 'E', 'É' => 'E',
38
            'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O',
39
            'Õ' => 'O', 'Ö' => 'O', 'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ý' => 'Y', 'Þ' => 'B', 'ß' => 'Ss',
40
            'à' => 'a', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', 'æ' => 'a', 'ç' => 'c', 'è' => 'e', 'é' => 'e',
41
            'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ð' => 'o', 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o',
42
            'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ø' => 'o', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ý' => 'y', 'þ' => 'b',
43
            'ÿ' => 'y', 'Ŕ' => 'R', 'ŕ' => 'r', '/' => '-', ' ' => '-'
44
        );
45
        $stripped = preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', $name);
46
        return strtolower(strtr($stripped, $characters));
47
    }
48
49
    protected function upload(): bool
50
    {
51
        foreach ($this->getFiles() as $file) {
0 ignored issues
show
It seems like getFiles() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

51
        foreach ($this->/** @scrutinizer ignore-call */ getFiles() as $file) {
Loading history...
52
            $this->createDir($file->path);
53
            $archive = $file->name . "." . $file->ext;
54
            $directory = $file->directory . "/";
55
            $path = $file->path . "/";
56
            if (file_exists($directory . $archive)) {
57
                $archive = $file->name . "-" . time() . mt_rand() . "." . $file->ext;
58
            }
59
            $this->setData($file->key, $path . $archive);
0 ignored issues
show
It seems like setData() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

59
            $this->/** @scrutinizer ignore-call */ 
60
                   setData($file->key, $path . $archive);
Loading history...
60
            if (!empty($file->tmp_name)) {
61
                move_uploaded_file($file->tmp_name, $directory . $archive);
62
            }else{
63
                file_put_contents($directory . $archive, file_get_contents($this->url));
64
            }
65
        }
66
        return true;
67
    }
68
69
    /**
70
     * @return bool
71
     */
72
    protected function uploadFiles(): bool
73
    {
74
        $upload = $_FILES;
75
        foreach ($upload as $key => $file) {
76
            $this->file = new stdClass();
0 ignored issues
show
Bug Best Practice introduced by
The property file does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
77
            if (!$this->mimetype($file)) {
78
                return false;
79
            }
80
            $this->mountFile($file, $key);
81
            $this->file->tmp_name = $file['tmp_name'];
82
            $files[$key] = (object)$this->file;
83
        }
84
        if (!empty($files)) {
85
            $this->files['upload_file'] = (object)$files;
0 ignored issues
show
Bug Best Practice introduced by
The property files does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
86
            return true;
87
        }
88
        return false;
89
    }
90
91
    /**
92
     * @return bool
93
     */
94
    protected function uploadUrl(): bool
95
    {
96
        $this->file = new stdClass();
0 ignored issues
show
Bug Best Practice introduced by
The property file does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
97
        $finfo = new finfo(FILEINFO_MIME_TYPE);
98
        $mime_type = $finfo->buffer(file_get_contents($this->url));
99
        $file['type'] = $mime_type;
100
101
        if (!$this->mimetype($file)) {
102
            return false;
103
        }
104
105
        $url_array = explode("/", $this->url);
106
        $file['name'] = end($url_array);
107
        $this->mountFile($file, $this->key);
108
        $files = array();
109
        $files[$this->key] = (object)$this->file;
110
        $this->files['upload_url'] = (object)$files;
0 ignored issues
show
Bug Best Practice introduced by
The property files does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
111
        return true;
112
    }
113
114
    /**
115
     * @param array $file
116
     */
117
    private function mountFile(array $file, string $key)
118
    {
119
        [$type] = explode("/", $file['type']);
120
        $this->setPath($type);
0 ignored issues
show
It seems like setPath() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

120
        $this->/** @scrutinizer ignore-call */ 
121
               setPath($type);
Loading history...
121
122
        $this->file->key = $key;
123
        $this->file->ext = pathinfo($file['name'], PATHINFO_EXTENSION);
124
        $this->file->name = $this->slug(pathinfo($file['name'], PATHINFO_FILENAME));
0 ignored issues
show
It seems like pathinfo($file['name'], ...load\PATHINFO_FILENAME) can also be of type array; however, parameter $name of Erykai\Upload\TraitUpload::slug() does only seem to accept 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

124
        $this->file->name = $this->slug(/** @scrutinizer ignore-type */ pathinfo($file['name'], PATHINFO_FILENAME));
Loading history...
125
        $this->file->path = $this->getPath();
0 ignored issues
show
It seems like getPath() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

125
        /** @scrutinizer ignore-call */ 
126
        $this->file->path = $this->getPath();
Loading history...
126
        $this->file->directory = dirname(__DIR__, 4) . "/" . $this->getPath();
127
    }
128
129
    /**
130
     * @param array $type
131
     * @return bool
132
     */
133
    private function mimetype(array $type): bool
134
    {
135
        if (!in_array($type['type'], $this->getMimeType(), true)) {
0 ignored issues
show
It seems like getMimeType() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

135
        if (!in_array($type['type'], $this->/** @scrutinizer ignore-call */ getMimeType(), true)) {
Loading history...
136
            $this->files = null;
0 ignored issues
show
Bug Best Practice introduced by
The property files does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
137
            $this->setResponse(400, "error", "invalid file format " . $type['type'], "upload", dynamic: $type['type']);
0 ignored issues
show
It seems like setResponse() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

137
            $this->/** @scrutinizer ignore-call */ 
138
                   setResponse(400, "error", "invalid file format " . $type['type'], "upload", dynamic: $type['type']);
Loading history...
138
            return false;
139
        }
140
        return true;
141
    }
142
}