PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

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