chamilo_session.class.php 3.6 KB

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