From d5f1976e62cb0a32b8bafdb71d7d082d16413256 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Wed, 11 Dec 2024 19:57:12 -0500 Subject: [PATCH] make ip nullable, refactor get_ip and get_req_route in router --- src/db/migrations/0002.sql | 12 +++++++ src/web/core/controller.php | 1 - src/web/core/router.php | 65 ++++++++++--------------------------- 3 files changed, 29 insertions(+), 49 deletions(-) create mode 100644 src/db/migrations/0002.sql diff --git a/src/db/migrations/0002.sql b/src/db/migrations/0002.sql new file mode 100644 index 0000000..99f1494 --- /dev/null +++ b/src/db/migrations/0002.sql @@ -0,0 +1,12 @@ +BEGIN TRANSACTION; +SET search_path = public; + +-- Migration Start +ALTER TABLE admin.comment + ALTER COLUMN ip DROP NOT NULL; +-- Migration End; + +-- Set Current Revision +UPDATE sys.database_info SET curr_revision = 3 WHERE name = current_database(); + +COMMIT TRANSACTION; diff --git a/src/web/core/controller.php b/src/web/core/controller.php index 340bbb1..ac1e458 100644 --- a/src/web/core/controller.php +++ b/src/web/core/controller.php @@ -25,7 +25,6 @@ abstract class Controller extends Component { if (is_file($__path)) { extract($data); require($__path); - return; } } diff --git a/src/web/core/router.php b/src/web/core/router.php index a1307cf..6a543aa 100644 --- a/src/web/core/router.php +++ b/src/web/core/router.php @@ -52,47 +52,11 @@ class Router extends Component { $parts = explode('/', $path); // get the length $len = count($parts); - // get route info $route = array(); - // e.g. / - if ($path === '') - $route = array( - 'app' => 'index', - 'slug' => 'index', - 'args' => array(), - ); - // e.g. /home /login - else if ($len === 1) - $route = array( - 'app' => $parts[0], - 'slug' => 'index', - 'args' => array(), - ); - // e.g. /home/posts - else if ($len === 2) - $route = array ( - 'app' => $parts[0], - 'slug' => $parts[1], - 'args' => array(), - ); - // e.g. /home/posts/joe - else - $route = array ( - 'app' => $parts[0], - 'slug' => $parts[1], - 'args' => array_slice($parts, 2), - ); - - if (isset($routes[$path])) { - $parts = explode('/', $routes[$path]); - if (count($parts) == 1) { - $route['app'] = $parts[0]; - } else { - $route['app'] = $parts[0]; - $route['slug'] = $parts[1]; - } - } + $route['app'] = $len > 0 ? $parts[0] : 'index'; + $route['slug'] = $len > 1 ? $parts[1] : 'index'; + $route['args'] = array_slice($parts, 2); return $route; } @@ -100,17 +64,22 @@ class Router extends Component { /** * Gets the users ip */ - private function get_ip(): string + private function get_ip(): ?string { - $ip = ''; - if (!empty($_SERVER['HTTP_CLIENT_IP'])) { - $ip = $_SERVER['HTTP_CLIENT_IP']; - } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { - $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; - } else { - $ip = $_SERVER['REMOTE_ADDR']; + $headers = array ( + 'HTTP_CLIENT_IP', + 'HTTP_X_FORWARDED_FOR', + 'HTTP_X_FORWARDED', + 'HTTP_FORWARDED_FOR', + 'HTTP_FORWARDED', + 'HTTP_X_REAL_IP', + 'REMOTE_ADDR' + ); + foreach ($headers as $header) { + if (isset($_SERVER[$header])) + return $_SERVER[$header]; } - return $ip; + return NULL; } /**