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

/includes/classes/sessions.php

https://github.com/validoc/Pikmas
PHP | 521 lines | 350 code | 112 blank | 59 comment | 59 complexity | 52ad94d4ed11c8bc33cbfb1b3e840db0 MD5 | raw file
  1. <?php
  2. /*
  3. $Id: sessions.php 1739 2007-12-20 00:52:16Z hpdl $
  4. osCommerce, Open Source E-Commerce Solutions
  5. http://www.oscommerce.com
  6. Copyright (c) 2003 osCommerce
  7. Released under the GNU General Public License
  8. Original source from Web Application Development with PHP (Tobias Ratschiller, Till Gerken)
  9. Copyright (c) 2000, New Riders Publishing
  10. */
  11. $SID = '';
  12. class php3session {
  13. var $name = PHP_SESSION_NAME;
  14. var $auto_start = false;
  15. var $referer_check = false;
  16. var $save_path = PHP_SESSION_SAVE_PATH;
  17. var $save_handler = 'php3session_files';
  18. var $lifetime = 0;
  19. var $cache_limiter = 'nocache';
  20. var $cache_expire = 180;
  21. var $use_cookies = true;
  22. var $cookie_lifetime = 0;
  23. var $cookie_path = PHP_SESSION_PATH;
  24. var $cookie_domain = PHP_SESSION_DOMAIN;
  25. var $gc_probability = 1;
  26. var $gc_maxlifetime = 0;
  27. var $serialize_handler = 'php';
  28. var $ID;
  29. var $nr_open_sessions = 0;
  30. var $mod_name = '';
  31. var $id;
  32. var $delimiter = "\n";
  33. var $delimiter_value = '[==]';
  34. var $vars;
  35. function php3session() {
  36. $this->mod_name = $this->save_handler;
  37. $this->vars = array();
  38. }
  39. }
  40. class php3session_user {
  41. var $open_func, $close_func, $read_func, $write_func, $destroy_func, $gc_func;
  42. function open($save_path, $sess_name) {
  43. $func = $this->open_func;
  44. if (function_exists($func)) {
  45. return $func($save_path, $sess_name);
  46. }
  47. return true;
  48. }
  49. function close($save_path, $sess_name) {
  50. $func = $this->close_func;
  51. if (function_exists($func)) {
  52. return $func();
  53. }
  54. return true;
  55. }
  56. function read($sess_id) {
  57. $func = $this->read_func;
  58. return $func($sess_id);
  59. }
  60. function write($sess_id, $val) {
  61. $func = $this->write_func;
  62. return $func($sess_id, $val);
  63. }
  64. function destroy($sess_id) {
  65. $func = $this->destroy_func;
  66. if (function_exists($func)) {
  67. return $func($sess_id);
  68. }
  69. return true;
  70. }
  71. function gc($max_lifetime) {
  72. $func = $this->gc_func;
  73. if (function_exists($func)) {
  74. return $func($max_lifetime);
  75. }
  76. return true;
  77. }
  78. }
  79. class php3session_files {
  80. function open($save_path, $sess_name) {
  81. return true;
  82. }
  83. function close() {
  84. return true;
  85. }
  86. function read($sess_id) {
  87. global $session;
  88. // Open, read in, close file with session data
  89. $file = $session->save_path . '/sess_' . $sess_id;
  90. if (!file_exists($file)) {
  91. // Create it
  92. touch($file);
  93. }
  94. $fp = fopen($file, 'r') or die('Could not open session file (' . $file . ').');
  95. $val = fread($fp, filesize($file));
  96. fclose($fp);
  97. return $val;
  98. }
  99. function write($sess_id, $val) {
  100. global $session;
  101. // Open, write to, close file with session data
  102. $file = $session->save_path . '/sess_' . $sess_id;
  103. $fp = fopen($file, 'w') or die('Could not write session file (' . $file . ')');
  104. $val = fputs($fp, $val);
  105. fclose($fp);
  106. return true;
  107. }
  108. function destroy($sess_id) {
  109. global $session;
  110. $file = $session->save_path . '/sess_' . $sess_id;
  111. unlink($file);
  112. return true;
  113. }
  114. function gc($max_lifetime) {
  115. // We return true, since all cleanup should be handled by
  116. // an external entity (i.e. find -ctime x | xargs rm)
  117. return true;
  118. }
  119. }
  120. function _session_create_id() {
  121. return md5(uniqid(microtime()));
  122. }
  123. function _session_cache_limiter() {
  124. global $session;
  125. switch ($session->cache_limiter) {
  126. case 'nocache':
  127. header('Expires: Thu, 19 Nov 1981 08:52:00 GMT');
  128. header('Cache-Control: no-cache');
  129. header('Pragma: no-cache');
  130. break;
  131. case 'private':
  132. header('Expires: Thu, 19 Nov 1981 08:52:00 GMT');
  133. header(sprintf('Cache-Control: private, max-age=%s', $session->cache_expire * 60));
  134. header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime(basename($GLOBALS['PHP_SELF']))) . ' GMT');
  135. break;
  136. case 'public':
  137. $now = time();
  138. $now += $session->cache_expire * 60;
  139. $now = gmdate('D, d M Y H:i:s', $now) . ' GMT';
  140. header('Expires: ' . $now);
  141. header(sprintf('Cache-Control: public, max-age=%s', $session->cache_expire * 60));
  142. header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime(basename($GLOBALS['PHP_SELF']))) . ' GMT');
  143. break;
  144. default:
  145. die('Caching method ' . $session->cache_limiter . ' not implemented.');
  146. }
  147. }
  148. function _php_encode() {
  149. global $session;
  150. $ret = '';
  151. // Create a string containing the serialized variables
  152. for (reset($session->vars); list($i)=each($session->vars);) {
  153. $ret .= $session->vars[$i] . $session->delimiter_value . serialize($GLOBALS[$session->vars[$i]]) . $session->delimiter;
  154. }
  155. return $ret;
  156. }
  157. function _php_decode($data) {
  158. global $session;
  159. $data = trim($data);
  160. $vars = explode($session->delimiter, $data);
  161. // Add the variables to the global namespace
  162. for (reset($vars); list($i)=each($vars);) {
  163. $tmp = explode($session->delimiter_value, $vars[$i]);
  164. $name = trim($tmp[0]);
  165. $value = trim($tmp[1]);
  166. $GLOBALS[$name] = unserialize($value);
  167. $session->vars[] = trim($name);
  168. }
  169. }
  170. function _wddx_encode($data) {
  171. global $session;
  172. $ret = wddx_serialize_vars($session->vars);
  173. return $ret;
  174. }
  175. function _wddx_decode($data) {
  176. return wddx_deserialize($data);
  177. }
  178. function session_name($name = '') {
  179. global $session;
  180. if (empty($name)) {
  181. return $session->name;
  182. }
  183. $session->name = $name;
  184. }
  185. function session_set_save_handler($open, $close, $read, $write, $destroy, $gc) {
  186. global $session, $php3session_user;
  187. $php3session_user = new php3session_user;
  188. $php3session_user->open_func = $open;
  189. $php3session_user->close_func = $close;
  190. $php3session_user->read_func = $read;
  191. $php3session_user->write_func = $write;
  192. $php3session_user->destroy_func = $destroy;
  193. $php3session_user->gc_func = $gc;
  194. $session->mod_name = 'php3session_user';
  195. }
  196. function session_module_name($name = '') {
  197. global $session;
  198. if (empty($name)) {
  199. return $session->mod_name;
  200. }
  201. $session->mod_name = $name;
  202. }
  203. function session_save_path($path = '') {
  204. global $session;
  205. if(empty($path)) {
  206. return $session->save_path;
  207. }
  208. $session->save_path = $path;
  209. }
  210. function session_id($id = '') {
  211. global $session;
  212. if(empty($id)) {
  213. return $session->id;
  214. }
  215. $session->id = $id;
  216. }
  217. function session_register($var) {
  218. global $session;
  219. if ($session->nr_open_sessions == 0) {
  220. session_start();
  221. }
  222. $session->vars[] = trim($var);
  223. }
  224. function session_unregister($var) {
  225. global $session;
  226. for (reset($session->vars); list($i)=each($session->vars);) {
  227. if ($session->vars[$i] == trim($var)) {
  228. unset($session->vars[$i]);
  229. break;
  230. }
  231. }
  232. }
  233. function session_is_registered($var) {
  234. global $session;
  235. for (reset($session->vars); list($i)=each($session->vars);) {
  236. if ($session->vars[$i] == trim($var)) {
  237. return true;
  238. }
  239. }
  240. return false;
  241. }
  242. function session_encode() {
  243. global $session;
  244. $serializer = '_' . $session->serialize_handler . '_encode';
  245. $ret = $serializer();
  246. return $ret;
  247. }
  248. function session_decode($data) {
  249. global $session;
  250. $serializer = '_' . $session->serialize_handler . '_decode';
  251. $ret = $serializer($data);
  252. return $ret;
  253. }
  254. function session_start() {
  255. global $session, $SID, $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $HTTP_POST_VARS;
  256. // Define the global variable $SID?
  257. $define_sid = true;
  258. // Send the session cookie?
  259. $send_cookie = true;
  260. // Is track_vars enabled?
  261. $track_vars = ( (isset($HTTP_COOKIE_VARS)) || (isset($HTTP_GET_VARS)) || (isset($HTTP_POST_VARS)) ) ? true : false;
  262. // Check if session_start() has been called once already
  263. if ($session->nr_open_sessions != 0) {
  264. return false;
  265. }
  266. // If our only resource is the global symbol_table, then check it.
  267. // If track_vars are enabled, we prefer these, because they are more
  268. // reliable, and we always know whether the user has accepted the
  269. // cookie.
  270. if ( (isset($GLOBALS[$session->name])) && (!empty($GLOBALS[$session->name])) && (!$track_vars) ) {
  271. $session->id = $GLOBALS[$session->name];
  272. $send_cookie = false;
  273. }
  274. // Now check the track_vars. Cookies are preferred, because initially
  275. // cookie and get variables will be available.
  276. if ( (empty($session->id)) && ($track_vars) ) {
  277. if (isset($HTTP_COOKIE_VARS[$session->name])) {
  278. $session->id = $HTTP_COOKIE_VARS[$session->name];
  279. $define_sid = false;
  280. $send_cookie = false;
  281. }
  282. if (isset($HTTP_GET_VARS[$session->name])) {
  283. $session->id = $HTTP_GET_VARS[$session->name];
  284. }
  285. if (isset($HTTP_POST_VARS[$session->name])) {
  286. $session->id = $HTTP_POST_VARS[$session->name];
  287. }
  288. }
  289. if (!empty($session->id)) {
  290. if (preg_match('/^[a-zA-Z0-9]+$/', $session->id) == false) {
  291. unset($session->id);
  292. }
  293. }
  294. /*
  295. // Check the REQUEST_URI symbol for a string of the form
  296. // '<session-name>=<session-id>' to allow URLs of the form
  297. // http://yoursite/<session-name>=<session-id>/script.php
  298. if (empty($session->id)) {
  299. eregi($session->name . '=([^/]+)', $GLOBALS['REQUEST_URI'], $regs);
  300. $regs[1] = trim($regs[1]);
  301. if (!empty($regs[1])) {
  302. $session->id = $regs[1];
  303. }
  304. }
  305. */
  306. // Check whether the current request was referred to by
  307. // an external site which invalidates the previously found ID
  308. if ( (!empty($session->id)) && ($session->referer_check) ) {
  309. $url = parse_url($GLOBALS['HTTP_REFERER']);
  310. if (trim($url['host']) != $GLOBALS['SERVER_NAME']) {
  311. unset($session->id);
  312. $send_cookie = true;
  313. $define_sid = true;
  314. }
  315. }
  316. // Do we have an existing session ID?
  317. if (empty($session->id)) {
  318. // Create new session ID
  319. $session->id = _session_create_id();
  320. }
  321. // Is use_cookies set to false?
  322. if ( (!$session->use_cookies) && ($send_cookie) ) {
  323. $define_sid = true;
  324. $send_cookie = false;
  325. }
  326. // Should we send a cookie?
  327. if ($send_cookie) {
  328. setcookie($session->name, $session->id, $session->cookie_lifetime, $session->cookie_path, $session->cookie_domain);
  329. }
  330. // Should we define the SID?
  331. if($define_sid) {
  332. $SID = $session->name . '=' . $session->id;
  333. }
  334. $session->nr_open_sessions++;
  335. // Send caching headers
  336. // Start session
  337. $mod = $GLOBALS[$session->mod_name];
  338. if (!$mod->open($session->save_path, $session->name)) {
  339. die('Failed to initialize session module.');
  340. }
  341. // Read session data
  342. if ($val = $mod->read($session->id)) {
  343. // Decode session data
  344. session_decode($val);
  345. }
  346. // Send HTTP cache headers
  347. _session_cache_limiter();
  348. // Check if we should clean up (call the garbage collection routines)
  349. if ($session->gc_probability > 0) {
  350. $randmax = getrandmax();
  351. $nrand = (int)(100 * tep_rand() / $randmax);
  352. if ($nrand < $session->gc_probability) {
  353. $mod->gc($session->gc_maxlifetime);
  354. }
  355. }
  356. if ($define_sid) {
  357. define('SID', $SID);
  358. } else {
  359. define('SID', '');
  360. }
  361. return true;
  362. }
  363. function session_destroy() {
  364. global $session;
  365. if ($session->nr_open_sessions == 0) {
  366. return false;
  367. }
  368. // Destroy session
  369. $mod = $GLOBALS[$session->mod_name];
  370. if (!$mod->destroy($session->id)) {
  371. return false;
  372. }
  373. unset($session);
  374. $session = new php3session;
  375. return true;
  376. }
  377. function session_close() {
  378. global $session, $SID;
  379. if ($session->nr_open_sessions == 0) {
  380. return false;
  381. }
  382. // Encode session
  383. $val = session_encode();
  384. $len = strlen($val);
  385. // Save session
  386. $mod = $GLOBALS[$session->mod_name];
  387. if (!$mod->write($session->id, $val)) {
  388. die('Session could not be saved.');
  389. }
  390. // Close session
  391. if ( (function_exists($session->mod_name . '->close')) && (!$mod->close()) ) {
  392. die('Session could not be closed.');
  393. }
  394. $SID = '';
  395. $session->nr_open_sessions--;
  396. return true;
  397. }
  398. $session = new php3session;
  399. $mod = $session->save_handler;
  400. $$mod = new $mod;
  401. if ($session->auto_start) {
  402. $ret = session_start() or die('Session could not be started.');
  403. }
  404. register_shutdown_function('session_close');
  405. ?>