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.

242 lines
5.8 KiB

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