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

/ collman/ta_logindatabase.inc

http://collman.googlecode.com/
PHP | 293 lines | 158 code | 32 blank | 103 comment | 24 complexity | 6445643e7c69083ce200f836b190e1ad MD5 | raw file
  1. <?php
  2. /**
  3. * Database.php
  4. *
  5. * The Database class is meant to simplify the task of accessing
  6. * information from the website's database.
  7. *
  8. * Written by: Jpmaster77 a.k.a. The Grandmaster of C++ (GMC)
  9. * Last Updated: August 17, 2004
  10. */
  11. include("ta_loginconstants.inc");
  12. class MySQLDB
  13. {
  14. var $connection; //The MySQL database connection
  15. var $num_active_users; //Number of active users viewing site
  16. var $num_active_guests; //Number of active guests viewing site
  17. var $num_members; //Number of signed-up users
  18. /* Note: call getNumMembers() to access $num_members! */
  19. /* Class constructor */
  20. function MySQLDB(){
  21. /* Make connection to database */
  22. $this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
  23. mysql_select_db(DB_NAME, $this->connection) or die(mysql_error());
  24. /**
  25. * Only query database to find out number of members
  26. * when getNumMembers() is called for the first time,
  27. * until then, default value set.
  28. */
  29. $this->num_members = -1;
  30. if(TRACK_VISITORS){
  31. /* Calculate number of users at site */
  32. $this->calcNumActiveUsers();
  33. /* Calculate number of guests at site */
  34. $this->calcNumActiveGuests();
  35. }
  36. }
  37. /**
  38. * confirmUserPass - Checks whether or not the given
  39. * username is in the database, if so it checks if the
  40. * given password is the same password in the database
  41. * for that user. If the user doesn't exist or if the
  42. * passwords don't match up, it returns an error code
  43. * (1 or 2). On success it returns 0.
  44. */
  45. function confirmUserPass($username, $password){
  46. /* Add slashes if necessary (for query) */
  47. if(!get_magic_quotes_gpc()) {
  48. $username = addslashes($username);
  49. }
  50. /* Verify that user is in database */
  51. $q = "SELECT password FROM ".TBL_USERS." WHERE username = '$username'";
  52. $result = mysql_query($q, $this->connection);
  53. if(!$result || (mysql_numrows($result) < 1)){
  54. return 1; //Indicates username failure
  55. }
  56. /* Retrieve password from result, strip slashes */
  57. $dbarray = mysql_fetch_array($result);
  58. $dbarray['password'] = stripslashes($dbarray['password']);
  59. $password = stripslashes($password);
  60. /* Validate that password is correct */
  61. if($password == $dbarray['password']){
  62. return 0; //Success! Username and password confirmed
  63. }
  64. else{
  65. return 2; //Indicates password failure
  66. }
  67. }
  68. /**
  69. * confirmUserID - Checks whether or not the given
  70. * username is in the database, if so it checks if the
  71. * given userid is the same userid in the database
  72. * for that user. If the user doesn't exist or if the
  73. * userids don't match up, it returns an error code
  74. * (1 or 2). On success it returns 0.
  75. */
  76. function confirmUserID($username, $userid){
  77. /* Add slashes if necessary (for query) */
  78. if(!get_magic_quotes_gpc()) {
  79. $username = addslashes($username);
  80. }
  81. /* Verify that user is in database */
  82. $q = "SELECT userid FROM ".TBL_USERS." WHERE username = '$username'";
  83. $result = mysql_query($q, $this->connection);
  84. if(!$result || (mysql_numrows($result) < 1)){
  85. return 1; //Indicates username failure
  86. }
  87. /* Retrieve userid from result, strip slashes */
  88. $dbarray = mysql_fetch_array($result);
  89. $dbarray['userid'] = stripslashes($dbarray['userid']);
  90. $userid = stripslashes($userid);
  91. /* Validate that userid is correct */
  92. if($userid == $dbarray['userid']){
  93. return 0; //Success! Username and userid confirmed
  94. }
  95. else{
  96. return 2; //Indicates userid invalid
  97. }
  98. }
  99. /**
  100. * usernameTaken - Returns true if the username has
  101. * been taken by another user, false otherwise.
  102. */
  103. function usernameTaken($username){
  104. if(!get_magic_quotes_gpc()){
  105. $username = addslashes($username);
  106. }
  107. $q = "SELECT username FROM ".TBL_USERS." WHERE username = '$username'";
  108. $result = mysql_query($q, $this->connection);
  109. return (mysql_numrows($result) > 0);
  110. }
  111. /**
  112. * usernameBanned - Returns true if the username has
  113. * been banned by the administrator.
  114. */
  115. function usernameBanned($username){
  116. if(!get_magic_quotes_gpc()){
  117. $username = addslashes($username);
  118. }
  119. $q = "SELECT username FROM ".TBL_BANNED_USERS." WHERE username = '$username'";
  120. $result = mysql_query($q, $this->connection);
  121. return (mysql_numrows($result) > 0);
  122. }
  123. /**
  124. * addNewUser - Inserts the given (username, password, email)
  125. * info into the database. Appropriate user level is set.
  126. * Returns true on success, false otherwise.
  127. */
  128. function addNewUser($username, $password, $email){
  129. $time = time();
  130. /* If admin sign up, give admin user level */
  131. if(strcasecmp($username, ADMIN_NAME) == 0){
  132. $ulevel = ADMIN_LEVEL;
  133. }else{
  134. $ulevel = USER_LEVEL;
  135. }
  136. $q = "INSERT INTO ".TBL_USERS." VALUES ('$username', '$password', '0', $ulevel, '$email', '$userlanguage', $time)";
  137. return mysql_query($q, $this->connection);
  138. }
  139. /**
  140. * updateUserField - Updates a field, specified by the field
  141. * parameter, in the user's row of the database.
  142. */
  143. function updateUserField($username, $field, $value){
  144. $q = "UPDATE ".TBL_USERS." SET ".$field." = '$value' WHERE username = '$username'";
  145. return mysql_query($q, $this->connection);
  146. }
  147. /**
  148. * getUserInfo - Returns the result array from a mysql
  149. * query asking for all information stored regarding
  150. * the given username. If query fails, NULL is returned.
  151. */
  152. function getUserInfo($username){
  153. $q = "SELECT * FROM ".TBL_USERS." WHERE username = '$username'";
  154. $result = mysql_query($q, $this->connection);
  155. /* Error occurred, return given name by default */
  156. if(!$result || (mysql_numrows($result) < 1)){
  157. return NULL;
  158. }
  159. /* Return result array */
  160. $dbarray = mysql_fetch_array($result);
  161. return $dbarray;
  162. }
  163. /**
  164. * getNumMembers - Returns the number of signed-up users
  165. * of the website, banned members not included. The first
  166. * time the function is called on page load, the database
  167. * is queried, on subsequent calls, the stored result
  168. * is returned. This is to improve efficiency, effectively
  169. * not querying the database when no call is made.
  170. */
  171. function getNumMembers(){
  172. if($this->num_members < 0){
  173. $q = "SELECT * FROM ".TBL_USERS;
  174. $result = mysql_query($q, $this->connection);
  175. $this->num_members = mysql_numrows($result);
  176. }
  177. return $this->num_members;
  178. }
  179. /**
  180. * calcNumActiveUsers - Finds out how many active users
  181. * are viewing site and sets class variable accordingly.
  182. */
  183. function calcNumActiveUsers(){
  184. /* Calculate number of users at site */
  185. $q = "SELECT * FROM ".TBL_ACTIVE_USERS;
  186. $result = mysql_query($q, $this->connection);
  187. $this->num_active_users = mysql_numrows($result);
  188. }
  189. /**
  190. * calcNumActiveGuests - Finds out how many active guests
  191. * are viewing site and sets class variable accordingly.
  192. */
  193. function calcNumActiveGuests(){
  194. /* Calculate number of guests at site */
  195. $q = "SELECT * FROM ".TBL_ACTIVE_GUESTS;
  196. $result = mysql_query($q, $this->connection);
  197. $this->num_active_guests = mysql_numrows($result);
  198. }
  199. /**
  200. * addActiveUser - Updates username's last active timestamp
  201. * in the database, and also adds him to the table of
  202. * active users, or updates timestamp if already there.
  203. */
  204. function addActiveUser($username, $time){
  205. $q = "UPDATE ".TBL_USERS." SET timestamp = '$time' WHERE username = '$username'";
  206. mysql_query($q, $this->connection);
  207. if(!TRACK_VISITORS) return;
  208. $q = "REPLACE INTO ".TBL_ACTIVE_USERS." VALUES ('$username', '$time')";
  209. mysql_query($q, $this->connection);
  210. $this->calcNumActiveUsers();
  211. }
  212. /* addActiveGuest - Adds guest to active guests table */
  213. function addActiveGuest($ip, $time){
  214. if(!TRACK_VISITORS) return;
  215. $q = "REPLACE INTO ".TBL_ACTIVE_GUESTS." VALUES ('$ip', '$time')";
  216. mysql_query($q, $this->connection);
  217. $this->calcNumActiveGuests();
  218. }
  219. /* These functions are self explanatory, no need for comments */
  220. /* removeActiveUser */
  221. function removeActiveUser($username){
  222. if(!TRACK_VISITORS) return;
  223. $q = "DELETE FROM ".TBL_ACTIVE_USERS." WHERE username = '$username'";
  224. mysql_query($q, $this->connection);
  225. $this->calcNumActiveUsers();
  226. }
  227. /* removeActiveGuest */
  228. function removeActiveGuest($ip){
  229. if(!TRACK_VISITORS) return;
  230. $q = "DELETE FROM ".TBL_ACTIVE_GUESTS." WHERE ip = '$ip'";
  231. mysql_query($q, $this->connection);
  232. $this->calcNumActiveGuests();
  233. }
  234. /* removeInactiveUsers */
  235. function removeInactiveUsers(){
  236. if(!TRACK_VISITORS) return;
  237. $timeout = time()-USER_TIMEOUT*60;
  238. $q = "DELETE FROM ".TBL_ACTIVE_USERS." WHERE timestamp < $timeout";
  239. mysql_query($q, $this->connection);
  240. $this->calcNumActiveUsers();
  241. }
  242. /* removeInactiveGuests */
  243. function removeInactiveGuests(){
  244. if(!TRACK_VISITORS) return;
  245. $timeout = time()-GUEST_TIMEOUT*60;
  246. $q = "DELETE FROM ".TBL_ACTIVE_GUESTS." WHERE timestamp < $timeout";
  247. mysql_query($q, $this->connection);
  248. $this->calcNumActiveGuests();
  249. }
  250. /**
  251. * query - Performs the given query on the database and
  252. * returns the result, which may be false, true or a
  253. * resource identifier.
  254. */
  255. function query($query){
  256. return mysql_query($query, $this->connection);
  257. }
  258. };
  259. /* Create database connection */
  260. $database = new MySQLDB;
  261. ?>