Advertisement
Guest User

Untitled

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