PageRenderTime 30ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/unicornscan-0.4.7/www-front-end/lib/session_handler.php

#
PHP | 236 lines | 157 code | 52 blank | 27 comment | 25 complexity | bdef97a990356c2655f2d5dd3ee9b9b5 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /******************************************************************************
  3. * Copyright (C) 2002,2006 Jack Louis *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the Free Software *
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*
  18. ******************************************************************************/
  19. if (!(defined("session_handler_include"))) {
  20. define("session_handler_include", 1);
  21. if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {
  22. $proto="https://";
  23. }
  24. else {
  25. $proto="http://";
  26. }
  27. require($PHPLIB["filesystem_phplib"]."connect_todb.php");
  28. function get_sessid() {
  29. $session_id="";
  30. $fp=fopen("/dev/urandom", "r");
  31. if (!($fp)) {
  32. exit;
  33. }
  34. $rndbytes=fread($fp, 32);
  35. fclose($fp);
  36. if ($rndbytes == FALSE) {
  37. exit;
  38. }
  39. $session_id=base64_encode(pack("N", time()))."!";
  40. $session_id .= base64_encode($rndbytes);
  41. return $session_id;
  42. }
  43. function session_open($save_path, $sess_name) {
  44. return true;
  45. }
  46. function session_close() {
  47. return true;
  48. }
  49. function session_kill($sess_id) {
  50. global $PHPLIB;
  51. $db=$PHPLIB["database_db"];
  52. $query="delete from uni_session where sessid='".$db->_escape_string($sess_id)."'";
  53. $db->aquerydb($query);
  54. return true;
  55. }
  56. function session_read($sess_id) {
  57. global $PHPLIB;
  58. $db=$PHPLIB["database_db"];
  59. $curtime=time();
  60. $dsessid=$db->_escape_string($sess_id);
  61. $query="select data from uni_session where sessid='".$dsessid."'";
  62. $db->aquerydb($query);
  63. if ($db->numrows == 0) {
  64. $ua=$db->_escape_string($_SERVER["HTTP_USER_AGENT"]);
  65. $ra=$db->_escape_string($_SERVER["REMOTE_ADDR"]);
  66. @$rh=$db->_escape_string(gethostbyaddr($ra));
  67. $mtime=$curtime;
  68. $atime=$curtime;
  69. $ctime=$curtime;
  70. $query=<<<EOF
  71. insert into
  72. uni_session (sessid, remote_addr, remote_host, user_agent, c_time, m_time, a_time, uid, gid)
  73. values
  74. ('$dsessid', '$ra', '$rh', '$ua', $ctime, $mtime, $atime, -1, -1);
  75. EOF;
  76. $db->aquerydb($query);
  77. return "";
  78. }
  79. $db->data_step();
  80. $sessdata=$db->resultarr[0];
  81. $query=<<<EOF
  82. update
  83. uni_session
  84. set
  85. m_time=$curtime, a_time=$curtime
  86. where
  87. sessid='$dsessid'
  88. EOF;
  89. $db->aquerydb($query);
  90. if (strlen($sessdata) > 0) {
  91. return $sessdata;
  92. }
  93. else {
  94. return "";
  95. }
  96. }
  97. function session_write($sess_id, $val) {
  98. global $PHPLIB;
  99. $db=$PHPLIB["database_db"];
  100. if (strlen($val) < 1) {
  101. return true;
  102. }
  103. $sessdata=$db->_escape_string($val);
  104. $dsessid=$db->_escape_string($sess_id);
  105. $curtime=time();
  106. $query=<<<EOF
  107. update
  108. uni_session
  109. set
  110. data='$sessdata', m_time=$curtime
  111. where
  112. sessid='$dsessid'
  113. EOF;
  114. $db->aquerydb($query);
  115. return true;
  116. }
  117. function session_gc() {
  118. /* we dont do this */
  119. return true;
  120. }
  121. /*
  122. * now we make the session active
  123. */
  124. session_set_save_handler(
  125. "session_open",
  126. "session_close",
  127. "session_read",
  128. "session_write",
  129. "session_kill",
  130. "session_gc"
  131. );
  132. if (isset($_SESSION["sessid"])) {
  133. $sessid=$_SESSION["sessid"];
  134. }
  135. else if (isset($_COOKIE["sessid"])) {
  136. $sessid=$_COOKIE["sessid"];
  137. }
  138. else if (isset($_POST["sessid"])) {
  139. $sessid=$_POST["sessid"];
  140. }
  141. else if (isset($_GET["sessid"])) {
  142. $sessid=$_GET["sessid"];
  143. }
  144. else {
  145. session_name("sessid");
  146. $sessid=get_sessid();
  147. session_id($sessid);
  148. session_start();
  149. $uri=$proto.$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"]."?sessid=".$sessid;
  150. if (!(headers_sent())) {
  151. header("P3P: CP='CAO DSP OUR'");
  152. header("Location: ".$uri);
  153. print "you dont have a session, come again\n";
  154. exit;
  155. }
  156. exit;
  157. }
  158. /* now validate the session id */
  159. $query="select c_time from uni_session where sessid='".$db->_escape_string($sessid)."'";
  160. $db->aquerydb($query);
  161. if ($db->numrows != 1) {
  162. /* Now delete the cookie so to avoid loops ;) */
  163. session_name("sessid");
  164. $cookie=session_get_cookie_params();
  165. setcookie(session_name(), "", 0, $cookie["path"], $cookie["domain"]);
  166. $uri=$proto.$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"];
  167. /*
  168. * just in case the browser sucks, we dont
  169. * want it looping and bogging the server
  170. */
  171. usleep(2000);
  172. header("P3P: CP='CAO DSP OUR'");
  173. header("Location: ".$uri);
  174. print "Your session is dumb, come again\n";
  175. exit;
  176. }
  177. else {
  178. header("P3P: CP='CAO DSP OUR'");
  179. session_name("sessid");
  180. session_id($sessid);
  181. @session_start();
  182. }
  183. } /* include protect */
  184. ?>