/library/Zend/Session/Configuration/SessionConfiguration.php

https://github.com/Exercise/zf2 · PHP · 281 lines · 133 code · 26 blank · 122 comment · 10 complexity · b5cda9bbfd029848d6db06e63e347218 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-webat this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Session
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Session\Configuration;
  25. use Zend\Validator\Hostname\Hostname as HostnameValidator,
  26. Zend\Session\Exception as SessionException;
  27. /**
  28. * Session configuration proxying to session INI options
  29. *
  30. * @category Zend
  31. * @package Zend_Session
  32. * @subpackage Configuration
  33. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class SessionConfiguration extends StandardConfiguration
  37. {
  38. /**
  39. * Used with {@link _handleError()}; stores PHP error code
  40. * @var int
  41. */
  42. protected $_phpErrorCode = false;
  43. /**
  44. * Used with {@link _handleError()}; stores PHP error message
  45. * @var string
  46. */
  47. protected $_phpErrorMessage = false;
  48. /**
  49. * @var int Default number of seconds to make session sticky, when rememberMe() is called
  50. */
  51. protected $_rememberMeSeconds = 1209600; // 2 weeks
  52. /**
  53. * @var string session.serialize_handler
  54. */
  55. protected $_serializeHandler;
  56. /**
  57. * @var array Valid cache limiters (per session.cache_limiter)
  58. */
  59. protected $_validCacheLimiters = array(
  60. 'nocache',
  61. 'public',
  62. 'private',
  63. 'private_no_expire',
  64. );
  65. /**
  66. * @var array Valid hash bits per character (per session.hash_bits_per_character)
  67. */
  68. protected $_validHashBitsPerCharacters = array(
  69. 4,
  70. 5,
  71. 6,
  72. );
  73. /**
  74. * @var array Valid hash functions (per session.hash_function)
  75. */
  76. protected $_validHashFunctions;
  77. /**
  78. * Set storage option in backend configuration store
  79. *
  80. * Does nothing in this implementation; others might use it to set things
  81. * such as INI settings.
  82. *
  83. * @param string $name
  84. * @param mixed $value
  85. * @return Zend\Session\Configuration\StandardConfiguration
  86. */
  87. public function setStorageOption($name, $value)
  88. {
  89. $key = false;
  90. switch ($name) {
  91. case 'remember_me_seconds':
  92. // do nothing; not an INI option
  93. return;
  94. case 'url_rewriter_tags':
  95. $key = 'url_rewriter.tags';
  96. break;
  97. default:
  98. $key = 'session.' . $name;
  99. break;
  100. }
  101. ini_set($key, $value);
  102. }
  103. /**
  104. * Retrieve a storage option from a backend configuration store
  105. *
  106. * Used to retrieve default values from a backend configuration store.
  107. *
  108. * @param string $name
  109. * @return mixed
  110. */
  111. public function getStorageOption($name)
  112. {
  113. $key = false;
  114. $transform = false;
  115. switch ($name) {
  116. case 'remember_me_seconds':
  117. // No remote storage option; just return the current value
  118. return $this->_rememberMeSeconds;
  119. case 'url_rewriter_tags':
  120. $key = 'url_rewriter.tags';
  121. break;
  122. // The following all need a transformation on the retrieved value;
  123. // however they use the same key naming scheme
  124. case 'use_cookies':
  125. case 'use_only_cookies':
  126. case 'use_trans_sid':
  127. $transform = function ($value) {
  128. return (bool) $value;
  129. };
  130. default:
  131. $key = 'session.' . $name;
  132. break;
  133. }
  134. $value = ini_get($key);
  135. if (false !== $transform) {
  136. $value = $transform($value);
  137. }
  138. return $value;
  139. }
  140. /**
  141. * Handle PHP errors
  142. *
  143. * @param int $code
  144. * @param string $message
  145. * @return void
  146. */
  147. protected function _handleError($code, $message)
  148. {
  149. $this->_phpErrorCode = $code;
  150. $this->_phpErrorMessage = $message;
  151. }
  152. /**
  153. * Set session.save_handler
  154. *
  155. * @param string $saveHandler
  156. * @return SessionConfiguration
  157. * @throws SessionException
  158. */
  159. public function setSaveHandler($saveHandler)
  160. {
  161. $saveHandler = (string) $saveHandler;
  162. set_error_handler(array($this, '_handleError'));
  163. ini_set('session.save_handler', $saveHandler);
  164. restore_error_handler();
  165. if ($this->_phpErrorCode >= E_WARNING) {
  166. throw new SessionException('Invalid save handler specified');
  167. }
  168. $this->setOption('save_handler', $saveHandler);
  169. return $this;
  170. }
  171. /**
  172. * Set session.serialize_handler
  173. *
  174. * @param string $serializeHandler
  175. * @return SessionConfiguration
  176. * @throws SessionException
  177. */
  178. public function setSerializeHandler($serializeHandler)
  179. {
  180. $serializeHandler = (string) $serializeHandler;
  181. set_error_handler(array($this, '_handleError'));
  182. ini_set('session.serialize_handler', $serializeHandler);
  183. restore_error_handler();
  184. if ($this->_phpErrorCode >= E_WARNING) {
  185. throw new SessionException('Invalid serialize handler specified');
  186. }
  187. $this->_serializeHandler = (string) $serializeHandler;
  188. return $this;
  189. }
  190. // session.cache_limiter
  191. public function setCacheLimiter($cacheLimiter)
  192. {
  193. $cacheLimiter = (string) $cacheLimiter;
  194. if (!in_array($cacheLimiter, $this->_validCacheLimiters)) {
  195. throw new SessionException('Invalid cache limiter provided');
  196. }
  197. $this->setOption('cache_limiter', $cacheLimiter);
  198. ini_set('session.cache_limiter', $cacheLimiter);
  199. return $this;
  200. }
  201. /**
  202. * Retrieve list of valid hash functions
  203. *
  204. * @return array
  205. */
  206. protected function _getHashFunctions()
  207. {
  208. if (empty($this->_validHashFunctions)) {
  209. /**
  210. * @see http://php.net/manual/en/session.configuration.php#ini.session.hash-function
  211. * "0" and "1" refer to MD5-128 and SHA1-160, respectively, and are
  212. * valid in addition to whatever is reported by hash_algos()
  213. */
  214. $this->_validHashFunctions = array('0', '1') + hash_algos();
  215. }
  216. return $this->_validHashFunctions;
  217. }
  218. /**
  219. * Set session.hash_function
  220. *
  221. * @param string|int $hashFunction
  222. * @return SessionConfiguration
  223. * @throws SessionException
  224. */
  225. public function setHashFunction($hashFunction)
  226. {
  227. $hashFunction = (string) $hashFunction;
  228. $validHashFunctions = $this->_getHashFunctions();
  229. if (!in_array($hashFunction, $this->_getHashFunctions(), true)) {
  230. throw new SessionException('Invalid hash function provided');
  231. }
  232. $this->setOption('hash_function', $hashFunction);
  233. ini_set('session.hash_function', $hashFunction);
  234. return $this;
  235. }
  236. /**
  237. * Set session.hash_bits_per_character
  238. *
  239. * @param int $hashBitsPerCharacter
  240. * @return SessionConfiguration
  241. * @throws SessionException
  242. */
  243. public function setHashBitsPerCharacter($hashBitsPerCharacter)
  244. {
  245. if (!is_numeric($hashBitsPerCharacter)
  246. || !in_array($hashBitsPerCharacter, $this->_validHashBitsPerCharacters)
  247. ) {
  248. throw new SessionException('Invalid hash bits per character provided');
  249. }
  250. $hashBitsPerCharacter = (int) $hashBitsPerCharacter;
  251. $this->setOption('hash_bits_per_character', $hashBitsPerCharacter);
  252. ini_set('session.hash_bits_per_character', $hashBitsPerCharacter);
  253. return $this;
  254. }
  255. }