You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
168 lines
3.4 KiB
168 lines
3.4 KiB
![]()
14 years ago
|
<?php
|
||
![]()
8 years ago
|
/* For licensing terms, see /license.txt */
|
||
![]()
10 years ago
|
|
||
![]()
8 years ago
|
use Chamilo\CoreBundle\Framework\Container;
|
||
![]()
10 years ago
|
|
||
![]()
14 years ago
|
/**
|
||
![]()
10 years ago
|
* @todo replace all $_SESSION calls with this class.
|
||
![]()
13 years ago
|
*/
|
||
![]()
8 years ago
|
class ChamiloSession implements \ArrayAccess
|
||
![]()
14 years ago
|
{
|
||
![]()
8 years ago
|
/**
|
||
|
* @param string $name
|
||
|
*/
|
||
|
public function __unset($name)
|
||
|
{
|
||
|
unset($_SESSION[$name]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param string $name
|
||
|
*
|
||
|
* @return bool
|
||
|
*/
|
||
|
public function __isset($name)
|
||
|
{
|
||
|
return self::has($name);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* It it exists returns the value stored at the specified offset.
|
||
|
* If offset does not exists returns null. Do not trigger a warning.
|
||
|
*
|
||
|
* @param string $name
|
||
|
*
|
||
|
* @return mixed
|
||
|
*/
|
||
|
public function __get($name)
|
||
|
{
|
||
|
return self::read($name);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param string $name
|
||
|
* @param mixed $value
|
||
|
*/
|
||
|
public function __set($name, $value)
|
||
|
{
|
||
|
self::write($name, $value);
|
||
|
}
|
||
|
|
||
![]()
14 years ago
|
/**
|
||
![]()
8 years ago
|
* @param string $variable
|
||
![]()
8 years ago
|
* @param null $default
|
||
|
*
|
||
![]()
8 years ago
|
* @return mixed|null
|
||
![]()
14 years ago
|
*/
|
||
![]()
8 years ago
|
public static function read($variable, $default = null)
|
||
![]()
14 years ago
|
{
|
||
![]()
8 years ago
|
$session = Container::getSession();
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
$result = null;
|
||
|
if (isset($session)) {
|
||
|
$result = $session->get($variable);
|
||
|
}
|
||
|
|
||
|
// Check if the value exists in the $_SESSION array
|
||
![]()
14 years ago
|
if (empty($result)) {
|
||
![]()
8 years ago
|
if (isset($_SESSION[$variable])) {
|
||
|
return $_SESSION[$variable];
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
return $default;
|
||
|
} else {
|
||
|
return $result;
|
||
![]()
14 years ago
|
}
|
||
|
}
|
||
|
|
||
![]()
13 years ago
|
/**
|
||
![]()
8 years ago
|
* @param string $variable
|
||
![]()
8 years ago
|
* @param mixed $value
|
||
![]()
13 years ago
|
*/
|
||
![]()
8 years ago
|
public static function write($variable, $value)
|
||
![]()
14 years ago
|
{
|
||
![]()
8 years ago
|
$session = Container::getSession();
|
||
|
// Writing the session in 2 instances because
|
||
|
$_SESSION[$variable] = $value;
|
||
|
$session->set($variable, $value);
|
||
![]()
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
![]()
8 years ago
|
* @param string $variable
|
||
|
*/
|
||
|
public static function erase($variable)
|
||
|
{
|
||
|
$variable = (string) $variable;
|
||
|
$session = Container::getSession();
|
||
|
$session->remove($variable);
|
||
|
|
||
|
if (isset($GLOBALS[$variable])) {
|
||
|
unset($GLOBALS[$variable]);
|
||
|
}
|
||
|
if (isset($_SESSION[$variable])) {
|
||
|
unset($_SESSION[$variable]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns true if session has variable set up, false otherwise.
|
||
![]()
14 years ago
|
*
|
||
![]()
8 years ago
|
* @param string $variable
|
||
![]()
14 years ago
|
*
|
||
![]()
8 years ago
|
* @return bool
|
||
![]()
14 years ago
|
*/
|
||
![]()
8 years ago
|
public static function has($variable)
|
||
![]()
14 years ago
|
{
|
||
![]()
8 years ago
|
return isset($_SESSION[$variable]);
|
||
|
}
|
||
![]()
14 years ago
|
|
||
![]()
8 years ago
|
/**
|
||
![]()
8 years ago
|
* Clear.
|
||
![]()
8 years ago
|
*/
|
||
|
public static function clear()
|
||
|
{
|
||
|
$session = Container::getSession();
|
||
|
$session->clear();
|
||
![]()
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
![]()
8 years ago
|
* Destroy.
|
||
![]()
8 years ago
|
*/
|
||
|
public static function destroy()
|
||
|
{
|
||
|
$session = Container::getSession();
|
||
|
$session->invalidate();
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* ArrayAccess
|
||
![]()
14 years ago
|
*/
|
||
![]()
8 years ago
|
public function offsetExists($offset)
|
||
![]()
14 years ago
|
{
|
||
![]()
8 years ago
|
return isset($_SESSION[$offset]);
|
||
![]()
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
![]()
8 years ago
|
* It it exists returns the value stored at the specified offset.
|
||
|
* If offset does not exists returns null. Do not trigger a warning.
|
||
|
*
|
||
|
* @param string $offset
|
||
![]()
8 years ago
|
*
|
||
![]()
8 years ago
|
* @return any
|
||
|
*/
|
||
|
public function offsetGet($offset)
|
||
|
{
|
||
|
return self::read($offset);
|
||
|
}
|
||
|
|
||
|
public function offsetSet($offset, $value)
|
||
|
{
|
||
|
self::write($offset, $value);
|
||
|
}
|
||
|
|
||
|
public function offsetUnset($offset)
|
||
|
{
|
||
|
unset($_SESSION[$offset]);
|
||
|
}
|
||
![]()
11 years ago
|
}
|