Robert 2 months ago
parent 3b0d18c6f9
commit f76f5f99eb
  1. 2
      src/classes/app.php
  2. 15
      src/classes/http/kernel.php
  3. 6
      src/classes/http/request.php
  4. 6
      src/classes/http/response.php
  5. 12
      src/classes/traits/security/session_hijacking_functions.php

@ -175,7 +175,7 @@ class app {
$use_api = misc::is_api(); $use_api = misc::is_api();
$test = $this->get_ctrl_dir(); $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); $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 ]; // Collect controller-level middleware Directly from the controller file, IE: public static array $middleware = [ \Project\classes\auth_middleware::class ];

@ -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."<br><blockquote style='color: blue;'>" . $e->getMessage() . "</blockquote>" . PHP_EOL . "<br>File:" . $e->getFile() . PHP_EOL ."<br>\t On Line #" . $e->getLine() . PHP_EOL . "<hr>";
}
return $e_codes;
}
protected function handle_exception(\Throwable $e): response { protected function handle_exception(\Throwable $e): response {
// Basic exception handling - override in child class // Basic exception handling - override in child class
$response = new response(); $response = new response();
return $response return $response
->set_status_code(500) ->set_status_code(500)
->set_content('Server Error: ' . $e->getMessage()); ->set_content(self::wrap_errors($e));
} }
public function run(): void { public function run(): void {

@ -39,8 +39,14 @@ class request {
public static function create_from_globals(): self { public static function create_from_globals(): self {
if (\CodeHydrater\console_app::is_cli()) { if (\CodeHydrater\console_app::is_cli()) {
$get_vars = \CodeHydrater\bootstrap\site_helper::get_params();
if ($get_vars === null) {
return new self(); return new self();
} }
return new self(
$get_vars
);
}
return new self( return new self(
$_GET, $_GET,
$_POST, $_POST,

@ -21,7 +21,7 @@ class response
protected array $headers = [] protected array $headers = []
) { } ) { }
public function send(bool $backtrace = false): void { public function send(): void {
http_response_code($this->status_code); http_response_code($this->status_code);
foreach ($this->headers as $name => $value) { foreach ($this->headers as $name => $value) {
@ -30,11 +30,7 @@ class response
if ($this->status_code > 499) { if ($this->status_code > 499) {
if (is_string($this->content) && ! empty($this->content)) { 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 { } else {
throw new \Exception("Status Code #" . $this->status_code); throw new \Exception("Status Code #" . $this->status_code);
} }

@ -91,13 +91,15 @@ trait session_hijacking_functions {
} }
// If session is not valid, end and redirect to login page. // 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()) { if (!self::is_session_valid()) {
self::end_session(); self::end_session();
// Note that header redirection requires output buffering // Note that header redirection requires output buffering
// to be turned on or requires nothing has been output // to be turned on or requires nothing has been output
// (not even whitespace). // (not even whitespace).
header("Location: login.php"); header("Location: " . $login );
exit; exit;
} }
} }
@ -108,13 +110,15 @@ trait session_hijacking_functions {
} }
// If user is not logged in, end and redirect to login page. // 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()) { if (!self::is_logged_in()) {
self::end_session(); self::end_session();
// Note that header redirection requires output buffering // Note that header redirection requires output buffering
// to be turned on or requires nothing has been output // to be turned on or requires nothing has been output
// (not even whitespace). // (not even whitespace).
header("Location: login.php"); header("Location: " . $login);
exit; exit;
} }
} }

Loading…
Cancel
Save