From 1fe7d2e5ffd275e7182bd7d3419d75204b75315f Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 23 Dec 2022 22:14:22 -0500 Subject: [PATCH] request into misc class. --- documents/folders.txt | 5 +-- src/bootstrap/common.php | 2 +- src/bootstrap/errors.php | 4 +- src/bootstrap/safer_io.php | 2 +- src/classes/api.php | 6 +-- src/classes/app.php | 6 +-- src/classes/database/help_save.php | 2 +- src/classes/html_document.php | 2 +- src/classes/memory_usage.php | 2 +- src/classes/{request.php => misc.php} | 44 +++++++------------ src/classes/page_not_found.php | 2 +- src/classes/router.php | 2 +- .../http_requests/http_curl_request.php | 2 +- .../services/obsolete/http_socket_request.php | 2 +- src/classes/tag_matches.php | 4 +- .../traits/security/csrf_token_functions.php | 4 +- src/classes/url_encode.php | 21 --------- src/classes/view.php | 16 +++---- 18 files changed, 47 insertions(+), 81 deletions(-) rename src/classes/{request.php => misc.php} (93%) delete mode 100644 src/classes/url_encode.php diff --git a/documents/folders.txt b/documents/folders.txt index 61f895f..22e2717 100644 --- a/documents/folders.txt +++ b/documents/folders.txt @@ -43,8 +43,8 @@ tts_framework/src │   ├── json.php (Outputs DB Records into JSON format) │   ├── loadall.php (Creates loadall.php files if do not exist for Services & Config-Files) │   ├── memory_usage.php (Displays PHP Memory Usage, when debug is set) +│   ├── misc.php (create a short url from get_url, post_var, misc. filter FNs) │   ├── page_not_found.php (CLI or tts built in views/404 page not found error) -│   ├── request.php (create a short url from get_url, post_var, misc. filter FNs) │   ├── router.php (router::get/post, etc... used to setup PHP routes) │   ├── safer_sql.php (Play-Testing SQL filter) │   ├── security.php (hasing, csrf_token\session_hijacking_functions) @@ -80,7 +80,6 @@ tts_framework/src │   │   └── security │   │   ├── csrf_token_functions.php (Get an Cross-Site Request Forge - Prevention Token) │   │   └── session_hijacking_functions.php (Prevent SESSION Hijacking and Fixation in PHP) -│   ├── url_encode.php (base64url Encode/Decode) │ ├── validator.php (validates HTML Forms) │   └── view.php (Loads view files from common folders you defined) ├── main.inc.php (Bootstraps App, sets configure, registry, di, and name-spaces) @@ -92,4 +91,4 @@ tts_framework/src │   └── broken.php (Debug Trace) └── errors.php (when Live, this: Sorry, we had an error... Page is used) -~73 files +~72 files diff --git a/src/bootstrap/common.php b/src/bootstrap/common.php index a81a5c7..2cedb27 100644 --- a/src/bootstrap/common.php +++ b/src/bootstrap/common.php @@ -248,7 +248,7 @@ final class common { * Clear out from memory given variable by Reference! * @param type $sensitive_data */ - public static function wipe(& $sensitive_data) { + public static function wipe(& $sensitive_data): void { if (function_exists("sodium_memzero")) { sodium_memzero($sensitive_data); } diff --git a/src/bootstrap/errors.php b/src/bootstrap/errors.php index 771ff36..c18bd9b 100644 --- a/src/bootstrap/errors.php +++ b/src/bootstrap/errors.php @@ -25,7 +25,7 @@ function tts_broken_error($ex = ''): void { echo $ex; exit(1); } - $use_api = \tts\request::is_api(); + $use_api = \tts\misc::is_api(); if ($use_api === true) { $internal_error = \tts\api::INTERNAL_ERROR; @@ -230,7 +230,7 @@ function tts_json_error_handler($data) { $is_callback = filter_has_var(INPUT_GET, 'callback'); $is_json = filter_has_var(INPUT_GET, 'json'); - if ( !\tts\request::is_ajax() && !$is_callback && !$is_json ) { + if ( !\tts\misc::is_ajax() && !$is_callback && !$is_json ) { return false; } diff --git a/src/bootstrap/safer_io.php b/src/bootstrap/safer_io.php index b02ce04..1724821 100644 --- a/src/bootstrap/safer_io.php +++ b/src/bootstrap/safer_io.php @@ -255,7 +255,7 @@ final class safer_io { } } - public static function safer_html(string $input, HTML_FLAG $safety_level = HTML_FLAG::escape): string { + private static function safer_html(string $input, HTML_FLAG $safety_level = HTML_FLAG::escape): string { switch ($safety_level) { case HTML_FLAG::raw : throw new \Exception('Raw HTML not supported!'); diff --git a/src/classes/api.php b/src/classes/api.php index dd6015f..d5eb8c4 100644 --- a/src/classes/api.php +++ b/src/classes/api.php @@ -58,7 +58,7 @@ class api { * @param type $status_code */ public static function encode($data, $status_code): void { - $response_type = \tts\request::request_var('return'); + $response_type = \tts\misc::request_var('return'); switch ($response_type) { case 'xml': self::xml_encode($data, $status_code, null); @@ -231,7 +231,7 @@ class api { $data['code'] = $long_code; - $memory_check = \bs_tts\common::get_bool(\tts\request::request_var('debug')); + $memory_check = \bs_tts\common::get_bool(\tts\misc::request_var('debug')); if ($memory_check) { $echo = false; $data['memory_used'] = \tts\memory_usage::get_memory_stats($echo); @@ -248,7 +248,7 @@ class api { $data['result'] = true; $code = 200; // OK - $memory_check = \bs_tts\common::get_bool(\tts\request::request_var('debug')); + $memory_check = \bs_tts\common::get_bool(\tts\misc::request_var('debug')); if ($memory_check) { $echo = false; $data['memory_used'] = \tts\memory_usage::get_memory_stats($echo); diff --git a/src/classes/app.php b/src/classes/app.php index 09916ce..0d7a079 100644 --- a/src/classes/app.php +++ b/src/classes/app.php @@ -75,7 +75,7 @@ class app { $file = ""; $class = ""; - if (\tts\request::is_empty($route)) { + if (\tts\misc::is_empty($route)) { $uri = '/app/' . \main_tts\configure::get('tts', 'default_project'); } else { $uri = $route; @@ -117,7 +117,7 @@ class app { } } - if (\tts\request::is_empty($method)) { + if (\tts\misc::is_empty($method)) { $method = ""; // Clear out :null if exists } @@ -161,7 +161,7 @@ class app { $this->local404(); } - $use_api = \tts\request::is_api(); + $use_api = \tts\misc::is_api(); $test = $this->get_ctrl_dir(); $project_folder = \bs_tts\site_helper::get_project(); diff --git a/src/classes/database/help_save.php b/src/classes/database/help_save.php index 8ee49a5..b46418e 100644 --- a/src/classes/database/help_save.php +++ b/src/classes/database/help_save.php @@ -140,7 +140,7 @@ final class help_save { continue; } if (in_array($key, $only_these) || !count($only_these)) { - $this->members[$key] = (\tts\request::is_not_empty($data)) ? $data : ""; + $this->members[$key] = (\tts\misc::is_not_empty($data)) ? $data : ""; } } } diff --git a/src/classes/html_document.php b/src/classes/html_document.php index b53a677..30e1bfd 100644 --- a/src/classes/html_document.php +++ b/src/classes/html_document.php @@ -282,7 +282,7 @@ class html_document { * @param string $log */ public function js_log(string $log): void { - $this->add_to_javascript("tts_log('{$log}');"); + $this->add_to_javascript("console.log('{$log}');"); } /** diff --git a/src/classes/memory_usage.php b/src/classes/memory_usage.php index 816faaf..6daae6e 100644 --- a/src/classes/memory_usage.php +++ b/src/classes/memory_usage.php @@ -29,7 +29,7 @@ final class memory_usage { public static function get_memory_stats($echo = true) { global $mem_baseline; - $check = \bs_tts\common::get_bool(\tts\request::request_var('debug')); + $check = \bs_tts\common::get_bool(\tts\misc::request_var('debug')); if ($check || defined('DEBUG') && DEBUG === true) { $now_mem = memory_get_usage(); diff --git a/src/classes/request.php b/src/classes/misc.php similarity index 93% rename from src/classes/request.php rename to src/classes/misc.php index 334d0c8..6e0f740 100644 --- a/src/classes/request.php +++ b/src/classes/misc.php @@ -10,7 +10,7 @@ declare(strict_types=1); namespace tts; -final class request { +final class misc { public static function post_var(string $var, int $filter = FILTER_UNSAFE_RAW): mixed { return filter_input(INPUT_POST, $var, $filter); @@ -86,10 +86,7 @@ final class request { * @retval bool */ public static function is_not_empty(?string $var): bool { - if ($var === null) { - return false; - } - return ($var !==null && $var !== ':null' && !empty(trim($var))) ? true : false; + return ($var !== null && !empty(trim($var))); } /** @@ -98,29 +95,7 @@ final class request { * @retval bool */ public static function is_empty(?string $var): bool { - if ($var === null) { - return true; - } - - return ($var === null || $var === ':null' || empty(trim($var))) ? true : false; - } - - /** - * Check if string was set - * @param string $var - * @retval bool true is set, false is null - */ - public static function is_set(?string $var): bool { - return ($var !== ':null' && $var !== null) ? true : false; - } - - /** - * Check if string is null or not set - * @param string $var - * @retval bool - */ - public static function is_not_set(?string $var): bool { - return ($var === ':null' || $var === null) ? true : false; + return ($var === null || empty(trim($var)) ); } /** @@ -353,6 +328,19 @@ final class request { } } + /** + * site http://php.net/manual/en/function.base64-encode.php + */ + public static function base64url_encode(string $data): string { + return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); + } + + public static function base64url_decode(string $data): string { + //return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT)); + return base64_decode( strtr( $data, '-_', '+/') . str_repeat('=', 3 - ( 3 + strlen( $data )) % 4 )); + } + + public static function get_globals(array $skip = ['route', 'm'], array $only_these = []): string { $the_request = ''; diff --git a/src/classes/page_not_found.php b/src/classes/page_not_found.php index b840507..c306c9b 100644 --- a/src/classes/page_not_found.php +++ b/src/classes/page_not_found.php @@ -41,7 +41,7 @@ class page_not_found { if (\tts\console_app::is_cli()) { self::tts_error404_cli(); } else { - $use_api = \tts\request::is_api(); + $use_api = \tts\misc::is_api(); } if ($use_api === true) { diff --git a/src/classes/router.php b/src/classes/router.php index f2f1bcd..95012d8 100644 --- a/src/classes/router.php +++ b/src/classes/router.php @@ -453,7 +453,7 @@ class router // Find route foreach (self::$routes as $routeKey => $route) { - $post_method = \tts\request::post_var("_method"); + $post_method = \tts\misc::post_var("_method"); $matchMethod = in_array($request_method, $route['method']) || ($post_method !== null && in_array($post_method, $route['method'])); if (preg_match($route['pattern'], $request, $match) && $matchMethod) { diff --git a/src/classes/services/http_requests/http_curl_request.php b/src/classes/services/http_requests/http_curl_request.php index ef98ab0..0cf0807 100644 --- a/src/classes/services/http_requests/http_curl_request.php +++ b/src/classes/services/http_requests/http_curl_request.php @@ -144,7 +144,7 @@ class http_curl_request { return true; } - $action_response = \tts\request::abort_on_crlf($action_response); + $action_response = \tts\misc::abort_on_crlf($action_response); if (! $options->get_include_header() ) { return ( $options->get_json_decode() ) ? json_decode($action_response, true) : $action_response; diff --git a/src/classes/services/obsolete/http_socket_request.php b/src/classes/services/obsolete/http_socket_request.php index d1e6a7c..01755db 100644 --- a/src/classes/services/obsolete/http_socket_request.php +++ b/src/classes/services/obsolete/http_socket_request.php @@ -65,7 +65,7 @@ final class http_socket_request { } fclose($fp); - $ret = \tts\request::abort_on_crlf($ret); + $ret = \tts\misc::abort_on_crlf($ret); if (! $options->get_include_http_response_headers() ) { $ret = substr($ret, strpos($ret, "\r\n\r\n") + 4); diff --git a/src/classes/tag_matches.php b/src/classes/tag_matches.php index aaf7a6e..5769006 100644 --- a/src/classes/tag_matches.php +++ b/src/classes/tag_matches.php @@ -49,11 +49,11 @@ public static function check_tags(string $page): array { if ($total_still_open > 0) { $msg = "{$total_still_open} possibly MISSING closing {$tag_name} !!!"; - $alert .= "tts_log('{$msg}');\r\n"; + $alert .= "console.log('{$msg}');\r\n"; $output .= (\main_tts\configure::get('tts', 'live') === true) ? "\r\n" : "{$ui}{$msg}{$ui_end}\r\n"; } elseif ($total_still_open < 0) { $msg = abs($total_still_open) . " possibly MISSING opening {$tag_name} !!!"; - $alert .= "tts_log('{$msg}');\r\n"; + $alert .= "console.log('{$msg}');\r\n"; $output .= (\main_tts\configure::get('tts', 'live') === true) ? "\r\n" : "{$ui}{$msg}{$ui_end}\r\n"; } } diff --git a/src/classes/traits/security/csrf_token_functions.php b/src/classes/traits/security/csrf_token_functions.php index 69073dc..c06b11d 100644 --- a/src/classes/traits/security/csrf_token_functions.php +++ b/src/classes/traits/security/csrf_token_functions.php @@ -50,12 +50,12 @@ trait csrf_token_functions { public static function csrf_token_is_valid(): bool { $is_csrf = filter_has_var(INPUT_POST, 'csrf_token'); if ($is_csrf) { - $user_token = \tts\request::post_var('csrf_token'); + $user_token = \tts\misc::post_var('csrf_token'); $stored_token = $_SESSION['csrf_token'] ?? ''; if (empty($stored_token)) { return false; } - return \tts\request::compair_it($user_token, $stored_token); + return \tts\misc::compair_it($user_token, $stored_token); } else { return false; } diff --git a/src/classes/url_encode.php b/src/classes/url_encode.php deleted file mode 100644 index 7df39ba..0000000 --- a/src/classes/url_encode.php +++ /dev/null @@ -1,21 +0,0 @@ -get_file($view_file, $default, $render_path); if ( ! empty($file) ) { $found = true; @@ -224,7 +224,7 @@ final class view { $page_output .= $tags['output']; $page_output .= ''; foreach($this->files as $bad) { - $page_output .= ""; + $page_output .= ""; } } }