PageRenderTime 35ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/dnejedly/eaparts
PHP | 439 lines | 242 code | 45 blank | 152 comment | 39 complexity | c3670c3e2dcc434e21e52402fdb00063 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) 2012 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. * Configure 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 'memcached':
  56. ini_set('session.save_handler', 'memcached');
  57. session_save_path($this->getSessionSavePath());
  58. break;
  59. case 'eaccelerator':
  60. ini_set('session.save_handler', 'eaccelerator');
  61. break;
  62. default:
  63. session_module_name($this->getSessionSaveMethod());
  64. if (is_writable($this->getSessionSavePath())) {
  65. session_save_path($this->getSessionSavePath());
  66. }
  67. break;
  68. }
  69. $cookie = $this->getCookie();
  70. if (Mage::app()->getStore()->isAdmin()) {
  71. $sessionMaxLifetime = Mage_Core_Model_Resource_Session::SEESION_MAX_COOKIE_LIFETIME;
  72. $adminSessionLifetime = (int)Mage::getStoreConfig('admin/security/session_cookie_lifetime');
  73. if ($adminSessionLifetime > $sessionMaxLifetime) {
  74. $adminSessionLifetime = $sessionMaxLifetime;
  75. }
  76. if ($adminSessionLifetime > 60) {
  77. $cookie->setLifetime($adminSessionLifetime);
  78. }
  79. }
  80. // session cookie params
  81. $cookieParams = array(
  82. 'lifetime' => $cookie->getLifetime(),
  83. 'path' => $cookie->getPath(),
  84. 'domain' => $cookie->getConfigDomain(),
  85. 'secure' => $cookie->isSecure(),
  86. 'httponly' => $cookie->getHttponly()
  87. );
  88. if (!$cookieParams['httponly']) {
  89. unset($cookieParams['httponly']);
  90. if (!$cookieParams['secure']) {
  91. unset($cookieParams['secure']);
  92. if (!$cookieParams['domain']) {
  93. unset($cookieParams['domain']);
  94. }
  95. }
  96. }
  97. if (isset($cookieParams['domain'])) {
  98. $cookieParams['domain'] = $cookie->getDomain();
  99. }
  100. call_user_func_array('session_set_cookie_params', $cookieParams);
  101. if (!empty($sessionName)) {
  102. $this->setSessionName($sessionName);
  103. }
  104. // potential custom logic for session id (ex. switching between hosts)
  105. $this->setSessionId();
  106. Varien_Profiler::start(__METHOD__.'/start');
  107. $sessionCacheLimiter = Mage::getConfig()->getNode('global/session_cache_limiter');
  108. if ($sessionCacheLimiter) {
  109. session_cache_limiter((string)$sessionCacheLimiter);
  110. }
  111. session_start();
  112. /**
  113. * Renew cookie expiration time if session id did not change
  114. */
  115. if ($cookie->get(session_name()) == $this->getSessionId()) {
  116. $cookie->renew(session_name());
  117. }
  118. Varien_Profiler::stop(__METHOD__.'/start');
  119. return $this;
  120. }
  121. /**
  122. * Retrieve cookie object
  123. *
  124. * @return Mage_Core_Model_Cookie
  125. */
  126. public function getCookie()
  127. {
  128. return Mage::getSingleton('core/cookie');
  129. }
  130. /**
  131. * Revalidate cookie
  132. * @deprecated after 1.4 cookie renew moved to session start method
  133. * @return Mage_Core_Model_Session_Abstract_Varien
  134. */
  135. public function revalidateCookie()
  136. {
  137. return $this;
  138. }
  139. /**
  140. * Init session with namespace
  141. *
  142. * @param string $namespace
  143. * @param string $sessionName
  144. * @return Mage_Core_Model_Session_Abstract_Varien
  145. */
  146. public function init($namespace, $sessionName=null)
  147. {
  148. if (!isset($_SESSION)) {
  149. $this->start($sessionName);
  150. }
  151. if (!isset($_SESSION[$namespace])) {
  152. $_SESSION[$namespace] = array();
  153. }
  154. $this->_data = &$_SESSION[$namespace];
  155. $this->validate();
  156. $this->revalidateCookie();
  157. return $this;
  158. }
  159. /**
  160. * Additional get data with clear mode
  161. *
  162. * @param string $key
  163. * @param bool $clear
  164. * @return mixed
  165. */
  166. public function getData($key='', $clear = false)
  167. {
  168. $data = parent::getData($key);
  169. if ($clear && isset($this->_data[$key])) {
  170. unset($this->_data[$key]);
  171. }
  172. return $data;
  173. }
  174. /**
  175. * Retrieve session Id
  176. *
  177. * @return string
  178. */
  179. public function getSessionId()
  180. {
  181. return session_id();
  182. }
  183. /**
  184. * Set custom session id
  185. *
  186. * @param string $id
  187. * @return Mage_Core_Model_Session_Abstract_Varien
  188. */
  189. public function setSessionId($id=null)
  190. {
  191. if (!is_null($id) && preg_match('#^[0-9a-zA-Z,-]+$#', $id)) {
  192. session_id($id);
  193. }
  194. return $this;
  195. }
  196. /**
  197. * Retrieve session name
  198. *
  199. * @return string
  200. */
  201. public function getSessionName()
  202. {
  203. return session_name();
  204. }
  205. /**
  206. * Set session name
  207. *
  208. * @param string $name
  209. * @return Mage_Core_Model_Session_Abstract_Varien
  210. */
  211. public function setSessionName($name)
  212. {
  213. session_name($name);
  214. return $this;
  215. }
  216. /**
  217. * Unset all data
  218. *
  219. * @return Mage_Core_Model_Session_Abstract_Varien
  220. */
  221. public function unsetAll()
  222. {
  223. $this->unsetData();
  224. return $this;
  225. }
  226. /**
  227. * Alias for unsetAll
  228. *
  229. * @return Mage_Core_Model_Session_Abstract_Varien
  230. */
  231. public function clear()
  232. {
  233. return $this->unsetAll();
  234. }
  235. /**
  236. * Retrieve session save method
  237. * Default files
  238. *
  239. * @return string
  240. */
  241. public function getSessionSaveMethod()
  242. {
  243. return 'files';
  244. }
  245. /**
  246. * Get sesssion save path
  247. *
  248. * @return string
  249. */
  250. public function getSessionSavePath()
  251. {
  252. return Mage::getBaseDir('session');
  253. }
  254. /**
  255. * Use REMOTE_ADDR in validator key
  256. *
  257. * @return bool
  258. */
  259. public function useValidateRemoteAddr()
  260. {
  261. return true;
  262. }
  263. /**
  264. * Use HTTP_VIA in validator key
  265. *
  266. * @return bool
  267. */
  268. public function useValidateHttpVia()
  269. {
  270. return true;
  271. }
  272. /**
  273. * Use HTTP_X_FORWARDED_FOR in validator key
  274. *
  275. * @return bool
  276. */
  277. public function useValidateHttpXForwardedFor()
  278. {
  279. return true;
  280. }
  281. /**
  282. * Use HTTP_USER_AGENT in validator key
  283. *
  284. * @return bool
  285. */
  286. public function useValidateHttpUserAgent()
  287. {
  288. return true;
  289. }
  290. /**
  291. * Retrieve skip User Agent validation strings (Flash etc)
  292. *
  293. * @return array
  294. */
  295. public function getValidateHttpUserAgentSkip()
  296. {
  297. return array();
  298. }
  299. /**
  300. * Validate session
  301. *
  302. * @param string $namespace
  303. * @return Mage_Core_Model_Session_Abstract_Varien
  304. */
  305. public function validate()
  306. {
  307. if (!isset($this->_data[self::VALIDATOR_KEY])) {
  308. $this->_data[self::VALIDATOR_KEY] = $this->getValidatorData();
  309. }
  310. else {
  311. if (!$this->_validate()) {
  312. $this->getCookie()->delete(session_name());
  313. // throw core session exception
  314. throw new Mage_Core_Model_Session_Exception('');
  315. }
  316. }
  317. return $this;
  318. }
  319. /**
  320. * Validate data
  321. *
  322. * @return bool
  323. */
  324. protected function _validate()
  325. {
  326. $sessionData = $this->_data[self::VALIDATOR_KEY];
  327. $validatorData = $this->getValidatorData();
  328. if ($this->useValidateRemoteAddr()
  329. && $sessionData[self::VALIDATOR_REMOTE_ADDR_KEY] != $validatorData[self::VALIDATOR_REMOTE_ADDR_KEY]) {
  330. return false;
  331. }
  332. if ($this->useValidateHttpVia()
  333. && $sessionData[self::VALIDATOR_HTTP_VIA_KEY] != $validatorData[self::VALIDATOR_HTTP_VIA_KEY]) {
  334. return false;
  335. }
  336. $sessionValidateHttpXForwardedForKey = $sessionData[self::VALIDATOR_HTTP_X_FORVARDED_FOR_KEY];
  337. $validatorValidateHttpXForwardedForKey = $validatorData[self::VALIDATOR_HTTP_X_FORVARDED_FOR_KEY];
  338. if ($this->useValidateHttpXForwardedFor()
  339. && $sessionValidateHttpXForwardedForKey != $validatorValidateHttpXForwardedForKey ) {
  340. return false;
  341. }
  342. if ($this->useValidateHttpUserAgent()
  343. && $sessionData[self::VALIDATOR_HTTP_USER_AGENT_KEY] != $validatorData[self::VALIDATOR_HTTP_USER_AGENT_KEY]
  344. ) {
  345. $userAgentValidated = $this->getValidateHttpUserAgentSkip();
  346. foreach ($userAgentValidated as $agent) {
  347. if (preg_match('/' . $agent . '/iu', $validatorData[self::VALIDATOR_HTTP_USER_AGENT_KEY])) {
  348. return true;
  349. }
  350. }
  351. return false;
  352. }
  353. return true;
  354. }
  355. /**
  356. * Retrieve unique user data for validator
  357. *
  358. * @return array
  359. */
  360. public function getValidatorData()
  361. {
  362. $parts = array(
  363. self::VALIDATOR_REMOTE_ADDR_KEY => '',
  364. self::VALIDATOR_HTTP_VIA_KEY => '',
  365. self::VALIDATOR_HTTP_X_FORVARDED_FOR_KEY => '',
  366. self::VALIDATOR_HTTP_USER_AGENT_KEY => ''
  367. );
  368. // collect ip data
  369. if (Mage::helper('core/http')->getRemoteAddr()) {
  370. $parts[self::VALIDATOR_REMOTE_ADDR_KEY] = Mage::helper('core/http')->getRemoteAddr();
  371. }
  372. if (isset($_ENV['HTTP_VIA'])) {
  373. $parts[self::VALIDATOR_HTTP_VIA_KEY] = (string)$_ENV['HTTP_VIA'];
  374. }
  375. if (isset($_ENV['HTTP_X_FORWARDED_FOR'])) {
  376. $parts[self::VALIDATOR_HTTP_X_FORVARDED_FOR_KEY] = (string)$_ENV['HTTP_X_FORWARDED_FOR'];
  377. }
  378. // collect user agent data
  379. if (isset($_SERVER['HTTP_USER_AGENT'])) {
  380. $parts[self::VALIDATOR_HTTP_USER_AGENT_KEY] = (string)$_SERVER['HTTP_USER_AGENT'];
  381. }
  382. return $parts;
  383. }
  384. /**
  385. * Regenerate session Id
  386. *
  387. * @return Mage_Core_Model_Session_Abstract_Varien
  388. */
  389. public function regenerateSessionId()
  390. {
  391. session_regenerate_id(true);
  392. return $this;
  393. }
  394. }