From b8903396199c3437cf980d93c94159a00899a10d Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Sun, 8 Nov 2020 18:04:12 +0100 Subject: Make SITE_BASE_PATH a method of Env This allows us to get rid of utils.php --- ratatoeskr/backend.php | 22 +++++++++------- ratatoeskr/frontend.php | 1 - ratatoeskr/sys/Env.php | 11 ++++++++ ratatoeskr/sys/db.php | 2 -- ratatoeskr/sys/models.php | 53 +++++++++++++++++++++------------------ ratatoeskr/sys/plugin_api.php | 4 +-- ratatoeskr/sys/textprocessors.php | 2 -- ratatoeskr/sys/translation.php | 1 - ratatoeskr/sys/utils.php | 17 ------------- setup.php | 1 - 10 files changed, 55 insertions(+), 59 deletions(-) delete mode 100644 ratatoeskr/sys/utils.php diff --git a/ratatoeskr/backend.php b/ratatoeskr/backend.php index 36e2fb3..42ed21b 100644 --- a/ratatoeskr/backend.php +++ b/ratatoeskr/backend.php @@ -949,6 +949,8 @@ function build_backend_subactions() "templates" => function (&$data, $url_now, &$url_next) { global $ste, $translation; + $env = Env::getGlobal(); + list($template) = $url_next; $url_next = []; @@ -961,11 +963,11 @@ function build_backend_subactions() if (preg_match("/^[a-zA-Z0-9\\-_\\.]+$/", $template) == 0) { /* Prevent a possible LFI attack. */ throw new NotFoundError(); } - if (!is_file(SITE_BASE_PATH . "/ratatoeskr/templates/src/usertemplates/$template")) { + if (!is_file($env->siteBasePath() . "/ratatoeskr/templates/src/usertemplates/$template")) { throw new NotFoundError(); } $ste->vars["template_name"] = $template; - $ste->vars["template_code"] = file_get_contents(SITE_BASE_PATH . "/ratatoeskr/templates/src/usertemplates/$template"); + $ste->vars["template_code"] = file_get_contents($env->siteBasePath() . "/ratatoeskr/templates/src/usertemplates/$template"); } /* Was there a delete request? */ @@ -974,8 +976,8 @@ function build_backend_subactions() if (preg_match("/^[a-zA-Z0-9\\-_\\.]+$/", $tplname) == 0) { /* Prevent a possible LFI attack. */ continue; } - if (is_file(SITE_BASE_PATH . "/ratatoeskr/templates/src/usertemplates/$tplname")) { - @unlink(SITE_BASE_PATH . "/ratatoeskr/templates/src/usertemplates/$tplname"); + if (is_file($env->siteBasePath() . "/ratatoeskr/templates/src/usertemplates/$tplname")) { + @unlink($env->siteBasePath() . "/ratatoeskr/templates/src/usertemplates/$tplname"); } } $ste->vars["success"] = $translation["templates_successfully_deleted"]; @@ -989,7 +991,7 @@ function build_backend_subactions() try { Transcompiler::transcompile(Parser::parse($_POST["template_code"], $_POST["template_name"])); - file_put_contents(SITE_BASE_PATH . "/ratatoeskr/templates/src/usertemplates/" . $_POST["template_name"], $_POST["template_code"]); + file_put_contents($env->siteBasePath() . "/ratatoeskr/templates/src/usertemplates/" . $_POST["template_name"], $_POST["template_code"]); $ste->vars["success"] = $translation["template_successfully_saved"]; } catch (ParseCompileError $e) { $e->rewrite($_POST["template_code"]); @@ -1002,7 +1004,7 @@ function build_backend_subactions() /* Get all templates */ $ste->vars["templates"] = []; - $tpldir = new DirectoryIterator(SITE_BASE_PATH . "/ratatoeskr/templates/src/usertemplates"); + $tpldir = new DirectoryIterator($env->siteBasePath() . "/ratatoeskr/templates/src/usertemplates"); foreach ($tpldir as $fo) { if ($fo->isFile()) { $ste->vars["templates"][] = $fo->getFilename(); @@ -1078,6 +1080,8 @@ function build_backend_subactions() "sections" => function (&$data, $url_now, &$url_next) { global $ste, $translation, $languages, $ratatoeskr_settings; + $env = Env::getGlobal(); + $url_next = []; $ste->vars["section"] = "design"; @@ -1090,7 +1094,7 @@ function build_backend_subactions() Section::by_name($_POST["section_name"]); $ste->vars["error"] = $translation["section_already_exists"]; } catch (DoesNotExistError $e) { - if ((preg_match("/^[a-zA-Z0-9\\-_\\.]+$/", $_POST["template"]) == 0) or (!is_file(SITE_BASE_PATH . "/ratatoeskr/templates/src/usertemplates/{$_POST['template']}"))) { + if ((preg_match("/^[a-zA-Z0-9\\-_\\.]+$/", $_POST["template"]) == 0) or (!is_file($env->siteBasePath() . "/ratatoeskr/templates/src/usertemplates/{$_POST['template']}"))) { $ste->vars["error"] = $translation["unknown_template"]; } elseif (!Section::test_name($_POST["section_name"])) { $ste->vars["error"] = $translation["invalid_section_name"]; @@ -1149,7 +1153,7 @@ function build_backend_subactions() if (isset($_POST["set_template"]) and isset($_POST["section_select"])) { try { $section = Section::by_name($_POST["section_select"]); - if ((preg_match("/^[a-zA-Z0-9\\-_\\.]+$/", $_POST["set_template_to"]) == 0) or (!is_file(SITE_BASE_PATH . "/ratatoeskr/templates/src/usertemplates/{$_POST['set_template_to']}"))) { + if ((preg_match("/^[a-zA-Z0-9\\-_\\.]+$/", $_POST["set_template_to"]) == 0) or (!is_file($env->siteBasePath() . "/ratatoeskr/templates/src/usertemplates/{$_POST['set_template_to']}"))) { $ste->vars["error"] = $translation["unknown_template"]; } else { $section->template = $_POST["set_template_to"]; @@ -1192,7 +1196,7 @@ function build_backend_subactions() /* Get all templates */ $ste->vars["templates"] = []; - $tpldir = new DirectoryIterator(SITE_BASE_PATH . "/ratatoeskr/templates/src/usertemplates"); + $tpldir = new DirectoryIterator($env->siteBasePath() . "/ratatoeskr/templates/src/usertemplates"); foreach ($tpldir as $fo) { if ($fo->isFile()) { $ste->vars["templates"][] = $fo->getFilename(); diff --git a/ratatoeskr/frontend.php b/ratatoeskr/frontend.php index 8fab0a1..5382a5d 100644 --- a/ratatoeskr/frontend.php +++ b/ratatoeskr/frontend.php @@ -13,7 +13,6 @@ use r7r\ste; use r7r\cms\sys\Env; use r7r\cms\sys\Esc; -require_once(dirname(__FILE__) . "/sys/utils.php"); require_once(dirname(__FILE__) . "/languages.php"); require_once(dirname(__FILE__) . "/sys/models.php"); require_once(dirname(__FILE__) . "/sys/textprocessors.php"); diff --git a/ratatoeskr/sys/Env.php b/ratatoeskr/sys/Env.php index 63d079d..612418e 100644 --- a/ratatoeskr/sys/Env.php +++ b/ratatoeskr/sys/Env.php @@ -43,4 +43,15 @@ class Env return Database::fromConfig($config); }); } + + /** + * The Base path of this ratatoeskr site. + * @return string + */ + public function siteBasePath(): string + { + return $this->lazy("siteBasePath", static function () { + return dirname(dirname(dirname(__FILE__))); + }); + } } diff --git a/ratatoeskr/sys/db.php b/ratatoeskr/sys/db.php index b4f2efb..4d1f493 100644 --- a/ratatoeskr/sys/db.php +++ b/ratatoeskr/sys/db.php @@ -13,5 +13,3 @@ if (!defined("SETUP")) { require_once(dirname(__FILE__) . "/../config.php"); } - -require_once(dirname(__FILE__) . "/utils.php"); diff --git a/ratatoeskr/sys/models.php b/ratatoeskr/sys/models.php index d2ed544..3edb1d6 100644 --- a/ratatoeskr/sys/models.php +++ b/ratatoeskr/sys/models.php @@ -16,7 +16,6 @@ use r7r\cms\sys\Database; use r7r\cms\sys\DbTransaction; require_once(dirname(__FILE__) . "/db.php"); -require_once(dirname(__FILE__) . "/utils.php"); require_once(dirname(__FILE__) . "/textprocessors.php"); require_once(dirname(__FILE__) . "/pluginpackage.php"); @@ -1613,9 +1612,10 @@ class Plugin rmdir($dir); } - public function delete(?Database $db = null): void + public function delete(?Database $db = null, ?Env $env = null): void { - $db = $db ?? Env::getGlobal()->database(); + $env = $env ?? Env::getGlobal(); + $db = $db ?? $env->database(); $tx = new DbTransaction($db); try { @@ -1628,14 +1628,14 @@ class Plugin throw $e; } - if (is_dir(SITE_BASE_PATH . "/ratatoeskr/plugin_extradata/private/" . $this->id)) { - self::delete_directory(SITE_BASE_PATH . "/ratatoeskr/plugin_extradata/private/" . $this->id); + if (is_dir($env->siteBasePath() . "/ratatoeskr/plugin_extradata/private/" . $this->id)) { + self::delete_directory($env->siteBasePath() . "/ratatoeskr/plugin_extradata/private/" . $this->id); } - if (is_dir(SITE_BASE_PATH . "/ratatoeskr/plugin_extradata/public/" . $this->id)) { - self::delete_directory(SITE_BASE_PATH . "/ratatoeskr/plugin_extradata/public/" . $this->id); + if (is_dir($env->siteBasePath() . "/ratatoeskr/plugin_extradata/public/" . $this->id)) { + self::delete_directory($env->siteBasePath() . "/ratatoeskr/plugin_extradata/public/" . $this->id); } - if (is_dir(SITE_BASE_PATH . "/ratatoeskr/templates/src/plugintemplates/" . $this->id)) { - self::delete_directory(SITE_BASE_PATH . "/ratatoeskr/templates/src/plugintemplates/" . $this->id); + if (is_dir($env->siteBasePath() . "/ratatoeskr/templates/src/plugintemplates/" . $this->id)) { + self::delete_directory($env->siteBasePath() . "/ratatoeskr/templates/src/plugintemplates/" . $this->id); } } } @@ -2281,11 +2281,15 @@ class Image extends BySQLRowEnabled * * @param string|mixed $file - Location of new image (move_uploaded_file must be able to move the file!) * @param Database|null $db + * @param Env|null $env * @throws IOError * @throws UnknownFileFormat */ - public function exchange_image($file, ?Database $db = null): void + public function exchange_image($file, ?Database $db = null, ?Env $env = null): void { + $env = $env ?? Env::getGlobal(); + $db = $db ?? $env->database(); + $file = (string)$file; if (!is_file($file)) { @@ -2298,11 +2302,11 @@ class Image extends BySQLRowEnabled if (!isset(self::IMAGETYPE_FILE_EXTENSIONS[$imageinfo[2]])) { throw new UnknownFileFormat(); } - if (is_file(SITE_BASE_PATH . "/images/" . $this->filename)) { - unlink(SITE_BASE_PATH . "/images/" . $this->filename); + if (is_file($env->siteBasePath() . "/images/" . $this->filename)) { + unlink($env->siteBasePath() . "/images/" . $this->filename); } $new_fn = $this->id . "." . self::IMAGETYPE_FILE_EXTENSIONS[$imageinfo[2]]; - if (!move_uploaded_file($file, SITE_BASE_PATH . "/images/" . $new_fn)) { + if (!move_uploaded_file($file, $env->siteBasePath() . "/images/" . $new_fn)) { throw new IOError("Can not move file."); } $this->filename = $new_fn; @@ -2310,9 +2314,9 @@ class Image extends BySQLRowEnabled /* make preview image */ switch ($imageinfo[2]) { - case IMAGETYPE_GIF: $img = imagecreatefromgif(SITE_BASE_PATH . "/images/" . $new_fn); break; - case IMAGETYPE_JPEG: $img = imagecreatefromjpeg(SITE_BASE_PATH . "/images/" . $new_fn); break; - case IMAGETYPE_PNG: $img = imagecreatefrompng(SITE_BASE_PATH . "/images/" . $new_fn); break; + case IMAGETYPE_GIF: $img = imagecreatefromgif($env->siteBasePath() . "/images/" . $new_fn); break; + case IMAGETYPE_JPEG: $img = imagecreatefromjpeg($env->siteBasePath() . "/images/" . $new_fn); break; + case IMAGETYPE_PNG: $img = imagecreatefrompng($env->siteBasePath() . "/images/" . $new_fn); break; default: $img = imagecreatetruecolor(40, 40); imagefill($img, 1, 1, imagecolorallocate($img, 127, 127, 127)); break; } $w_orig = imagesx($img); @@ -2328,9 +2332,9 @@ class Image extends BySQLRowEnabled } $preview = imagecreatetruecolor($w_new, $h_new); imagecopyresized($preview, $img, 0, 0, 0, 0, $w_new, $h_new, $w_orig, $h_orig); - imagepng($preview, SITE_BASE_PATH . "/images/previews/{$this->id}.png"); + imagepng($preview, $env->siteBasePath() . "/images/previews/{$this->id}.png"); } else { - imagepng($img, SITE_BASE_PATH . "/images/previews/{$this->id}.png"); + imagepng($img, $env->siteBasePath() . "/images/previews/{$this->id}.png"); } } @@ -2346,16 +2350,17 @@ class Image extends BySQLRowEnabled ); } - public function delete(?Database $db = null): void + public function delete(?Database $db = null, ?Env $env = null): void { - $db = $db ?? Env::getGlobal()->database(); + $env = $env ?? Env::getGlobal(); + $db = $db ?? $env->database(); $db->query("DELETE FROM `PREFIX_images` WHERE `id` = ?", $this->id); - if (is_file(SITE_BASE_PATH . "/images/" . $this->filename)) { - unlink(SITE_BASE_PATH . "/images/" . $this->filename); + if (is_file($env->siteBasePath() . "/images/" . $this->filename)) { + unlink($env->siteBasePath() . "/images/" . $this->filename); } - if (is_file(SITE_BASE_PATH . "/images/previews/{$this->id}.png")) { - unlink(SITE_BASE_PATH . "/images/previews/{$this->id}.png"); + if (is_file($env->siteBasePath() . "/images/previews/{$this->id}.png")) { + unlink($env->siteBasePath() . "/images/previews/{$this->id}.png"); } } } diff --git a/ratatoeskr/sys/plugin_api.php b/ratatoeskr/sys/plugin_api.php index b85ce57..082b605 100644 --- a/ratatoeskr/sys/plugin_api.php +++ b/ratatoeskr/sys/plugin_api.php @@ -96,7 +96,7 @@ abstract class RatatoeskrPlugin */ final protected function get_custompriv_dir() { - return SITE_BASE_PATH . "/ratatoeskr/plugin_extradata/private/" . $this->id; + return $this->env->siteBasePath() . "/ratatoeskr/plugin_extradata/private/" . $this->id; } /** @@ -105,7 +105,7 @@ abstract class RatatoeskrPlugin */ final protected function get_custompub_dir() { - return SITE_BASE_PATH . "/ratatoeskr/plugin_extradata/public/" . $this->id; + return $this->env->siteBasePath() . "/ratatoeskr/plugin_extradata/public/" . $this->id; } /** diff --git a/ratatoeskr/sys/textprocessors.php b/ratatoeskr/sys/textprocessors.php index da52043..288157d 100644 --- a/ratatoeskr/sys/textprocessors.php +++ b/ratatoeskr/sys/textprocessors.php @@ -13,8 +13,6 @@ use r7r\cms\sys\Env; use r7r\cms\sys\textprocessors\LegacyTextprocessor; use r7r\cms\sys\textprocessors\TextprocessorRepository; -require_once(dirname(__FILE__) . "/utils.php"); - /** * Register a textprocessor. * diff --git a/ratatoeskr/sys/translation.php b/ratatoeskr/sys/translation.php index d9d6cfd..e5387a1 100644 --- a/ratatoeskr/sys/translation.php +++ b/ratatoeskr/sys/translation.php @@ -12,7 +12,6 @@ use r7r\ste; use r7r\cms\sys\Esc; -require_once(dirname(__FILE__) . "/utils.php"); require_once(dirname(__FILE__) . "/init_ste.php"); /** @var ste\STECore $ste */ diff --git a/ratatoeskr/sys/utils.php b/ratatoeskr/sys/utils.php deleted file mode 100644 index 5e0415c..0000000 --- a/ratatoeskr/sys/utils.php +++ /dev/null @@ -1,17 +0,0 @@ -