PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/classes/session.php

https://github.com/LisaX/oscommerce
PHP | 311 lines | 114 code | 65 blank | 132 comment | 26 complexity | ce57639e42aed21cb81f880b0a8a66a0 MD5 | raw file
  1. <?php
  2. /*
  3. $Id$
  4. osCommerce, Open Source E-Commerce Solutions
  5. http://www.oscommerce.com
  6. Copyright (c) 2009 osCommerce
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License v2 (1991)
  9. as published by the Free Software Foundation.
  10. */
  11. /**
  12. * The osC_Session class manages the session data and custom storage handlers
  13. */
  14. class osC_Session {
  15. /**
  16. * Holds the session cookie parameters (lifetime, path, domain, secure, httponly)
  17. *
  18. * @var array
  19. * @access protected
  20. */
  21. protected $_cookie_parameters = array();
  22. /**
  23. * Defines if the session has been started or not
  24. *
  25. * @var boolean
  26. * @access protected
  27. */
  28. protected $_is_started = false;
  29. /**
  30. * Holds the name of the session
  31. *
  32. * @var string
  33. * @access protected
  34. */
  35. protected $_name = 'osCsid';
  36. /**
  37. * Holds the session id
  38. *
  39. * @var string
  40. * @access protected
  41. */
  42. protected $_id = null;
  43. /**
  44. * Holds the file system save path for file based session storage
  45. *
  46. * @var string
  47. * @access protected
  48. */
  49. protected $_save_path = DIR_FS_WORK;
  50. /**
  51. * Holds the life time in seconds of the session
  52. *
  53. * @var string
  54. * @access protected
  55. */
  56. protected $_life_time = SERVICE_SESSION_EXPIRATION_TIME;
  57. /**
  58. * Constructor, loads custom session handle module if defined
  59. *
  60. * @param string $name The name of the session
  61. * @access public
  62. */
  63. public function __construct($name = null) {
  64. global $request_type;
  65. $this->setName($name);
  66. if ( $this->_life_time > 0 ) {
  67. $this->_life_time = $this->_life_time * 60;
  68. ini_set('session.gc_maxlifetime', $this->_life_time);
  69. } else {
  70. $this->_life_time = ini_get('session.gc_maxlifetime');
  71. }
  72. session_set_cookie_params($this->_life_time, (($request_type == 'NONSSL') ? HTTP_COOKIE_PATH : HTTPS_COOKIE_PATH), (($request_type == 'NONSSL') ? HTTP_COOKIE_DOMAIN : HTTPS_COOKIE_DOMAIN));
  73. register_shutdown_function(array($this, 'close'));
  74. }
  75. /**
  76. * Loads the session storage handler
  77. *
  78. * @param string $name The name of the session
  79. * @access public
  80. */
  81. public static function load($name = null) {
  82. $class_name = 'osC_Session';
  83. if ( !osc_empty(basename(STORE_SESSIONS)) && file_exists(dirname(__FILE__) . '/session/' . basename(STORE_SESSIONS) . '.php') ) {
  84. include(dirname(__FILE__) . '/session/' . basename(STORE_SESSIONS) . '.php');
  85. $class_name = 'osC_Session_' . basename(STORE_SESSIONS);
  86. }
  87. return new $class_name($name);
  88. }
  89. /**
  90. * Verify an existing session ID and create or resume the session if the existing session ID is valid
  91. *
  92. * @access public
  93. * @return boolean
  94. */
  95. public function start() {
  96. $sane_session_id = true;
  97. if ( isset($_GET[$this->_name]) && (empty($_GET[$this->_name]) || (ctype_alnum($_GET[$this->_name]) === false)) ) {
  98. $sane_session_id = false;
  99. } elseif ( isset($_POST[$this->_name]) && (empty($_POST[$this->_name]) || (ctype_alnum($_POST[$this->_name]) === false)) ) {
  100. $sane_session_id = false;
  101. } elseif ( isset($_COOKIE[$this->_name]) && (empty($_COOKIE[$this->_name]) || (ctype_alnum($_COOKIE[$this->_name]) === false)) ) {
  102. $sane_session_id = false;
  103. }
  104. if ( $sane_session_id === false ) {
  105. if ( isset($_COOKIE[$this->_name]) ) {
  106. setcookie($this->_name, '', time()-42000, $this->getCookieParameters('path'), $this->getCookieParameters('domain'));
  107. }
  108. osc_redirect(osc_href_link(FILENAME_DEFAULT, null, 'NONSSL', false));
  109. } elseif ( session_start() ) {
  110. $this->_is_started = true;
  111. $this->_id = session_id();
  112. return true;
  113. }
  114. return false;
  115. }
  116. /**
  117. * Checks if the session has been started or not
  118. *
  119. * @access public
  120. * @return boolean
  121. */
  122. public function hasStarted() {
  123. return $this->_is_started;
  124. }
  125. /**
  126. * Closes the session and writes the session data to the storage handler
  127. *
  128. * @access public
  129. */
  130. public function close() {
  131. if ( $this->_is_started === true ) {
  132. $this->_is_started = false;
  133. return session_write_close();
  134. }
  135. }
  136. /**
  137. * Deletes an existing session
  138. *
  139. * @access public
  140. */
  141. public function destroy() {
  142. if ( $this->_is_started === true ) {
  143. if ( isset($_COOKIE[$this->_name]) ) {
  144. setcookie($this->_name, '', time()-42000, $this->getCookieParameters('path'), $this->getCookieParameters('domain'));
  145. }
  146. $this->delete();
  147. return session_destroy();
  148. }
  149. }
  150. /**
  151. * Deletes an existing session from the storage handler
  152. *
  153. * @param string $id The ID of the session
  154. * @access public
  155. */
  156. public function delete($id = null) {
  157. if ( empty($id) ) {
  158. $id = $this->_id;
  159. }
  160. if ( file_exists($this->_save_path . '/' . $id) ) {
  161. @unlink($this->_save_path . '/' . $id);
  162. }
  163. }
  164. /**
  165. * Delete an existing session and move the session data to a new session with a new session ID
  166. *
  167. * @access public
  168. */
  169. public function recreate() {
  170. if ( $this->_is_started === true ) {
  171. return session_regenerate_id(true);
  172. }
  173. }
  174. /**
  175. * Return the session file based storage location
  176. *
  177. * @access public
  178. * @return string
  179. */
  180. public function getSavePath() {
  181. return $this->_save_path;
  182. }
  183. /**
  184. * Return the session ID
  185. *
  186. * @access public
  187. * @return string
  188. */
  189. public function getID() {
  190. return $this->_id;
  191. }
  192. /**
  193. * Return the name of the session
  194. *
  195. * @access public
  196. * @return string
  197. */
  198. public function getName() {
  199. return $this->_name;
  200. }
  201. /**
  202. * Sets the name of the session
  203. *
  204. * @param string $name The name of the session
  205. * @access public
  206. */
  207. public function setName($name) {
  208. if ( empty($name) ) {
  209. $name = 'osCsid';
  210. }
  211. session_name($name);
  212. $this->_name = session_name();
  213. }
  214. /**
  215. * Sets the storage location for the file based storage handler
  216. *
  217. * @param string $path The file path to store the session data in
  218. * @access public
  219. */
  220. public function setSavePath($path) {
  221. if ( substr($path, -1) == '/' ) {
  222. $path = substr($path, 0, -1);
  223. }
  224. session_save_path($path);
  225. $this->_save_path = session_save_path();
  226. }
  227. /**
  228. * Returns the cookie parameters for the session (lifetime, path, domain, secure, httponly)
  229. *
  230. * @param string $key If specified, return only the value of this cookie parameter setting
  231. * @access public
  232. */
  233. public function getCookieParameters($key = null) {
  234. if ( empty($this->_cookie_parameters) ) {
  235. $this->_cookie_parameters = session_get_cookie_params();
  236. }
  237. if ( !empty($key) ) {
  238. return $this->_cookie_parameters[$key];
  239. }
  240. return $this->_cookie_parameters;
  241. }
  242. }
  243. ?>