lepisi-pengumuman/Core/Router.php

194 lines
6.5 KiB
PHP

<?php
namespace Core;
class Router
{
protected
$routes = [],
$params = [];
public function getRoutes()
{
return $this->routes;
}
public function getParams()
{
return $this->params;
}
public function add($route, $params = [])
{
$route = preg_replace('/\//', '\/', $route);
$route = preg_replace('/\{([a-z]+)\}/', '(?P<\1>[a-z-]+)', $route);
$route = preg_replace('/\{([a-z]+):([^\}]+)\}/', '(?P<\1>\2)', $route);
$route = preg_replace('/\{\?([a-z]+):([^\}]+)\}/', '\?(?P<\1>[a-z]+=\2)', $route);
$route = '/^'.$route.'$/';
$this->routes[$route] = $params;
}
public function match($url)
{
$url = htmlspecialchars($url);
$url = substr_replace($url, '', 0, 1);
$url = rtrim($url, '/');
foreach ($this->routes as $route => $params) {
if (preg_match($route, $url, $matches)) {
foreach ($matches as $key => $match) {
if (is_string($key)) {
$params[$key] = $match;
}
}
if ($putParams = $this->params = $params) {
return true;
}
}
}
return false;
}
public function dispatch($url)
{
// Check token
if (isset($_SERVER['HTTP_X_Token'])) {
if (!$this->checkToken($_SERVER['HTTP_X_Token'])) {
// Logout
$controller = 'Home';
$controller = $this->getNamespace($controller);
$obj = new $controller();
$obj->logout();
Redirect::to('/');
}
}
if ($url != '/login' || $url != '/') {
$query_string = $this->getQueryStringVariable($url);
if ($query_string) {
$sessid = explode('=', $query_string[0]);
$sessid = $sessid[1];
$userid = explode('=', $query_string[1]);
$userid = $userid[1];
// Check if user login
$session = \App\Models\ClientSession::fetch([
'uid' => $userid,
'id' => $sessid
]);
if (is_array($session)) {
$token = Token::generate($userid);
header("X-Token: $token");
} else {
$ip_address = isset($_SERVER['HTTP_X_FORWADED_FOR']) ? $_SERVER['HTTP_X_FORWADED_FOR'] : $_SERVER['REMOTE_ADDR'];
$record = \App\Models\ClientSession::fetch(['ip_address' => $ip_address]);
$obj = 'Home';
$obj = $this->getNamespace($obj);
if (is_array($record)) {
$obj = new $obj();
header('client: api');
$obj->logout($record['uid']);
die();
} else {
$obj = new $obj();
$obj->logout($userid);
}
header('Location: /');
}
} elseif ($query_string == false) {
$ip_address = isset($_SERVER['HTTP_X_FORWADED_FOR']) ? $_SERVER['HTTP_X_FORWADED_FOR'] : $_SERVER['REMOTE_ADDR'];
$record = \App\Models\ClientSession::fetch(['ip_address' => $ip_address]);
if (is_array($record)) {
$obj = 'Home';
$obj = $this->getNamespace($obj);
$obj = new $obj();
$obj->logout($record['uid']);
// header('Location: /');
// die();
}
}
}
$url = $this->removeQueryStringVariable($url);
if ($this->match($url)) {
$controller = $this->params['controller'];
$controller = $this->convertToStudlyCaps($controller);
$controller = $this->getNamespace($controller);
if (class_exists($controller)) {
$object = new $controller();
$action = $this->params['action'];
$action = $this->convertToCamelCaps($action);
if (is_callable([$object, $action])) {
// Check if there's input to the current page
if (Input::exists('post')) {
$var = $_POST;
// } elseif (Input::exists('get')) {
// $get_var = $_GET;
} elseif ($_SERVER['REQUEST_METHOD'] != '') {
$data = json_decode(file_get_contents('php://input'), true);
}
if (isset($var['_method'])) {
$action = $var['_method'];
unset($var['_method']);
}
if (array_key_exists('id', $this->params)) {
$var['id'] = $this->params['id'];
} elseif (array_key_exists('status', $this->params)) {
$get_var = preg_replace('/^[a-z]+=/', '', $get_var['status']);
}
if (isset($var)) {
return $object->$action($var);
} elseif (isset($get_var)) {
return $object->$action($get_var);
} elseif (isset($data)) {
return $object->$action($data);
} else {
return $object->$action();
}
}
}
throw new \Exception("Method not found", 400);
}
throw new \Exception("Page not found", 404);
}
private function removeQueryStringVariable($url)
{
$parts = explode('&', $url);
$parts = explode('?', $parts[0]);
return $parts[0];
}
protected function getQueryStringVariable($url)
{
$exploded = explode('?', $url);
if (isset($exploded[1])) {
$variables = explode('&', $exploded[1]);
return $variables;
}
return false;
}
protected function convertToStudlyCaps($string)
{
return str_replace(' ', '', ucwords(str_replace('-', ' ', $string)));
}
protected function getNamespace($controller)
{
return $namespace = 'App\Controllers\\' . $controller;
}
protected function convertToCamelCaps($string)
{
return lcfirst($this->convertToStudlyCaps($string));
}
}