Advertisement
Guest User

Untitled

a guest
May 21st, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 17.09 KB | None | 0 0
  1. <?
  2. /**
  3.  * Session.php
  4.  *
  5.  * The Session class is meant to simplify the task of keeping
  6.  * track of logged in users and also guests.
  7.  *
  8.  * Written by: Jpmaster77 a.k.a. The Grandmaster of C++ (GMC)
  9.  * Last Updated: August 19, 2004
  10.  */
  11. include("database.php");
  12. include("mailer.php");
  13. include("form.php");
  14.  
  15. class Session
  16. {
  17.    var $username;     //Username given on sign-up
  18.    var $userid;       //Random value generated on current login
  19.    var $userlevel;    //The level to which the user pertains
  20.    var $time;         //Time user was last active (page loaded)
  21.    var $logged_in;    //True if user is logged in, false otherwise
  22.    var $userinfo = array();  //The array holding all user info
  23.    var $url;          //The page url current being viewed
  24.    var $referrer;     //Last recorded site page viewed
  25.    /**
  26.     * Note: referrer should really only be considered the actual
  27.     * page referrer in process.php, any other time it may be
  28.     * inaccurate.
  29.     */
  30.  
  31.    /* Class constructor */
  32.    function Session(){
  33.       $this->time = time();
  34.       $this->startSession();
  35.    }
  36.  
  37.    /**
  38.     * startSession - Performs all the actions necessary to
  39.     * initialize this session object. Tries to determine if the
  40.     * the user has logged in already, and sets the variables
  41.     * accordingly. Also takes advantage of this page load to
  42.     * update the active visitors tables.
  43.     */
  44.    function startSession(){
  45.       global $database;  //The database connection
  46.       session_start();   //Tell PHP to start the session
  47.  
  48.       /* Determine if user is logged in */
  49.       $this->logged_in = $this->checkLogin();
  50.  
  51.       /**
  52.        * Set guest value to users not logged in, and update
  53.        * active guests table accordingly.
  54.        */
  55.       if(!$this->logged_in){
  56.          $this->username = $_SESSION['username'] = GUEST_NAME;
  57.          $this->userlevel = GUEST_LEVEL;
  58.          $database->addActiveGuest($_SERVER['REMOTE_ADDR'], $this->time);
  59.       }
  60.       /* Update users last active timestamp */
  61.       else{
  62.          $database->addActiveUser($this->username, $this->time);
  63.       }
  64.      
  65.       /* Remove inactive visitors from database */
  66.       $database->removeInactiveUsers();
  67.       $database->removeInactiveGuests();
  68.      
  69.       /* Set referrer page */
  70.       if(isset($_SESSION['url'])){
  71.          $this->referrer = $_SESSION['url'];
  72.       }else{
  73.          $this->referrer = "/";
  74.       }
  75.  
  76.       /* Set current url */
  77.       $this->url = $_SESSION['url'] = $_SERVER['PHP_SELF'];
  78.    }
  79.  
  80.    /**
  81.     * checkLogin - Checks if the user has already previously
  82.     * logged in, and a session with the user has already been
  83.     * established. Also checks to see if user has been remembered.
  84.     * If so, the database is queried to make sure of the user's
  85.     * authenticity. Returns true if the user has logged in.
  86.     */
  87.    function checkLogin(){
  88.       global $database;  //The database connection
  89.       /* Check if user has been remembered */
  90.       if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookid'])){
  91.          $this->username = $_SESSION['username'] = $_COOKIE['cookname'];
  92.          $this->userid   = $_SESSION['userid']   = $_COOKIE['cookid'];
  93.       }
  94.  
  95.       /* Username and userid have been set and not guest */
  96.       if(isset($_SESSION['username']) && isset($_SESSION['userid']) &&
  97.          $_SESSION['username'] != GUEST_NAME){
  98.          /* Confirm that username and userid are valid */
  99.          if($database->confirmUserID($_SESSION['username'], $_SESSION['userid']) != 0){
  100.             /* Variables are incorrect, user not logged in */
  101.             unset($_SESSION['username']);
  102.             unset($_SESSION['userid']);
  103.             return false;
  104.          }
  105.  
  106.          /* User is logged in, set class variables */
  107.          $this->userinfo  = $database->getUserInfo($_SESSION['username']);
  108.          $this->username  = $this->userinfo['username'];
  109.          $this->userid    = $this->userinfo['userid'];
  110.          $this->userlevel = $this->userinfo['userlevel'];
  111.          return true;
  112.       }
  113.       /* User not logged in */
  114.       else{
  115.          return false;
  116.       }
  117.    }
  118.  
  119.    /**
  120.     * login - The user has submitted his username and password
  121.     * through the login form, this function checks the authenticity
  122.     * of that information in the database and creates the session.
  123.     * Effectively logging in the user if all goes well.
  124.     */
  125.    function login($subuser, $subpass, $subremember){
  126.       global $database, $form;  //The database and form object
  127.  
  128.       /* Username error checking */
  129.       $field = "user";  //Use field name for username
  130.       if(!$subuser || strlen($subuser = trim($subuser)) == 0){
  131.          $form->setError($field, "* Username not entered");
  132.       }
  133.       else{
  134.          /* Check if username is not alphanumeric */
  135.          if(!eregi("^([0-9a-z])*$", $subuser)){
  136.             $form->setError($field, "* Username not alphanumeric");
  137.          }
  138.       }
  139.  
  140.       /* Password error checking */
  141.       $field = "pass";  //Use field name for password
  142.       if(!$subpass){
  143.          $form->setError($field, "* Password not entered");
  144.       }
  145.      
  146.       /* Return if form errors exist */
  147.       if($form->num_errors > 0){
  148.          return false;
  149.       }
  150.  
  151.       /* Checks that username is in database and password is correct */
  152.       $subuser = stripslashes($subuser);
  153.       $result = $database->confirmUserPass($subuser, md5($subpass));
  154.  
  155.       /* Check error codes */
  156.       if($result == 1){
  157.          $field = "user";
  158.          $form->setError($field, "* Username not found");
  159.       }
  160.       else if($result == 2){
  161.          $field = "pass";
  162.          $form->setError($field, "* Invalid password");
  163.       }
  164.      
  165.       /* Return if form errors exist */
  166.       if($form->num_errors > 0){
  167.          return false;
  168.       }
  169.  
  170.       /* Username and password correct, register session variables */
  171.       $this->userinfo  = $database->getUserInfo($subuser);
  172.       $this->username  = $_SESSION['username'] = $this->userinfo['username'];
  173.       $this->userid    = $_SESSION['userid']   = $this->generateRandID();
  174.       $this->userlevel = $this->userinfo['userlevel'];
  175.      
  176.       /* Insert userid into database and update active users table */
  177.       $database->updateUserField($this->username, "userid", $this->userid);
  178.       $database->addActiveUser($this->username, $this->time);
  179.       $database->removeActiveGuest($_SERVER['REMOTE_ADDR']);
  180.  
  181.       /**
  182.        * This is the cool part: the user has requested that we remember that
  183.        * he's logged in, so we set two cookies. One to hold his username,
  184.        * and one to hold his random value userid. It expires by the time
  185.        * specified in constants.php. Now, next time he comes to our site, we will
  186.        * log him in automatically, but only if he didn't log out before he left.
  187.        */
  188.       if($subremember){
  189.          setcookie("cookname", $this->username, time()+COOKIE_EXPIRE, COOKIE_PATH);
  190.          setcookie("cookid",   $this->userid,   time()+COOKIE_EXPIRE, COOKIE_PATH);
  191.       }
  192.  
  193.       /* Login completed successfully */
  194.       return true;
  195.    }
  196.  
  197.    /**
  198.     * logout - Gets called when the user wants to be logged out of the
  199.     * website. It deletes any cookies that were stored on the users
  200.     * computer as a result of him wanting to be remembered, and also
  201.     * unsets session variables and demotes his user level to guest.
  202.     */
  203.    function logout(){
  204.       global $database;  //The database connection
  205.       /**
  206.        * Delete cookies - the time must be in the past,
  207.        * so just negate what you added when creating the
  208.        * cookie.
  209.        */
  210.       if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookid'])){
  211.          setcookie("cookname", "", time()-COOKIE_EXPIRE, COOKIE_PATH);
  212.          setcookie("cookid",   "", time()-COOKIE_EXPIRE, COOKIE_PATH);
  213.       }
  214.  
  215.       /* Unset PHP session variables */
  216.       unset($_SESSION['username']);
  217.       unset($_SESSION['userid']);
  218.  
  219.       /* Reflect fact that user has logged out */
  220.       $this->logged_in = false;
  221.      
  222.       /**
  223.        * Remove from active users table and add to
  224.        * active guests tables.
  225.        */
  226.       $database->removeActiveUser($this->username);
  227.       $database->addActiveGuest($_SERVER['REMOTE_ADDR'], $this->time);
  228.      
  229.       /* Set user level to guest */
  230.       $this->username  = GUEST_NAME;
  231.       $this->userlevel = GUEST_LEVEL;
  232.    }
  233.  
  234.    /**
  235.     * register - Gets called when the user has just submitted the
  236.     * registration form. Determines if there were any errors with
  237.     * the entry fields, if so, it records the errors and returns
  238.     * 1. If no errors were found, it registers the new user and
  239.     * returns 0. Returns 2 if registration failed.
  240.     */
  241.    function register($subuser, $subpass, $subpass2, $subemail, $subemail2, $fname, $lname, $gender, $grade){
  242.  
  243.       global $database, $form, $mailer;  //The database, form and mailer object
  244.      
  245.       /* Username error checking */
  246.       $field = "user";  //Use field name for username
  247.       if(!$subuser || strlen($subuser = trim($subuser)) == 0){
  248.          $form->setError($field, "* Username not entered");
  249.       }
  250.       else{
  251.          /* Spruce up username, check length */
  252.          $subuser = stripslashes($subuser);
  253.          if(strlen($subuser) < 5){
  254.             $form->setError($field, "* Username below 5 characters");
  255.          }
  256.          else if(strlen($subuser) > 30){
  257.             $form->setError($field, "* Username above 30 characters");
  258.          }
  259.          /* Check if username is not alphanumeric */
  260.          else if(!eregi("^([0-9a-z])+$", $subuser)){
  261.             $form->setError($field, "* Username not alphanumeric");
  262.          }
  263.          /* Check if username is reserved */
  264.          else if(strcasecmp($subuser, GUEST_NAME) == 0){
  265.             $form->setError($field, "* Username reserved word");
  266.          }
  267.          /* Check if username is already in use */
  268.          else if($database->usernameTaken($subuser)){
  269.             $form->setError($field, "* Username already in use");
  270.          }
  271.          /* Check if username is banned */
  272.          else if($database->usernameBanned($subuser)){
  273.             $form->setError($field, "* Username banned");
  274.          }
  275.       }
  276.  
  277.       /* Password error checking */
  278.       $field = "pass";  //Use field name for password
  279.       if(!$subpass){
  280.          $form->setError($field, "* Password not entered");
  281.       }
  282.       else{
  283.          /* Spruce up password and check length*/
  284.          $subpass = stripslashes($subpass);
  285.          if(strlen($subpass) < 4){
  286.             $form->setError($field, "* Password too short");
  287.          }
  288.          /* Check if password is not alphanumeric */
  289.          else if(!eregi("^([0-9a-z])+$", ($subpass = trim($subpass)))){
  290.             $form->setError($field, "* Password not alphanumeric");
  291.          }
  292.          /**
  293.           * Note: I trimmed the password only after I checked the length
  294.           * because if you fill the password field up with spaces
  295.           * it looks like a lot more characters than 4, so it looks
  296.           * kind of stupid to report "password too short".
  297.           */
  298.       }
  299.  
  300.  
  301.  
  302.     /* Password2 error checking */
  303.         $field = "pass";  //Use field name for password
  304.         $field2 = "pass2"; // Second field for password
  305.         if(!$subpass2){
  306.          $form->setError($field2, "* Password not entered");
  307.       }
  308.       else{
  309.          $subpass = stripslashes($subpass);
  310.          $subpass2 = stripslashes($subpass2);
  311.          if($subpass$subpass2){
  312.             $form->setError($field, "* Passwords does not match");
  313.          } 
  314.       }
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.      
  325.       /* Email error checking */
  326.       $field = "email";  //Use field name for email
  327.       if(!$subemail || strlen($subemail = trim($subemail)) == 0){
  328.          $form->setError($field, "* Email not entered");
  329.       }
  330.       else{
  331.          /* Check if valid email address */
  332.          $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
  333.                  ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
  334.                  ."\.([a-z]{2,}){1}$";
  335.          if(!eregi($regex,$subemail)){
  336.             $form->setError($field, "* Email invalid");
  337.          }
  338.          $subemail = stripslashes($subemail);
  339.       }
  340.  
  341.  
  342.       /* Errors exist, have user correct them */
  343.       if($form->num_errors > 0){
  344.          return 1;  //Errors with form
  345.       }
  346.       /* No errors, add the new account to the */
  347.       else{
  348.          if($database->addNewUser($subuser, md5($subpass), $subemail, $fname, $lname, $gender, $grade)){
  349.             if(EMAIL_WELCOME){
  350.                $mailer->sendWelcome($subuser,$subemail,$subpass);
  351.             }
  352.             return 0;  //New user added succesfully
  353.          }else{
  354.             return 2;  //Registration attempt failed
  355.          }
  356.       }
  357.    }
  358.    
  359.    /**
  360.     * editAccount - Attempts to edit the user's account information
  361.     * including the password, which it first makes sure is correct
  362.     * if entered, if so and the new password is in the right
  363.     * format, the change is made. All other fields are changed
  364.     * automatically.
  365.     */
  366.    function editAccount($subcurpass, $subnewpass, $subemail){
  367.       global $database, $form;  //The database and form object
  368.       /* New password entered */
  369.       if($subnewpass){
  370.          /* Current Password error checking */
  371.          $field = "curpass";  //Use field name for current password
  372.          if(!$subcurpass){
  373.             $form->setError($field, "* Current Password not entered");
  374.          }
  375.          else{
  376.             /* Check if password too short or is not alphanumeric */
  377.             $subcurpass = stripslashes($subcurpass);
  378.             if(strlen($subcurpass) < 4 ||
  379.                !eregi("^([0-9a-z])+$", ($subcurpass = trim($subcurpass)))){
  380.                $form->setError($field, "* Current Password incorrect");
  381.             }
  382.             /* Password entered is incorrect */
  383.             if($database->confirmUserPass($this->username,md5($subcurpass)) != 0){
  384.                $form->setError($field, "* Current Password incorrect");
  385.             }
  386.          }
  387.          
  388.          /* New Password error checking */
  389.          $field = "newpass";  //Use field name for new password
  390.          /* Spruce up password and check length*/
  391.          $subpass = stripslashes($subnewpass);
  392.          if(strlen($subnewpass) < 4){
  393.             $form->setError($field, "* New Password too short");
  394.          }
  395.          /* Check if password is not alphanumeric */
  396.          else if(!eregi("^([0-9a-z])+$", ($subnewpass = trim($subnewpass)))){
  397.             $form->setError($field, "* New Password not alphanumeric");
  398.          }
  399.       }
  400.       /* Change password attempted */
  401.       else if($subcurpass){
  402.          /* New Password error reporting */
  403.          $field = "newpass";  //Use field name for new password
  404.          $form->setError($field, "* New Password not entered");
  405.       }
  406.      
  407.       /* Email error checking */
  408.       $field = "email";  //Use field name for email
  409.       if($subemail && strlen($subemail = trim($subemail)) > 0){
  410.          /* Check if valid email address */
  411.          $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
  412.                  ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
  413.                  ."\.([a-z]{2,}){1}$";
  414.          if(!eregi($regex,$subemail)){
  415.             $form->setError($field, "* Email invalid");
  416.          }
  417.          $subemail = stripslashes($subemail);
  418.       }
  419.      
  420.       /* Errors exist, have user correct them */
  421.       if($form->num_errors > 0){
  422.          return false;  //Errors with form
  423.       }
  424.      
  425.       /* Update password since there were no errors */
  426.       if($subcurpass && $subnewpass){
  427.          $database->updateUserField($this->username,"password",md5($subnewpass));
  428.       }
  429.      
  430.       /* Change Email */
  431.       if($subemail){
  432.          $database->updateUserField($this->username,"email",$subemail);
  433.       }
  434.      
  435.       /* Success! */
  436.       return true;
  437.    }
  438.    
  439.    /**
  440.     * isAdmin - Returns true if currently logged in user is
  441.     * an administrator, false otherwise.
  442.     */
  443.    function isAdmin(){
  444.       return ($this->userlevel == ADMIN_LEVEL ||
  445.               $this->username  == ADMIN_NAME);
  446.    }
  447.    
  448.    /**
  449.     * generateRandID - Generates a string made up of randomized
  450.     * letters (lower and upper case) and digits and returns
  451.     * the md5 hash of it to be used as a userid.
  452.     */
  453.    function generateRandID(){
  454.       return md5($this->generateRandStr(16));
  455.    }
  456.    
  457.    /**
  458.     * generateRandStr - Generates a string made up of randomized
  459.     * letters (lower and upper case) and digits, the length
  460.     * is a specified parameter.
  461.     */
  462.    function generateRandStr($length){
  463.       $randstr = "";
  464.       for($i=0; $i<$length; $i++){
  465.          $randnum = mt_rand(0,61);
  466.          if($randnum < 10){
  467.             $randstr .= chr($randnum+48);
  468.          }else if($randnum < 36){
  469.             $randstr .= chr($randnum+55);
  470.          }else{
  471.             $randstr .= chr($randnum+61);
  472.          }
  473.       }
  474.       return $randstr;
  475.    }
  476. };
  477.  
  478.  
  479. /**
  480.  * Initialize session object - This must be initialized before
  481.  * the form object because the form uses session variables,
  482.  * which cannot be accessed unless the session has started.
  483.  */
  484. $session = new Session;
  485.  
  486. /* Initialize form object */
  487. $form = new Form;
  488.  
  489. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement