PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/hettema/Stages
PHP | 393 lines | 208 code | 45 blank | 140 comment | 34 complexity | 3f78c9932051e856c995b0aff289784b MD5 | raw file
  1. <?php
  2. /**
  3. * class Core_Model_Session_Absctract
  4. * Abstract class for the session object
  5. *
  6. * @package Core
  7. * @subpackage Session
  8. * @category Model
  9. * @copyright Copyright (c) 2010 Hettema&Bergsten
  10. * @author
  11. */
  12. class Core_Model_Session_Abstract extends Core_Model_Abstract
  13. {
  14. const VALIDATOR_KEY = '_session_validator_data';
  15. const VALIDATOR_HTTP_USER_AGENT_KEY = 'http_user_agent';
  16. const VALIDATOR_HTTP_X_FORVARDED_FOR_KEY = 'http_x_forwarded_for';
  17. const VALIDATOR_HTTP_VIA_KEY = 'http_via';
  18. const VALIDATOR_REMOTE_ADDR_KEY = 'remote_addr';
  19. /**
  20. * Conigure and start session
  21. *
  22. * @param string $sessionName
  23. * @return Core_Model_Session_Abstract
  24. */
  25. public function start($sessionName=null)
  26. {
  27. if (isset($_SESSION)) {
  28. return $this;
  29. }
  30. if (is_writable($this->getSessionSavePath())) {
  31. session_save_path($this->getSessionSavePath());
  32. }
  33. switch($this->getSessionSaveMethod()) {
  34. case 'db':
  35. ini_set('session.save_handler', 'user');
  36. $sessionResource = App_Main::getResourceSingleton('core/session');
  37. $sessionResource->setSaveHandler();
  38. break;
  39. case 'memcache':
  40. ini_set('session.save_handler', 'memcache');
  41. session_save_path($this->getSessionSavePath());
  42. break;
  43. default:
  44. session_module_name('files');
  45. break;
  46. }
  47. /*if ($sessionName == 'backend') {
  48. $adminSessionLifetime = App_Main::SESSION_ADMIN_LIFETIME;
  49. if ($adminSessionLifetime > 60) {
  50. App_Main::getSingleton('core/cookie')->setLifetime($adminSessionLifetime);
  51. }
  52. }*/
  53. // set session cookie params
  54. session_set_cookie_params(
  55. $this->getCookie()->getLifetime(),
  56. $this->getCookie()->getPath(),
  57. $this->getCookie()->getDomain(),
  58. $this->getCookie()->isSecure(),
  59. $this->getCookie()->getHttponly()
  60. );
  61. /*tmp vers */
  62. $a = $this->getCookie()->getLifetime();
  63. $b = $this->getCookie()->getPath();
  64. $c = $this->getCookie()->getDomain();
  65. $d = $this->getCookie()->isSecure();
  66. $e = $this->getCookie()->getHttponly();
  67. if (!empty($sessionName)) {
  68. $this->setSessionName($sessionName);
  69. }
  70. // potential custom logic for session id (ex. switching between hosts)
  71. $this->setSessionId();
  72. /*if ($sessionCacheLimiter = App_Main::SESSION_CACHE_LIMITER) {
  73. session_cache_limiter((string)$sessionCacheLimiter);
  74. }*/
  75. session_start();
  76. return $this;
  77. }
  78. /**
  79. * Retrieve cookie object
  80. *
  81. * @return Core_Model_Cookie
  82. */
  83. public function getCookie()
  84. {
  85. return App_Main::getSingleton('core/cookie');
  86. }
  87. /**
  88. * Init session with namespace
  89. *
  90. * @param string $namespace
  91. * @param string $sessionName
  92. * @return Core_Model_Session_Abstract
  93. */
  94. public function init($namespace, $sessionName=null)
  95. {
  96. if (!isset($_SESSION)) {
  97. $this->start($sessionName);
  98. }
  99. if (!isset($_SESSION[$namespace])) {
  100. $_SESSION[$namespace] = array();
  101. }
  102. $this->_data = &$_SESSION[$namespace];
  103. $this->validate();
  104. $this->revalidateCookie();
  105. return $this;
  106. }
  107. /**
  108. * Additional get data with clear mode
  109. *
  110. * @param string $key
  111. * @param bool $clear
  112. * @return mixed
  113. */
  114. public function getData($key='', $clear = false)
  115. {
  116. $data = parent::getData($key);
  117. if ($clear && isset($this->_data[$key])) {
  118. unset($this->_data[$key]);
  119. }
  120. return $data;
  121. }
  122. /**
  123. * Retrieve session Id
  124. *
  125. * @return string
  126. */
  127. public function getSessionId()
  128. {
  129. return session_id();
  130. }
  131. /**
  132. * Set custom session id
  133. *
  134. * @param string $id
  135. * @return Core_Model_Session_Abstract
  136. */
  137. public function setSessionId($id=null)
  138. {
  139. if (!is_null($id) && preg_match('#^[0-9a-zA-Z,-]+$#', $id)) {
  140. session_id($id);
  141. }
  142. return $this;
  143. }
  144. /**
  145. * Retrieve session name
  146. *
  147. * @return string
  148. */
  149. public function getSessionName()
  150. {
  151. return session_name();
  152. }
  153. /**
  154. * Set session name
  155. *
  156. * @param string $name
  157. * @return Core_Model_Session_Abstract
  158. */
  159. public function setSessionName($name)
  160. {
  161. session_name($name);
  162. return $this;
  163. }
  164. /**
  165. * Unset all data
  166. *
  167. * @return Core_Model_Session_Abstract
  168. */
  169. public function unsetAll()
  170. {
  171. $this->unsetData();
  172. return $this;
  173. }
  174. /**
  175. * Alias for unsetAll
  176. *
  177. * @return Core_Model_Session_Abstract
  178. */
  179. public function clear()
  180. {
  181. return $this->unsetAll();
  182. }
  183. /**
  184. * Retrieve session save method
  185. * Default files
  186. *
  187. * @return string
  188. */
  189. public function getSessionSaveMethod()
  190. {
  191. return 'files';
  192. }
  193. /**
  194. * Get sesssion save path
  195. *
  196. * @return string
  197. */
  198. public function getSessionSavePath()
  199. {
  200. return App_Main::getBaseDir('session');
  201. }
  202. /**
  203. * Use REMOTE_ADDR in validator key
  204. *
  205. * @return bool
  206. */
  207. public function useValidateRemoteAddr()
  208. {
  209. return true;
  210. }
  211. /**
  212. * Use HTTP_VIA in validator key
  213. *
  214. * @return bool
  215. */
  216. public function useValidateHttpVia()
  217. {
  218. return true;
  219. }
  220. /**
  221. * Use HTTP_X_FORWARDED_FOR in validator key
  222. *
  223. * @return bool
  224. */
  225. public function useValidateHttpXForwardedFor()
  226. {
  227. return true;
  228. }
  229. /**
  230. * Use HTTP_USER_AGENT in validator key
  231. *
  232. * @return bool
  233. */
  234. public function useValidateHttpUserAgent()
  235. {
  236. return true;
  237. }
  238. /**
  239. * Retrieve skip User Agent validation strings (Flash etc)
  240. *
  241. * @return array
  242. */
  243. public function getValidateHttpUserAgentSkip()
  244. {
  245. return array();
  246. }
  247. /**
  248. * Validate session
  249. *
  250. * @param string $namespace
  251. * @return Core_Model_Session_Abstract
  252. */
  253. public function validate()
  254. {
  255. if (!isset($this->_data[self::VALIDATOR_KEY])) {
  256. $this->_data[self::VALIDATOR_KEY] = $this->getValidatorData();
  257. }
  258. else {
  259. if (!$this->_validate()) {
  260. $this->getCookie()->delete(session_name());
  261. return false;
  262. // throw core session exception
  263. //throw new Core_Model_Session_Exception('');
  264. }
  265. }
  266. return $this;
  267. }
  268. /**
  269. * Validate data
  270. *
  271. * @return bool
  272. */
  273. protected function _validate()
  274. {
  275. $sessionData = $this->_data[self::VALIDATOR_KEY];
  276. $validatorData = $this->getValidatorData();
  277. if ($this->useValidateRemoteAddr() && $sessionData[self::VALIDATOR_REMOTE_ADDR_KEY] != $validatorData[self::VALIDATOR_REMOTE_ADDR_KEY]) {
  278. return false;
  279. }
  280. if ($this->useValidateHttpVia() && $sessionData[self::VALIDATOR_HTTP_VIA_KEY] != $validatorData[self::VALIDATOR_HTTP_VIA_KEY]) {
  281. return false;
  282. }
  283. if ($this->useValidateHttpXForwardedFor() && $sessionData[self::VALIDATOR_HTTP_X_FORVARDED_FOR_KEY] != $validatorData[self::VALIDATOR_HTTP_X_FORVARDED_FOR_KEY]) {
  284. return false;
  285. }
  286. if ($this->useValidateHttpUserAgent()
  287. && $sessionData[self::VALIDATOR_HTTP_USER_AGENT_KEY] != $validatorData[self::VALIDATOR_HTTP_USER_AGENT_KEY]
  288. && !in_array($validatorData[self::VALIDATOR_HTTP_USER_AGENT_KEY], $this->getValidateHttpUserAgentSkip())) {
  289. return false;
  290. }
  291. return true;
  292. }
  293. /**
  294. * Retrieve unique user data for validator
  295. *
  296. * @return array
  297. */
  298. public function getValidatorData()
  299. {
  300. $parts = array(
  301. self::VALIDATOR_REMOTE_ADDR_KEY => '',
  302. self::VALIDATOR_HTTP_VIA_KEY => '',
  303. self::VALIDATOR_HTTP_X_FORVARDED_FOR_KEY => '',
  304. self::VALIDATOR_HTTP_USER_AGENT_KEY => ''
  305. );
  306. // collect ip data
  307. if (App_Main::getHelper('core/http')->getRemoteAddr()) {
  308. $parts[self::VALIDATOR_REMOTE_ADDR_KEY] =App_Main::getHelper('core/http')->getRemoteAddr();
  309. }
  310. if (isset($_ENV['HTTP_VIA'])) {
  311. $parts[self::VALIDATOR_HTTP_VIA_KEY] = (string)$_ENV['HTTP_VIA'];
  312. }
  313. if (isset($_ENV['HTTP_X_FORWARDED_FOR'])) {
  314. $parts[self::VALIDATOR_HTTP_X_FORVARDED_FOR_KEY] = (string)$_ENV['HTTP_X_FORWARDED_FOR'];
  315. }
  316. // collect user agent data
  317. if (isset($_SERVER['HTTP_USER_AGENT'])) {
  318. $parts[self::VALIDATOR_HTTP_USER_AGENT_KEY] = (string)$_SERVER['HTTP_USER_AGENT'];
  319. }
  320. return $parts;
  321. }
  322. /**
  323. * Revalidate cookie
  324. *
  325. * @return Core_Model_Session_Abstract
  326. */
  327. public function revalidateCookie()
  328. {
  329. if (!$this->getCookie()->getLifetime()) {
  330. return $this;
  331. }
  332. if (empty($_SESSION['_cookie_revalidate'])) {
  333. $time = time() + round($this->getCookie()->getLifetime() / 4);
  334. $_SESSION['_cookie_revalidate'] = $time;
  335. }
  336. else {
  337. if ($_SESSION['_cookie_revalidate'] < time()) {
  338. if (!headers_sent()) {
  339. $this->getCookie()->set(session_name(), session_id());
  340. $time = time() + round($this->getCookie()->getLifetime() / 4);
  341. $_SESSION['_cookie_revalidate'] = $time;
  342. }
  343. }
  344. }
  345. return $this;
  346. }
  347. }
  348. ?>