1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Erykai\Upload; |
4
|
|
|
|
5
|
|
|
use finfo; |
6
|
|
|
use RuntimeException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Trait Upload |
10
|
|
|
*/ |
11
|
|
|
trait TraitUpload |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param string $path |
15
|
|
|
*/ |
16
|
|
|
protected function createDir(string $path): void |
17
|
|
|
{ |
18
|
|
|
$folders = explode("/", $path); |
19
|
|
|
$dir = dirname(__DIR__, 4); |
20
|
|
|
foreach ($folders as $folder) { |
21
|
|
|
$dir .= "/" . $folder; |
22
|
|
|
if (!file_exists($dir) && !mkdir($dir) && !is_dir($dir)) { |
23
|
|
|
throw new RuntimeException(sprintf('Directory "%s" was not created', $dir)); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $name |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
|
|
protected function slug(string $name): string |
33
|
|
|
{ |
34
|
|
|
$characters = array( |
35
|
|
|
'Š' => 'S', 'š' => 's', 'Đ' => 'Dj', 'đ' => 'dj', 'Ž' => 'Z', 'ž' => 'z', 'Č' => 'C', 'č' => 'c', 'Ć' => 'C', 'ć' => 'c', |
36
|
|
|
'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'A', 'Ç' => 'C', 'È' => 'E', 'É' => 'E', |
37
|
|
|
'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', |
38
|
|
|
'Õ' => 'O', 'Ö' => 'O', 'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ý' => 'Y', 'Þ' => 'B', 'ß' => 'Ss', |
39
|
|
|
'à' => 'a', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', 'æ' => 'a', 'ç' => 'c', 'è' => 'e', 'é' => 'e', |
40
|
|
|
'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ð' => 'o', 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', |
41
|
|
|
'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ø' => 'o', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ý' => 'y', 'þ' => 'b', |
42
|
|
|
'ÿ' => 'y', 'Ŕ' => 'R', 'ŕ' => 'r', '/' => '-', ' ' => '-' |
43
|
|
|
); |
44
|
|
|
$stripped = preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', $name); |
45
|
|
|
return strtolower(strtr($stripped, $characters)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function upload(): bool |
49
|
|
|
{ |
50
|
|
|
foreach ($this->getFiles() as $file) { |
|
|
|
|
51
|
|
|
$this->createDir($file->path); |
52
|
|
|
$archive = $file->name . "." . $file->ext; |
53
|
|
|
$directory = $file->directory . "/"; |
54
|
|
|
$path = $file->path . "/"; |
55
|
|
|
if (file_exists($directory . $archive)) { |
56
|
|
|
$archive = $file->name . "-" . time() . mt_rand() . "." . $file->ext; |
57
|
|
|
} |
58
|
|
|
$this->setData($file->key, $path . $archive); |
|
|
|
|
59
|
|
|
if (!empty($file->tmp_name)) { |
60
|
|
|
move_uploaded_file($file->tmp_name, $directory . $archive); |
61
|
|
|
}else{ |
62
|
|
|
file_put_contents($directory . $archive, file_get_contents($this->url)); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
return true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
protected function uploadFiles(): bool |
72
|
|
|
{ |
73
|
|
|
$upload = $_FILES; |
74
|
|
|
foreach ($upload as $key => $file) { |
75
|
|
|
$this->file = new \stdClass(); |
|
|
|
|
76
|
|
|
if (!$this->mimetype($file)) { |
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
$this->mountFile($file, $key); |
80
|
|
|
$this->file->tmp_name = $file['tmp_name']; |
81
|
|
|
$files[$key] = (object)$this->file; |
82
|
|
|
} |
83
|
|
|
if (!empty($files)) { |
84
|
|
|
$this->files['upload_file'] = (object)$files; |
|
|
|
|
85
|
|
|
return true; |
86
|
|
|
} |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
protected function uploadUrl(): bool |
94
|
|
|
{ |
95
|
|
|
$this->file = new \stdClass(); |
|
|
|
|
96
|
|
|
$finfo = new finfo(FILEINFO_MIME_TYPE); |
97
|
|
|
$mime_type = $finfo->buffer(file_get_contents($this->url)); |
98
|
|
|
$file['type'] = $mime_type; |
|
|
|
|
99
|
|
|
if (!$this->mimetype($file)) { |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
$url_array = explode("/", $this->url); |
103
|
|
|
$file['name'] = end($url_array); |
104
|
|
|
$this->mountFile($file, $this->key); |
105
|
|
|
$files = (object)$this->file; |
106
|
|
|
$this->files['upload_url'] = (object)$files; |
|
|
|
|
107
|
|
|
return true; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $file |
112
|
|
|
*/ |
113
|
|
|
private function mountFile(array $file, string $key) |
114
|
|
|
{ |
115
|
|
|
[$type] = explode("/", $file['type']); |
116
|
|
|
$this->setPath($type); |
|
|
|
|
117
|
|
|
|
118
|
|
|
$this->file->key = $key; |
119
|
|
|
$this->file->ext = pathinfo($file['name'], PATHINFO_EXTENSION); |
120
|
|
|
$this->file->name = $this->slug(pathinfo($file['name'], PATHINFO_FILENAME)); |
|
|
|
|
121
|
|
|
$this->file->path = $this->getPath(); |
|
|
|
|
122
|
|
|
$this->file->directory = dirname(__DIR__, 4) . "/" . $this->getPath(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param array $type |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
|
|
private function mimetype(array $type): bool |
130
|
|
|
{ |
131
|
|
|
if (!in_array($type['type'], $this->getMimeType(), true)) { |
|
|
|
|
132
|
|
|
$this->files = null; |
|
|
|
|
133
|
|
|
$this->setResponse(400, "error", "invalid file format " . $type['type'], "upload", dynamic: $type['type']); |
|
|
|
|
134
|
|
|
return false; |
135
|
|
|
} |
136
|
|
|
return true; |
137
|
|
|
} |
138
|
|
|
} |