Source of file SessionConfig.php
Size: 8,293 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Session/Config/SessionConfig.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Session\Config; use Zend\Session\Exception; /** * Session configuration proxying to session INI options */ class SessionConfig extends StandardConfig { /** * Used with {@link handleError()}; stores PHP error code * @var int */ protected $phpErrorCode = false; /** * Used with {@link handleError()}; stores PHP error message * @var string */ protected $phpErrorMessage = false; /** * @var int Default number of seconds to make session sticky, when rememberMe() is called */ protected $rememberMeSeconds = 1209600; // 2 weeks /** * @var string session.serialize_handler */ protected $serializeHandler; /** * @var array Valid cache limiters (per session.cache_limiter) */ protected $validCacheLimiters = array( '', 'nocache', 'public', 'private', 'private_no_expire', ); /** * @var array Valid hash bits per character (per session.hash_bits_per_character) */ protected $validHashBitsPerCharacters = array( 4, 5, 6, ); /** * @var array Valid hash functions (per session.hash_function) */ protected $validHashFunctions; /** * Set storage option in backend configuration store * * @param string $storageName * @param mixed $storageValue * @return SessionConfig * @throws Exception\InvalidArgumentException */ public function setStorageOption($storageName, $storageValue) { $key = false; switch ($storageName) { case 'remember_me_seconds': // do nothing; not an INI option return; case 'url_rewriter_tags': $key = 'url_rewriter.tags'; break; default: $key = 'session.' . $storageName; break; } $result = ini_set($key, $storageValue); if (FALSE === $result) { throw new Exception\InvalidArgumentException("'" . $key . "' is not a valid sessions-related ini setting."); } return $this; } /** * Retrieve a storage option from a backend configuration store * * Used to retrieve default values from a backend configuration store. * * @param string $storageOption * @return mixed */ public function getStorageOption($storageOption) { switch ($storageOption) { case 'remember_me_seconds': // No remote storage option; just return the current value return $this->rememberMeSeconds; case 'url_rewriter_tags': return ini_get('url_rewriter.tags'); // The following all need a transformation on the retrieved value; // however they use the same key naming scheme case 'use_cookies': case 'use_only_cookies': case 'use_trans_sid': case 'cookie_httponly': return (bool) ini_get('session.' . $storageOption); default: return ini_get('session.' . $storageOption); } } /** * Set session.save_handler * * @param string $phpSaveHandler * @return SessionConfig * @throws Exception\InvalidArgumentException */ public function setPhpSaveHandler($phpSaveHandler) { $phpSaveHandler = (string) $phpSaveHandler; set_error_handler(array($this, 'handleError')); ini_set('session.save_handler', $phpSaveHandler); restore_error_handler(); if ($this->phpErrorCode >= E_WARNING) { throw new Exception\InvalidArgumentException( 'Invalid save handler specified: ' . $this->phpErrorMessage ); } $this->setOption('save_handler', $phpSaveHandler); return $this; } /** * Set session.save_path * * @param string $savePath * @return SessionConfig * @throws Exception\InvalidArgumentException on invalid path */ public function setSavePath($savePath) { if ($this->getOption('save_handler') == 'files') { parent::setSavePath($savePath); } $this->savePath = $savePath; $this->setOption('save_path', $savePath); return $this; } /** * Set session.serialize_handler * * @param string $serializeHandler * @return SessionConfig * @throws Exception\InvalidArgumentException */ public function setSerializeHandler($serializeHandler) { $serializeHandler = (string) $serializeHandler; set_error_handler(array($this, 'handleError')); ini_set('session.serialize_handler', $serializeHandler); restore_error_handler(); if ($this->phpErrorCode >= E_WARNING) { throw new Exception\InvalidArgumentException('Invalid serialize handler specified'); } $this->serializeHandler = (string) $serializeHandler; return $this; } // session.cache_limiter /** * Set cache limiter * * @param $cacheLimiter * @return SessionConfig * @throws Exception\InvalidArgumentException */ public function setCacheLimiter($cacheLimiter) { $cacheLimiter = (string) $cacheLimiter; if (!in_array($cacheLimiter, $this->validCacheLimiters)) { throw new Exception\InvalidArgumentException('Invalid cache limiter provided'); } $this->setOption('cache_limiter', $cacheLimiter); ini_set('session.cache_limiter', $cacheLimiter); return $this; } /** * Set session.hash_function * * @param string|int $hashFunction * @return SessionConfig * @throws Exception\InvalidArgumentException */ public function setHashFunction($hashFunction) { $hashFunction = (string) $hashFunction; $validHashFunctions = $this->getHashFunctions(); if (!in_array($hashFunction, $validHashFunctions, true)) { throw new Exception\InvalidArgumentException('Invalid hash function provided'); } $this->setOption('hash_function', $hashFunction); ini_set('session.hash_function', $hashFunction); return $this; } /** * Set session.hash_bits_per_character * * @param int $hashBitsPerCharacter * @return SessionConfig * @throws Exception\InvalidArgumentException */ public function setHashBitsPerCharacter($hashBitsPerCharacter) { if (!is_numeric($hashBitsPerCharacter) || !in_array($hashBitsPerCharacter, $this->validHashBitsPerCharacters) ) { throw new Exception\InvalidArgumentException('Invalid hash bits per character provided'); } $hashBitsPerCharacter = (int) $hashBitsPerCharacter; $this->setOption('hash_bits_per_character', $hashBitsPerCharacter); ini_set('session.hash_bits_per_character', $hashBitsPerCharacter); return $this; } /** * Retrieve list of valid hash functions * * @return array */ protected function getHashFunctions() { if (empty($this->validHashFunctions)) { /** * @link http://php.net/manual/en/session.configuration.php#ini.session.hash-function * "0" and "1" refer to MD5-128 and SHA1-160, respectively, and are * valid in addition to whatever is reported by hash_algos() */ $this->validHashFunctions = array('0', '1') + hash_algos(); } return $this->validHashFunctions; } /** * Handle PHP errors * * @param int $code * @param string $message * @return void */ protected function handleError($code, $message) { $this->phpErrorCode = $code; $this->phpErrorMessage = $message; } } |