PageRenderTime 36ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/novo/admin/phpSecurePages/session.php

https://github.com/dericksaga/ibsite
PHP | 300 lines | 184 code | 73 blank | 43 comment | 34 complexity | 75ad94b81c5f285088bfdcaddd6ad5a3 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /* ------ MySQL session ------
  3. a MySQL based session management and variable registration system for PHP3
  4. joshua macadam, 2000
  5. josh@onestop.net
  6. */
  7. if (phpversion() < 4) {
  8. // phpversion = 3
  9. define("MAX_UNAUTH_IDLE", 1440); // How long may an unauthorized user be idle
  10. // (just to clean up out session table!)
  11. define("MAX_AUTH_IDLE", 720); // If you are logged in, how long for?
  12. if ( empty($cfgServerPort) ) {
  13. @mysql_connect($cfgServerHost, $cfgServerUser, $cfgServerPassword)
  14. or die($strNoConnection);
  15. } else {
  16. @mysql_connect($cfgServerHost . ":" . $cfgServerPort, $cfgServerUser, $cfgServerPassword)
  17. or die($strNoConnection);
  18. }
  19. if ($db) $dbOld = $db;
  20. $db = $cfgDbDatabase;
  21. $tableSessions = $cfgDbTableSessions;
  22. $tableSessionVars = $cfgDbTableSessionVars;
  23. $cookieName = "php3SessionID";
  24. $php3SessionID = $$cookieName;
  25. function session_destroy_php3() {
  26. global $db, $tableSessions, $tableSessionVars, $php3SessionID, $strNoDatabase, $cookieName;
  27. // $debug=1;
  28. // delete variables associated with the sessions we're about to DELETE
  29. $Query = "DELETE FROM $tableSessionVars WHERE session='$php3SessionID'";
  30. mysql($db, $Query) or die($strNoDatabase);
  31. if($debug) printf("Query=%s .<br>", $Query);
  32. // kill ID
  33. $Query = "DELETE FROM $tableSessions WHERE id='$php3SessionID'";
  34. mysql($db, $Query) or die($strNoDatabase);
  35. if($debug) printf("Query=%s .<br>", $Query);
  36. // remove cookie
  37. // setcookie($cookieName, "", 0);
  38. // no pageloads from cache or memory
  39. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
  40. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
  41. header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  42. header("Pragma: no-cache"); // HTTP/1.0
  43. }
  44. function session_killold() {
  45. global $db, $tableSessions, $tableSessionVars, $strNoDatabase;
  46. // $debug=1;
  47. // figure out who all we're about to delete for being too old!
  48. $Query = "SELECT vars.id FROM $tableSessions as sess, $tableSessionVars as vars
  49. WHERE sess.id=vars.session AND LastAction < '";
  50. $Query .= date("Y-m-d H:i:s", time()-MAX_UNAUTH_IDLE);
  51. $Query .= "'";
  52. // $result=mysql_query($Query, $db);
  53. $result=mysql($db, $Query) or die($strNoDatabase);
  54. echo mysql_error();
  55. if($debug) printf("Query=%s .<br>",$Query);
  56. // delete variables associated with the sessions we're about to DELETE
  57. while( $res = mysql_fetch_row($result) ) {
  58. $Query = "DELETE FROM $tableSessionVars WHERE id=$res[0]";
  59. mysql($db, $Query) or die($strNoDatabase);
  60. }
  61. // kill IDs after 12 hours for the sake of resources!
  62. $Query = "DELETE FROM $tableSessions WHERE LastAction < '";
  63. $Query .= date("Y-m-d H:i:s", time()-MAX_UNAUTH_IDLE);
  64. $Query .= "'";
  65. mysql($db, $Query) or die($strNoDatabase);
  66. if($debug) printf("Query=%s .<br>", $Query);
  67. // log users out if idle for 5 minutes
  68. // time problems resolved but
  69. // was noting ocassionally: 5 increments per second?
  70. $Query = "UPDATE $tableSessions SET userID=NULL WHERE LastAction < '";
  71. $Query .= date("Y-m-d H:i:s", time()-MAX_AUTH_IDLE);
  72. $Query .= "'";
  73. mysql($db, $Query) or die($strNoDatabase);
  74. if($debug) printf("Query=%s .<br>", $Query);
  75. if($debug) printf("Current Time=%s .<br>", date("Y-m-d H:i:s"));
  76. }
  77. function session_touch($sess) {
  78. global $db, $tableSessions, $strNoDatabase;
  79. // $debug=1;
  80. $Query="UPDATE $tableSessions SET LastAction=now() WHERE id='$sess'";
  81. mysql($db, $Query) or die($strNoDatabase);
  82. // if($debug) printf("Query=%s .<br>", $Query);
  83. }
  84. function session_valid_session($sess) {
  85. global $db, $tableSessions, $strNoDatabase;
  86. // $debug=1;
  87. session_killold();
  88. if($debug) printf("VALID: Recieved session=%s .<br>", $sess);
  89. if(!$sess) return 0;
  90. $Query = "SELECT * FROM $tableSessions WHERE id='$sess'";
  91. if($debug) printf("Query=%s .(validsess)<br>", $Query);
  92. $result = mysql($db, $Query) or die($strNoDatabase);
  93. $status = mysql_fetch_row($result);
  94. return $status;
  95. }
  96. function session_get_var($varname) {
  97. global $php3SessionID, $db, $tableSessionVars, $strNoDatabase;
  98. // $debug=1;
  99. $Query = "SELECT * FROM $tableSessionVars WHERE session='$php3SessionID' AND name='$varname'";
  100. if($debug) printf("Query=%s .(reg var)<br>", $Query);
  101. $result = mysql($db, $Query) or die($strNoDatabase);
  102. if(!$result) return 0;
  103. $obj = mysql_fetch_object($result);
  104. if(!$obj->intval) return $obj->strval;
  105. else return $obj->intval;
  106. }
  107. function session_isession_registered($varname) {
  108. global $php3SessionID, $db, $tableSessionVars, $strNoDatabase;
  109. // $debug=1;
  110. $Query = "SELECT * FROM $tableSessionVars WHERE session='$php3SessionID' AND name='$varname'";
  111. if($debug) printf("Query=%s .(is regged)<br>", $Query);
  112. $result = mysql($db, $Query) or die($strNoDatabase);
  113. $rows = mysql_num_rows($result);
  114. return $rows;
  115. }
  116. function session_register_php3($varname, $type, $value) {
  117. global $php3SessionID, $db, $tableSessionVars, $strNoDatabase;
  118. // $debug=1;
  119. $Query = "SELECT name FROM $tableSessionVars WHERE session='$php3SessionID' and name='$varname'";
  120. if($debug) printf("Query=%s .(is regged)<br>",$Query);
  121. $result = mysql($db, $Query) or die($strNoDatabase);
  122. $rows = mysql_num_rows($result);
  123. switch($type) {
  124. case "INT":
  125. $intval = $value;
  126. $strval = "NULL";
  127. break;
  128. case "STRING":
  129. $intval = "NULL";
  130. $strval = $value;
  131. break;
  132. }
  133. if($rows) $Query = "UPDATE $tableSessionVars SET intval=$intval, strval='$strval'
  134. WHERE session='$php3SessionID' AND name='$varname'";
  135. else $Query = "INSERT INTO $tableSessionVars (name, session, intval, strval)
  136. VALUES ('$varname', '$php3SessionID', $intval, '$strval')";
  137. $result = mysql($db, $Query);
  138. if($debug) printf("Query=%s .(set var)<br>", $Query);
  139. }
  140. // lookey lookey! persistant DATA!!!!!
  141. function session_loadvars($php3SessionID) {
  142. global $db, $tableSessionVars, $strNoDatabase;
  143. $Query = "SELECT * FROM $tableSessionVars WHERE session='$php3SessionID'";
  144. $result = mysql($db, $Query) or die($strNoDatabase);
  145. // echo "q = $Query";
  146. if($result) {
  147. while($data = mysql_fetch_object($result)) {
  148. // echo "load=$data->name<br>";
  149. if($data->intval)
  150. $GLOBALS[$data->name] = $data->intval;
  151. else $GLOBALS[$data->name] = $data->strval;
  152. } } }
  153. function session_start_php3() {
  154. // $debug=1;
  155. global $php3SessionID, $auctionref;
  156. if($debug) printf("Recieved session=%s<br>", $php3SessionID);
  157. if(!session_valid_session($php3SessionID)) {
  158. $php3SessionID = session_begin_session($auctionref);
  159. } else {
  160. session_loadvars($php3SessionID);
  161. }
  162. session_touch($php3SessionID);
  163. }
  164. function session_login($userID) {
  165. // $debug=1;
  166. global $php3SessionID, $db, $tableSessions, $strNoDatabase;
  167. session_start_php3();
  168. $Query = "UPDATE $tableSessions SET userID = '$userID' WHERE id='$php3SessionID'";
  169. mysql($db, $Query) or die($strNoDatabase);
  170. if($debug) printf("Query=%s .(session_login)<br>", $Query);
  171. }
  172. function session_gencode() {
  173. $php3SessionID_code_length=13;
  174. session_killold();
  175. srand(time());
  176. $Puddle = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  177. for($index = 0; $index < $php3SessionID_code_length; $index++) {
  178. $sid .= substr($Puddle, (rand()%(strlen($Puddle))), 1);
  179. }
  180. // If by some miracle this id exists, return 0. It will not pass
  181. // when it is checked next.
  182. if(session_valid_session($sid)) $sid = "INVALID";
  183. return $sid;
  184. }
  185. function session_begin_session() {
  186. global $REMOTE_ADDR, $db, $tableSessions, $strNoDatabase, $cookieName;
  187. // !!!Displaying anything before outputing setcookie will cause the
  188. // header generation (and thus the cookie bake) to fail!!!
  189. // $debug=1;
  190. $sesscode = session_gencode();
  191. if($debug) printf("Codemade=%s<br>", $sesscode);
  192. $Query = "INSERT INTO $tableSessions (id, LastAction, ip)
  193. VALUES ('$sesscode', now(), '$REMOTE_ADDR')";
  194. if($debug) printf("Query=%s .<br>", $Query);
  195. mysql($db, $Query) or die($strNoDatabase);
  196. setcookie($cookieName, $sesscode);
  197. // no pageloads from cache or memory
  198. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
  199. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
  200. header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  201. header("Pragma: no-cache"); // HTTP/1.0
  202. return $sesscode;
  203. }
  204. function session_return_var($variable) {
  205. global $php3SessionID, $db, $tableSessions, $strNoDatabase;
  206. $Query = "SELECT $variable FROM $tableSessions WHERE id='$php3SessionID'";
  207. if($debug) printf("Query=%s .<br>", $Query);
  208. $result = mysql($db, $Query) or die($strNoDatabase);
  209. $status = mysql_fetch_row($result);
  210. return $status[0];
  211. }
  212. // Checks to see if user is logged in and if so returns userID
  213. // -----------------------------------------------------------
  214. function session_logged_in() {
  215. global $php3SessionID, $db, $tableSessions, $strNoDatabase;
  216. session_killold();
  217. session_start_php3();
  218. $Query = "SELECT userID FROM $tableSessions WHERE id='$php3SessionID'";
  219. $result = mysql($db, $Query) or die($strNoDatabase);
  220. $status = mysql_fetch_row($result);
  221. return $status[0];
  222. }
  223. // AUTOMATIC SESSION DEMAND. If you do not wish to have to call session_start_php3
  224. // in every script, uncomment the following line:
  225. // session_start_php3();
  226. } // (phpversion() < 4
  227. ?>