added random_engine

main
Robert 3 years ago
parent bd307c8081
commit bacc3323d5
  1. 4
      src/classes/app.php
  2. 2
      src/classes/contracts/http_request_options.php
  3. 3
      src/classes/misc.php
  4. 10
      src/classes/page_not_found.php
  5. 106
      src/classes/random_engine.php
  6. 4
      src/classes/services/http_requests/http_curl_request.php
  7. 10
      src/classes/services/obsolete/crypto.php
  8. 12
      src/classes/services/obsolete/encryption.php
  9. 6
      src/classes/services/obsolete/http_socket_request.php
  10. 7
      src/classes/view.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)) {

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace tts\interfaces;
namespace tts\contacts;
final class http_request_options {

@ -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 = '';

@ -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'));

@ -0,0 +1,106 @@
<?php
declare(strict_types=1);
/**
* @author Robert Strutts <Robert@TryingToScale.com>
* @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 <Robert@TryingToScale.com>
*/
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);
}
}

@ -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) {

@ -2,6 +2,16 @@
declare(strict_types=1);
/**
* @author Robert Strutts <Robert@TryingToScale.com>
* @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 {

@ -2,11 +2,19 @@
declare(strict_types=1);
/**
* @author Robert Strutts <Robert@TryingToScale.com>
* @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 {

@ -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 = '';

@ -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, '.')) {

Loading…
Cancel
Save