From bacc3323d5f65b6911aa24e7091e68cb6d09b33c Mon Sep 17 00:00:00 2001 From: Robert Date: Sun, 25 Dec 2022 01:38:21 -0500 Subject: [PATCH] added random_engine --- src/classes/app.php | 4 +- .../contracts/http_request_options.php | 2 +- src/classes/misc.php | 3 +- src/classes/page_not_found.php | 10 +- src/classes/random_engine.php | 106 ++++++++++++++++++ .../http_requests/http_curl_request.php | 4 +- src/classes/services/obsolete/crypto.php | 10 ++ src/classes/services/obsolete/encryption.php | 12 +- .../services/obsolete/http_socket_request.php | 6 +- src/classes/view.php | 7 +- 10 files changed, 147 insertions(+), 17 deletions(-) create mode 100644 src/classes/random_engine.php diff --git a/src/classes/app.php b/src/classes/app.php index aa84022..06c2e74 100644 --- a/src/classes/app.php +++ b/src/classes/app.php @@ -140,7 +140,7 @@ class app { } private function local404() { - \tts\page_not_found::tts_error404(); + \tts\page_not_found::error404(); } /** @@ -175,7 +175,7 @@ class app { if (method_exists($controller, $method)) { return $controller->$method($params); } else { - \tts\page_not_found::tts_error404_cli(); + \tts\page_not_found::error404_cli(); } } else { if (!empty($method) && method_exists($controller, $method)) { diff --git a/src/classes/contracts/http_request_options.php b/src/classes/contracts/http_request_options.php index 7914495..98bc528 100644 --- a/src/classes/contracts/http_request_options.php +++ b/src/classes/contracts/http_request_options.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace tts\interfaces; +namespace tts\contacts; final class http_request_options { diff --git a/src/classes/misc.php b/src/classes/misc.php index 6e0f740..7ee37f7 100644 --- a/src/classes/misc.php +++ b/src/classes/misc.php @@ -338,8 +338,7 @@ final class misc { 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 c7ca65a..fb2bdc6 100644 --- a/src/classes/page_not_found.php +++ b/src/classes/page_not_found.php @@ -15,7 +15,7 @@ class page_not_found { /** * Command Line Route - Invalid Error */ - public static function tts_error404_cli(): void { + public static function error404_cli(): void { $err = "CLI 404 Page not found! Invalid Route/Method." . PHP_EOL; $argv = (isset($GLOBALS['argv'])) ? $GLOBALS['argv'] : array(); $num_args = count($argv); @@ -37,15 +37,15 @@ class page_not_found { /** * Displays 404 Page not Found */ - public static function tts_error404(): void { + public static function error404(): void { if (\tts\console_app::is_cli()) { - self::tts_error404_cli(); + self::error404_cli(); } else { $use_api = \tts\misc::is_api(); } if ($use_api === true) { - self::tts_api_method_not_found(); + self::api_method_not_found(); } $loaded = \bs_tts\requires::secure_include('views/404', 'tts'); // Show 404, Page Not Found Error Page! @@ -59,7 +59,7 @@ class page_not_found { /** * API Method was not found do API 400 Error */ - private static function tts_api_method_not_found(): void { + private static function api_method_not_found(): void { $status = 400; // Bad Request $bad_request = \tts\api::BAD_REQUEST; \tts\api::error(array('response' => $bad_request, 'code' => $status, 'reason' => 'Command not found')); diff --git a/src/classes/random_engine.php b/src/classes/random_engine.php new file mode 100644 index 0000000..f2e849e --- /dev/null +++ b/src/classes/random_engine.php @@ -0,0 +1,106 @@ + + * @copyright Copyright (c) 2022, Robert Strutts. + * @license https://mit-license.org/ + */ + +namespace tts; + +/** + * random_engine - Provides a high-level API to the randomness + * provided by an Random\Engine OR uses the next Fall Back FN. + * + * @author Robert Strutts + */ +class random_engine { + + private $engine = false; + + public function __construct(): string { + $version = (float) phpversion(); + if ($version > 8.1) { + $this->engine = new \Random\Randomizer(); + } + } + + public function get_bytes(int $bytes_length = 16): string { + return ($this->engine) ? $this->engine->getBytes($bytes_length) : + random_bytes($bytes_length); + } + + public function get_int(int $min, int $max): int { + if ($this->engine) { + return $this->engine->getInt($min, $max); + } + if (function_exists('random_int')) { + return random_int($min, $max); // secure fallback + } elseif (function_exists('mt_rand')) { + return mt_rand($min, $max); // fast + } + return rand($min, $max); // old + } + + // Took from source https://pageconfig.com/post/fixed-length-large-random-numbers-with-php + private function big_rand(int $len = 18 ): int { + $rand = ''; + while( !( isset( $rand[$len-1] ) ) ) { + $rand .= mt_rand( ); + } + return (int) substr( $rand , 0 , $len ); + } + + public function get_next_big_postive_int(): int { + if ($this->engine) { + return $this->engine->nextInt(); + } + return $this->big_rand(); + } + + private function select_from_array(array $a, int $num ): array { + $array_count = \bs_tts\common::get_count($a) - 1; + if ($array_count < 1) { + return []; + } + $ret = []; + for($i=0; $i<$num; $i++) { + $ret[] = $a[$this->get_int(0, $array_count)]; + } + return $ret; + } + + /** + * Pick random keys from an Array + */ + public function get_array_keys(array $a, int $num): array { + if ($this->engine) { + return $this->engine->pickArrayKeys($a, $num); + } + return $this->select_from_array($a, $num); + } + + public function get_shuffled_array(array $a): array { + if ($this->engine) { + return $this->engine->shuffleArray($a); + } + shuffle($a); + return $a; + } + + public function get_shuffled_bytes(string $bytes): string { + if ($this->engine) { + return $this->engine->shuffleBytes($bytes); + } + $len = mb_strlen($bytes); + $a = []; + while($len-- > 0) { + $a[] = mb_substr($bytes, $len, 1); + } + shuffle($a); + return join('', $a); + } + +} diff --git a/src/classes/services/http_requests/http_curl_request.php b/src/classes/services/http_requests/http_curl_request.php index 0cf0807..9ce3a68 100644 --- a/src/classes/services/http_requests/http_curl_request.php +++ b/src/classes/services/http_requests/http_curl_request.php @@ -10,6 +10,8 @@ declare(strict_types=1); namespace tts\services\http_requests; +use \tts\contacts\http_request_options as HTTP_Requests; + class http_curl_request { private $_status; @@ -33,7 +35,7 @@ class http_curl_request { return $this->_header_response; } - public function http_request($options) { + public function http_request(HTTP_Requests $options) { $uri = $options->get_uri(); $action_path = $uri; if (\bs_tts\common::is_string_found($uri, "://") === false) { diff --git a/src/classes/services/obsolete/crypto.php b/src/classes/services/obsolete/crypto.php index ba97f18..e62681b 100644 --- a/src/classes/services/obsolete/crypto.php +++ b/src/classes/services/obsolete/crypto.php @@ -2,6 +2,16 @@ declare(strict_types=1); +/** + * @author Robert Strutts + * @copyright Copyright (c) 2022, Robert Strutts. + * @license https://mit-license.org/ + */ + +/** + * NOTICE: This file is just for PLAY, not for PRODUCTION system! + */ + namespace tts\services; class crypto { diff --git a/src/classes/services/obsolete/encryption.php b/src/classes/services/obsolete/encryption.php index 3691829..bbaeea2 100644 --- a/src/classes/services/obsolete/encryption.php +++ b/src/classes/services/obsolete/encryption.php @@ -2,11 +2,19 @@ declare(strict_types=1); +/** + * @author Robert Strutts + * @copyright Copyright (c) 2022, Robert Strutts. + * @license https://mit-license.org/ + */ + namespace tts\services; /* - * px_print_array($enc->list_ssl_methods()); - * px_print_array($enc->list_hashes()); + * NOTICE: This file is just for PLAY, not for PRODUCTION system! + * + * var_dump($enc->list_ssl_methods()); + * var_dump($enc->list_hashes()); */ final class encryption { diff --git a/src/classes/services/obsolete/http_socket_request.php b/src/classes/services/obsolete/http_socket_request.php index 01755db..ce5a219 100644 --- a/src/classes/services/obsolete/http_socket_request.php +++ b/src/classes/services/obsolete/http_socket_request.php @@ -2,11 +2,13 @@ declare(strict_types=1); -namespace tts\services\http_requests; +namespace tts\services\obsolete\http_requests; + +use \tts\contacts\http_request_options as HTTP_Requests; final class http_socket_request { - public function http_request($options) { + public function http_request(HTTP_Requests $options) { $ret = ''; $verb = strtoupper( $options->get_verb() ); $cookie_str = ''; diff --git a/src/classes/view.php b/src/classes/view.php index 5386f4e..45bb8cc 100644 --- a/src/classes/view.php +++ b/src/classes/view.php @@ -23,9 +23,9 @@ namespace tts; final class view { public $white_space_control = false; public $page_output; - private $vars = array(); + private $vars = []; private $project_dir; - private $files = array(); + private $files = []; private $template = false; private $use_template_engine = false; private $template_type = 'tpl'; @@ -35,6 +35,9 @@ final class view { $this->project_dir = \bs_tts\site_helper::get_project(); } + /** + * @todo Ignore render path tts, should go prj/,...,then tts + */ private function get_file(string $view_file, string $default, string $render_path = 'project'): string { $file_ext = \bs_tts\common::get_string_right($view_file, 4); if (! \bs_tts\common::is_string_found($file_ext, '.')) {