Skip to content

Commit 1336453

Browse files
committed
Added handle_form_data method to ease handling of form data.
Added index as parameter passed to the handle_file_upload method. Refactored the file upload validation.
1 parent cc28a16 commit 1336453

File tree

1 file changed

+38
-26
lines changed

1 file changed

+38
-26
lines changed

server/php/upload.class.php

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/*
3-
* jQuery File Upload Plugin PHP Class 5.9.2
3+
* jQuery File Upload Plugin PHP Class 5.10
44
* https://github.com/blueimp/jQuery-File-Upload
55
*
66
* Copyright 2010, Sebastian Tschan
@@ -13,7 +13,7 @@
1313
class UploadHandler
1414
{
1515
protected $options;
16-
16+
1717
function __construct($options=null) {
1818
$this->options = array(
1919
'script_url' => $this->getFullUrl().'/',
@@ -68,7 +68,7 @@ protected function getFullUrl() {
6868
$_SERVER['SERVER_PORT'] === 80 ? '' : ':'.$_SERVER['SERVER_PORT']))).
6969
substr($_SERVER['SCRIPT_NAME'],0, strrpos($_SERVER['SCRIPT_NAME'], '/'));
7070
}
71-
71+
7272
protected function set_file_delete_url($file) {
7373
$file->delete_url = $this->options['script_url']
7474
.'?file='.rawurlencode($file->name);
@@ -77,7 +77,7 @@ protected function set_file_delete_url($file) {
7777
$file->delete_url .= '&_method=DELETE';
7878
}
7979
}
80-
80+
8181
protected function get_file_object($file_name) {
8282
$file_path = $this->options['upload_dir'].$file_name;
8383
if (is_file($file_path) && $file_name[0] !== '.') {
@@ -96,7 +96,7 @@ protected function get_file_object($file_name) {
9696
}
9797
return null;
9898
}
99-
99+
100100
protected function get_file_objects() {
101101
return array_values(array_filter(array_map(
102102
array($this, 'get_file_object'),
@@ -164,13 +164,19 @@ protected function create_scaled_image($file_name, $options) {
164164
@imagedestroy($new_img);
165165
return $success;
166166
}
167-
168-
protected function has_error($uploaded_file, $file, $error) {
167+
168+
protected function validate($uploaded_file, $file, $error, $index) {
169169
if ($error) {
170-
return $error;
170+
$file->error = $error;
171+
return false;
172+
}
173+
if (!$file->name) {
174+
$file->error = 'missingFileName';
175+
return false;
171176
}
172177
if (!preg_match($this->options['accept_file_types'], $file->name)) {
173-
return 'acceptFileTypes';
178+
$file->error = 'acceptFileTypes';
179+
return false;
174180
}
175181
if ($uploaded_file && is_uploaded_file($uploaded_file)) {
176182
$file_size = filesize($uploaded_file);
@@ -181,18 +187,21 @@ protected function has_error($uploaded_file, $file, $error) {
181187
$file_size > $this->options['max_file_size'] ||
182188
$file->size > $this->options['max_file_size'])
183189
) {
184-
return 'maxFileSize';
190+
$file->error = 'maxFileSize';
191+
return false;
185192
}
186193
if ($this->options['min_file_size'] &&
187194
$file_size < $this->options['min_file_size']) {
188-
return 'minFileSize';
195+
$file->error = 'minFileSize';
196+
return false;
189197
}
190198
if (is_int($this->options['max_number_of_files']) && (
191199
count($this->get_file_objects()) >= $this->options['max_number_of_files'])
192200
) {
193-
return 'maxNumberOfFiles';
201+
$file->error = 'maxNumberOfFiles';
202+
return false;
194203
}
195-
return $error;
204+
return true;
196205
}
197206

198207
protected function upcount_name_callback($matches) {
@@ -209,8 +218,8 @@ protected function upcount_name($name) {
209218
1
210219
);
211220
}
212-
213-
protected function trim_file_name($name, $type) {
221+
222+
protected function trim_file_name($name, $type, $index) {
214223
// Remove path information and dots around the filename, to prevent uploading
215224
// into different directories or replacing hidden system files.
216225
// Also remove control characters and spaces (\x00..\x20) around the filename:
@@ -228,6 +237,10 @@ protected function trim_file_name($name, $type) {
228237
return $file_name;
229238
}
230239

240+
protected function handle_form_data($file, $index) {
241+
// Handle form data, e.g. $_REQUEST['description'][$index]
242+
}
243+
231244
protected function orient_image($file_path) {
232245
$exif = @exif_read_data($file_path);
233246
if ($exif === false) {
@@ -256,14 +269,14 @@ protected function orient_image($file_path) {
256269
@imagedestroy($image);
257270
return $success;
258271
}
259-
260-
protected function handle_file_upload($uploaded_file, $name, $size, $type, $error) {
272+
273+
protected function handle_file_upload($uploaded_file, $name, $size, $type, $error, $index) {
261274
$file = new stdClass();
262-
$file->name = $this->trim_file_name($name, $type);
275+
$file->name = $this->trim_file_name($name, $type, $index);
263276
$file->size = intval($size);
264277
$file->type = $type;
265-
$error = $this->has_error($uploaded_file, $file, $error);
266-
if (!$error && $file->name) {
278+
if ($this->validate($uploaded_file, $file, $error, $index)) {
279+
$this->handle_form_data($file, $index);
267280
$file_path = $this->options['upload_dir'].$file->name;
268281
$append_file = !$this->options['discard_aborted_uploads'] &&
269282
is_file($file_path) && $file->size > filesize($file_path);
@@ -310,12 +323,10 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro
310323
}
311324
$file->size = $file_size;
312325
$this->set_file_delete_url($file);
313-
} else {
314-
$file->error = $error;
315326
}
316327
return $file;
317328
}
318-
329+
319330
public function get() {
320331
$file_name = isset($_REQUEST['file']) ?
321332
basename(stripslashes($_REQUEST['file'])) : null;
@@ -327,7 +338,7 @@ public function get() {
327338
header('Content-type: application/json');
328339
echo json_encode($info);
329340
}
330-
341+
331342
public function post() {
332343
if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
333344
return $this->delete();
@@ -347,7 +358,8 @@ public function post() {
347358
$_SERVER['HTTP_X_FILE_SIZE'] : $upload['size'][$index],
348359
isset($_SERVER['HTTP_X_FILE_TYPE']) ?
349360
$_SERVER['HTTP_X_FILE_TYPE'] : $upload['type'][$index],
350-
$upload['error'][$index]
361+
$upload['error'][$index],
362+
$index
351363
);
352364
}
353365
} elseif ($upload || isset($_SERVER['HTTP_X_FILE_NAME'])) {
@@ -383,7 +395,7 @@ public function post() {
383395
}
384396
echo $json;
385397
}
386-
398+
387399
public function delete() {
388400
$file_name = isset($_REQUEST['file']) ?
389401
basename(stripslashes($_REQUEST['file'])) : null;

0 commit comments

Comments
 (0)