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_Session_Abstract extends Mage_Core_Model_Session_Abstract_Varien
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_NODE_SESSION_SAVE = 'global/session_save';
00041 const XML_NODE_SESSION_SAVE_PATH = 'global/session_save_path';
00042
00043 const XML_PATH_USE_REMOTE_ADDR = 'web/session/use_remote_addr';
00044 const XML_PATH_USE_HTTP_VIA = 'web/session/use_http_via';
00045 const XML_PATH_USE_X_FORWARDED = 'web/session/use_http_x_forwarded_for';
00046 const XML_PATH_USE_USER_AGENT = 'web/session/use_http_user_agent';
00047
00048 const XML_NODE_USET_AGENT_SKIP = 'global/session/validation/http_user_agent_skip';
00049 const XML_PATH_LOG_EXCEPTION_FILE = 'dev/log/exception_file';
00050
00051 const SESSION_ID_QUERY_PARAM = 'SID';
00052
00053
00054
00055
00056
00057
00058 protected static $_urlHostCache = array();
00059
00060
00061
00062
00063
00064
00065 protected static $_encryptedSessionId;
00066
00067
00068
00069
00070
00071
00072 protected $_skipSessionIdFlag = false;
00073
00074
00075
00076
00077
00078
00079
00080
00081 public function init($namespace, $sessionName=null)
00082 {
00083 parent::init($namespace, $sessionName);
00084 $this->addHost(true);
00085 return $this;
00086 }
00087
00088
00089
00090
00091
00092
00093 public function getCookieDomain()
00094 {
00095 return $this->getCookie()->getDomain();
00096 }
00097
00098
00099
00100
00101
00102
00103 public function getCookiePath()
00104 {
00105 return $this->getCookie()->getPath();
00106 }
00107
00108
00109
00110
00111
00112
00113 public function getCookieLifetime()
00114 {
00115 return $this->getCookie()->getLifetime();
00116 }
00117
00118
00119
00120
00121
00122
00123 public function useValidateRemoteAddr()
00124 {
00125 $use = Mage::getStoreConfig(self::XML_PATH_USE_REMOTE_ADDR);
00126 if (is_null($use)) {
00127 return parent::useValidateRemoteAddr();
00128 }
00129 return (bool)$use;
00130 }
00131
00132
00133
00134
00135
00136
00137 public function useValidateHttpVia()
00138 {
00139 $use = Mage::getStoreConfig(self::XML_PATH_USE_HTTP_VIA);
00140 if (is_null($use)) {
00141 return parent::useValidateHttpVia();
00142 }
00143 return (bool)$use;
00144 }
00145
00146
00147
00148
00149
00150
00151 public function useValidateHttpXForwardedFor()
00152 {
00153 $use = Mage::getStoreConfig(self::XML_PATH_USE_X_FORWARDED);
00154 if (is_null($use)) {
00155 return parent::useValidateHttpXForwardedFor();
00156 }
00157 return (bool)$use;
00158 }
00159
00160
00161
00162
00163
00164
00165 public function useValidateHttpUserAgent()
00166 {
00167 $use = Mage::getStoreConfig(self::XML_PATH_USE_USER_AGENT);
00168 if (is_null($use)) {
00169 return parent::useValidateHttpUserAgent();
00170 }
00171 return (bool)$use;
00172 }
00173
00174
00175
00176
00177
00178
00179 public function getValidateHttpUserAgentSkip()
00180 {
00181 $userAgents = array();
00182 $skip = Mage::getConfig()->getNode(self::XML_NODE_USET_AGENT_SKIP);
00183 foreach ($skip->children() as $userAgent) {
00184 $userAgents[] = (string)$userAgent;
00185 }
00186 return $userAgents;
00187 }
00188
00189
00190
00191
00192
00193
00194
00195 public function getMessages($clear=false)
00196 {
00197 if (!$this->getData('messages')) {
00198 $this->setMessages(Mage::getModel('core/message_collection'));
00199 }
00200
00201 if ($clear) {
00202 $messages = clone $this->getData('messages');
00203 $this->getData('messages')->clear();
00204 return $messages;
00205 }
00206 return $this->getData('messages');
00207 }
00208
00209
00210
00211
00212
00213
00214
00215
00216 public function addException(Exception $exception, $alternativeText)
00217 {
00218
00219 $message = sprintf('Exception message: %s%sTrace: %s',
00220 $exception->getMessage(),
00221 "\n",
00222 $exception->getTraceAsString());
00223 $file = Mage::getStoreConfig(self::XML_PATH_LOG_EXCEPTION_FILE);
00224 Mage::log($message, Zend_Log::DEBUG, $file);
00225
00226 $this->addMessage(Mage::getSingleton('core/message')->error($alternativeText));
00227 return $this;
00228 }
00229
00230
00231
00232
00233
00234
00235
00236 public function addMessage(Mage_Core_Model_Message_Abstract $message)
00237 {
00238 $this->getMessages()->add($message);
00239 return $this;
00240 }
00241
00242
00243
00244
00245
00246
00247
00248 public function addError($message)
00249 {
00250 $this->addMessage(Mage::getSingleton('core/message')->error($message));
00251 return $this;
00252 }
00253
00254
00255
00256
00257
00258
00259
00260 public function addWarning($message)
00261 {
00262 $this->addMessage(Mage::getSingleton('core/message')->warning($message));
00263 return $this;
00264 }
00265
00266
00267
00268
00269
00270
00271
00272 public function addNotice($message)
00273 {
00274 $this->addMessage(Mage::getSingleton('core/message')->notice($message));
00275 return $this;
00276 }
00277
00278
00279
00280
00281
00282
00283
00284 public function addSuccess($message)
00285 {
00286 $this->addMessage(Mage::getSingleton('core/message')->success($message));
00287 return $this;
00288 }
00289
00290
00291
00292
00293
00294
00295
00296 public function addMessages($messages)
00297 {
00298 if (is_array($messages)) {
00299 foreach ($messages as $message) {
00300 $this->addMessage($message);
00301 }
00302 }
00303 return $this;
00304 }
00305
00306
00307
00308
00309
00310
00311
00312 public function setSessionId($id=null)
00313 {
00314 if (is_null($id)) {
00315 $_queryParam = $this->getSessionIdQueryParam();
00316 if (isset($_GET[$_queryParam])) {
00317 $id = $_GET[$_queryParam];
00318
00319
00320
00321
00322
00323
00324 }
00325 }
00326
00327 $this->addHost(true);
00328 return parent::setSessionId($id);
00329 }
00330
00331
00332
00333
00334
00335
00336
00337
00338 public function getEncryptedSessionId()
00339 {
00340 if (!self::$_encryptedSessionId) {
00341
00342
00343
00344
00345
00346 self::$_encryptedSessionId = $this->getSessionId();
00347 }
00348 return self::$_encryptedSessionId;
00349 }
00350
00351 public function getSessionIdQueryParam()
00352 {
00353 $_sessionName = $this->getSessionName();
00354 if ($_sessionName && $queryParam = (string)Mage::getConfig()->getNode($_sessionName . '/session/query_param')) {
00355 return $queryParam;
00356 }
00357 return self::SESSION_ID_QUERY_PARAM;
00358 }
00359
00360
00361
00362
00363
00364
00365
00366 public function setSkipSessionIdFlag($flag)
00367 {
00368 $this->_skipSessionIdFlag = $flag;
00369 return $this;
00370 }
00371
00372
00373
00374
00375
00376
00377 public function getSkipSessionIdFlag()
00378 {
00379 return $this->_skipSessionIdFlag;
00380 }
00381
00382
00383
00384
00385
00386
00387
00388 public function getSessionIdForHost($urlHost)
00389 {
00390 if ($this->getSkipSessionIdFlag() === true) {
00391 return '';
00392 }
00393
00394 if (!$httpHost = Mage::app()->getFrontController()->getRequest()->getHttpHost()) {
00395 return '';
00396 }
00397
00398 $urlHostArr = explode('/', $urlHost, 4);
00399 if (!empty($urlHostArr[2])) {
00400 $urlHost = $urlHostArr[2];
00401 }
00402
00403 if (!isset(self::$_urlHostCache[$urlHost])) {
00404 $urlHostArr = explode(':', $urlHost);
00405 $urlHost = $urlHostArr[0];
00406
00407 if ($httpHost !== $urlHost && !$this->isValidForHost($urlHost)) {
00408 $sessionId = $this->getEncryptedSessionId();
00409 } else {
00410 $sessionId = '';
00411 }
00412 self::$_urlHostCache[$urlHost] = $sessionId;
00413 }
00414 return self::$_urlHostCache[$urlHost];
00415 }
00416
00417
00418
00419
00420
00421
00422
00423 public function isValidForHost($host)
00424 {
00425 $hostArr = explode(':', $host);
00426 $hosts = $this->getSessionHosts();
00427 return (!empty($hosts[$hostArr[0]]));
00428 }
00429
00430
00431
00432
00433
00434
00435
00436 public function addHost($host)
00437 {
00438 if ($host === true) {
00439 if (!$host = Mage::app()->getFrontController()->getRequest()->getHttpHost()) {
00440 return $this;
00441 }
00442 }
00443
00444 if (!$host) {
00445 return $this;
00446 }
00447
00448 $hosts = $this->getSessionHosts();
00449 $hosts[$host] = true;
00450 $this->setSessionHosts($hosts);
00451 return $this;
00452 }
00453
00454
00455
00456
00457
00458
00459 public function getSessionHosts()
00460 {
00461 return $this->getData('session_hosts');
00462 }
00463
00464
00465
00466
00467
00468
00469 public function getSessionSaveMethod()
00470 {
00471 if (Mage::isInstalled() && $sessionSave = Mage::getConfig()->getNode(self::XML_NODE_SESSION_SAVE)) {
00472 return $sessionSave;
00473 }
00474 return parent::getSessionSaveMethod();
00475 }
00476
00477
00478
00479
00480
00481
00482 public function getSessionSavePath()
00483 {
00484 if (Mage::isInstalled() && $sessionSavePath = Mage::getConfig()->getNode(self::XML_NODE_SESSION_SAVE_PATH)) {
00485 return $sessionSavePath;
00486 }
00487 return parent::getSessionSavePath();
00488 }
00489 }