1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class SonglistMediaUploader |
4
|
|
|
{ |
5
|
|
|
var $mediaName; |
6
|
|
|
var $mediaType; |
7
|
|
|
var $mediaSize; |
8
|
|
|
var $mediaTmpName; |
9
|
|
|
var $mediaError; |
10
|
|
|
|
11
|
|
|
var $uploadDir = ''; |
12
|
|
|
|
13
|
|
|
var $allowedMimeTypes = array(); |
14
|
|
|
var $allowedExtensions = array(); |
15
|
|
|
|
16
|
|
|
var $maxFileSize = 3299999999; |
17
|
|
|
var $maxWidth; |
18
|
|
|
var $maxHeight; |
19
|
|
|
|
20
|
|
|
var $targetFileName; |
21
|
|
|
|
22
|
|
|
var $prefix; |
23
|
|
|
|
24
|
|
|
var $errors = array(); |
25
|
|
|
|
26
|
|
|
var $savedDestination; |
27
|
|
|
|
28
|
|
|
var $savedFileName; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor |
32
|
|
|
* |
33
|
|
|
* @param string $uploadDir |
34
|
|
|
* @param array $allowedMimeTypes |
35
|
|
|
* @param int $maxFileSize |
36
|
|
|
* @param int $maxWidth |
37
|
|
|
* @param int $maxHeight |
38
|
|
|
* @param int $cmodvalue |
39
|
|
|
* @param array $allowedExtensions |
40
|
|
|
**/ |
41
|
|
|
function SonglistMediaUploader($uploadDir, $allowedMimeTypes, $maxFileSize, $maxWidth=null, $maxHeight=null, $allowedExtensions=null ) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
if (is_array($allowedMimeTypes)) { |
|
|
|
|
44
|
|
|
$this->allowedMimeTypes =& $allowedMimeTypes; |
45
|
|
|
} |
46
|
|
|
$this->uploadDir = $uploadDir.(substr($uploadDir, strlen($uploadDir)-1,1)!=DS?DS:''); |
47
|
|
|
if (is_dir($uploadDir)) { |
48
|
|
|
foreach(explode(DS, $uploadDir) as $folder) { |
49
|
|
|
$path .= DS . $folder; |
|
|
|
|
50
|
|
|
mkdir($path, 0777); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
$this->maxFileSize = intval($maxFileSize); |
54
|
|
|
if(isset($maxWidth)) { |
55
|
|
|
$this->maxWidth = intval($maxWidth); |
56
|
|
|
} |
57
|
|
|
if(isset($maxHeight)) { |
58
|
|
|
$this->maxHeight = intval($maxHeight); |
59
|
|
|
} |
60
|
|
|
if( isset( $allowedExtensions ) && is_array( $allowedExtensions ) ) { |
61
|
|
|
$this->allowedExtensions =& $allowedExtensions ; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Fetch the uploaded file |
67
|
|
|
* |
68
|
|
|
* @param string $media_name Name of the file field |
69
|
|
|
* @param int $index Index of the file (if more than one uploaded under that name) |
70
|
|
|
* @return bool |
71
|
|
|
**/ |
72
|
|
|
function fetchMedia($index_name, $index = null) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
if (!isset($_FILES[$index_name])) { |
75
|
|
|
$this->setErrors('File not found'); |
76
|
|
|
return false; |
77
|
|
|
} elseif (is_array($_FILES[$index_name]['name'][$index]) && isset($index)) { |
78
|
|
|
$this->mediaName = $_FILES[$index_name]['name'][$index]; |
79
|
|
|
$this->mediaType = $_FILES[$index_name]['type'][$index]; |
80
|
|
|
$this->mediaSize = $_FILES[$index_name]['size'][$index]; |
81
|
|
|
$this->mediaTmpName = $_FILES[$index_name]['tmp_name'][$index]; |
82
|
|
|
$this->mediaError = !empty($_FILES[$index_name]['error'][$index]) ? $_FILES[$index_name]['errir'][$index] : 0; |
83
|
|
|
} else { |
84
|
|
|
$this->mediaName = $_FILES[$index_name]['name']; |
85
|
|
|
$this->mediaType = $_FILES[$index_name]['type']; |
86
|
|
|
$this->mediaSize = $_FILES[$index_name]['size']; |
87
|
|
|
$this->mediaTmpName = $_FILES[$index_name]['tmp_name']; |
88
|
|
|
$this->mediaError = !empty($_FILES[$index_name]['error']) ? $_FILES[$index_name]['error'] : 0; |
89
|
|
|
} |
90
|
|
|
$this->errors = array(); |
91
|
|
|
if (intval($this->mediaSize) < 0) { |
92
|
|
|
$this->setErrors('Invalid File Size'); |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
if ($this->mediaName == '') { |
96
|
|
|
$this->setErrors('Filename Is Empty'); |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
if ($this->mediaTmpName == 'none' || !is_uploaded_file($this->mediaTmpName) || $this->mediaSize == 0 ) { |
100
|
|
|
$this->setErrors('No file uploaded'); |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
if ($this->mediaError > 0) { |
104
|
|
|
$this->setErrors('Error occurred: Error #'.$this->mediaError); |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
return true; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Set the target filename |
112
|
|
|
* |
113
|
|
|
* @param string $value |
114
|
|
|
**/ |
115
|
|
|
function setTargetFileName($value){ |
|
|
|
|
116
|
|
|
$this->targetFileName = strval(trim($value)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Set the prefix |
121
|
|
|
* |
122
|
|
|
* @param string $value |
123
|
|
|
**/ |
124
|
|
|
function setPrefix($value){ |
|
|
|
|
125
|
|
|
$this->prefix = strval(trim($value)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get the uploaded filename |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
**/ |
133
|
|
|
function getMediaName() |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
return $this->mediaName; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get the type of the uploaded file |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
**/ |
143
|
|
|
function getMediaType() |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
return $this->mediaType; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get the size of the uploaded file |
150
|
|
|
* |
151
|
|
|
* @return int |
152
|
|
|
**/ |
153
|
|
|
function getMediaSize() |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
return $this->mediaSize; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get the temporary name that the uploaded file was stored under |
160
|
|
|
* |
161
|
|
|
* @return string |
162
|
|
|
**/ |
163
|
|
|
function getMediaTmpName() |
|
|
|
|
164
|
|
|
{ |
165
|
|
|
return $this->mediaTmpName; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get the saved filename |
170
|
|
|
* |
171
|
|
|
* @return string |
172
|
|
|
**/ |
173
|
|
|
function getSavedFileName(){ |
|
|
|
|
174
|
|
|
return $this->savedFileName; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get the destination the file is saved to |
179
|
|
|
* |
180
|
|
|
* @return string |
181
|
|
|
**/ |
182
|
|
|
function getSavedDestination(){ |
|
|
|
|
183
|
|
|
return $this->savedDestination; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Check the file and copy it to the destination |
188
|
|
|
* |
189
|
|
|
* @return bool |
190
|
|
|
**/ |
191
|
|
|
function upload($chmod = 0644) |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
if ($this->uploadDir == '') { |
194
|
|
|
$this->setErrors('Upload directory not set'); |
195
|
|
|
return false; |
196
|
|
|
} |
197
|
|
|
if (!is_dir($this->uploadDir)) { |
198
|
|
|
$this->setErrors('Failed opening directory: '.$this->uploadDir); |
199
|
|
|
return false; |
200
|
|
|
} |
201
|
|
|
if (!is_writeable($this->uploadDir)) { |
202
|
|
|
$this->setErrors('Failed opening directory with write permission: '.$this->uploadDir); |
203
|
|
|
return false; |
204
|
|
|
} |
205
|
|
|
if (!$this->checkMimeType()) { |
206
|
|
|
$this->setErrors('MIME type not allowed: '.$this->mediaType); |
207
|
|
|
return false; |
208
|
|
|
} |
209
|
|
|
if (!$this->checkExtension()) { |
210
|
|
|
$this->setErrors('Extension not allowed'); |
211
|
|
|
return false; |
212
|
|
|
} |
213
|
|
|
if (!$this->checkMaxFileSize()) { |
214
|
|
|
$this->setErrors('File size too large: '.$this->mediaSize); |
215
|
|
|
} |
216
|
|
|
if (!$this->checkMaxWidth()) { |
217
|
|
|
$this->setErrors(sprintf('File width must be smaller than %u', $this->maxWidth)); |
218
|
|
|
} |
219
|
|
|
if (!$this->checkMaxHeight()) { |
220
|
|
|
$this->setErrors(sprintf('File height must be smaller than %u', $this->maxHeight)); |
221
|
|
|
} |
222
|
|
|
if (count($this->errors) > 0) { |
223
|
|
|
return false; |
224
|
|
|
} |
225
|
|
|
if (!$this->_copyFile($chmod)) { |
226
|
|
|
$this->setErrors('Failed uploading file: '.$this->mediaName); |
227
|
|
|
return false; |
228
|
|
|
} |
229
|
|
|
return true; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Copy the file to its destination |
234
|
|
|
* |
235
|
|
|
* @return bool |
236
|
|
|
**/ |
237
|
|
|
function _copyFile($chmod) |
|
|
|
|
238
|
|
|
{ |
239
|
|
|
$matched = array(); |
240
|
|
|
if (!preg_match("/\.([a-zA-Z0-9]+)$/", $this->mediaName, $matched)) { |
241
|
|
|
return false; |
242
|
|
|
} |
243
|
|
|
if (isset($this->targetFileName)) { |
244
|
|
|
$this->savedFileName = $this->targetFileName; |
245
|
|
|
} elseif (isset($this->prefix)) { |
246
|
|
|
$this->savedFileName = uniqid($this->prefix).'.'.strtolower($matched[1]); |
247
|
|
|
} else { |
248
|
|
|
$this->savedFileName = strtolower($this->mediaName); |
249
|
|
|
} |
250
|
|
|
$this->savedDestination = $this->uploadDir.'/'.$this->savedFileName; |
251
|
|
|
if (!move_uploaded_file($this->mediaTmpName, $this->savedDestination)) { |
252
|
|
|
return false; |
253
|
|
|
} |
254
|
|
|
@chmod($this->savedDestination, $chmod); |
|
|
|
|
255
|
|
|
return true; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Is the file the right size? |
260
|
|
|
* |
261
|
|
|
* @return bool |
262
|
|
|
**/ |
263
|
|
|
function checkMaxFileSize() |
|
|
|
|
264
|
|
|
{ |
265
|
|
|
if ($this->mediaSize > $this->maxFileSize) { |
266
|
|
|
return false; |
267
|
|
|
} |
268
|
|
|
return true; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Is the picture the right width? |
273
|
|
|
* |
274
|
|
|
* @return bool |
275
|
|
|
**/ |
276
|
|
|
function checkMaxWidth() |
|
|
|
|
277
|
|
|
{ |
278
|
|
|
if (!isset($this->maxWidth)||$this->maxWidth<1) { |
279
|
|
|
return true; |
280
|
|
|
} |
281
|
|
|
if (false !== $dimension = getimagesize($this->mediaTmpName)) { |
282
|
|
|
if ($dimension[0] > $this->maxWidth) { |
283
|
|
|
return false; |
284
|
|
|
} |
285
|
|
|
} else { |
286
|
|
|
trigger_error(sprintf('Failed fetching image size of %s, skipping max width check..', $this->mediaTmpName), E_USER_WARNING); |
287
|
|
|
} |
288
|
|
|
return true; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Is the picture the right height? |
293
|
|
|
* |
294
|
|
|
* @return bool |
295
|
|
|
**/ |
296
|
|
|
function checkMaxHeight() |
|
|
|
|
297
|
|
|
{ |
298
|
|
|
if (!isset($this->maxHeight)||$this->maxHeight<1) { |
299
|
|
|
return true; |
300
|
|
|
} |
301
|
|
|
if (false !== $dimension = getimagesize($this->mediaTmpName)) { |
302
|
|
|
if ($dimension[1] > $this->maxHeight) { |
303
|
|
|
return false; |
304
|
|
|
} |
305
|
|
|
} else { |
306
|
|
|
trigger_error(sprintf('Failed fetching image size of %s, skipping max height check..', $this->mediaTmpName), E_USER_WARNING); |
307
|
|
|
} |
308
|
|
|
return true; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Is the file the right Mime type |
313
|
|
|
* |
314
|
|
|
* (is there a right type of mime? ;-) |
315
|
|
|
* |
316
|
|
|
* @return bool |
317
|
|
|
**/ |
318
|
|
|
function checkMimeType() |
|
|
|
|
319
|
|
|
{ |
320
|
|
|
if (count($this->allowedMimeTypes) > 0 && !in_array($this->mediaType, $this->allowedMimeTypes)) { |
321
|
|
|
return false; |
322
|
|
|
} else { |
323
|
|
|
return true; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Is the file the right extension |
329
|
|
|
* |
330
|
|
|
* @return bool |
331
|
|
|
**/ |
332
|
|
|
function checkExtension() |
|
|
|
|
333
|
|
|
{ |
334
|
|
|
$ext = substr( strrchr( $this->mediaName , '.' ) , 1 ) ; |
335
|
|
|
if( ! empty( $this->allowedExtensions ) && ! in_array( strtolower( $ext ) , $this->allowedExtensions ) ) { |
336
|
|
|
return false; |
337
|
|
|
} else { |
338
|
|
|
return true; |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Add an error |
344
|
|
|
* |
345
|
|
|
* @param string $error |
346
|
|
|
**/ |
347
|
|
|
function setErrors($error) |
|
|
|
|
348
|
|
|
{ |
349
|
|
|
$this->errors[] = trim($error); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Get generated errors |
354
|
|
|
* |
355
|
|
|
* @param bool $ashtml Format using HTML? |
356
|
|
|
* |
357
|
|
|
* @return array|string Array of array messages OR HTML string |
358
|
|
|
*/ |
359
|
|
|
function &getErrors($ashtml = true) |
360
|
|
|
{ |
361
|
|
|
if (!$ashtml) { |
362
|
|
|
return $this->errors; |
363
|
|
|
} else { |
364
|
|
|
$ret = ''; |
365
|
|
|
if (count($this->errors) > 0) { |
366
|
|
|
$ret = '<h4>Errors Returned While Uploading</h4>'; |
367
|
|
|
foreach ($this->errors as $error) { |
368
|
|
|
$ret .= $error.'<br />'; |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
return $ret; |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
} |
375
|
|
|
?> |
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.