PageRenderTime 32ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Solar/Session/Manager/Adapter/Native.php

https://github.com/ggramlich/core
PHP | 221 lines | 67 code | 23 blank | 131 comment | 2 complexity | e97bcc7857d21fea77272a0fd8d93be4 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * Session manager for native PHP sessions
  5. *
  6. * @category Solar
  7. *
  8. * @package Solar_Session
  9. *
  10. * @author Antti Holvikari <anttih@gmail.com>
  11. *
  12. * @license http://opensource.org/licenses/bsd-license.php BSD
  13. *
  14. * @version $Id: Native.php 3366 2008-08-26 01:36:49Z pmjones $
  15. *
  16. */
  17. class Solar_Session_Manager_Adapter_Native extends Solar_Session_Manager_Adapter
  18. {
  19. /**
  20. *
  21. * Default configuration values.
  22. *
  23. * @config dependency handler A Solar_Session_Handler dependency injection. Default
  24. * is the string 'php', which means to use the native PHP session save.
  25. * handler instead of a dependency injection.
  26. *
  27. * @var array
  28. *
  29. */
  30. protected $_Solar_Session_Manager_Adapter_Native = array(
  31. 'handler' => null,
  32. );
  33. /**
  34. *
  35. * The session save handler object.
  36. *
  37. * @var Solar_Session_Handler_Adapter
  38. *
  39. */
  40. static protected $_handler;
  41. /**
  42. *
  43. * The current request object.
  44. *
  45. * @var Solar_Request
  46. *
  47. */
  48. protected $_request;
  49. /**
  50. *
  51. * A list of sessions.
  52. *
  53. * @var array
  54. *
  55. */
  56. protected $_sessions = array();
  57. /**
  58. *
  59. * Has a session already been stopped in this request?
  60. *
  61. * @var bool
  62. *
  63. */
  64. protected $_stopped = false;
  65. /**
  66. *
  67. * Post-construction tasks to complete object construction.
  68. *
  69. * @return void
  70. *
  71. */
  72. protected function _postConstruct()
  73. {
  74. parent::_postConstruct();
  75. // only set up the handler if it doesn't exist yet.
  76. if (! self::$_handler) {
  77. self::$_handler = Solar::dependency(
  78. 'Solar_Session_Handler',
  79. $this->_config['handler']
  80. );
  81. }
  82. $this->_request = Solar_Registry::get('request');
  83. }
  84. /**
  85. *
  86. * unload all related sessions
  87. *
  88. * @return bool
  89. *
  90. */
  91. public function _unloadAll()
  92. {
  93. foreach($this->_sessions as $segment) {
  94. $segment->unload();
  95. }
  96. }
  97. /**
  98. *
  99. * Starts the session
  100. *
  101. * @return void
  102. *
  103. */
  104. public function start()
  105. {
  106. session_start();
  107. }
  108. /**
  109. *
  110. * Regenerates the session ID.
  111. *
  112. * Use this every time there is a privilege change.
  113. *
  114. * @return void
  115. *
  116. * @see [[php::session_regenerate_id()]]
  117. *
  118. */
  119. public function regenerateId()
  120. {
  121. session_regenerate_id(true);
  122. }
  123. /**
  124. *
  125. * Has a session been started yet?
  126. *
  127. * @return bool
  128. *
  129. */
  130. public function isStarted()
  131. {
  132. return session_id() !== '';
  133. }
  134. /**
  135. *
  136. * Has the user requested a prior session?
  137. *
  138. * @return bool
  139. *
  140. */
  141. public function isContinuing()
  142. {
  143. if ($this->_stopped) {
  144. // Don't attempt to continue a session we've already destroyed
  145. return false;
  146. }
  147. $name = session_name();
  148. return $this->_request->cookie($name);
  149. }
  150. /**
  151. *
  152. * Remove this session and kill the cookie
  153. *
  154. * @return bool
  155. *
  156. */
  157. public function stop()
  158. {
  159. // remove the session cookie
  160. $params = session_get_cookie_params();
  161. setcookie(session_name(), '', time() - 42000,
  162. $params['path'], $params['domain'], $params['secure'], $params['httponly']);
  163. // Kill the backend storage of the session
  164. session_destroy();
  165. // Let all the sessions know that their data is no longer valid
  166. $this->_unloadAll();
  167. // We've already processed one session during this request
  168. $this->_stopped = true;
  169. }
  170. /**
  171. *
  172. * Allow sessions to register with the mothership
  173. *
  174. * @return void
  175. *
  176. */
  177. public function addSession(Solar_Session $session)
  178. {
  179. $this->_sessions[] = $session;
  180. }
  181. /**
  182. *
  183. * Close this session for use in this request, writing the results
  184. * to storage for the next request
  185. *
  186. * @return void
  187. *
  188. */
  189. public function close()
  190. {
  191. session_write_close();
  192. // Let all the sessions know that their data is no longer valid
  193. $this->_unloadAll();
  194. // clean out the session data, further changes to $_SESSION will not
  195. // be written out unless the sesion is restarted
  196. $_SESSION = array();
  197. }
  198. }