Skip to content
This repository has been archived by the owner on Feb 23, 2021. It is now read-only.

Resolve #155 by allowing kcfinder to send back a JSON response if requ… #168

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 42 additions & 5 deletions core/class/uploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ class uploader {
* @var string */
protected $cms = "";

/** Ouput format.
* @var string */
protected $outputFormat = 'js';

/** Magic method which allows read-only access to protected or private class properties
* @param string $property
* @return mixed */
Expand All @@ -103,7 +107,7 @@ public function __construct() {
)
$this->cms = $_GET['cms'];

// LINKING UPLOADED FILE
// LINKING UPLOADED FILE
if (count($_FILES))
$this->file = &$_FILES[key($_FILES)];

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

// Output Format
if (isset($_GET['format'])) {
$this->outputFormat = $_GET['format'];
}

// HOST APPLICATIONS INIT
if (isset($_GET['CKEditorFuncNum'])) {
$this->opener['name'] = "ckeditor";
Expand Down Expand Up @@ -705,11 +714,18 @@ protected function callBack($url, $message="") {
$js = $this->$method($url, $message);
}

if (!isset($js))
if ($this->outputFormat == 'json') {
header('Content-Type: application/json');
$json = $this->callBack_json($url, $message);
echo json_encode($json);
}
else {
if (!isset($js)) {
$js = $this->callBack_default($url, $message);

header("Content-Type: text/html; charset={$this->charset}");
echo "<html><body>$js</body></html>";
}
header("Content-Type: text/html; charset={$this->charset}");
echo "<html><body>$js</body></html>";
}
}

protected function callBack_ckeditor($url, $message) {
Expand Down Expand Up @@ -763,4 +779,25 @@ protected function callBack_default($url, $message) {
protected function get_htaccess() {
return file_get_contents("conf/upload.htaccess");
}

protected function callBack_json($url, $message) {
$uploaded = !empty($url) ? 1 : 0;
$result = [
'uploaded' => $uploaded
];
if ($uploaded) {
$result['url'] = $url;
$urlPieces = explode('/', $url);
end($urlPieces);
$fileNamekey = key($urlPieces);
$result['fileName'] = $urlPieces[$fileNamekey];
}
else {
$result['error'] = [
'message' => $message,
];
}
return $result;
}

}