Advertisement
Guest User

Untitled

a guest
Jul 8th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.89 KB | None | 0 0
  1. <?
  2. include("constants.php");
  3.  
  4. class MySQLDB
  5.  
  6. {
  7.  
  8. var $connection; //The MySQL database connection
  9.  
  10. var $num_active_users; //Number of active users viewing site
  11.  
  12. var $num_active_guests; //Number of active guests viewing site
  13.  
  14. var $num_members; //Number of signed-up users
  15.  
  16. /* Note: call getNumMembers() to access $num_members! */
  17.  
  18.  
  19.  
  20. /* Class constructor */
  21.  
  22. function MySQLDB(){
  23.  
  24. /* Make connection to database */
  25.  
  26. $this->connection = mysql_connect(localhost, igrerero_travian, leonidas@13) or die(mysql_error());
  27.  
  28. mysql_select_db(igrerero_travian, $this->connection) or die(mysql_error());
  29.  
  30.  
  31.  
  32. /**
  33.  
  34. * Only query database to find out number of members
  35.  
  36. * when getNumMembers() is called for the first time,
  37.  
  38. * until then, default value set.
  39.  
  40. */
  41.  
  42. $this->num_members = -1;
  43.  
  44. if(TRACK_VISITORS){
  45.  
  46. /* Calculate number of users at site */
  47.  
  48. $this->calcNumActiveUsers();
  49.  
  50.  
  51.  
  52. /* Calculate number of guests at site */
  53.  
  54. $this->calcNumActiveGuests();
  55.  
  56. }
  57.  
  58. }
  59.  
  60.  
  61.  
  62. /**
  63.  
  64. * confirmUserPass - Checks whether or not the given
  65.  
  66. * username is in the database, if so it checks if the
  67.  
  68. * given password is the same password in the database
  69.  
  70. * for that user. If the user doesn't exist or if the
  71.  
  72. * passwords don't match up, it returns an error code
  73.  
  74. * (1 or 2). On success it returns 0.
  75.  
  76. */
  77.  
  78. function confirmUserPass($username, $password){
  79.  
  80. /* Add slashes if necessary (for query) */
  81.  
  82. if(!get_magic_quotes_gpc()) {
  83.  
  84. $username = addslashes($username);
  85.  
  86. }
  87.  
  88.  
  89.  
  90. /* Verify that user is in database */
  91.  
  92. $q = "SELECT password FROM ".TBL_USERS." WHERE username = '$username'";
  93.  
  94. $result = mysql_query($q, $this->connection);
  95.  
  96. if(!$result || (mysql_numrows($result) < 1)){
  97.  
  98. return 1; //Indicates username failure
  99.  
  100. }
  101.  
  102.  
  103.  
  104. /* Retrieve password from result, strip slashes */
  105.  
  106. $dbarray = mysql_fetch_array($result);
  107.  
  108. $dbarray['password'] = stripslashes($dbarray['password']);
  109.  
  110. $password = stripslashes($password);
  111.  
  112.  
  113.  
  114. /* Validate that password is correct */
  115.  
  116. if($password == $dbarray['password']){
  117.  
  118. return 0; //Success! Username and password confirmed
  119.  
  120. }
  121.  
  122. else{
  123.  
  124. return 2; //Indicates password failure
  125.  
  126. }
  127.  
  128. }
  129.  
  130.  
  131.  
  132. /**
  133.  
  134. * confirmUserID - Checks whether or not the given
  135.  
  136. * username is in the database, if so it checks if the
  137.  
  138. * given userid is the same userid in the database
  139.  
  140. * for that user. If the user doesn't exist or if the
  141.  
  142. * userids don't match up, it returns an error code
  143.  
  144. * (1 or 2). On success it returns 0.
  145.  
  146. */
  147.  
  148. function confirmUserID($username, $userid){
  149.  
  150. /* Add slashes if necessary (for query) */
  151.  
  152. if(!get_magic_quotes_gpc()) {
  153.  
  154. $username = addslashes($username);
  155.  
  156. }
  157.  
  158.  
  159.  
  160. /* Verify that user is in database */
  161.  
  162. $q = "SELECT userid FROM ".TBL_USERS." WHERE username = '$username'";
  163.  
  164. $result = mysql_query($q, $this->connection);
  165.  
  166. if(!$result || (mysql_numrows($result) < 1)){
  167.  
  168. return 1; //Indicates username failure
  169.  
  170. }
  171.  
  172.  
  173.  
  174. /* Retrieve userid from result, strip slashes */
  175.  
  176. $dbarray = mysql_fetch_array($result);
  177.  
  178. $dbarray['userid'] = stripslashes($dbarray['userid']);
  179.  
  180. $userid = stripslashes($userid);
  181.  
  182.  
  183.  
  184. /* Validate that userid is correct */
  185.  
  186. if($userid == $dbarray['userid']){
  187.  
  188. return 0; //Success! Username and userid confirmed
  189.  
  190. }
  191.  
  192. else{
  193.  
  194. return 2; //Indicates userid invalid
  195.  
  196. }
  197.  
  198. }
  199.  
  200.  
  201.  
  202. /**
  203.  
  204. * usernameTaken - Returns true if the username has
  205.  
  206. * been taken by another user, false otherwise.
  207.  
  208. */
  209.  
  210. function usernameTaken($username){
  211.  
  212. if(!get_magic_quotes_gpc()){
  213.  
  214. $username = addslashes($username);
  215.  
  216. }
  217.  
  218. $q = "SELECT username FROM ".TBL_USERS." WHERE username = '$username'";
  219.  
  220. $result = mysql_query($q, $this->connection);
  221.  
  222. return (mysql_numrows($result) > 0);
  223.  
  224. }
  225.  
  226.  
  227.  
  228. /**
  229.  
  230. * usernameBanned - Returns true if the username has
  231.  
  232. * been banned by the administrator.
  233.  
  234. */
  235.  
  236. function usernameBanned($username){
  237.  
  238. if(!get_magic_quotes_gpc()){
  239.  
  240. $username = addslashes($username);
  241.  
  242. }
  243.  
  244. $q = "SELECT username FROM ".TBL_BANNED_USERS." WHERE username = '$username'";
  245.  
  246. $result = mysql_query($q, $this->connection);
  247.  
  248. return (mysql_numrows($result) > 0);
  249.  
  250. }
  251.  
  252.  
  253.  
  254. /**
  255.  
  256. * addNewUser - Inserts the given (username, password, email)
  257.  
  258. * info into the database. Appropriate user level is set.
  259.  
  260. * Returns true on success, false otherwise.
  261.  
  262. */
  263.  
  264. function addNewUser($username, $password, $email, $tribe, $act){
  265.  
  266. $time = time();
  267.  
  268. /* If admin sign up, give admin user level */
  269.  
  270. if(strcasecmp($username, ADMIN_NAME) == 0){
  271. $ulevel = ADMIN_LEVEL;
  272. }else{
  273. $ulevel = USER_LEVEL;
  274. }
  275. $q = "INSERT INTO ".TBL_USERS." VALUES (0, '$username', '$password', '0', $ulevel, '$email', $time, $tribe, '$act')";
  276.  
  277. return mysql_query($q, $this->connection);
  278.  
  279. }
  280.  
  281.  
  282.  
  283. /**
  284.  
  285. * updateUserField - Updates a field, specified by the field
  286.  
  287. * parameter, in the user's row of the database.
  288.  
  289. */
  290.  
  291. function updateUserField($username, $field, $value){
  292.  
  293. $q = "UPDATE ".TBL_USERS." SET ".$field." = '$value' WHERE username = '$username'";
  294.  
  295. return mysql_query($q, $this->connection);
  296.  
  297. }
  298.  
  299.  
  300. /**
  301.  
  302. * getUserInfo - Returns the result array from a mysql
  303.  
  304. * query asking for all information stored regarding
  305.  
  306. * the given username. If query fails, NULL is returned.
  307.  
  308. */
  309.  
  310. function getUserInfo($username){
  311.  
  312. $q = "SELECT * FROM ".TBL_USERS." WHERE username = '$username'";
  313.  
  314. $result = mysql_query($q, $this->connection);
  315.  
  316. /* Error occurred, return given name by default */
  317.  
  318. if(!$result || (mysql_numrows($result) < 1)){
  319.  
  320. return NULL;
  321.  
  322. }
  323.  
  324. /* Return result array */
  325.  
  326. $dbarray = mysql_fetch_array($result);
  327.  
  328. return $dbarray;
  329.  
  330. }
  331.  
  332.  
  333.  
  334. /**
  335.  
  336. * getNumMembers - Returns the number of signed-up users
  337.  
  338. * of the website, banned members not included. The first
  339.  
  340. * time the function is called on page load, the database
  341.  
  342. * is queried, on subsequent calls, the stored result
  343.  
  344. * is returned. This is to improve efficiency, effectively
  345.  
  346. * not querying the database when no call is made.
  347.  
  348. */
  349.  
  350. function getNumMembers(){
  351.  
  352. if($this->num_members < 0){
  353.  
  354. $q = "SELECT * FROM ".TBL_USERS;
  355.  
  356. $result = mysql_query($q, $this->connection);
  357.  
  358. $this->num_members = mysql_numrows($result);
  359.  
  360. }
  361.  
  362. return $this->num_members;
  363.  
  364. }
  365.  
  366.  
  367.  
  368. /**
  369.  
  370. * calcNumActiveUsers - Finds out how many active users
  371.  
  372. * are viewing site and sets class variable accordingly.
  373.  
  374. */
  375.  
  376. function calcNumActiveUsers(){
  377.  
  378. /* Calculate number of users at site */
  379.  
  380. $q = "SELECT * FROM ".TBL_ACTIVE_USERS;
  381.  
  382. $result = mysql_query($q, $this->connection);
  383.  
  384. $this->num_active_users = mysql_numrows($result);
  385.  
  386. }
  387.  
  388.  
  389.  
  390. /**
  391.  
  392. * calcNumActiveGuests - Finds out how many active guests
  393.  
  394. * are viewing site and sets class variable accordingly.
  395.  
  396. */
  397.  
  398. function calcNumActiveGuests(){
  399.  
  400. /* Calculate number of guests at site */
  401.  
  402. $q = "SELECT * FROM ".TBL_ACTIVE_GUESTS;
  403.  
  404. $result = mysql_query($q, $this->connection);
  405.  
  406. $this->num_active_guests = mysql_numrows($result);
  407.  
  408. }
  409.  
  410.  
  411.  
  412. /**
  413.  
  414. * addActiveUser - Updates username's last active timestamp
  415.  
  416. * in the database, and also adds him to the table of
  417.  
  418. * active users, or updates timestamp if already there.
  419.  
  420. */
  421.  
  422. function addActiveUser($username, $time){
  423.  
  424. $q = "UPDATE ".TBL_USERS." SET timestamp = '$time' WHERE username = '$username'";
  425.  
  426. mysql_query($q, $this->connection);
  427.  
  428.  
  429.  
  430. if(!TRACK_VISITORS) return;
  431.  
  432. $q = "REPLACE INTO ".TBL_ACTIVE_USERS." VALUES ('$username', '$time')";
  433.  
  434. mysql_query($q, $this->connection);
  435.  
  436. $this->calcNumActiveUsers();
  437.  
  438. }
  439.  
  440.  
  441.  
  442. /* addActiveGuest - Adds guest to active guests table */
  443.  
  444. function addActiveGuest($ip, $time){
  445.  
  446. if(!TRACK_VISITORS) return;
  447.  
  448. $q = "REPLACE INTO ".TBL_ACTIVE_GUESTS." VALUES ('$ip', '$time')";
  449.  
  450. mysql_query($q, $this->connection);
  451.  
  452. $this->calcNumActiveGuests();
  453.  
  454. }
  455.  
  456.  
  457.  
  458. /* These functions are self explanatory, no need for comments */
  459.  
  460.  
  461.  
  462. /* removeActiveUser */
  463.  
  464. function removeActiveUser($username){
  465.  
  466. if(!TRACK_VISITORS) return;
  467.  
  468. $q = "DELETE FROM ".TBL_ACTIVE_USERS." WHERE username = '$username'";
  469.  
  470. mysql_query($q, $this->connection);
  471.  
  472. $this->calcNumActiveUsers();
  473.  
  474. }
  475.  
  476.  
  477.  
  478. /* removeActiveGuest */
  479.  
  480. function removeActiveGuest($ip){
  481.  
  482. if(!TRACK_VISITORS) return;
  483.  
  484. $q = "DELETE FROM ".TBL_ACTIVE_GUESTS." WHERE ip = '$ip'";
  485.  
  486. mysql_query($q, $this->connection);
  487.  
  488. $this->calcNumActiveGuests();
  489.  
  490. }
  491.  
  492.  
  493.  
  494. /* removeInactiveUsers */
  495.  
  496. function removeInactiveUsers(){
  497.  
  498. if(!TRACK_VISITORS) return;
  499.  
  500. $timeout = time()-USER_TIMEOUT*60;
  501.  
  502. $q = "DELETE FROM ".TBL_ACTIVE_USERS." WHERE timestamp < $timeout";
  503.  
  504. mysql_query($q, $this->connection);
  505.  
  506. $this->calcNumActiveUsers();
  507.  
  508. }
  509.  
  510. /* removeInactiveGuests */
  511.  
  512. function removeInactiveGuests(){
  513.  
  514. if(!TRACK_VISITORS) return;
  515.  
  516. $timeout = time()-GUEST_TIMEOUT*60;
  517.  
  518. $q = "DELETE FROM ".TBL_ACTIVE_GUESTS." WHERE timestamp < $timeout";
  519.  
  520. mysql_query($q, $this->connection);
  521.  
  522. $this->calcNumActiveGuests();
  523.  
  524. }
  525.  
  526.  
  527. /**
  528.  
  529. * updateVillageField - Updates a field, specified by the field
  530.  
  531. * parameter, in the village's row of the database.
  532.  
  533. */
  534.  
  535. function updateVillageField($uid, $field, $value){
  536.  
  537. $q = "UPDATE ".TBL_VILLAGE." SET ".$field." = '$value' WHERE owner = '$uid'";
  538.  
  539. return mysql_query($q, $this->connection);
  540.  
  541. }
  542.  
  543.  
  544. /**
  545.  
  546. * getVillageInfo - Returns the result array from a mysql
  547.  
  548. * query asking for all information stored regarding
  549.  
  550. * the given village. If query fails, NULL is returned.
  551.  
  552. */
  553.  
  554. function getVillageInfo($uid){
  555.  
  556. $q = "SELECT * FROM ".TBL_VILLAGE." WHERE owner = '$uid'";
  557.  
  558. $result = mysql_query($q, $this->connection);
  559.  
  560. /* Error occurred, return given name by default */
  561.  
  562. if(!$result || (mysql_numrows($result) < 1)){
  563.  
  564. return NULL;
  565.  
  566. }
  567.  
  568. /* Return result array */
  569.  
  570. $dbarray = mysql_fetch_array($result);
  571.  
  572. return $dbarray;
  573.  
  574. }
  575.  
  576. /**
  577.  
  578. * getVillageInfo - Returns the result array from a mysql
  579.  
  580. * query asking for all information stored regarding
  581.  
  582. * the given village. If query fails, NULL is returned.
  583.  
  584. */
  585.  
  586. function getFieldInfo($fid){
  587.  
  588. $q = "SELECT * FROM ".TBL_FIELDS." WHERE id = '$fid'";
  589.  
  590. $result = mysql_query($q, $this->connection);
  591.  
  592. /* Error occurred, return given name by default */
  593.  
  594. if(!$result || (mysql_numrows($result) < 1)){
  595.  
  596. return NULL;
  597.  
  598. }
  599.  
  600. /* Return result array */
  601.  
  602. $dbarray = mysql_fetch_array($result);
  603.  
  604. return $dbarray;
  605.  
  606. }
  607.  
  608. /**
  609.  
  610. * query - Performs the given query on the database and
  611.  
  612. * returns the result, which may be false, true or a
  613.  
  614. * resource identifier.
  615.  
  616. */
  617.  
  618. function query($query){
  619.  
  620. return mysql_query($query, $this->connection);
  621.  
  622. }
  623.  
  624. };
  625.  
  626. /* Create database connection */
  627.  
  628. $database = new MySQLDB;
  629.  
  630. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement