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.

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