1
1
<?php
2
2
/*
3
- * jQuery File Upload Plugin PHP Class 5.15
3
+ * jQuery File Upload Plugin PHP Class 5.17
4
4
* https://github.com/blueimp/jQuery-File-Upload
5
5
*
6
6
* Copyright 2010, Sebastian Tschan
@@ -38,16 +38,23 @@ function __construct($options = null, $initialize = true) {
38
38
'script_url ' => $ this ->get_full_url ().'/ ' ,
39
39
'upload_dir ' => dirname ($ _SERVER ['SCRIPT_FILENAME ' ]).'/files/ ' ,
40
40
'upload_url ' => $ this ->get_full_url ().'/files/ ' ,
41
+ 'user_dirs ' => false ,
42
+ 'mkdir_mode ' => 0755 ,
41
43
'param_name ' => 'files ' ,
42
44
// Set the following option to 'POST', if your server does not support
43
45
// DELETE requests. This is a parameter sent to the client:
44
46
'delete_type ' => 'DELETE ' ,
45
47
'access_control_allow_origin ' => '* ' ,
48
+ // Enable to provide file downloads via GET requests to the PHP script:
49
+ 'download_via_php ' => false ,
50
+ // Defines which files can be displayed inline when downloaded:
51
+ 'inline_file_types ' => '/\.(gif|jpe?g|png)$/i ' ,
52
+ // Defines which files (based on their names) are accepted for upload:
53
+ 'accept_file_types ' => '/.+$/i ' ,
46
54
// The php.ini settings upload_max_filesize and post_max_size
47
55
// take precedence over the following max_file_size setting:
48
56
'max_file_size ' => null ,
49
57
'min_file_size ' => 1 ,
50
- 'accept_file_types ' => '/.+$/i ' ,
51
58
// The maximum number of files for the upload directory:
52
59
'max_number_of_files ' => null ,
53
60
// Image resolution restrictions:
@@ -61,20 +68,23 @@ function __construct($options = null, $initialize = true) {
61
68
'orient_image ' => false ,
62
69
'image_versions ' => array (
63
70
// Uncomment the following version to restrict the size of
64
- // uploaded images. You can also add additional versions with
65
- // their own upload directories:
71
+ // uploaded images:
66
72
/*
67
- 'large' => array(
68
- 'upload_dir' => dirname($_SERVER['SCRIPT_FILENAME']).'/files/',
69
- 'upload_url' => $this->get_full_url().'/files/',
73
+ '' => array(
70
74
'max_width' => 1920,
71
75
'max_height' => 1200,
72
76
'jpeg_quality' => 95
73
77
),
74
78
*/
79
+ // Uncomment the following to create medium sized images:
80
+ /*
81
+ 'medium' => array(
82
+ 'max_width' => 800,
83
+ 'max_height' => 600,
84
+ 'jpeg_quality' => 80
85
+ ),
86
+ */
75
87
'thumbnail ' => array (
76
- 'upload_dir ' => dirname ($ _SERVER ['SCRIPT_FILENAME ' ]).'/thumbnails/ ' ,
77
- 'upload_url ' => $ this ->get_full_url ().'/thumbnails/ ' ,
78
88
'max_width ' => 80 ,
79
89
'max_height ' => 80
80
90
)
@@ -98,11 +108,7 @@ protected function initialize() {
98
108
$ this ->get ();
99
109
break ;
100
110
case 'POST ' :
101
- if (isset ($ _REQUEST ['_method ' ]) && $ _REQUEST ['_method ' ] === 'DELETE ' ) {
102
- $ this ->delete ();
103
- } else {
104
- $ this ->post ();
105
- }
111
+ $ this ->post ();
106
112
break ;
107
113
case 'DELETE ' :
108
114
$ this ->delete ();
@@ -123,7 +129,39 @@ protected function get_full_url() {
123
129
substr ($ _SERVER ['SCRIPT_NAME ' ],0 , strrpos ($ _SERVER ['SCRIPT_NAME ' ], '/ ' ));
124
130
}
125
131
126
- protected function set_file_delete_url ($ file ) {
132
+ protected function get_user_id () {
133
+ @session_start ();
134
+ return session_id ();
135
+ }
136
+
137
+ protected function get_user_path () {
138
+ if ($ this ->options ['user_dirs ' ]) {
139
+ return $ this ->get_user_id ().'/ ' ;
140
+ }
141
+ return '' ;
142
+ }
143
+
144
+ protected function get_upload_path ($ file_name = null , $ version = null ) {
145
+ $ file_name = $ file_name ? $ file_name : '' ;
146
+ $ version_path = empty ($ version ) ? '' : $ version .'/ ' ;
147
+ return $ this ->options ['upload_dir ' ].$ this ->get_user_path ()
148
+ .$ version_path .$ file_name ;
149
+ }
150
+
151
+ protected function get_download_url ($ file_name , $ version = null ) {
152
+ if ($ this ->options ['download_via_php ' ]) {
153
+ $ url = $ this ->options ['script_url ' ].'?file= ' .rawurlencode ($ file_name );
154
+ if ($ version ) {
155
+ $ url .= '&version= ' .rawurlencode ($ version );
156
+ }
157
+ return $ url .'&download=1 ' ;
158
+ }
159
+ $ version_path = empty ($ version ) ? '' : rawurlencode ($ version ).'/ ' ;
160
+ return $ this ->options ['upload_url ' ].$ this ->get_user_path ()
161
+ .$ version_path .rawurlencode ($ file_name );
162
+ }
163
+
164
+ protected function set_file_delete_properties ($ file ) {
127
165
$ file ->delete_url = $ this ->options ['script_url ' ]
128
166
.'?file= ' .rawurlencode ($ file ->name );
129
167
$ file ->delete_type = $ this ->options ['delete_type ' ];
@@ -149,35 +187,64 @@ protected function get_file_size($file_path, $clear_stat_cache = false) {
149
187
150
188
}
151
189
152
- protected function get_file_object ($ file_name ) {
153
- $ file_path = $ this ->options [ ' upload_dir ' ]. $ file_name ;
190
+ protected function is_valid_file_object ($ file_name ) {
191
+ $ file_path = $ this ->get_upload_path ( $ file_name) ;
154
192
if (is_file ($ file_path ) && $ file_name [0 ] !== '. ' ) {
193
+ return true ;
194
+ }
195
+ return false ;
196
+ }
197
+
198
+ protected function get_file_object ($ file_name ) {
199
+ if ($ this ->is_valid_file_object ($ file_name )) {
155
200
$ file = new stdClass ();
156
201
$ file ->name = $ file_name ;
157
- $ file ->size = $ this ->get_file_size ($ file_path );
158
- $ file ->url = $ this ->options ['upload_url ' ].rawurlencode ($ file ->name );
202
+ $ file ->size = $ this ->get_file_size (
203
+ $ this ->get_upload_path ($ file_name )
204
+ );
205
+ $ file ->url = $ this ->get_download_url ($ file ->name );
159
206
foreach ($ this ->options ['image_versions ' ] as $ version => $ options ) {
160
- if (is_file ($ options ['upload_dir ' ].$ file_name )) {
161
- $ file ->{$ version .'_url ' } = $ options ['upload_url ' ]
162
- .rawurlencode ($ file ->name );
207
+ if (!empty ($ version )) {
208
+ if (is_file ($ this ->get_upload_path ($ file_name , $ version ))) {
209
+ $ file ->{$ version .'_url ' } = $ this ->get_download_url (
210
+ $ file ->name ,
211
+ $ version
212
+ );
213
+ }
163
214
}
164
215
}
165
- $ this ->set_file_delete_url ($ file );
216
+ $ this ->set_file_delete_properties ($ file );
166
217
return $ file ;
167
218
}
168
219
return null ;
169
220
}
170
221
171
- protected function get_file_objects () {
222
+ protected function get_file_objects ($ iteration_method = 'get_file_object ' ) {
223
+ $ upload_dir = $ this ->get_upload_path ();
224
+ if (!is_dir ($ upload_dir )) {
225
+ mkdir ($ upload_dir , $ this ->options ['mkdir_mode ' ]);
226
+ }
172
227
return array_values (array_filter (array_map (
173
- array ($ this , ' get_file_object ' ),
174
- scandir ($ this -> options [ ' upload_dir ' ] )
228
+ array ($ this , $ iteration_method ),
229
+ scandir ($ upload_dir )
175
230
)));
176
231
}
177
232
178
- protected function create_scaled_image ($ file_name , $ options ) {
179
- $ file_path = $ this ->options ['upload_dir ' ].$ file_name ;
180
- $ new_file_path = $ options ['upload_dir ' ].$ file_name ;
233
+ protected function count_file_objects () {
234
+ return count ($ this ->get_file_objects ('is_valid_file_object ' ));
235
+ }
236
+
237
+ protected function create_scaled_image ($ file_name , $ version , $ options ) {
238
+ $ file_path = $ this ->get_upload_path ($ file_name );
239
+ if (!empty ($ version )) {
240
+ $ version_dir = $ this ->get_upload_path (null , $ version );
241
+ if (!is_dir ($ version_dir )) {
242
+ mkdir ($ version_dir , $ this ->options ['mkdir_mode ' ]);
243
+ }
244
+ $ new_file_path = $ version_dir .'/ ' .$ file_name ;
245
+ } else {
246
+ $ new_file_path = $ file_path ;
247
+ }
181
248
list ($ img_width , $ img_height ) = @getimagesize ($ file_path );
182
249
if (!$ img_width || !$ img_height ) {
183
250
return false ;
@@ -272,7 +339,7 @@ protected function validate($uploaded_file, $file, $error, $index) {
272
339
return false ;
273
340
}
274
341
if (is_int ($ this ->options ['max_number_of_files ' ]) && (
275
- count ( $ this ->get_file_objects () ) >= $ this ->options ['max_number_of_files ' ])
342
+ $ this ->count_file_objects ( ) >= $ this ->options ['max_number_of_files ' ])
276
343
) {
277
344
$ file ->error = $ this ->get_error_message ('max_number_of_files ' );
278
345
return false ;
@@ -324,10 +391,13 @@ protected function trim_file_name($name, $type, $index, $content_range) {
324
391
preg_match ('/^image\/(gif|jpe?g|png)/ ' , $ type , $ matches )) {
325
392
$ file_name .= '. ' .$ matches [1 ];
326
393
}
394
+ while (is_dir ($ this ->get_upload_path ($ file_name ))) {
395
+ $ file_name = $ this ->upcount_name ($ file_name );
396
+ }
327
397
$ uploaded_bytes = $ this ->fix_integer_overflow (intval ($ content_range [1 ]));
328
- while (is_file ($ this ->options [ ' upload_dir ' ]. $ file_name )) {
398
+ while (is_file ($ this ->get_upload_path ( $ file_name) )) {
329
399
if ($ uploaded_bytes === $ this ->get_file_size (
330
- $ this ->options [ ' upload_dir ' ]. $ file_name )) {
400
+ $ this ->get_upload_path ( $ file_name) )) {
331
401
break ;
332
402
}
333
403
$ file_name = $ this ->upcount_name ($ file_name );
@@ -376,7 +446,11 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro
376
446
$ file ->type = $ type ;
377
447
if ($ this ->validate ($ uploaded_file , $ file , $ error , $ index )) {
378
448
$ this ->handle_form_data ($ file , $ index );
379
- $ file_path = $ this ->options ['upload_dir ' ].$ file ->name ;
449
+ $ upload_dir = $ this ->get_upload_path ();
450
+ if (!is_dir ($ upload_dir )) {
451
+ mkdir ($ upload_dir , $ this ->options ['mkdir_mode ' ]);
452
+ }
453
+ $ file_path = $ this ->get_upload_path ($ file ->name );
380
454
$ append_file = $ content_range && is_file ($ file_path ) &&
381
455
$ file ->size > $ this ->get_file_size ($ file_path );
382
456
if ($ uploaded_file && is_uploaded_file ($ uploaded_file )) {
@@ -403,12 +477,14 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro
403
477
if ($ this ->options ['orient_image ' ]) {
404
478
$ this ->orient_image ($ file_path );
405
479
}
406
- $ file ->url = $ this ->options [ ' upload_url ' ]. rawurlencode ($ file ->name );
480
+ $ file ->url = $ this ->get_download_url ($ file ->name );
407
481
foreach ($ this ->options ['image_versions ' ] as $ version => $ options ) {
408
- if ($ this ->create_scaled_image ($ file ->name , $ options )) {
409
- if ($ this ->options ['upload_dir ' ] !== $ options ['upload_dir ' ]) {
410
- $ file ->{$ version .'_url ' } = $ options ['upload_url ' ]
411
- .rawurlencode ($ file ->name );
482
+ if ($ this ->create_scaled_image ($ file ->name , $ version , $ options )) {
483
+ if (!empty ($ version )) {
484
+ $ file ->{$ version .'_url ' } = $ this ->get_download_url (
485
+ $ file ->name ,
486
+ $ version
487
+ );
412
488
} else {
413
489
$ file_size = $ this ->get_file_size ($ file_path , true );
414
490
}
@@ -419,7 +495,7 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro
419
495
$ file ->error = 'abort ' ;
420
496
}
421
497
$ file ->size = $ file_size ;
422
- $ this ->set_file_delete_url ($ file );
498
+ $ this ->set_file_delete_properties ($ file );
423
499
}
424
500
return $ file ;
425
501
}
@@ -434,15 +510,69 @@ protected function generate_response($content, $print_response = true) {
434
510
return ;
435
511
}
436
512
$ this ->head ();
513
+ if (isset ($ _SERVER ['HTTP_CONTENT_RANGE ' ]) && is_array ($ content ) &&
514
+ is_object ($ content [0 ]) && $ content [0 ]->size ) {
515
+ header ('Range: 0- ' .($ this ->fix_integer_overflow (intval ($ content [0 ]->size )) - 1 ));
516
+ }
437
517
echo $ json ;
438
518
}
439
519
return $ content ;
440
520
}
441
521
522
+ protected function get_version_param () {
523
+ return isset ($ _GET ['version ' ]) ? basename (stripslashes ($ _GET ['version ' ])) : null ;
524
+ }
525
+
526
+ protected function get_file_name_param () {
527
+ return isset ($ _GET ['file ' ]) ? basename (stripslashes ($ _GET ['file ' ])) : null ;
528
+ }
529
+
530
+ protected function get_file_type ($ file_path ) {
531
+ switch (strtolower (pathinfo ($ file_path , PATHINFO_EXTENSION ))) {
532
+ case 'jpeg ' :
533
+ case 'jpg ' :
534
+ return 'image/jpeg ' ;
535
+ case 'png ' :
536
+ return 'image/png ' ;
537
+ case 'gif ' :
538
+ return 'image/gif ' ;
539
+ default :
540
+ return '' ;
541
+ }
542
+ }
543
+
544
+ protected function download () {
545
+ if (!$ this ->options ['download_via_php ' ]) {
546
+ header ('HTTP/1.1 403 Forbidden ' );
547
+ return ;
548
+ }
549
+ $ file_name = $ this ->get_file_name_param ();
550
+ if ($ this ->is_valid_file_object ($ file_name )) {
551
+ $ file_path = $ this ->get_upload_path ($ file_name , $ this ->get_version_param ());
552
+ if (is_file ($ file_path )) {
553
+ if (!preg_match ($ this ->options ['inline_file_types ' ], $ file_name )) {
554
+ header ('Content-Description: File Transfer ' );
555
+ header ('Content-Type: application/octet-stream ' );
556
+ header ('Content-Disposition: attachment; filename= ' .$ file_name );
557
+ header ('Content-Transfer-Encoding: binary ' );
558
+ } else {
559
+ // Prevent Internet Explorer from MIME-sniffing the content-type:
560
+ header ('X-Content-Type-Options: nosniff ' );
561
+ header ('Content-Type: ' .$ this ->get_file_type ($ file_path ));
562
+ header ('Content-Disposition: inline; filename=" ' .$ file_name .'" ' );
563
+ }
564
+ header ('Content-Length: ' .$ this ->get_file_size ($ file_path ));
565
+ header ('Last-Modified: ' .gmdate ('D, d M Y H:i:s T ' , filemtime ($ file_path )));
566
+ readfile ($ file_path );
567
+ }
568
+ }
569
+ }
570
+
442
571
public function head () {
443
572
header ('Pragma: no-cache ' );
444
573
header ('Cache-Control: no-store, no-cache, must-revalidate ' );
445
574
header ('Content-Disposition: inline; filename="files.json" ' );
575
+ // Prevent Internet Explorer from MIME-sniffing the content-type:
446
576
header ('X-Content-Type-Options: nosniff ' );
447
577
if ($ this ->options ['access_control_allow_origin ' ]) {
448
578
header ('Access-Control-Allow-Origin: ' .$ this ->options ['access_control_allow_origin ' ]);
@@ -459,8 +589,10 @@ public function head() {
459
589
}
460
590
461
591
public function get ($ print_response = true ) {
462
- $ file_name = isset ($ _REQUEST ['file ' ]) ?
463
- basename (stripslashes ($ _REQUEST ['file ' ])) : null ;
592
+ if ($ print_response && isset ($ _GET ['download ' ])) {
593
+ return $ this ->download ();
594
+ }
595
+ $ file_name = $ this ->get_file_name_param ();
464
596
if ($ file_name ) {
465
597
$ info = $ this ->get_file_object ($ file_name );
466
598
} else {
@@ -471,7 +603,7 @@ public function get($print_response = true) {
471
603
472
604
public function post ($ print_response = true ) {
473
605
if (isset ($ _REQUEST ['_method ' ]) && $ _REQUEST ['_method ' ] === 'DELETE ' ) {
474
- return $ this ->delete ();
606
+ return $ this ->delete ($ print_response );
475
607
}
476
608
$ upload = isset ($ _FILES [$ this ->options ['param_name ' ]]) ?
477
609
$ _FILES [$ this ->options ['param_name ' ]] : null ;
@@ -519,15 +651,16 @@ public function post($print_response = true) {
519
651
}
520
652
521
653
public function delete ($ print_response = true ) {
522
- $ file_name = isset ($ _REQUEST ['file ' ]) ?
523
- basename (stripslashes ($ _REQUEST ['file ' ])) : null ;
524
- $ file_path = $ this ->options ['upload_dir ' ].$ file_name ;
654
+ $ file_name = $ this ->get_file_name_param ();
655
+ $ file_path = $ this ->get_upload_path ($ file_name );
525
656
$ success = is_file ($ file_path ) && $ file_name [0 ] !== '. ' && unlink ($ file_path );
526
657
if ($ success ) {
527
658
foreach ($ this ->options ['image_versions ' ] as $ version => $ options ) {
528
- $ file = $ options ['upload_dir ' ].$ file_name ;
529
- if (is_file ($ file )) {
530
- unlink ($ file );
659
+ if (!empty ($ version )) {
660
+ $ file = $ this ->get_upload_path ($ file_name , $ version );
661
+ if (is_file ($ file )) {
662
+ unlink ($ file );
663
+ }
531
664
}
532
665
}
533
666
}
0 commit comments