|
1
|
|
|
<?php namespace Comodojo\Dispatcher\Request; |
|
2
|
|
|
|
|
3
|
|
|
use \Exception; |
|
4
|
|
|
use \Comodojo\Exception\DispatcherException; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* @package Comodojo Dispatcher |
|
8
|
|
|
* @author Marco Giovinazzi <[email protected]> |
|
9
|
|
|
* @author Marco Castiello <[email protected]> |
|
10
|
|
|
* @license GPL-3.0+ |
|
11
|
|
|
* |
|
12
|
|
|
* LICENSE: |
|
13
|
|
|
* |
|
14
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
15
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
16
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
17
|
|
|
* License, or (at your option) any later version. |
|
18
|
|
|
* |
|
19
|
|
|
* This program is distributed in the hope that it will be useful, |
|
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
22
|
|
|
* GNU Affero General Public License for more details. |
|
23
|
|
|
* |
|
24
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
25
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
class File { |
|
29
|
|
|
|
|
30
|
|
|
private $path = ''; |
|
31
|
|
|
|
|
32
|
|
|
private $slug = ''; |
|
33
|
|
|
|
|
34
|
|
|
private $fname = ''; |
|
35
|
|
|
|
|
36
|
|
|
private $tname = ''; |
|
37
|
|
|
|
|
38
|
|
|
private $upld = ''; |
|
39
|
|
|
|
|
40
|
|
|
private $ctype = 'text/plain'; |
|
41
|
|
|
|
|
42
|
|
|
private $size = 0; |
|
43
|
|
|
|
|
44
|
|
|
public function __construct($fileControl = null, $repository = '') { |
|
45
|
|
|
|
|
46
|
|
|
$this->path = $repository; |
|
47
|
|
|
|
|
48
|
|
|
if (!is_null($fileControl)) { |
|
49
|
|
|
|
|
50
|
|
|
$this->load($fileControl); |
|
51
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function getTemporaryName() { |
|
57
|
|
|
|
|
58
|
|
|
return $this->tname; |
|
59
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getLocalName() { |
|
63
|
|
|
|
|
64
|
|
|
if (!empty($this->path)) |
|
65
|
|
|
return $this->path . "/" . $this->slug; |
|
66
|
|
|
else |
|
67
|
|
|
return ''; |
|
68
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function getFileName() { |
|
72
|
|
|
|
|
73
|
|
|
return $this->fname; |
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getSlug() { |
|
78
|
|
|
|
|
79
|
|
|
return $this->slug; |
|
80
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function getContentType() { |
|
84
|
|
|
|
|
85
|
|
|
return $this->ctype; |
|
86
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function getSize() { |
|
90
|
|
|
|
|
91
|
|
|
return $this->size; |
|
92
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function getUploadTime() { |
|
96
|
|
|
|
|
97
|
|
|
return $this->upld; |
|
98
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function getFileData() { |
|
102
|
|
|
|
|
103
|
|
|
$file = $this->getTemporaryName(); |
|
104
|
|
|
|
|
105
|
|
|
if (file_exists($file)) |
|
106
|
|
|
return file_get_contents($file); |
|
107
|
|
|
|
|
108
|
|
|
throw new DispatcherException("File does not exists"); |
|
109
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function fileIsSaved() { |
|
113
|
|
|
|
|
114
|
|
|
return ( |
|
115
|
|
|
!empty($this->path) && |
|
116
|
|
|
!empty($this->slug) && |
|
117
|
|
|
file_exists($this->getLocalName()) |
|
118
|
|
|
); |
|
119
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function load($slugOrControl) { |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
if (isset($_FILES) && isset($_FILES[$slugOrControl])) { |
|
125
|
|
|
|
|
126
|
|
|
$this->loadFromUploadedFile($slugOrControl); |
|
127
|
|
|
|
|
128
|
|
|
return $this; |
|
129
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
throw new DispatcherException("The requested file has not been uploaded"); |
|
133
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function save($repository = '') { |
|
137
|
|
|
|
|
138
|
|
|
if (!empty($repository)) { |
|
139
|
|
|
|
|
140
|
|
|
$this->path = $repository; |
|
141
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if (!empty($this->path) && file_exists($this->path)) { |
|
145
|
|
|
|
|
146
|
|
|
if (move_uploaded_file($this->getTemporaryName(), $this->getLocalName())) { |
|
147
|
|
|
|
|
148
|
|
|
return $this->fileIsSaved(); |
|
149
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
throw new DispatcherException("Unable to save file"); |
|
153
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
throw new DispatcherException("Repository path is not available"); |
|
157
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
private function loadFromUploadedFile($fileControl) { |
|
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
if (isset($_FILES[$fileControl])) { |
|
163
|
|
|
|
|
164
|
|
|
$file = $_FILES[$fileControl]; |
|
165
|
|
|
|
|
166
|
|
|
$this->tname = $file['tmp_name']; |
|
167
|
|
|
$this->fname = $file['name']; |
|
168
|
|
|
$this->ctype = $file['type']; |
|
169
|
|
|
$this->size = intval($file['size']); |
|
170
|
|
|
$this->upld = filectime($file['tmp_name']); |
|
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
$this->createSlug(); |
|
173
|
|
|
|
|
174
|
|
|
return $this; |
|
175
|
|
|
|
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
throw new DispatcherException("File not uploaded"); |
|
179
|
|
|
|
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
private function createSlug() { |
|
183
|
|
|
|
|
184
|
|
|
preg_match_all("/[a-z0-9]+/", iconv("UTF-8", "ASCII//TRANSLIT", strtolower(preg_replace('/\..*?$/', '', $this->fname))), $matches); |
|
185
|
|
|
|
|
186
|
|
|
$this->slug = implode('-', $matches[0]); |
|
187
|
|
|
|
|
188
|
|
|
if (!empty($this->path)) { |
|
189
|
|
|
|
|
190
|
|
|
$files = glob($this->path . "/" . $slug . "*"); |
|
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
$count = count ($files); |
|
193
|
|
|
|
|
194
|
|
|
if ($count > 0) { |
|
195
|
|
|
|
|
196
|
|
|
$this->slug .= "-" . $count; |
|
197
|
|
|
|
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
5 |
|
public static function fromUploadedFiles($repository = '') { |
|
|
|
|
|
|
205
|
|
|
|
|
206
|
5 |
|
$files = array(); |
|
207
|
|
|
|
|
208
|
5 |
|
foreach ($_FILES as $idx => $data) { |
|
209
|
|
|
|
|
210
|
|
|
$files[] = new File($idx, $repository); |
|
211
|
|
|
|
|
212
|
5 |
|
} |
|
213
|
|
|
|
|
214
|
5 |
|
return $files; |
|
215
|
|
|
|
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
} |
|
219
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: