From 58763ec0dac9a7d2c911f6914f72942bfde9ac57 Mon Sep 17 00:00:00 2001 From: Robert Date: Sun, 18 Dec 2022 04:22:09 -0500 Subject: [PATCH] Added default projects to main pages. --- composer.json | 2 +- src/bootstrap/common.php | 24 +++++---------- src/bootstrap/errors.php | 5 ++- src/bootstrap/site_helper.php | 58 +++++++++++++++-------------------- src/classes/assets.php | 34 ++++++++++++++++++-- src/classes/security.php | 2 +- src/classes/view.php | 9 ++++-- 7 files changed, 73 insertions(+), 61 deletions(-) diff --git a/composer.json b/composer.json index 0eb7ead..d32ac04 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "type": "library", "description": "A modern PHP Framework", "keywords": ["PHP Framework"], - "homepage": "https://github.com/tryingtoscale/tts_framework", + "homepage": "https://git.mysnippetsofcode.com/tts/tts_framework", "license": "MIT", "authors": [ { diff --git a/src/bootstrap/common.php b/src/bootstrap/common.php index d3c12d9..2b0c0f0 100644 --- a/src/bootstrap/common.php +++ b/src/bootstrap/common.php @@ -40,7 +40,7 @@ final class common { */ public static function is_error($ret): bool { $lr = self::string_to_lowercase(trim($ret)); - return ($ret === false || $lr === 'false' || $lr === ':false') ? true : false; + return ($ret === false || $lr === 'false') ? true : false; } public static function return_bool_as_yes_no(bool $b_data): string { @@ -201,30 +201,20 @@ final class common { * Will get only left part of string by length. * @param string $str * @param int $length - * @retval type string or :false + * @retval type string or false */ - public static function get_string_left(string $str, int $length): string { - $ret = self::string_sub_part($str, 0, $length); - if ($ret === false) { - return ':false'; - } else { - return $ret; - } + public static function get_string_left(string $str, int $length): false | string { + return self::string_sub_part($str, 0, $length); } /** * Will get only the right part of string by length. * @param string $str * @param int $length - * @retval type string or :false + * @retval type string or false */ - public static function get_string_right(string $str, int $length): string { - $ret = self::string_sub_part($str, -$length); - if ($ret === false) { - return ':false'; - } else { - return $ret; - } + public static function get_string_right(string $str, int $length): false | string { + return self::string_sub_part($str, -$length); } public static function real_time_output(): void { diff --git a/src/bootstrap/errors.php b/src/bootstrap/errors.php index ed24dd8..e6dd96c 100644 --- a/src/bootstrap/errors.php +++ b/src/bootstrap/errors.php @@ -34,7 +34,7 @@ function tts_broken_error($ex = ''): void { $view = new \tts\view(); $view->set_view('broken', 'tts'); $view->set('ex', $ex); - $view->fetch($this); + $view->fetch([]); } } @@ -361,8 +361,7 @@ function tts_global_error_handler(int $errno = 0, string $errstr = '', string $e if (\tts\common::is_string_found($ref, '/') === false) { $ref .= '/'; } - $vf = \tts\site_helper::get_project(); - header('Location: ' . $ref . '/' . $vf . '/app/' . $prj . '/error.html', TRUE, $http_response_code); + header('Location: ' . $ref . '/app/' . $prj . '/error.html', TRUE, $http_response_code); exit(1); } diff --git a/src/bootstrap/site_helper.php b/src/bootstrap/site_helper.php index 8ebafdc..2872dbb 100644 --- a/src/bootstrap/site_helper.php +++ b/src/bootstrap/site_helper.php @@ -12,7 +12,12 @@ final class site_helper { private static $TESTING; private static $queryParams; private static $DEFAULT_PROJECT; + private static $all_projects = []; + public static function set_all_projects(array $projects): void { + self::$all_projects = $projects; + } + public static function get_root(): string { return self::$ROOT; } @@ -166,8 +171,9 @@ final class site_helper { $root = str_replace(self::$ROOT, "", $uri[0]); $routes = explode('/', trim($root, '/')); $project = $routes[0] ?? ""; - self::set_project($project); - array_shift($routes); // POP out the PRJ ROUTE!! + if (self::set_project($project)) { + array_shift($routes); // POP out the PRJ ROUTE!! + } self::$ROUTE = implode('/', $routes); } @@ -175,20 +181,15 @@ final class site_helper { * Note: using preg_replace here as * \tts\security::filter_class has not YET loaded! */ - private static function set_project(string $project): void { - $default = preg_replace('/[^a-zA-Z0-9_]/', '', self::$DEFAULT_PROJECT); - $folder = match ($project) { - '/' => $default, - '' => $default, - 'f' => 'final', - 'l' => 'live', - 'm' => 'mockup', - 'p' => 'prototype', - 'fun' => 'play', - 's' => 'staging', - default => preg_replace('/[^a-zA-Z0-9_]/', '', $project) - }; - self::$PRJ = $folder; + private static function set_project(string $project): bool { + foreach(self::$all_projects as $project_name) { + if ($project == $project_name) { + self::$PRJ = preg_replace('/[^a-zA-Z0-9_]/', '', $project_name); + return true; + } + } + self::$PRJ = preg_replace('/[^a-zA-Z0-9_]/', '', self::$DEFAULT_PROJECT); + return false; } public static function get_asset(string $file): string { @@ -216,11 +217,8 @@ final class site_helper { self::set_params(); self::set_route(); - if (empty(self::get_route())) { - define('PROJECT_ASSETS_BASE_REF', ''); - } else { - define('PROJECT_ASSETS_BASE_REF', "/assets/" . self::$PRJ); - } + define('PROJECT_ASSETS_BASE_REF', "/assets/" . self::$PRJ); + define('ASSETS_BASE_REF', "/assets/"); } public static function set_project_namespace() { @@ -230,19 +228,11 @@ final class site_helper { $up_one = dirname(self::$ROOT, 1); $project = self::$PRJ; - if (!empty(self::get_route())) { - define('TTS_SITE_URL', self::tts_site_url()); - define('TTS_PROJECT_BASE_REF', TTS_SITE_URL . $project); - define("TTS_BROWSER", \tts\safer_io::get_clean_server_var('HTTP_USER_AGENT')); - define("PROJECT_ASSETS_DIR", $up_one . "/public/assets/" . $project); - } else { - // Web Server URI not found - define('TTS_PROTOCOL', ''); - define('TTS_SITE_URL', ''); - define('TTS_PROJECT_BASE_REF', ''); - define("TTS_BROWSER", ''); - define("PROJECT_ASSETS_DIR", ''); - } + define('TTS_SITE_URL', self::tts_site_url()); + define('TTS_PROJECT_BASE_REF', TTS_SITE_URL . $project); + define("TTS_BROWSER", \tts\safer_io::get_clean_server_var('HTTP_USER_AGENT')); + define("PROJECT_ASSETS_DIR", $up_one . "/public/assets/" . $project); + define("ASSETS_DIR", $up_one . "/public/assets/"); \tts\site_helper::do_load_all_files(); } diff --git a/src/classes/assets.php b/src/classes/assets.php index 033696a..3a7236f 100644 --- a/src/classes/assets.php +++ b/src/classes/assets.php @@ -12,7 +12,32 @@ final class assets { return "?ver=" . date('Y.m.d_H.i.s', filemtime($safe_file)); } + /** + * Check for / or absolute path not belonging to /var/www or PRJ:ROOT + * @param string|null $path + * @param string $open_base_dir_path + * @return bool + */ + private static function attempts_root_dir(?string $path): bool { + // up from src and back up to public + $open_base_dir_path = dirname(\tts\site_helper::get_root(), 1) . '/public/'; + if ($path === null || $path === '') { + return false; + } + if (\tts\common::get_string_left($path, strlen($open_base_dir_path)) == $open_base_dir_path) { + return false; + } + if (\tts\common::get_string_left($path, 1) == '/') { + return true; + } + return false; + } + + public static function get_ajax_files(string $ajax_folder): string { + if (self::attempts_root_dir($ajax_folder)) { + return ""; + } $safe_folder = \tts\security::filter_uri($ajax_folder); $ret = "var assets_files = [];" . PHP_EOL; @@ -80,9 +105,9 @@ final class assets { return ($tts !== false) ? PROJECT_ASSETS_BASE_REF . "/" . $tts : false; } if ($scope === 'assets') { - $path = dirname(\tts\site_helper::get_root(), 1) . "/public/assets/"; + $path = ASSETS_DIR . "/"; $tts = self::is_minified($path, $file); - return ($tts !== false) ? "/assets/" . $tts : false; + return ($tts !== false) ? ASSETS_BASE_REF . "/" . $tts : false; } return $file; } @@ -99,7 +124,7 @@ final class assets { } else if ($scope === 'project' || $scope === 'app') { $path = PROJECT_ASSETS_DIR . "/"; } else if ($scope === 'assets') { - $path = dirname(\tts\site_helper::get_root(), 1) . "/public/assets/"; + $path = ASSETS_DIR . "/"; } else { return ""; } @@ -172,6 +197,9 @@ final class assets { } public static function is_minified(string $path, string $file) { + if (self::attempts_root_dir($path)) { + return false; + } $safe_path = \tts\security::filter_uri($path); $safe_file = \tts\security::filter_uri($file); diff --git a/src/classes/security.php b/src/classes/security.php index 0bdd3aa..0659290 100644 --- a/src/classes/security.php +++ b/src/classes/security.php @@ -118,7 +118,7 @@ final class security { } return self::use_hmac($level, $pepper); } - + /** * @method filter_class * @param type $class diff --git a/src/classes/view.php b/src/classes/view.php index db59822..9f9c4d9 100644 --- a/src/classes/view.php +++ b/src/classes/view.php @@ -21,8 +21,13 @@ final class view { if (! \tts\common::is_string_found($file_ext, '.')) { $view_file .= '.php'; } - $file = (empty($default)) ? "{$this->project_dir}/views/{$view_file}" : "{$this->project_dir}/views/{$default}/{$view_file}"; - $path = ($render_path === 'project') ? \tts\site_helper::get_root() : \tts\main\TTS_FRAMEWORK; + if ($render_path === 'tts') { + $file = (empty($default)) ? "views/{$view_file}" : "views/{$default}/{$view_file}"; + $path = \tts\main\TTS_FRAMEWORK; + } else { + $file = (empty($default)) ? "{$this->project_dir}/views/{$view_file}" : "{$this->project_dir}/views/{$default}/{$view_file}"; + $path = \tts\site_helper::get_root(); + } $vf = $path . $file; if ( \tts\requires::safer_file_exists($vf) !== false) { return $file;