chamilo_session.class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Framework\Container;
  4. /**
  5. * @todo replace all $_SESSION calls with this class.
  6. */
  7. class ChamiloSession implements \ArrayAccess
  8. {
  9. /**
  10. * @param string $name
  11. */
  12. public function __unset($name)
  13. {
  14. unset($_SESSION[$name]);
  15. }
  16. /**
  17. * @param string $name
  18. *
  19. * @return bool
  20. */
  21. public function __isset($name)
  22. {
  23. return self::has($name);
  24. }
  25. /**
  26. * It it exists returns the value stored at the specified offset.
  27. * If offset does not exists returns null. Do not trigger a warning.
  28. *
  29. * @param string $name
  30. *
  31. * @return mixed
  32. */
  33. public function __get($name)
  34. {
  35. return self::read($name);
  36. }
  37. /**
  38. * @param string $name
  39. * @param mixed $value
  40. */
  41. public function __set($name, $value)
  42. {
  43. self::write($name, $value);
  44. }
  45. /**
  46. * @param string $variable
  47. * @param null $default
  48. *
  49. * @return mixed|null
  50. */
  51. public static function read($variable, $default = null)
  52. {
  53. $session = Container::getSession();
  54. $result = null;
  55. if (isset($session)) {
  56. $result = $session->get($variable);
  57. }
  58. // Check if the value exists in the $_SESSION array
  59. if (empty($result)) {
  60. if (isset($_SESSION[$variable])) {
  61. return $_SESSION[$variable];
  62. }
  63. return $default;
  64. } else {
  65. return $result;
  66. }
  67. }
  68. /**
  69. * @param string $variable
  70. * @param mixed $value
  71. */
  72. public static function write($variable, $value)
  73. {
  74. $session = Container::getSession();
  75. // Writing the session in 2 instances because
  76. $_SESSION[$variable] = $value;
  77. $session->set($variable, $value);
  78. }
  79. /**
  80. * @param string $variable
  81. */
  82. public static function erase($variable)
  83. {
  84. $variable = (string) $variable;
  85. $session = Container::getSession();
  86. $session->remove($variable);
  87. if (isset($GLOBALS[$variable])) {
  88. unset($GLOBALS[$variable]);
  89. }
  90. if (isset($_SESSION[$variable])) {
  91. unset($_SESSION[$variable]);
  92. }
  93. }
  94. /**
  95. * Returns true if session has variable set up, false otherwise.
  96. *
  97. * @param string $variable
  98. *
  99. * @return bool
  100. */
  101. public static function has($variable)
  102. {
  103. return isset($_SESSION[$variable]);
  104. }
  105. /**
  106. * Clear.
  107. */
  108. public static function clear()
  109. {
  110. $session = Container::getSession();
  111. $session->clear();
  112. }
  113. /**
  114. * Destroy.
  115. */
  116. public static function destroy()
  117. {
  118. $session = Container::getSession();
  119. $session->invalidate();
  120. }
  121. /*
  122. * ArrayAccess
  123. */
  124. public function offsetExists($offset)
  125. {
  126. return isset($_SESSION[$offset]);
  127. }
  128. /**
  129. * It it exists returns the value stored at the specified offset.
  130. * If offset does not exists returns null. Do not trigger a warning.
  131. *
  132. * @param string $offset
  133. *
  134. * @return any
  135. */
  136. public function offsetGet($offset)
  137. {
  138. return self::read($offset);
  139. }
  140. public function offsetSet($offset, $value)
  141. {
  142. self::write($offset, $value);
  143. }
  144. public function offsetUnset($offset)
  145. {
  146. unset($_SESSION[$offset]);
  147. }
  148. }