00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Core_Model_Cookie
00036 {
00037 const XML_PATH_COOKIE_DOMAIN = 'web/cookie/cookie_domain';
00038 const XML_PATH_COOKIE_PATH = 'web/cookie/cookie_path';
00039 const XML_PATH_COOKIE_LIFETIME = 'web/cookie/cookie_lifetime';
00040 const XML_PATH_COOKIE_HTTPONLY = 'web/cookie/cookie_httponly';
00041
00042 protected $_lifetime;
00043
00044
00045
00046
00047
00048
00049 protected $_store;
00050
00051
00052
00053
00054
00055
00056
00057 public function setStore($store)
00058 {
00059 $this->_store = Mage::app()->getStore($store);
00060 return $this;
00061 }
00062
00063
00064
00065
00066
00067
00068 public function getStore()
00069 {
00070 if (is_null($this->_store)) {
00071 $this->_store = Mage::app()->getStore();
00072 }
00073 return $this->_store;
00074 }
00075
00076
00077
00078
00079
00080
00081 protected function _getRequest()
00082 {
00083 return Mage::app()->getRequest();
00084 }
00085
00086
00087
00088
00089
00090
00091 protected function _getResponse()
00092 {
00093 return Mage::app()->getResponse();
00094 }
00095
00096
00097
00098
00099
00100
00101 public function getDomain()
00102 {
00103 $domain = Mage::getStoreConfig(self::XML_PATH_COOKIE_DOMAIN, $this->getStore());
00104 if (empty($domain)) {
00105 $domain = $this->_getRequest()->getHttpHost();
00106 }
00107 return $domain;
00108 }
00109
00110
00111
00112
00113
00114
00115 public function getPath()
00116 {
00117 $path = Mage::getStoreConfig(self::XML_PATH_COOKIE_PATH, $this->getStore());
00118 if (empty($path)) {
00119 $path = $this->_getRequest()->getBasePath();
00120 }
00121 return $path;
00122 }
00123
00124
00125
00126
00127
00128
00129 public function getLifetime()
00130 {
00131 if (null !== $this->_lifetime) {
00132 $lifetime = $this->_lifetime;
00133 }
00134 else {
00135 $lifetime = Mage::getStoreConfig(self::XML_PATH_COOKIE_LIFETIME, $this->getStore());
00136 }
00137 if (!is_numeric($lifetime)) {
00138 $lifetime = 3600;
00139 }
00140 return $lifetime;
00141 }
00142
00143
00144
00145
00146
00147
00148
00149 public function setLifetime($lifetime)
00150 {
00151 $this->_lifetime = (int)$lifetime;
00152 return $this;
00153 }
00154
00155
00156
00157
00158
00159
00160 public function getHttponly()
00161 {
00162 $httponly = Mage::getStoreConfig(self::XML_PATH_COOKIE_HTTPONLY, $this->getStore());
00163 if (is_null($httponly)) {
00164 return null;
00165 }
00166 return (bool)$httponly;
00167 }
00168
00169
00170
00171
00172
00173
00174
00175 public function isSecure()
00176 {
00177 if ($this->getStore()->isAdmin()) {
00178 return $this->_getRequest()->isSecure();
00179 }
00180 return false;
00181 }
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 public function set($name, $value, $period = null, $path = null, $domain = null, $secure = null, $httponly = null)
00195 {
00196
00197
00198
00199 if (!$this->_getResponse()->canSendHeaders(false)) {
00200 return $this;
00201 }
00202
00203 if ($period === true) {
00204 $period = 3600 * 24 * 365;
00205 } elseif (is_null($period)) {
00206 $period = $this->getLifetime();
00207 }
00208
00209 if ($period == 0) {
00210 $expire = 0;
00211 }
00212 else {
00213 $expire = time() + $period;
00214 }
00215 if (is_null($path)) {
00216 $path = $this->getPath();
00217 }
00218 if (is_null($domain)) {
00219 $domain = $this->getDomain();
00220 }
00221 if (is_null($secure)) {
00222 $secure = $this->isSecure();
00223 }
00224 if (is_null($httponly)) {
00225 $httponly = $this->getHttponly();
00226 }
00227
00228 setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
00229
00230 return $this;
00231 }
00232
00233
00234
00235
00236
00237
00238
00239 public function get($name = null)
00240 {
00241 return $this->_getRequest()->getCookie($name, false);
00242 }
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254 public function delete($name, $path = null, $domain = null, $secure = null, $httponly = null)
00255 {
00256
00257
00258
00259 if (!$this->_getResponse()->canSendHeaders(false)) {
00260 return $this;
00261 }
00262
00263 if (is_null($path)) {
00264 $path = $this->getPath();
00265 }
00266 if (is_null($domain)) {
00267 $domain = $this->getDomain();
00268 }
00269 if (is_null($secure)) {
00270 $secure = $this->isSecure();
00271 }
00272 if (is_null($httponly)) {
00273 $httponly = $this->getHttponly();
00274 }
00275
00276 setcookie($name, null, null, $path, $domain, $secure, $httponly);
00277 return $this;
00278 }
00279 }