Mithril as main method

This commit is contained in:
2017-10-25 12:08:41 +07:00
parent 6040809710
commit d1d5ee1b0c
157 changed files with 19593 additions and 716 deletions

View File

@@ -51,6 +51,50 @@ class Router
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('/mithril');
}
}
$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
if (is_array(\App\Models\ClientSession::fetch([
'uid' => $userid,
'id' => $sessid
]))) {
$token = Token::generate($userid);
header("X-Token: $token");
}
} elseif ($query_string == false) {
// echo ['status' => true, 'message' => 'atas'];die();
// Get user ip
$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: http://lepisi.dev/mithril');
// Redirect::to('/mithril');
die();
}
}
$url = $this->removeQueryStringVariable($url);
if ($this->match($url)) {
$controller = $this->params['controller'];
@@ -67,30 +111,29 @@ class Router
// Check if there's input to the current page
if (Input::exists('post')) {
$var = $_POST;
// Check the token
if (Token::check($var['_token'])) {
// Get the method
if (isset($var['_method'])) {
$action = $var['_method'];
}
} else {
// Token invalid
throw new \Exception("Token invalid", 498);
}
unset($var['_token']);
unset($var['_method']);
} elseif (Input::exists('get')) {
$get_var = $_GET;
// } 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();
}
@@ -104,7 +147,18 @@ class Router
private function removeQueryStringVariable($url)
{
$parts = explode('&', $url);
return $url = $parts[0];
$parts = explode('?', $parts[0]);
return $parts[0];
}
private function getQueryStringVariable($url)
{
$exploded = explode('?', $url);
if (isset($exploded[1])) {
$variables = explode('&', $exploded[1]);
return $variables;
}
return false;
}
protected function convertToStudlyCaps($string)