Skip to content

Commit 5eaf885

Browse files
committed
Merge branch 'master' into jquery-ui
2 parents 9475f2b + 04c6bda commit 5eaf885

File tree

2 files changed

+71
-56
lines changed

2 files changed

+71
-56
lines changed

server/php/index.php

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/*
3-
* jQuery File Upload Plugin PHP Example 5.7
3+
* jQuery File Upload Plugin PHP Example 5.14
44
* https://github.com/blueimp/jQuery-File-Upload
55
*
66
* Copyright 2010, Sebastian Tschan
@@ -11,36 +11,5 @@
1111
*/
1212

1313
error_reporting(E_ALL | E_STRICT);
14-
1514
require('upload.class.php');
16-
1715
$upload_handler = new UploadHandler();
18-
19-
header('Pragma: no-cache');
20-
header('Cache-Control: no-store, no-cache, must-revalidate');
21-
header('Content-Disposition: inline; filename="files.json"');
22-
header('X-Content-Type-Options: nosniff');
23-
header('Access-Control-Allow-Origin: *');
24-
header('Access-Control-Allow-Methods: OPTIONS, HEAD, GET, POST, PUT, DELETE');
25-
header('Access-Control-Allow-Headers: X-File-Name, X-File-Type, X-File-Size');
26-
27-
switch ($_SERVER['REQUEST_METHOD']) {
28-
case 'OPTIONS':
29-
break;
30-
case 'HEAD':
31-
case 'GET':
32-
$upload_handler->get();
33-
break;
34-
case 'POST':
35-
if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
36-
$upload_handler->delete();
37-
} else {
38-
$upload_handler->post();
39-
}
40-
break;
41-
case 'DELETE':
42-
$upload_handler->delete();
43-
break;
44-
default:
45-
header('HTTP/1.1 405 Method Not Allowed');
46-
}

server/php/upload.class.php

Lines changed: 70 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/*
3-
* jQuery File Upload Plugin PHP Class 5.13.1
3+
* jQuery File Upload Plugin PHP Class 5.14
44
* https://github.com/blueimp/jQuery-File-Upload
55
*
66
* Copyright 2010, Sebastian Tschan
@@ -33,7 +33,7 @@ class UploadHandler
3333
'min_height' => 'Image requires a minimum height'
3434
);
3535

36-
function __construct($options=null) {
36+
function __construct($options = null, $initialize = true) {
3737
$this->options = array(
3838
'script_url' => $this->get_full_url().'/',
3939
'upload_dir' => dirname($_SERVER['SCRIPT_FILENAME']).'/files/',
@@ -42,6 +42,7 @@ function __construct($options=null) {
4242
// Set the following option to 'POST', if your server does not support
4343
// DELETE requests. This is a parameter sent to the client:
4444
'delete_type' => 'DELETE',
45+
'access_control_allow_origin' => '*',
4546
// The php.ini settings upload_max_filesize and post_max_size
4647
// take precedence over the following max_file_size setting:
4748
'max_file_size' => null,
@@ -82,6 +83,33 @@ function __construct($options=null) {
8283
if ($options) {
8384
$this->options = array_replace_recursive($this->options, $options);
8485
}
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+
}
85113
}
86114

87115
protected function get_full_url() {
@@ -392,19 +420,52 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro
392420
return $file;
393421
}
394422

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) {
396458
$file_name = isset($_REQUEST['file']) ?
397459
basename(stripslashes($_REQUEST['file'])) : null;
398460
if ($file_name) {
399461
$info = $this->get_file_object($file_name);
400462
} else {
401463
$info = $this->get_file_objects();
402464
}
403-
header('Content-type: application/json');
404-
echo json_encode($info);
465+
return $this->generate_response($info, $print_response);
405466
}
406467

407-
public function post() {
468+
public function post($print_response = true) {
408469
if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
409470
return $this->delete();
410471
}
@@ -444,24 +505,10 @@ public function post() {
444505
isset($upload['error']) ? $upload['error'] : null
445506
);
446507
}
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);
462509
}
463510

464-
public function delete() {
511+
public function delete($print_response = true) {
465512
$file_name = isset($_REQUEST['file']) ?
466513
basename(stripslashes($_REQUEST['file'])) : null;
467514
$file_path = $this->options['upload_dir'].$file_name;
@@ -474,8 +521,7 @@ public function delete() {
474521
}
475522
}
476523
}
477-
header('Content-type: application/json');
478-
echo json_encode($success);
524+
return $this->generate_response($info, $print_response);
479525
}
480526

481527
}

0 commit comments

Comments
 (0)