1
1
<?php
2
2
/*
3
- * jQuery File Upload Plugin PHP Class 5.13.1
3
+ * jQuery File Upload Plugin PHP Class 5.14
4
4
* https://github.com/blueimp/jQuery-File-Upload
5
5
*
6
6
* Copyright 2010, Sebastian Tschan
@@ -33,7 +33,7 @@ class UploadHandler
33
33
'min_height ' => 'Image requires a minimum height '
34
34
);
35
35
36
- function __construct ($ options= null ) {
36
+ function __construct ($ options = null , $ initialize = true ) {
37
37
$ this ->options = array (
38
38
'script_url ' => $ this ->get_full_url ().'/ ' ,
39
39
'upload_dir ' => dirname ($ _SERVER ['SCRIPT_FILENAME ' ]).'/files/ ' ,
@@ -42,6 +42,7 @@ function __construct($options=null) {
42
42
// Set the following option to 'POST', if your server does not support
43
43
// DELETE requests. This is a parameter sent to the client:
44
44
'delete_type ' => 'DELETE ' ,
45
+ 'access_control_allow_origin ' => '* ' ,
45
46
// The php.ini settings upload_max_filesize and post_max_size
46
47
// take precedence over the following max_file_size setting:
47
48
'max_file_size ' => null ,
@@ -82,6 +83,33 @@ function __construct($options=null) {
82
83
if ($ options ) {
83
84
$ this ->options = array_replace_recursive ($ this ->options , $ options );
84
85
}
86
+ if ($ initialize ) {
87
+ $ this ->initialize ();
88
+ }
89
+ }
90
+
91
+ protected function initialize () {
92
+ switch ($ _SERVER ['REQUEST_METHOD ' ]) {
93
+ case 'OPTIONS ' :
94
+ case 'HEAD ' :
95
+ $ this ->head ();
96
+ break ;
97
+ case 'GET ' :
98
+ $ this ->get ();
99
+ break ;
100
+ case 'POST ' :
101
+ if (isset ($ _REQUEST ['_method ' ]) && $ _REQUEST ['_method ' ] === 'DELETE ' ) {
102
+ $ this ->delete ();
103
+ } else {
104
+ $ this ->post ();
105
+ }
106
+ break ;
107
+ case 'DELETE ' :
108
+ $ this ->delete ();
109
+ break ;
110
+ default :
111
+ header ('HTTP/1.1 405 Method Not Allowed ' );
112
+ }
85
113
}
86
114
87
115
protected function get_full_url () {
@@ -392,19 +420,52 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro
392
420
return $ file ;
393
421
}
394
422
395
- public function get () {
423
+ protected function generate_response ($ content , $ print_response = true ) {
424
+ if ($ print_response ) {
425
+ $ json = json_encode ($ content );
426
+ $ redirect = isset ($ _REQUEST ['redirect ' ]) ?
427
+ stripslashes ($ _REQUEST ['redirect ' ]) : null ;
428
+ if ($ redirect ) {
429
+ header ('Location: ' .sprintf ($ redirect , rawurlencode ($ json )));
430
+ return ;
431
+ }
432
+ $ this ->head ();
433
+ echo $ json ;
434
+ }
435
+ return $ content ;
436
+ }
437
+
438
+ public function head () {
439
+ header ('Pragma: no-cache ' );
440
+ header ('Cache-Control: no-store, no-cache, must-revalidate ' );
441
+ header ('Content-Disposition: inline; filename="files.json" ' );
442
+ header ('X-Content-Type-Options: nosniff ' );
443
+ if ($ this ->options ['access_control_allow_origin ' ]) {
444
+ header ('Access-Control-Allow-Origin: ' .$ this ->options ['access_control_allow_origin ' ]);
445
+ header ('Access-Control-Allow-Methods: OPTIONS, HEAD, GET, POST, PUT, DELETE ' );
446
+ header ('Access-Control-Allow-Headers: X-File-Name, X-File-Type, X-File-Size ' );
447
+ }
448
+ header ('Vary: Accept ' );
449
+ if (isset ($ _SERVER ['HTTP_ACCEPT ' ]) &&
450
+ (strpos ($ _SERVER ['HTTP_ACCEPT ' ], 'application/json ' ) !== false )) {
451
+ header ('Content-type: application/json ' );
452
+ } else {
453
+ header ('Content-type: text/plain ' );
454
+ }
455
+ }
456
+
457
+ public function get ($ print_response = true ) {
396
458
$ file_name = isset ($ _REQUEST ['file ' ]) ?
397
459
basename (stripslashes ($ _REQUEST ['file ' ])) : null ;
398
460
if ($ file_name ) {
399
461
$ info = $ this ->get_file_object ($ file_name );
400
462
} else {
401
463
$ info = $ this ->get_file_objects ();
402
464
}
403
- header ('Content-type: application/json ' );
404
- echo json_encode ($ info );
465
+ return $ this ->generate_response ($ info , $ print_response );
405
466
}
406
467
407
- public function post () {
468
+ public function post ($ print_response = true ) {
408
469
if (isset ($ _REQUEST ['_method ' ]) && $ _REQUEST ['_method ' ] === 'DELETE ' ) {
409
470
return $ this ->delete ();
410
471
}
@@ -444,24 +505,10 @@ public function post() {
444
505
isset ($ upload ['error ' ]) ? $ upload ['error ' ] : null
445
506
);
446
507
}
447
- header ('Vary: Accept ' );
448
- $ json = json_encode ($ info );
449
- $ redirect = isset ($ _REQUEST ['redirect ' ]) ?
450
- stripslashes ($ _REQUEST ['redirect ' ]) : null ;
451
- if ($ redirect ) {
452
- header ('Location: ' .sprintf ($ redirect , rawurlencode ($ json )));
453
- return ;
454
- }
455
- if (isset ($ _SERVER ['HTTP_ACCEPT ' ]) &&
456
- (strpos ($ _SERVER ['HTTP_ACCEPT ' ], 'application/json ' ) !== false )) {
457
- header ('Content-type: application/json ' );
458
- } else {
459
- header ('Content-type: text/plain ' );
460
- }
461
- echo $ json ;
508
+ return $ this ->generate_response ($ info , $ print_response );
462
509
}
463
510
464
- public function delete () {
511
+ public function delete ($ print_response = true ) {
465
512
$ file_name = isset ($ _REQUEST ['file ' ]) ?
466
513
basename (stripslashes ($ _REQUEST ['file ' ])) : null ;
467
514
$ file_path = $ this ->options ['upload_dir ' ].$ file_name ;
@@ -474,8 +521,7 @@ public function delete() {
474
521
}
475
522
}
476
523
}
477
- header ('Content-type: application/json ' );
478
- echo json_encode ($ success );
524
+ return $ this ->generate_response ($ info , $ print_response );
479
525
}
480
526
481
527
}
0 commit comments