From d6443a8d988e24872aca2fdd14fe0b9c91e9829e Mon Sep 17 00:00:00 2001 From: wangyidong Date: Wed, 22 Nov 2023 17:27:25 +0800 Subject: [PATCH] * Rewrite page save page. --- module/editor/control.php | 4 +-- module/editor/model.php | 23 ++++++++-------- module/editor/test/editor.class.php | 14 ++++++++++ module/editor/test/model/save.php | 41 +++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 14 deletions(-) create mode 100644 module/editor/test/model/save.php diff --git a/module/editor/control.php b/module/editor/control.php index 1027c12206..ccd6bfb096 100644 --- a/module/editor/control.php +++ b/module/editor/control.php @@ -129,7 +129,7 @@ class editor extends control * @access public * @return void */ - public function save($filePath = '', $action = '') + public function save(string $filePath = '', string $action = '') { if($filePath and $_POST) { @@ -147,7 +147,7 @@ class editor extends control if($action != 'edit' and $action != 'newPage' and file_exists($filePath) and !$this->post->override) return $this->send(array('result' => 'fail', 'message' => $this->lang->editor->repeatFile)); $result = $this->editor->save($filePath); - if(is_string($result)) return $this->send(array('result' => 'fail', 'message' => $result)); + if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError())); return $this->send(array('result' => 'success', 'load' => inlink('edit', "filePath=" . helper::safe64Encode($filePath) . "&action=edit"), 'callback' => 'reloadExtendWin()')); } diff --git a/module/editor/model.php b/module/editor/model.php index eb04169f18..8ef6011f8a 100644 --- a/module/editor/model.php +++ b/module/editor/model.php @@ -302,13 +302,12 @@ class editorModel extends model /** * Get api link. * - * @param int $filePath - * @param int $action - * @param string $type + * @param string $filePath + * @param string $action * @access public * @return string */ - public function getAPILink($filePath, $action) + public function getAPILink(string $filePath, string $action): string { return helper::createLink('api', 'debug', "filePath=" . helper::safe64Encode($filePath) . "&action=$action"); } @@ -318,25 +317,25 @@ class editorModel extends model * * @param string $filePath * @access public - * @return string + * @return string|bool */ - public function save($filePath) + public function save(string $filePath): string|bool { /* Reduce expiration time for check safe file. */ $this->config->safeFileTimeout = 15 * 60; $statusFile = $this->loadModel('common')->checkSafeFile(); - if($statusFile) return sprintf($this->lang->editor->noticeOkFile, str_replace('\\', '/', $statusFile)); + if($statusFile) return dao::$errors[] = sprintf($this->lang->editor->noticeOkFile, str_replace('\\', '/', $statusFile)); $dirPath = dirname($filePath); $extFilePath = substr($filePath, 0, strpos($filePath, DS . 'ext' . DS) + 4); if(!is_dir($dirPath) and is_writable($extFilePath)) mkdir($dirPath, 0777, true); if(!is_dir($dirPath)) { - if(is_dir($extFilePath)) return sprintf($this->lang->editor->notWritable, $extFilePath); - return sprintf($this->lang->editor->notExists, $extFilePath); + if(is_dir($extFilePath)) return dao::$errors[] = sprintf($this->lang->editor->notWritable, $extFilePath); + return dao::$errors[] = sprintf($this->lang->editor->notExists, $extFilePath); } - if(!is_writable($dirPath)) return sprintf($this->lang->editor->notWritable, $extFilePath); - if(strpos(strtolower(realpath($dirPath)), strtolower($this->app->getBasePath())) !== 0) return $this->lang->editor->editFileError; + if(!is_writable($dirPath)) return dao::$errors[] = sprintf($this->lang->editor->notWritable, $extFilePath); + if(strpos(strtolower(realpath($dirPath)), strtolower($this->app->getBasePath())) !== 0) return dao::$errors[] = $this->lang->editor->editFileError; $fileContent = $this->post->fileContent; $evils = array('eval', 'exec', 'passthru', 'proc_open', 'shell_exec', 'system', '$$', 'include', 'require', 'assert', 'javascript', 'onclick'); @@ -528,7 +527,7 @@ EOD; if(strpos($editName, '.php') !== false) return $extPath . 'lang' . DS . basename($editName, ".{$fileExtension}") . DS . $fileName; return $extPath . $fileExtension . DS . basename($editName, ".{$fileExtension}") . DS . $fileName; default: - if(empty($fileName)) return dao::$error[] = $this->lang->editor->emptyFileName; + if(empty($fileName)) return dao::$errors[] = $this->lang->editor->emptyFileName; $action = strtolower(str_replace('new', '', $action)); if($action == 'hook') return $extPath . 'view' . DS . $fileName; diff --git a/module/editor/test/editor.class.php b/module/editor/test/editor.class.php index eaab73b185..f91c60bcbf 100644 --- a/module/editor/test/editor.class.php +++ b/module/editor/test/editor.class.php @@ -371,6 +371,20 @@ class editorTest return $result; } + /** + * Test for save method. + * + * @param string $filePath + * @access public + * @return string|bool + */ + public function saveTest(string $filePath): string|bool + { + $_POST['fileContent'] = "objectModel->save($filePath); + } + /** * Test for get class name by path. * diff --git a/module/editor/test/model/save.php b/module/editor/test/model/save.php new file mode 100644 index 0000000000..d82f6258be --- /dev/null +++ b/module/editor/test/model/save.php @@ -0,0 +1,41 @@ +#!/usr/bin/env php +objectModel->app->getTmpRoot() . 'test.php'; +$editor->saveTest($filePath); + +$errors = dao::getError(); +r((int)str_contains($errors[0], '为了安全起见,系统需要确认您的管理员身份')) && p() && e('1'); //不创建OK文件。 + +$oldENV = getenv('IS_CONTAINER'); +putenv('IS_CONTAINER=true'); + +$filePath = $editor->objectModel->app->getTmpRoot() . '/test/test.php'; +$editor->saveTest($filePath); +$errors = dao::getError(); +r((int)str_contains($errors[0], '不存在该目录,请先创建')) && p() && e('1'); //不存在目录。 + +$filePath = '/home/test.php'; +$editor->saveTest($filePath); +$errors = dao::getError(); +r((int)str_contains($errors[0], '无法写入,可能没有权限')) && p() && e('1'); //目录不可写。 + +$filePath = '/tmp/test.php'; +$editor->saveTest($filePath); +$errors = dao::getError(); +r((int)str_contains($errors[0], '只能修改禅道文件')) && p() && e('1'); //不修改禅道文件。 + +putenv("IS_CONTAINER={$oldENV}");