File indexing completed on 2024-11-17 05:19:45
0001 <?php 0002 0003 /** 0004 * Flooer Framework 0005 * 0006 * LICENSE: BSD License (2 Clause) 0007 * 0008 * @category Flooer 0009 * @package Flooer_Http 0010 * @subpackage Cookie 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 * $cookie = new Flooer_Http_Cookie(); 0021 * $cookie->setExpire(time() + 3600); 0022 * $cookie->setPath('/'); 0023 * $cookie->key = $value; 0024 */ 0025 0026 /** 0027 * HTTP cookie class 0028 * 0029 * @category Flooer 0030 * @package Flooer_Http 0031 * @subpackage Cookie 0032 * @author Akira Ohgaki <akiraohgaki@gmail.com> 0033 */ 0034 class Flooer_Http_Cookie 0035 { 0036 0037 /** 0038 * Configuration options 0039 * 0040 * @var array 0041 */ 0042 protected $_config = array( 0043 'importPredefinedVars' => true, 0044 'expire' => 0, 0045 'path' => null, 0046 'domain' => null, 0047 'secure' => false, 0048 'httponly' => false 0049 ); 0050 0051 /** 0052 * Overloaded properties 0053 * 0054 * @var array 0055 */ 0056 protected $_properties = array(); 0057 0058 /** 0059 * Constructor 0060 * 0061 * @param array $config 0062 * @return void 0063 */ 0064 public function __construct(array $config = null) 0065 { 0066 if ($config) { 0067 $this->_config = $config + $this->_config; 0068 } 0069 if ($this->_config['importPredefinedVars']) { 0070 $this->importPredefinedVars(); 0071 } 0072 } 0073 0074 /** 0075 * Magic method to set a property 0076 * 0077 * @param string $key 0078 * @param mixed $value 0079 * @return void 0080 */ 0081 public function __set($key, $value) 0082 { 0083 $bool = setcookie( 0084 $key, 0085 $value, 0086 $this->_config['expire'], 0087 $this->_config['path'], 0088 $this->_config['domain'], 0089 $this->_config['secure'], 0090 $this->_config['httponly'] 0091 ); 0092 if ($bool) { 0093 $this->_properties[$key] = $value; 0094 } 0095 } 0096 0097 /** 0098 * Magic method to get a property 0099 * 0100 * @param string $key 0101 * @return mixed 0102 */ 0103 public function __get($key) 0104 { 0105 if (isset($this->_properties[$key])) { 0106 return $this->_properties[$key]; 0107 } 0108 return null; 0109 } 0110 0111 /** 0112 * Magic method to check a property 0113 * 0114 * @param string $key 0115 * @return bool 0116 */ 0117 public function __isset($key) 0118 { 0119 return isset($this->_properties[$key]); 0120 } 0121 0122 /** 0123 * Magic method to unset a property 0124 * 0125 * @param string $key 0126 * @return void 0127 */ 0128 public function __unset($key) 0129 { 0130 $bool = setcookie( 0131 $key, 0132 '', 0133 time() - 60, 0134 $this->_config['path'], 0135 $this->_config['domain'], 0136 $this->_config['secure'], 0137 $this->_config['httponly'] 0138 ); 0139 if ($bool) { 0140 unset($this->_properties[$key]); 0141 } 0142 } 0143 0144 /** 0145 * Import a predefined variables 0146 * 0147 * @return void 0148 */ 0149 public function importPredefinedVars() 0150 { 0151 if (!empty($_COOKIE)) { 0152 $this->_properties += $_COOKIE; 0153 } 0154 } 0155 0156 /** 0157 * Encode a data 0158 * 0159 * @param mixed $data 0160 * @return string Encoded data 0161 */ 0162 public function encode($data) 0163 { 0164 return base64_encode(serialize($data)); 0165 } 0166 0167 /** 0168 * Decode a data 0169 * 0170 * @param string $data Encoded data 0171 * @return mixed 0172 */ 0173 public function decode($data) 0174 { 0175 return unserialize(base64_decode($data)); 0176 } 0177 0178 /** 0179 * Set a cookie expires 0180 * 0181 * @param int $expire 0182 * @return void 0183 */ 0184 public function setExpire($expire) 0185 { 0186 $this->_config['expire'] = $expire; 0187 } 0188 0189 /** 0190 * Get a cookie expires 0191 * 0192 * @return int 0193 */ 0194 public function getExpire() 0195 { 0196 return $this->_config['expire']; 0197 } 0198 0199 /** 0200 * Set a path on the server 0201 * 0202 * @param string $path 0203 * @return void 0204 */ 0205 public function setPath($path) 0206 { 0207 $this->_config['path'] = $path; 0208 } 0209 0210 /** 0211 * Get a path on the server 0212 * 0213 * @return string 0214 */ 0215 public function getPath() 0216 { 0217 return $this->_config['path']; 0218 } 0219 0220 /** 0221 * Set a domain 0222 * 0223 * @param string $domain 0224 * @return void 0225 */ 0226 public function setDomain($domain) 0227 { 0228 $this->_config['domain'] = $domain; 0229 } 0230 0231 /** 0232 * Get a domain 0233 * 0234 * @return string 0235 */ 0236 public function getDomain() 0237 { 0238 return $this->_config['domain']; 0239 } 0240 0241 /** 0242 * Set a secure option 0243 * 0244 * @param bool $secure 0245 * @return void 0246 */ 0247 public function setSecure($secure) 0248 { 0249 $this->_config['secure'] = $secure; 0250 } 0251 0252 /** 0253 * Get a secure option 0254 * 0255 * @return bool 0256 */ 0257 public function getSecure() 0258 { 0259 return $this->_config['secure']; 0260 } 0261 0262 /** 0263 * Set an HTTP access option 0264 * 0265 * @param bool $httponly 0266 * @return void 0267 */ 0268 public function setHttponly($httponly) 0269 { 0270 $this->_config['httponly'] = $httponly; 0271 } 0272 0273 /** 0274 * Get an HTTP access option 0275 * 0276 * @return bool 0277 */ 0278 public function getHttponly() 0279 { 0280 return $this->_config['httponly']; 0281 } 0282 0283 }