1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151:
<?php
namespace Core\Providers;
/**
* Manages the <b>$_SESSION</b> global with getters, setters and destructors.
*
* @usage Please see {@see \Core\Session::__callStatic}
*
* @package Core
*/
class Session
{
protected static $roleGuest = __ROLE_GUEST__;
protected static $roleUser = __ROLE_USER__;
protected static $roleAdmin = __ROLE_ADMIN__;
/**
* Sets a <b>$_SESSION[$key] = $value</b>. Do not use directly.
* @see Session::__callStatic
* @param string $key The key where to save it
* @param mixed $value The value to save
* @return mixed The value set
*/
public static function set($key, $value) {
$_SESSION[$key] = $value;
return $value;
}
/**
* Gets the value of a <b>$_SESSION</b> key. Do not use directly.
* @see Session::__callStatic
* @param string $key
* @return mixed <b>$_SESSION[key]</b> if it exists, <b>NULL</b> otherwise
*/
public static function get($key = NULL) {
if (is_null($key)) {
return $_SESSION;
} elseif (isset($_SESSION[$key])) {
return $_SESSION[$key];
}
return NULL;
}
/**
* Gets <b>$_SESSION[$key][$subKey]</b> if exists, <b>$_SESSION[$key]</b> otherwise or <b>NULL</b>. Do not use directly.
* @see Session::__callStatic
* @param string $key The key where it's set
* @param string $subKey The subkey where it's set
* @return mixed SESSION[key][subKey] if exists or NULL if it doesn't
*/
public static function getExt($key, $subKey) {
if (isset($_SESSION[$key])) {
if(isset($_SESSION[$key][$subKey])) {
return $_SESSION[$key][$subKey];
}
return $_SESSION[$key];
}
return NULL;
}
/**
* Cleans a session parameter if $key is specified, or cleans the whole <b>$_SESSION</b> otherwise. Do not use directly.
* @see Session::__callStatic
* @param string $key = NULL. If not specified, sets <b>$_SESSION = NULL</b>; otherwise, unsets <b>$_SESSION[key]</b>
* @return null
*/
public static function clean($key = NULL) {
$value = NULL;
if (is_null($key)) {
$value = $_SESSION;
$_SESSION = NULL;
unset($_SESSION);
} elseif (!empty($_SESSION[$key])) {
$value = $_SESSION[$key];
$_SESSION[$key] = NULL;
unset($_SESSION[$key]);
} else {
// nothing; we return null
}
return $value;
}
/**
* Returns whether the user is authenticated or not
* @return bool Whether the user is logged in or not (i.e. whether is user|TRUE or guest|FALSE)
*/
public static function isUser() {
return self::get('role') >= self::$roleGuest;
}
/**
* Returns whether the user is superuser (<b>admin</b>) or not
* @return bool Whether the user is sudo (<b>role = __ROLE_ADMIN__</b>) or not
*/
public static function isAdmin() {
return self::get('role') == self::$roleAdmin;
}
/**
* Returns whether the user is authenticated or not. If it is, returns its role (<b>__ROLE_<role>__</b>)
* @return mixed FALSE if not authenticated, $role (!= 0) if authenticated
*/
public static function isAuthenticated() {
$role = self::get('role');
if ($role > self::$roleGuest) {
return $role;
} else {
return FALSE;
}
}
/**
* Manages all getters, setters and destroyers for Session. Use <b>getFoo()</b>, <b>getFoo('bar')</b> or <b>cleanFoo()</b>.
* See example for more details and how to use.
* @param string $methodName the method to perform. Ex: setUser, getCart
* @param mixed $args the value
* @example
* Set a variable (any type) with:
* <code>Session::setFoo($fooValue)</code>
* To read it, use <code>$foo = Session::getFoo()</code>
* To delete a variable, use <code>$foo = Session::cleanFoo()</code>
* (Cleaning a variable returns its value so it can be used as a flash var.)
* @return mixed $args
*/
public static function __callStatic($methodName, $args) {
if (preg_match('/^(set|get|clean|is)([A-Z])(.*)$/', $methodName, $matches)) {
$property = strtolower($matches[2]) . $matches[3];
switch ($matches[1]) {
case 'set':
return self::set($property, $args[0]);
case 'get':
if (!count($args)) {
return self::get($property);
} else {
return self::getExt($property, $args[0]);
}
case 'clean':
self::clean($property);
break;
default:
break;
}
return $args;
} else {
return NULL;
}
}
}