diff --git a/core/class/uploader.php b/core/class/uploader.php index 447d0ff..c79da63 100644 --- a/core/class/uploader.php +++ b/core/class/uploader.php @@ -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 */ @@ -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)]; @@ -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"; @@ -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 "$js"; + } + header("Content-Type: text/html; charset={$this->charset}"); + echo "$js"; + } } protected function callBack_ckeditor($url, $message) { @@ -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; + } + }