Advertisement
Guest User

Untitled

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