sfSessionStorage.class.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. * (c) 2004-2006 Sean Kerr <sean@code-box.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * sfSessionStorage allows you to store persistent symfony data in the user session.
  12. *
  13. * <b>Optional parameters:</b>
  14. *
  15. * # <b>auto_start</b> - [Yes] - Should session_start() automatically be called?
  16. * # <b>session_name</b> - [symfony] - The name of the session.
  17. *
  18. * @package symfony
  19. * @subpackage storage
  20. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  21. * @author Sean Kerr <sean@code-box.org>
  22. * @version SVN: $Id: sfSessionStorage.class.php 10425 2008-07-22 15:03:11Z nicolas $
  23. */
  24. class sfSessionStorage extends sfStorage
  25. {
  26. static protected
  27. $sessionIdRegenerated = false,
  28. $sessionStarted = false;
  29. /**
  30. * Available options:
  31. *
  32. * * session_name: The cookie name (symfony by default)
  33. * * session_id: The session id (null by default)
  34. * * auto_start: Whether to start the session (true by default)
  35. * * session_cookie_lifetime: Cookie lifetime
  36. * * session_cookie_path: Cookie path
  37. * * session_cookie_domain: Cookie domain
  38. * * session_cookie_secure: Cookie secure
  39. * * session_cookie_httponly: Cookie http only (only for PHP >= 5.2)
  40. *
  41. * The default values for all 'session_cookie_*' options are those returned by the session_get_cookie_params() function
  42. *
  43. * @param array $options An associative array of options
  44. *
  45. * @see sfStorage
  46. */
  47. public function initialize($options = null)
  48. {
  49. $cookieDefaults = session_get_cookie_params();
  50. $options = array_merge(array(
  51. 'session_name' => 'symfony',
  52. 'session_id' => null,
  53. 'auto_start' => true,
  54. 'session_cookie_lifetime' => $cookieDefaults['lifetime'],
  55. 'session_cookie_path' => $cookieDefaults['path'],
  56. 'session_cookie_domain' => $cookieDefaults['domain'],
  57. 'session_cookie_secure' => $cookieDefaults['secure'],
  58. 'session_cookie_httponly' => isset($cookieDefaults['httponly']) ? $cookieDefaults['httponly'] : false,
  59. ), $options);
  60. // initialize parent
  61. parent::initialize($options);
  62. // set session name
  63. $sessionName = $this->options['session_name'];
  64. session_name($sessionName);
  65. if (!(boolean) ini_get('session.use_cookies') && $sessionId = $this->options['session_id'])
  66. {
  67. session_id($sessionId);
  68. }
  69. $lifetime = $this->options['session_cookie_lifetime'];
  70. $path = $this->options['session_cookie_path'];
  71. $domain = $this->options['session_cookie_domain'];
  72. $secure = $this->options['session_cookie_secure'];
  73. $httpOnly = $this->options['session_cookie_httponly'];
  74. if (version_compare(phpversion(), '5.2', '>='))
  75. {
  76. session_set_cookie_params($lifetime, $path, $domain, $secure, $httpOnly);
  77. }
  78. else
  79. {
  80. session_set_cookie_params($lifetime, $path, $domain, $secure);
  81. }
  82. if ($this->options['auto_start'] && !self::$sessionStarted)
  83. {
  84. session_start();
  85. self::$sessionStarted = true;
  86. }
  87. }
  88. /**
  89. * Reads data from this storage.
  90. *
  91. * The preferred format for a key is directory style so naming conflicts can be avoided.
  92. *
  93. * @param string $key A unique key identifying your data
  94. *
  95. * @return mixed Data associated with the key
  96. */
  97. public function read($key)
  98. {
  99. $retval = null;
  100. if (isset($_SESSION[$key]))
  101. {
  102. $retval = $_SESSION[$key];
  103. }
  104. return $retval;
  105. }
  106. /**
  107. * Removes data from this storage.
  108. *
  109. * The preferred format for a key is directory style so naming conflicts can be avoided.
  110. *
  111. * @param string $key A unique key identifying your data
  112. *
  113. * @return mixed Data associated with the key
  114. */
  115. public function remove($key)
  116. {
  117. $retval = null;
  118. if (isset($_SESSION[$key]))
  119. {
  120. $retval = $_SESSION[$key];
  121. unset($_SESSION[$key]);
  122. }
  123. return $retval;
  124. }
  125. /**
  126. * Writes data to this storage.
  127. *
  128. * The preferred format for a key is directory style so naming conflicts can be avoided.
  129. *
  130. * @param string $key A unique key identifying your data
  131. * @param mixed $data Data associated with your key
  132. *
  133. */
  134. public function write($key, $data)
  135. {
  136. $_SESSION[$key] = $data;
  137. }
  138. /**
  139. * Regenerates id that represents this storage.
  140. *
  141. * @param boolean $destroy Destroy session when regenerating?
  142. *
  143. * @return boolean True if session regenerated, false if error
  144. *
  145. */
  146. public function regenerate($destroy = false)
  147. {
  148. if (self::$sessionIdRegenerated)
  149. {
  150. return;
  151. }
  152. // regenerate a new session id once per object
  153. session_regenerate_id($destroy);
  154. self::$sessionIdRegenerated = true;
  155. }
  156. /**
  157. * Executes the shutdown procedure.
  158. *
  159. */
  160. public function shutdown()
  161. {
  162. // don't need a shutdown procedure because read/write do it in real-time
  163. }
  164. }