Skip to content

Commit 966fbf8

Browse files
authored
Merge pull request #5 from seamuslee001/return_json
Resolve sunhater#155 by allowing kcfinder to send back a JSON response if req…
2 parents f3c8128 + aace599 commit 966fbf8

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

core/class/uploader.php

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ class uploader {
8787
* @var string */
8888
protected $cms = "";
8989

90+
/** Ouput format.
91+
* @var string */
92+
protected $outputFormat = 'js';
93+
9094
/** Magic method which allows read-only access to protected or private class properties
9195
* @param string $property
9296
* @return mixed */
@@ -103,7 +107,7 @@ public function __construct() {
103107
)
104108
$this->cms = $_GET['cms'];
105109

106-
// LINKING UPLOADED FILE
110+
// LINKING UPLOADED FILE
107111
if (count($_FILES))
108112
$this->file = &$_FILES[key($_FILES)];
109113

@@ -195,6 +199,11 @@ public function __construct() {
195199
$this->typeURL = "{$this->config['uploadURL']}/{$this->type}";
196200
}
197201

202+
// Output Format
203+
if (isset($_GET['format'])) {
204+
$this->outputFormat = $_GET['format'];
205+
}
206+
198207
// HOST APPLICATIONS INIT
199208
if (isset($_GET['CKEditorFuncNum'])) {
200209
$this->opener['name'] = "ckeditor";
@@ -729,11 +738,18 @@ protected function callBack($url, $message="") {
729738
$js = $this->$method($url, $message);
730739
}
731740

732-
if (!isset($js))
741+
if ($this->outputFormat == 'json') {
742+
header('Content-Type: application/json');
743+
$json = $this->callBack_json($url, $message);
744+
echo json_encode($json);
745+
}
746+
else {
747+
if (!isset($js)) {
733748
$js = $this->callBack_default($url, $message);
734-
735-
header("Content-Type: text/html; charset={$this->charset}");
736-
echo "<html><body>$js</body></html>";
749+
}
750+
header("Content-Type: text/html; charset={$this->charset}");
751+
echo "<html><body>$js</body></html>";
752+
}
737753
}
738754

739755
protected function callBack_ckeditor($url, $message) {
@@ -787,4 +803,25 @@ protected function callBack_default($url, $message) {
787803
protected function get_htaccess() {
788804
return file_get_contents("conf/upload.htaccess");
789805
}
806+
807+
protected function callBack_json($url, $message) {
808+
$uploaded = !empty($url) ? 1 : 0;
809+
$result = [
810+
'uploaded' => $uploaded
811+
];
812+
if ($uploaded) {
813+
$result['url'] = $url;
814+
$urlPieces = explode('/', $url);
815+
end($urlPieces);
816+
$fileNamekey = key($urlPieces);
817+
$result['fileName'] = $urlPieces[$fileNamekey];
818+
}
819+
else {
820+
$result['error'] = [
821+
'message' => $message,
822+
];
823+
}
824+
return $result;
825+
}
826+
790827
}

0 commit comments

Comments
 (0)