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.

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