From f76f5f99eb13ec3548f5679272988d4cd0c0e894 Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 10 Dec 2025 18:37:10 -0500 Subject: [PATCH] cli --- src/classes/app.php | 2 +- src/classes/http/kernel.php | 15 ++++++++++++++- src/classes/http/request.php | 8 +++++++- src/classes/http/response.php | 8 ++------ .../security/session_hijacking_functions.php | 12 ++++++++---- 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/classes/app.php b/src/classes/app.php index 511b918..0012a90 100644 --- a/src/classes/app.php +++ b/src/classes/app.php @@ -175,7 +175,7 @@ class app { $use_api = misc::is_api(); $test = $this->get_ctrl_dir(); - $call_class = "\\Project\\" . $test . 'controllers\\' . $class; + $call_class = "\\Project\\" . $test . "controllers\\" . $class; $controller = new $call_class($this->request, $this->response); // Collect controller-level middleware Directly from the controller file, IE: public static array $middleware = [ \Project\classes\auth_middleware::class ]; diff --git a/src/classes/http/kernel.php b/src/classes/http/kernel.php index 67430b2..5e04327 100644 --- a/src/classes/http/kernel.php +++ b/src/classes/http/kernel.php @@ -83,12 +83,25 @@ class kernel { ); } + public static function wrap_errors(\Throwable $e): string { + if (PHP_SAPI === 'cli') { + $red = "\033[31m"; + $cyan = "\033[36m"; + $reset = "\033[0m"; + + $e_codes = "Server Error: $red".PHP_EOL. $e->getMessage() . PHP_EOL . "$cyan File:" . $e->getFile() . PHP_EOL ."\t On Line #" . $e->getLine() . PHP_EOL . $reset; + } else { + $e_codes = "Server Error: ".PHP_EOL."
" . $e->getMessage() . "
" . PHP_EOL . "
File:" . $e->getFile() . PHP_EOL ."
\t On Line #" . $e->getLine() . PHP_EOL . "
"; + } + return $e_codes; + } + protected function handle_exception(\Throwable $e): response { // Basic exception handling - override in child class $response = new response(); return $response ->set_status_code(500) - ->set_content('Server Error: ' . $e->getMessage()); + ->set_content(self::wrap_errors($e)); } public function run(): void { diff --git a/src/classes/http/request.php b/src/classes/http/request.php index 7b6adb0..70e91cd 100644 --- a/src/classes/http/request.php +++ b/src/classes/http/request.php @@ -39,7 +39,13 @@ class request { public static function create_from_globals(): self { if (\CodeHydrater\console_app::is_cli()) { - return new self(); + $get_vars = \CodeHydrater\bootstrap\site_helper::get_params(); + if ($get_vars === null) { + return new self(); + } + return new self( + $get_vars + ); } return new self( $_GET, diff --git a/src/classes/http/response.php b/src/classes/http/response.php index fbde1ba..13de276 100644 --- a/src/classes/http/response.php +++ b/src/classes/http/response.php @@ -21,7 +21,7 @@ class response protected array $headers = [] ) { } - public function send(bool $backtrace = false): void { + public function send(): void { http_response_code($this->status_code); foreach ($this->headers as $name => $value) { @@ -30,11 +30,7 @@ class response if ($this->status_code > 499) { if (is_string($this->content) && ! empty($this->content)) { - if ($backtrace) { - dd(debug_backtrace()); - } else { - throw new \Exception($this->content); - } + throw new \Exception($this->content); } else { throw new \Exception("Status Code #" . $this->status_code); } diff --git a/src/classes/traits/security/session_hijacking_functions.php b/src/classes/traits/security/session_hijacking_functions.php index 63967d9..bf0ff75 100644 --- a/src/classes/traits/security/session_hijacking_functions.php +++ b/src/classes/traits/security/session_hijacking_functions.php @@ -91,13 +91,15 @@ trait session_hijacking_functions { } // If session is not valid, end and redirect to login page. - public static function confirm_session_is_valid() { + public static function confirm_session_is_valid( + string $login = "login.php" + ) { if (!self::is_session_valid()) { self::end_session(); // Note that header redirection requires output buffering // to be turned on or requires nothing has been output // (not even whitespace). - header("Location: login.php"); + header("Location: " . $login ); exit; } } @@ -108,13 +110,15 @@ trait session_hijacking_functions { } // If user is not logged in, end and redirect to login page. - public static function confirm_user_logged_in() { + public static function confirm_user_logged_in( + string $login = "login.php" + ) { if (!self::is_logged_in()) { self::end_session(); // Note that header redirection requires output buffering // to be turned on or requires nothing has been output // (not even whitespace). - header("Location: login.php"); + header("Location: " . $login); exit; } }