Added input handler, redirector, session handler, and token handler.

This commit is contained in:
Gregorio Chiko Putra 2017-09-04 16:25:11 +07:00
parent e1d70ed8f4
commit 06c4e2a74e
4 changed files with 139 additions and 0 deletions

35
Core/Input.php Normal file
View File

@ -0,0 +1,35 @@
<?php
namespace Core;
class Input
{
public static function exists($type = 'post')
{
switch ($type) {
case 'post':
return (!empty($_POST)) ? true : false;
break;
case 'get':
return (!empty($_GET)) ? true : false;
break;
default:
return false;
break;
}
}
public static function get($item)
{
if(isset($_POST[$item]))
{
return $_POST[$item];
}
elseif(isset($_GET[$item]))
{
return $_GET[$item];
}
return '';
}
}

19
Core/Redirect.php Normal file
View File

@ -0,0 +1,19 @@
<?php
namespace Core;
class Redirect
{
public static function to($url)
{
if($url)
{
$url = htmlspecialchars($url);
$url = rtrim($url, '/');
header("Location:$url");
return true;
}
return false;
}
}

42
Core/Session.php Normal file
View File

@ -0,0 +1,42 @@
<?php
namespace Core;
class Session
{
public static function exists($name)
{
return (isset($_SESSION[$name])) ? true : false;
}
public static function put($name, $value = '')
{
return $_SESSION[$name] = $value;
}
public static function get($name)
{
return $_SESSION[$name];
}
public static function delete($name)
{
if(self::exists($name))
{
unset($_SESSION[$name]);
}
}
public static function flash($name, $string = '')
{
if(self::exists($name))
{
$session = self::get($name);
self::delete($name);
return $session;
}
else
{
self::put($name, $string);
}
}
}

43
Core/Token.php Normal file
View File

@ -0,0 +1,43 @@
<?php
namespace Core;
class Token
{
public static function generate()
{
if(!Session::exists('tokens'))
{
Session::put('tokens', []);
}
$tokens = Session::get('tokens');
if(count($tokens) >= 10)
{
array_shift($tokens);
}
$tokens[] = md5(uniqid());
Session::put('tokens', $tokens);
return end($tokens);
}
public static function check($token)
{
$tokenName = 'tokens';
$tokenNow = $token;
if(Session::exists($tokenName))
{
if(in_array($tokenNow, Session::get($tokenName)))
{
Session::delete($tokenName);
return true;
}
}
return false;
}
}