PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Core/Model/Session/Abstract/Varien.php

https://github.com/su2921iw/ma
PHP | 425 lines | 228 code | 45 blank | 152 comment | 38 complexity | 808718284770b83a97598f8961aba613 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Core
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. class Mage_Core_Model_Session_Abstract_Varien extends Varien_Object
  27. {
  28. const VALIDATOR_KEY = '_session_validator_data';
  29. const VALIDATOR_HTTP_USER_AGENT_KEY = 'http_user_agent';
  30. const VALIDATOR_HTTP_X_FORVARDED_FOR_KEY = 'http_x_forwarded_for';
  31. const VALIDATOR_HTTP_VIA_KEY = 'http_via';
  32. const VALIDATOR_REMOTE_ADDR_KEY = 'remote_addr';
  33. /**
  34. * Conigure and start session
  35. *
  36. * @param string $sessionName
  37. * @return Mage_Core_Model_Session_Abstract_Varien
  38. */
  39. public function start($sessionName=null)
  40. {
  41. if (isset($_SESSION)) {
  42. return $this;
  43. }
  44. switch($this->getSessionSaveMethod()) {
  45. case 'db':
  46. ini_set('session.save_handler', 'user');
  47. $sessionResource = Mage::getResourceSingleton('core/session');
  48. /* @var $sessionResource Mage_Core_Model_Mysql4_Session */
  49. $sessionResource->setSaveHandler();
  50. break;
  51. case 'memcache':
  52. ini_set('session.save_handler', 'memcache');
  53. session_save_path($this->getSessionSavePath());
  54. break;
  55. case 'eaccelerator':
  56. ini_set('session.save_handler', 'eaccelerator');
  57. break;
  58. default:
  59. session_module_name($this->getSessionSaveMethod());
  60. if (is_writable($this->getSessionSavePath())) {
  61. session_save_path($this->getSessionSavePath());
  62. }
  63. break;
  64. }
  65. $cookie = $this->getCookie();
  66. if (Mage::app()->getStore()->isAdmin()) {
  67. $adminSessionLifetime = (int)Mage::getStoreConfig('admin/security/session_cookie_lifetime');
  68. if ($adminSessionLifetime > 60) {
  69. $cookie->setLifetime($adminSessionLifetime);
  70. }
  71. }
  72. // session cookie params
  73. $cookieParams = array(
  74. 'lifetime' => $cookie->getLifetime(),
  75. 'path' => $cookie->getPath(),
  76. 'domain' => $cookie->getConfigDomain(),
  77. 'secure' => $cookie->isSecure(),
  78. 'httponly' => $cookie->getHttponly()
  79. );
  80. if (!$cookieParams['httponly']) {
  81. unset($cookieParams['httponly']);
  82. if (!$cookieParams['secure']) {
  83. unset($cookieParams['secure']);
  84. if (!$cookieParams['domain']) {
  85. unset($cookieParams['domain']);
  86. }
  87. }
  88. }
  89. if (isset($cookieParams['domain'])) {
  90. $cookieParams['domain'] = $cookie->getDomain();
  91. }
  92. call_user_func_array('session_set_cookie_params', $cookieParams);
  93. if (!empty($sessionName)) {
  94. $this->setSessionName($sessionName);
  95. }
  96. // potential custom logic for session id (ex. switching between hosts)
  97. $this->setSessionId();
  98. Varien_Profiler::start(__METHOD__.'/start');
  99. $sessionCacheLimiter = Mage::getConfig()->getNode('global/session_cache_limiter');
  100. if ($sessionCacheLimiter) {
  101. session_cache_limiter((string)$sessionCacheLimiter);
  102. }
  103. session_start();
  104. /**
  105. * Renew cookie expiration time if session id did not change
  106. */
  107. if ($cookie->get(session_name()) == $this->getSessionId()) {
  108. $cookie->renew(session_name());
  109. }
  110. Varien_Profiler::stop(__METHOD__.'/start');
  111. return $this;
  112. }
  113. /**
  114. * Retrieve cookie object
  115. *
  116. * @return Mage_Core_Model_Cookie
  117. */
  118. public function getCookie()
  119. {
  120. return Mage::getSingleton('core/cookie');
  121. }
  122. /**
  123. * Revalidate cookie
  124. * @deprecated after 1.4 cookie renew moved to session start method
  125. * @return Mage_Core_Model_Session_Abstract_Varien
  126. */
  127. public function revalidateCookie()
  128. {
  129. return $this;
  130. }
  131. /**
  132. * Init session with namespace
  133. *
  134. * @param string $namespace
  135. * @param string $sessionName
  136. * @return Mage_Core_Model_Session_Abstract_Varien
  137. */
  138. public function init($namespace, $sessionName=null)
  139. {
  140. if (!isset($_SESSION)) {
  141. $this->start($sessionName);
  142. }
  143. if (!isset($_SESSION[$namespace])) {
  144. $_SESSION[$namespace] = array();
  145. }
  146. $this->_data = &$_SESSION[$namespace];
  147. $this->validate();
  148. $this->revalidateCookie();
  149. return $this;
  150. }
  151. /**
  152. * Additional get data with clear mode
  153. *
  154. * @param string $key
  155. * @param bool $clear
  156. * @return mixed
  157. */
  158. public function getData($key='', $clear = false)
  159. {
  160. $data = parent::getData($key);
  161. if ($clear && isset($this->_data[$key])) {
  162. unset($this->_data[$key]);
  163. }
  164. return $data;
  165. }
  166. /**
  167. * Retrieve session Id
  168. *
  169. * @return string
  170. */
  171. public function getSessionId()
  172. {
  173. return session_id();
  174. }
  175. /**
  176. * Set custom session id
  177. *
  178. * @param string $id
  179. * @return Mage_Core_Model_Session_Abstract_Varien
  180. */
  181. public function setSessionId($id=null)
  182. {
  183. if (!is_null($id) && preg_match('#^[0-9a-zA-Z,-]+$#', $id)) {
  184. session_id($id);
  185. }
  186. return $this;
  187. }
  188. /**
  189. * Retrieve session name
  190. *
  191. * @return string
  192. */
  193. public function getSessionName()
  194. {
  195. return session_name();
  196. }
  197. /**
  198. * Set session name
  199. *
  200. * @param string $name
  201. * @return Mage_Core_Model_Session_Abstract_Varien
  202. */
  203. public function setSessionName($name)
  204. {
  205. session_name($name);
  206. return $this;
  207. }
  208. /**
  209. * Unset all data
  210. *
  211. * @return Mage_Core_Model_Session_Abstract_Varien
  212. */
  213. public function unsetAll()
  214. {
  215. $this->unsetData();
  216. return $this;
  217. }
  218. /**
  219. * Alias for unsetAll
  220. *
  221. * @return Mage_Core_Model_Session_Abstract_Varien
  222. */
  223. public function clear()
  224. {
  225. return $this->unsetAll();
  226. }
  227. /**
  228. * Retrieve session save method
  229. * Default files
  230. *
  231. * @return string
  232. */
  233. public function getSessionSaveMethod()
  234. {
  235. return 'files';
  236. }
  237. /**
  238. * Get sesssion save path
  239. *
  240. * @return string
  241. */
  242. public function getSessionSavePath()
  243. {
  244. return Mage::getBaseDir('session');
  245. }
  246. /**
  247. * Use REMOTE_ADDR in validator key
  248. *
  249. * @return bool
  250. */
  251. public function useValidateRemoteAddr()
  252. {
  253. return true;
  254. }
  255. /**
  256. * Use HTTP_VIA in validator key
  257. *
  258. * @return bool
  259. */
  260. public function useValidateHttpVia()
  261. {
  262. return true;
  263. }
  264. /**
  265. * Use HTTP_X_FORWARDED_FOR in validator key
  266. *
  267. * @return bool
  268. */
  269. public function useValidateHttpXForwardedFor()
  270. {
  271. return true;
  272. }
  273. /**
  274. * Use HTTP_USER_AGENT in validator key
  275. *
  276. * @return bool
  277. */
  278. public function useValidateHttpUserAgent()
  279. {
  280. return true;
  281. }
  282. /**
  283. * Retrieve skip User Agent validation strings (Flash etc)
  284. *
  285. * @return array
  286. */
  287. public function getValidateHttpUserAgentSkip()
  288. {
  289. return array();
  290. }
  291. /**
  292. * Validate session
  293. *
  294. * @param string $namespace
  295. * @return Mage_Core_Model_Session_Abstract_Varien
  296. */
  297. public function validate()
  298. {
  299. if (!isset($this->_data[self::VALIDATOR_KEY])) {
  300. $this->_data[self::VALIDATOR_KEY] = $this->getValidatorData();
  301. }
  302. else {
  303. if (!$this->_validate()) {
  304. $this->getCookie()->delete(session_name());
  305. // throw core session exception
  306. throw new Mage_Core_Model_Session_Exception('');
  307. }
  308. }
  309. return $this;
  310. }
  311. /**
  312. * Validate data
  313. *
  314. * @return bool
  315. */
  316. protected function _validate()
  317. {
  318. $sessionData = $this->_data[self::VALIDATOR_KEY];
  319. $validatorData = $this->getValidatorData();
  320. if ($this->useValidateRemoteAddr()
  321. && $sessionData[self::VALIDATOR_REMOTE_ADDR_KEY] != $validatorData[self::VALIDATOR_REMOTE_ADDR_KEY]) {
  322. return false;
  323. }
  324. if ($this->useValidateHttpVia()
  325. && $sessionData[self::VALIDATOR_HTTP_VIA_KEY] != $validatorData[self::VALIDATOR_HTTP_VIA_KEY]) {
  326. return false;
  327. }
  328. $sessionValidateHttpXForwardedForKey = $sessionData[self::VALIDATOR_HTTP_X_FORVARDED_FOR_KEY];
  329. $validatorValidateHttpXForwardedForKey = $validatorData[self::VALIDATOR_HTTP_X_FORVARDED_FOR_KEY];
  330. if ($this->useValidateHttpXForwardedFor()
  331. && $sessionValidateHttpXForwardedForKey != $validatorValidateHttpXForwardedForKey ) {
  332. return false;
  333. }
  334. if ($this->useValidateHttpUserAgent()
  335. && $sessionData[self::VALIDATOR_HTTP_USER_AGENT_KEY] != $validatorData[self::VALIDATOR_HTTP_USER_AGENT_KEY]
  336. && !in_array($validatorData[self::VALIDATOR_HTTP_USER_AGENT_KEY], $this->getValidateHttpUserAgentSkip())) {
  337. return false;
  338. }
  339. return true;
  340. }
  341. /**
  342. * Retrieve unique user data for validator
  343. *
  344. * @return array
  345. */
  346. public function getValidatorData()
  347. {
  348. $parts = array(
  349. self::VALIDATOR_REMOTE_ADDR_KEY => '',
  350. self::VALIDATOR_HTTP_VIA_KEY => '',
  351. self::VALIDATOR_HTTP_X_FORVARDED_FOR_KEY => '',
  352. self::VALIDATOR_HTTP_USER_AGENT_KEY => ''
  353. );
  354. // collect ip data
  355. if (Mage::helper('core/http')->getRemoteAddr()) {
  356. $parts[self::VALIDATOR_REMOTE_ADDR_KEY] = Mage::helper('core/http')->getRemoteAddr();
  357. }
  358. if (isset($_ENV['HTTP_VIA'])) {
  359. $parts[self::VALIDATOR_HTTP_VIA_KEY] = (string)$_ENV['HTTP_VIA'];
  360. }
  361. if (isset($_ENV['HTTP_X_FORWARDED_FOR'])) {
  362. $parts[self::VALIDATOR_HTTP_X_FORVARDED_FOR_KEY] = (string)$_ENV['HTTP_X_FORWARDED_FOR'];
  363. }
  364. // collect user agent data
  365. if (isset($_SERVER['HTTP_USER_AGENT'])) {
  366. $parts[self::VALIDATOR_HTTP_USER_AGENT_KEY] = (string)$_SERVER['HTTP_USER_AGENT'];
  367. }
  368. return $parts;
  369. }
  370. /**
  371. * Regenerate session Id
  372. *
  373. * @return Mage_Core_Model_Session_Abstract_Varien
  374. */
  375. public function regenerateSessionId()
  376. {
  377. session_regenerate_id(true);
  378. return $this;
  379. }
  380. }