make ip nullable, refactor get_ip and get_req_route in router
This commit is contained in:
parent
bb6844a2c4
commit
d5f1976e62
3 changed files with 29 additions and 49 deletions
12
src/db/migrations/0002.sql
Normal file
12
src/db/migrations/0002.sql
Normal 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;
|
|
@ -25,7 +25,6 @@ abstract class Controller extends Component {
|
||||||
if (is_file($__path)) {
|
if (is_file($__path)) {
|
||||||
extract($data);
|
extract($data);
|
||||||
require($__path);
|
require($__path);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -52,47 +52,11 @@ class Router extends Component {
|
||||||
$parts = explode('/', $path);
|
$parts = explode('/', $path);
|
||||||
// get the length
|
// get the length
|
||||||
$len = count($parts);
|
$len = count($parts);
|
||||||
|
|
||||||
// get route info
|
// get route info
|
||||||
$route = array();
|
$route = array();
|
||||||
// e.g. /
|
$route['app'] = $len > 0 ? $parts[0] : 'index';
|
||||||
if ($path === '')
|
$route['slug'] = $len > 1 ? $parts[1] : 'index';
|
||||||
$route = array(
|
$route['args'] = array_slice($parts, 2);
|
||||||
'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];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $route;
|
return $route;
|
||||||
}
|
}
|
||||||
|
@ -100,17 +64,22 @@ class Router extends Component {
|
||||||
/**
|
/**
|
||||||
* Gets the users ip
|
* Gets the users ip
|
||||||
*/
|
*/
|
||||||
private function get_ip(): string
|
private function get_ip(): ?string
|
||||||
{
|
{
|
||||||
$ip = '';
|
$headers = array (
|
||||||
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
|
'HTTP_CLIENT_IP',
|
||||||
$ip = $_SERVER['HTTP_CLIENT_IP'];
|
'HTTP_X_FORWARDED_FOR',
|
||||||
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
'HTTP_X_FORWARDED',
|
||||||
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
|
'HTTP_FORWARDED_FOR',
|
||||||
} else {
|
'HTTP_FORWARDED',
|
||||||
$ip = $_SERVER['REMOTE_ADDR'];
|
'HTTP_X_REAL_IP',
|
||||||
|
'REMOTE_ADDR'
|
||||||
|
);
|
||||||
|
foreach ($headers as $header) {
|
||||||
|
if (isset($_SERVER[$header]))
|
||||||
|
return $_SERVER[$header];
|
||||||
}
|
}
|
||||||
return $ip;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue