diff --git a/src/main.php b/src/main.php index 54bd8a5..d92bd60 100644 --- a/src/main.php +++ b/src/main.php @@ -14,8 +14,8 @@ error_reporting(E_ALL); ini_set('display_errors', 0); class IFMException extends Exception { - public $forUser = false; - public function __construct($message, $forUser = false, $code = 0, Exception $previous = null) { + public $forUser = true; + public function __construct($message, $forUser = true, $code = 0, Exception $previous = null) { $this->forUser = $forUser; parent::__construct($message, $code, $previous); } @@ -92,7 +92,9 @@ class IFM { // load config from passed array $this->config = array_merge($this->config, $config); - + $i18n = []; + @@@vars:languageincludes@@@ + $this->i18n = $i18n; if ($this->config['timezone']) date_default_timezone_set($this->config['timezone']); @@ -147,84 +149,6 @@ f00bar; * main functions */ - private function dispatch() { - // APIs which do not need authentication - switch ($_REQUEST['api']) { - case "checkAuth": - try { - if ($this->checkAuth()) - $this->jsonResponse(["status" => "OK", "message" => "Authenticated"]); - } catch (Exception $e) { - $this->jsonResponse(["status" => "ERROR", "message" => "Not authenticated"]); - } - return; - case "getConfig": - $this->getConfig(); - return; - case "getTemplates": - $this->getTemplates(); - return; - case "getI18N": - $this->getI18N($_REQUEST); - return; - case "logout": - unset($_SESSION); - session_destroy(); - header("Location: " . strtok($_SERVER["REQUEST_URI"], '?')); - return; - } - - // check authentication - if (!$this->checkAuth()) - throw new IFMException("Not authenticated", true); - - // api requests which work without a valid working directory - switch ($_REQUEST['api']) { - case "getRealpath": - if (isset($_REQUEST["dir"]) && $_REQUEST["dir"] != "") - $this->jsonResponse(array("realpath" => $this->getValidDir($_REQUEST["dir"]))); - else - $this->jsonResponse(array("realpath" => "")); - return; - case "getFiles": - if (isset($_REQUEST["dir"]) && $this->isPathValid($_REQUEST["dir"])) - $this->getFiles($_REQUEST["dir"]); - else - $this->getFiles(""); - return; - case "getFolders": - $this->getFolders($_REQUEST); - return; - } - - // checking working directory - if (!isset($_REQUEST["dir"]) || !$this->isPathValid($_REQUEST["dir"])) - throw new IFMException("Invalid working directory", true); - - $this->chDirIfNecessary($_REQUEST['dir']); - switch ($_REQUEST["api"]) { - case "createDir": $this->createDir($_REQUEST); return; - case "saveFile": $this->saveFile($_REQUEST); return; - case "getContent": $this->getContent($_REQUEST); return; - case "delete": $this->deleteFiles($_REQUEST); return; - case "rename": $this->renameFile($_REQUEST); return; - case "download": $this->downloadFile($_REQUEST); return; - case "extract": $this->extractFile($_REQUEST); return; - case "upload": $this->uploadFile($_REQUEST); return; - case "copyMove": $this->copyMove($_REQUEST); return; - case "changePermissions": $this->changePermissions($_REQUEST); return; - case "zipnload": $this->zipnload($_REQUEST); return; - case "remoteUpload": $this->remoteUpload($_REQUEST); return; - case "searchItems": $this->searchItems($_REQUEST); return; - case "getFolderTree": $this->getFolderTree($_REQUEST); return; - case "createArchive": $this->createArchive($_REQUEST); return; - case "proxy": $this->downloadFile($_REQUEST, false); return; - default: - throw new IFMException("Invalid api action given", true); - return; - } - } - public function run($mode="standalone") { try { if (!is_dir(realpath($this->config['root_dir'])) || !is_readable(realpath($this->config['root_dir']))) @@ -234,15 +158,83 @@ f00bar; $this->mode = $mode; if (isset($_REQUEST['api']) || $mode == "api") - $this->dispatch(); + $this->jsonResponse($this->dispatch()); elseif ($mode == "standalone") $this->getApplication(); else $this->getInlineApplication(); } catch (IFMException $e) { - $this->jsonResponse(["status" => "ERROR", "message" => $e->getMessage()]); + throw new IFMException($e->getMessage()); } catch (Exception $e) { - $this->jsonResponse(["status" => "ERROR", "message" => $e->getMessage()]); + throw new IFMException($e->getMessage()); + } + } + + private function dispatch() { + // APIs which do not need authentication + switch ($_REQUEST['api']) { + case "checkAuth": + if ($this->checkAuth()) + return ["status" => "OK", "message" => "Authenticated"]; + else + return ["status" => "ERROR", "message" => "Not authenticated"]; + case "getConfig": + return $this->getConfig(); + case "getTemplates": + return $this->getTemplates(); + case "getI18N": + return $this->getI18N($_REQUEST); + case "logout": + unset($_SESSION); + session_destroy(); + header("Location: " . strtok($_SERVER["REQUEST_URI"], '?')); + exit; + } + + // check authentication + if (!$this->checkAuth()) + throw new IFMException("Not authenticated"); + + // api requests which work without a valid working directory + switch ($_REQUEST['api']) { + case "getRealpath": + if (isset($_REQUEST["dir"]) && $_REQUEST["dir"] != "") + return ["realpath" => $this->getValidDir($_REQUEST["dir"])]; + else + return ["realpath" => ""]; + case "getFiles": + if (isset($_REQUEST["dir"]) && $this->isPathValid($_REQUEST["dir"])) + return $this->getFiles($_REQUEST["dir"]); + else + return $this->getFiles(""); + case "getFolders": + return $this->getFolders($_REQUEST); + } + + // checking working directory + if (!isset($_REQUEST["dir"]) || !$this->isPathValid($_REQUEST["dir"])) + throw new IFMException($this->l("invalid_dir")); + + $this->chDirIfNecessary($_REQUEST['dir']); + switch ($_REQUEST["api"]) { + case "createDir": return $this->createDir($_REQUEST); + case "saveFile": return $this->saveFile($_REQUEST); + case "getContent": return $this->getContent($_REQUEST); + case "delete": return $this->deleteFiles($_REQUEST); + case "rename": return $this->renameFile($_REQUEST); + case "download": return $this->downloadFile($_REQUEST); + case "extract": return $this->extractFile($_REQUEST); + case "upload": return $this->uploadFile($_REQUEST); + case "copyMove": return $this->copyMove($_REQUEST); + case "changePermissions": return $this->changePermissions($_REQUEST); + case "zipnload": return $this->zipnload($_REQUEST); + case "remoteUpload": return $this->remoteUpload($_REQUEST); + case "searchItems": return $this->searchItems($_REQUEST); + case "getFolderTree": return $this->getFolderTree($_REQUEST); + case "createArchive": return $this->createArchive($_REQUEST); + case "proxy": return $this->downloadFile($_REQUEST, false); + default: + throw new IFMException($this->l("invalid_action")); } } @@ -251,15 +243,12 @@ f00bar; */ private function getI18N($lang="en") { - $i18n = []; - @@@vars:languageincludes@@@ - $this->i18n = $i18n; if (in_array($lang, array_keys($i18n))) // Merge english with the language in case of missing keys - $this->jsonResponse(array_merge($i18n['en'], $i18n[$lang])); + return array_merge($this->i18n['en'], $this->i18n[$lang]); else - $this->jsonResponse($i18n['en']); + return $this->i18n['en']; } private function getTemplates() { @@ -322,7 +311,7 @@ f00bar; $templates['uploadconfirmoverwrite'] = <<<'f00bar' @@@file:src/templates/modal.uploadconfirmoverwrite.html@@@ f00bar; - $this->jsonResponse($templates); + return $templates; } private function getFiles($dir) { @@ -353,7 +342,7 @@ f00bar; usort($dirs, [$this, "sortByName"]); usort($files, [$this, "sortByName"]); - $this->jsonResponse(array_merge($dirs, $files)); + return array_merge($dirs, $files); } private function getItemInformation($name) { @@ -424,7 +413,7 @@ f00bar; foreach (["auth_source", "root_dir"] as $field) unset($ret[$field]); - $this->jsonResponse($ret); + return $ret; } private function getFolders( $d ) { @@ -432,7 +421,7 @@ f00bar; $d['dir'] = $this->getRootDir(); if (!$this->isPathValid($d['dir'])) - echo "[]"; + return []; else { $ret = []; foreach (glob($this->pathCombine($d['dir'], "*"), GLOB_ONLYDIR) as $dir) { @@ -453,27 +442,19 @@ f00bar; ], $ret ); - $this->jsonResponse($ret); + return $ret; } } private function searchItems($d) { - if ($this->config['search'] != 1) { - $this->jsonResponse(["status" => "ERROR", "message" => $this->l['nopermissions']]); - return; - } + if ($this->config['search'] != 1) + throw new IFMException($this->l('nopermissions')); - if (strpos($d['pattern'], '/') !== false) { - $this->jsonResponse(["status" => "ERROR", "message" => $this->l['pattern_error_slashes']]); - exit(1); - } + if (strpos($d['pattern'], '/') !== false) + throw new IFMException($this->l('pattern_error_slashes')); - try { - $results = $this->searchItemsRecursive($d['pattern']); - $this->jsonResponse($results); - } catch (Exception $e) { - $this->jsonResponse(["status" => "ERROR", "message" => $this->l['error'] . " " . $e->getMessage()]); - } + $results = $this->searchItemsRecursive($d['pattern']); + return $results; } private function searchItemsRecursive( $pattern, $dir="" ) { @@ -490,17 +471,15 @@ f00bar; } private function getFolderTree( $d ) { - $this->jsonResponse( - array_merge( - [ - 0 => [ - "text" => "/ [root]", - "nodes" => [], - "dataAttributes" => ["path" => $this->getRootDir()] - ] - ], - $this->getFolderTreeRecursive($d['dir']) - ) + return array_merge( + [ + 0 => [ + "text" => "/ [root]", + "nodes" => [], + "dataAttributes" => ["path" => $this->getRootDir()] + ] + ], + $this->getFolderTreeRecursive($d['dir']) ); } @@ -523,25 +502,17 @@ f00bar; } private function copyMove($d) { - if ($this->config['copymove'] != 1) { - $this->jsonResponse( array( "status" => "ERROR", "message" => $this->l['nopermissions'] ) ); - exit(1); - } + if ($this->config['copymove'] != 1) + throw new IFMException($this->l('nopermissions')); - if (!isset($d['destination']) || !$this->isPathValid(realpath($d['destination']))) { - $this->jsonResponse(["status" => "ERROR", "message" => $this->l['invalid_dir']]); - exit(1); - } + if (!isset($d['destination']) || !$this->isPathValid(realpath($d['destination']))) + throw new IFMException($this->l('invalid_dir')); - if (!is_array($d['filenames'])) { - $this->jsonResponse(["status" => "ERROR", "message" => $this->l['invalid_params']]); - exit(1); - } + if (!is_array($d['filenames'])) + throw new IFMException($this->l('invalid_params')); - if (!in_array($d['action'], ['copy', 'move'])) { - $this->jsonResponse(["status" => "ERROR", "message" => $this->l['invalid_action']]); - exit(1); - } + if (!in_array($d['action'], ['copy', 'move'])) + throw new IFMException($this->l('invalid_action')); $err = []; $errFlag = -1; // -1 -> all errors; 0 -> at least some errors; 1 -> no errors foreach ($d['filenames'] as $file) { @@ -562,35 +533,32 @@ f00bar; } $action = ($d['action'] == "copy") ? "copied" : "moved"; if (empty($err)) { - $this->jsonResponse([ + return [ "status" => "OK", - "message" => ($d['action'] == "copy" ? $this->l['copy_success'] : $this->l['move_success'] ), + "message" => ($d['action'] == "copy" ? $this->l('copy_success') : $this->l['move_success'] ), "errflag" => "1" - ]); + ]; } else { - $errmsg = ($d['action'] == "copy" ? $this->l['copy_error'] : $this->l['move_error'] ) . "