PageRenderTime 55ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/www/libs/Zend/Session/Config/SessionConfig.php

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