Advertisement
Guest User

Untitled

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