make ip nullable, refactor get_ip and get_req_route in router

This commit is contained in:
Murphy 2024-12-11 19:57:12 -05:00
parent bb6844a2c4
commit d5f1976e62
Signed by: freya
GPG key ID: 9FBC6FFD6D2DBF17
3 changed files with 29 additions and 49 deletions

View file

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

View file

@ -25,7 +25,6 @@ abstract class Controller extends Component {
if (is_file($__path)) {
extract($data);
require($__path);
return;
}
}

View file

@ -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;
}
/**