Skip to content

Commit

Permalink
Resolve sunhater#155 by allowing kcfinder to send back a JSON respons…
Browse files Browse the repository at this point in the history
…e if requested by using the url param format=json
  • Loading branch information
seamuslee001 committed Apr 27, 2018
1 parent b645d2a commit aace599
Showing 1 changed file with 42 additions and 5 deletions.
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;
}

}

0 comments on commit aace599

Please sign in to comment.