Advertisement
Guest User

Untitled

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