chamilo_session.class.php 3.3 KB

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