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.

244 lines
5.8 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage session
  7. * @since 2010-03-14
  8. */
  9. namespace Majestic;
  10. class Session
  11. {
  12. /**
  13. * Default number of seconds the session will be remembered
  14. * for when asked to be remembered
  15. *
  16. * @var int
  17. */
  18. private static $remember_time = 1209600; // 2 weeks
  19. private static $started = false;
  20. /**
  21. * Avoid new
  22. * @codeCoverageIgnoreStart
  23. */
  24. private function __construct(){}
  25. /**
  26. * @codeCoverageIgnoreEnd
  27. */
  28. /**
  29. * Avoid cloning
  30. * @codeCoverageIgnoreStart
  31. */
  32. private function __clone(){}
  33. /**
  34. * @codeCoverageIgnoreEnd
  35. */
  36. /**
  37. * Retrieve a member of the $_SESSION
  38. *
  39. * If no $key is passed, returns the entire $_SESSION array.
  40. *
  41. * @param string $key
  42. * @param mixed $default Default value to use if key not found
  43. * @return mixed Returns null if key does not exist
  44. */
  45. public static function get($key = null, $default = null)
  46. {
  47. if (!self::$started) {
  48. if (self::isExists()) {
  49. self::start();
  50. } else {
  51. return $default;
  52. }
  53. }
  54. if (null === $key) {
  55. return $_SESSION;
  56. }
  57. return isset($_SESSION[$key]) ? $_SESSION[$key] : $default;
  58. }
  59. /**
  60. * Set $_SESSION values
  61. *
  62. * @param string|array $spec
  63. * @param null|mixed $value
  64. * @return void
  65. */
  66. public static function set($spec, $value = null)
  67. {
  68. if (!self::$started) {
  69. self::start();
  70. }
  71. if (is_array($spec)) {
  72. foreach ($spec as $key => $value) {
  73. self::set($key, $value);
  74. }
  75. return;
  76. }
  77. $_SESSION[(string) $spec] = $value;
  78. }
  79. /**
  80. * Delete Session value
  81. *
  82. * @param string $key
  83. */
  84. public static function del($key)
  85. {
  86. if (!self::$started) {
  87. if (self::isExists()) {
  88. self::start();
  89. } else {
  90. return;
  91. }
  92. }
  93. if (isset($_SESSION[$key])) {
  94. unset($_SESSION[$key]);
  95. }
  96. }
  97. /**
  98. * remember() - Write a persistent cookie that expires after a number
  99. * of seconds in the future. If no number of seconds is specified,
  100. * then this defaults to self::$rememberMeSeconds.
  101. *
  102. * @param $seconds integer - OPTIONAL specifies TTL for cookie in seconds from present time
  103. * @return void
  104. */
  105. public static function remember($seconds = null)
  106. {
  107. $seconds = (int) $seconds;
  108. $seconds = ($seconds > 0) ? $seconds : self::$remember_time;
  109. self::rememberUntil($seconds);
  110. }
  111. /**
  112. * forget() - Write a volatile session cookie, removing any persistent
  113. * cookie that may have existed. The session would end upon,
  114. * for example, termination of a web browser program.
  115. *
  116. * @return void
  117. */
  118. public static function forget()
  119. {
  120. self::rememberUntil();
  121. }
  122. /**
  123. * rememberUntil() - This method does the work of changing the state of the session
  124. * cookie and making sure that it gets resent to the browser via regenerateId()
  125. *
  126. * @param int $seconds
  127. * @return void
  128. */
  129. public static function rememberUntil($seconds = 0)
  130. {
  131. $params = session_get_cookie_params();
  132. session_set_cookie_params(
  133. $seconds,
  134. $params['path'],
  135. $params['domain'],
  136. $params['secure']
  137. );
  138. self::regenerateId();
  139. }
  140. /**
  141. * regenerateId() - Regenerate the session id. Best practice is to call this after
  142. * session is started. If called prior to session starting, session id will be regenerated
  143. * at start time.
  144. *
  145. * @throws Zend_Session_Exception
  146. * @return void
  147. */
  148. public static function regenerateId()
  149. {
  150. if (self::$started) {
  151. session_regenerate_id(true);
  152. }
  153. }
  154. public static function isExists()
  155. {
  156. if (isset($_COOKIE[session_name()])) {
  157. return true;
  158. }
  159. return false;
  160. }
  161. public static function start()
  162. {
  163. if (self::$started) {
  164. return;
  165. }
  166. self::$started = true;
  167. session_start();
  168. }
  169. /**
  170. * getId() - get the current session id
  171. *
  172. * @return string
  173. */
  174. public static function getId()
  175. {
  176. return session_id();
  177. }
  178. /**
  179. * destroy() - This is used to destroy session data, and optionally,
  180. * the session cookie itself
  181. *
  182. * @param bool $remove_cookie - OPTIONAL remove session id cookie,
  183. * defaults to true (remove cookie)
  184. * @return void
  185. */
  186. public static function destroy($remove_cookie = true)
  187. {
  188. session_destroy();
  189. if ($remove_cookie) {
  190. self::expireSessionCookie();
  191. }
  192. }
  193. /**
  194. * expireSessionCookie() - Sends an expired session id cookie, causing the client to delete the session cookie
  195. *
  196. * @return void
  197. */
  198. public static function expireSessionCookie()
  199. {
  200. if (isset($_COOKIE[session_name()])) {
  201. $params = session_get_cookie_params();
  202. setcookie(
  203. session_name(),
  204. false,
  205. 0,
  206. $params['path'],
  207. $params['domain'],
  208. $params['secure']
  209. );
  210. }
  211. }
  212. public static function setSessionHandler($handler)
  213. {
  214. session_set_save_handler(
  215. array($handler, 'open'),
  216. array($handler, 'close'),
  217. array($handler, 'read'),
  218. array($handler, 'write'),
  219. array($handler, 'destroy'),
  220. array($handler, 'gc')
  221. );
  222. }
  223. }