File indexing completed on 2025-03-09 05:22:13
0001 <?php 0002 0003 /** 0004 * Flooer Framework 0005 * 0006 * LICENSE: BSD License (2 Clause) 0007 * 0008 * @category Flooer 0009 * @package Flooer_Http 0010 * @subpackage Session 0011 * @author Akira Ohgaki <akiraohgaki@gmail.com> 0012 * @copyright Akira Ohgaki 0013 * @license https://opensource.org/licenses/BSD-2-Clause BSD License (2 Clause) 0014 * @link https://github.com/akiraohgaki/flooer 0015 */ 0016 0017 /** 0018 * Usage 0019 * 0020 * $session = new Flooer_Http_Session(); 0021 * $session->setName('SessionID'); 0022 * $session->start(); 0023 * $session->key = $value; 0024 */ 0025 0026 /** 0027 * Session class 0028 * 0029 * @category Flooer 0030 * @package Flooer_Http 0031 * @subpackage Session 0032 * @author Akira Ohgaki <akiraohgaki@gmail.com> 0033 */ 0034 class Flooer_Http_Session 0035 { 0036 0037 /** 0038 * Configuration options 0039 * 0040 * @var array 0041 */ 0042 protected $_config = array( 0043 'importPredefinedVars' => true, 0044 'start' => false, 0045 'moduleName' => null, 0046 'savePath' => null, 0047 'saveHandlerOpen' => null, 0048 'saveHandlerClose' => null, 0049 'saveHandlerRead' => null, 0050 'saveHandlerWrite' => null, 0051 'saveHandlerDestroy' => null, 0052 'saveHandlerGc' => null, 0053 'cacheParamsLimiter' => null, 0054 'cacheParamsExpire' => null, 0055 'cookieParamsLifetime' => null, 0056 'cookieParamsPath' => null, 0057 'cookieParamsDomain' => null, 0058 'cookieParamsSecure' => false, 0059 'cookieParamsHttponly' => false, 0060 'name' => null, 0061 'id' => null 0062 ); 0063 0064 /** 0065 * Overloaded properties 0066 * 0067 * @var array 0068 */ 0069 protected $_properties = array(); 0070 0071 /** 0072 * Constructor 0073 * 0074 * @param array $config 0075 * @return void 0076 */ 0077 public function __construct(array $config = null) 0078 { 0079 if ($config) { 0080 $this->_config = $config + $this->_config; 0081 } 0082 if ($this->_config['moduleName']) { 0083 $this->setModuleName($this->_config['moduleName']); 0084 } 0085 if ($this->_config['savePath']) { 0086 $this->setSavePath($this->_config['savePath']); 0087 } 0088 if ($this->_config['saveHandlerOpen'] 0089 || $this->_config['saveHandlerClose'] 0090 || $this->_config['saveHandlerRead'] 0091 || $this->_config['saveHandlerWrite'] 0092 || $this->_config['saveHandlerDestroy'] 0093 || $this->_config['saveHandlerGc'] 0094 ) { 0095 $this->setSaveHandler( 0096 $this->_config['saveHandlerOpen'], 0097 $this->_config['saveHandlerClose'], 0098 $this->_config['saveHandlerRead'], 0099 $this->_config['saveHandlerWrite'], 0100 $this->_config['saveHandlerDestroy'], 0101 $this->_config['saveHandlerGc'] 0102 ); 0103 } 0104 if ($this->_config['cacheParamsLimiter']) { 0105 $this->setCacheParams( 0106 $this->_config['cacheParamsLimiter'], 0107 $this->_config['cacheParamsExpire'] 0108 ); 0109 } 0110 if ($this->_config['cookieParamsLifetime']) { 0111 $this->setCookieParams( 0112 $this->_config['cookieParamsLifetime'], 0113 $this->_config['cookieParamsPath'], 0114 $this->_config['cookieParamsDomain'], 0115 $this->_config['cookieParamsSecure'], 0116 $this->_config['cookieParamsHttponly'] 0117 ); 0118 } 0119 if ($this->_config['name']) { 0120 $this->setName($this->_config['name']); 0121 } 0122 if ($this->_config['id']) { 0123 $this->setId($this->_config['id']); 0124 } 0125 if ($this->_config['start']) { 0126 $this->start($this->_config['importPredefinedVars']); 0127 } 0128 } 0129 0130 /** 0131 * Destructor 0132 * 0133 * @return void 0134 */ 0135 public function __destruct() 0136 { 0137 if (isset($_SESSION)) { 0138 $this->writeClose(); 0139 } 0140 } 0141 0142 /** 0143 * Magic method to set a property 0144 * 0145 * @param string $key 0146 * @param mixed $value 0147 * @return void 0148 */ 0149 public function __set($key, $value) 0150 { 0151 $this->_properties[$key] = $value; 0152 } 0153 0154 /** 0155 * Magic method to get a property 0156 * 0157 * @param string $key 0158 * @return mixed 0159 */ 0160 public function __get($key) 0161 { 0162 if (isset($this->_properties[$key])) { 0163 return $this->_properties[$key]; 0164 } 0165 return null; 0166 } 0167 0168 /** 0169 * Magic method to check a property 0170 * 0171 * @param string $key 0172 * @return bool 0173 */ 0174 public function __isset($key) 0175 { 0176 return isset($this->_properties[$key]); 0177 } 0178 0179 /** 0180 * Magic method to unset a property 0181 * 0182 * @param string $key 0183 * @return void 0184 */ 0185 public function __unset($key) 0186 { 0187 unset($this->_properties[$key]); 0188 } 0189 0190 /** 0191 * Import a predefined variables 0192 * 0193 * @return void 0194 */ 0195 public function importPredefinedVars() 0196 { 0197 if (!empty($_SESSION)) { 0198 $this->_properties += $_SESSION; 0199 } 0200 } 0201 0202 /** 0203 * Start a session 0204 * 0205 * @param bool $importPredefinedVars 0206 * @return bool 0207 */ 0208 public function start($importPredefinedVars = true) 0209 { 0210 $bool = session_start(); 0211 if ($bool && $importPredefinedVars) { 0212 $this->importPredefinedVars(); 0213 } 0214 return $bool; 0215 } 0216 0217 /** 0218 * Write and close a session 0219 * 0220 * @return void 0221 */ 0222 public function writeClose() 0223 { 0224 $_SESSION = $this->_properties; 0225 session_write_close(); 0226 } 0227 0228 /** 0229 * Regenerate a session ID 0230 * 0231 * @param bool $deleteOldSession 0232 * @return bool 0233 */ 0234 public function regenerateId($deleteOldSession = true) 0235 { 0236 return session_regenerate_id($deleteOldSession); 0237 } 0238 0239 /** 0240 * Destroy a session data 0241 * 0242 * @param bool $removeCookie 0243 * @param bool $removeVars 0244 * @return bool 0245 */ 0246 public function destroy($removeCookie = true, $removeVars = true) 0247 { 0248 if ($removeVars) { 0249 $this->_properties = array(); 0250 $_SESSION = array(); 0251 } 0252 if ($removeCookie) { 0253 $name = session_name(); 0254 $params = session_get_cookie_params(); 0255 if (isset($_COOKIE[$name])) { 0256 setcookie( 0257 $name, 0258 '', 0259 time() - 60, 0260 $params['path'], 0261 $params['domain'], 0262 $params['secure'], 0263 $params['httponly'] 0264 ); 0265 } 0266 } 0267 return session_destroy(); 0268 } 0269 0270 /** 0271 * Encode a session data 0272 * 0273 * @return string Encoded session data 0274 */ 0275 public function encode() 0276 { 0277 $_SESSION = $this->_properties; 0278 session_write_close(); 0279 return session_encode(); 0280 } 0281 0282 /** 0283 * Decode a session data 0284 * 0285 * @param string $data Encoded session data 0286 * @return bool 0287 */ 0288 public function decode($data) 0289 { 0290 $bool = session_decode($data); 0291 if ($bool) { 0292 $this->_properties = $_SESSION; 0293 } 0294 return $bool; 0295 } 0296 0297 /** 0298 * Set a session module name 0299 * 0300 * @param string $module 0301 * @return void 0302 */ 0303 public function setModuleName($module) 0304 { 0305 session_module_name($module); 0306 } 0307 0308 /** 0309 * Get a session module name 0310 * 0311 * @return string 0312 */ 0313 public function getModuleName() 0314 { 0315 return session_module_name(); 0316 } 0317 0318 /** 0319 * Set a session save path 0320 * 0321 * @param string $path 0322 * @return void 0323 */ 0324 public function setSavePath($path) 0325 { 0326 session_save_path($path); 0327 } 0328 0329 /** 0330 * Get a session save path 0331 * 0332 * @return string 0333 */ 0334 public function getSavePath() 0335 { 0336 return session_save_path(); 0337 } 0338 0339 /** 0340 * Set a session save handler 0341 * 0342 * @param callback $open 0343 * @param callback $close 0344 * @param callback $read 0345 * @param callback $write 0346 * @param callback $destroy 0347 * @param callback $gc 0348 * @return bool 0349 */ 0350 public function setSaveHandler($open = null, $close = null, $read = null, $write = null, $destroy = null, $gc = null) 0351 { 0352 return session_set_save_handler( 0353 $open, 0354 $close, 0355 $read, 0356 $write, 0357 $destroy, 0358 $gc 0359 ); 0360 } 0361 0362 /** 0363 * Set a session cache parameters 0364 * 0365 * @param string $limiter Available values to session_cache_limiter() 0366 * @param int $expire Minutes 0367 * @return void 0368 */ 0369 public function setCacheParams($limiter, $expire = null) 0370 { 0371 session_cache_limiter($limiter); 0372 if ($limiter != 'nocache' && $expire !== null) { 0373 session_cache_expire($expire); 0374 } 0375 } 0376 0377 /** 0378 * Get a session cache parameters 0379 * 0380 * @return array 0381 */ 0382 public function getCacheParams() 0383 { 0384 $cacheParams = array( 0385 'limiter' => session_cache_limiter(), 0386 'expire' => session_cache_expire() 0387 ); 0388 return $cacheParams; 0389 } 0390 0391 /** 0392 * Set a session cookie parameters 0393 * 0394 * @param int $lifetime 0395 * @param string $path 0396 * @param string $domain 0397 * @param bool $secure 0398 * @param bool $httponly 0399 * @return void 0400 */ 0401 public function setCookieParams($lifetime, $path = null, $domain = null, $secure = false, $httponly = false) 0402 { 0403 session_set_cookie_params( 0404 $lifetime, 0405 $path, 0406 $domain, 0407 $secure, 0408 $httponly 0409 ); 0410 } 0411 0412 /** 0413 * Get a session cookie parameters 0414 * 0415 * @return array 0416 */ 0417 public function getCookieParams() 0418 { 0419 return session_get_cookie_params(); 0420 } 0421 0422 /** 0423 * Set a session name 0424 * 0425 * @param string $name 0426 * @return void 0427 */ 0428 public function setName($name) 0429 { 0430 session_name($name); 0431 } 0432 0433 /** 0434 * Get a session name 0435 * 0436 * @return string 0437 */ 0438 public function getName() 0439 { 0440 return session_name(); 0441 } 0442 0443 /** 0444 * Set a session ID 0445 * 0446 * @param string $id 0447 * @return void 0448 */ 0449 public function setId($id) 0450 { 0451 session_id($id); 0452 } 0453 0454 /** 0455 * Get a session ID 0456 * 0457 * @return string 0458 */ 0459 public function getId() 0460 { 0461 return session_id(); 0462 } 0463 0464 }