Advertisement
Rojod

Session.php

Jul 5th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 27.22 KB | None | 0 0
  1. <?php
  2. // session.php
  3.  
  4. include("database.php");
  5. include("mailer.php");
  6. include("form.php");
  7.  
  8. class Session
  9. {
  10.    var $username;     //Username given on sign-up
  11.    var $userid;       //Random value generated on current login
  12.    var $userlevel;    //The level to which the user pertains
  13.    var $time;         //Time user was last active (page loaded)
  14.    var $logged_in;    //True if user is logged in, false otherwise
  15.    var $userinfo = array();  //The array holding all user info
  16.    var $url;          //The page url current being viewed
  17.    var $referrer;     //Last recorded site page viewed
  18.    /**
  19.     * Note: referrer should really only be considered the actual
  20.     * page referrer in process.php, any other time it may be
  21.     * inaccurate.
  22.     */
  23.  
  24.    /* Class constructor */
  25.    function Session(){
  26.       $this->time = time();
  27.       $this->startSession();
  28.    }
  29.  
  30.    /**
  31.     * startSession - Performs all the actions necessary to
  32.     * initialize this session object. Tries to determine if the
  33.     * the user has logged in already, and sets the variables
  34.     * accordingly. Also takes advantage of this page load to
  35.     * update the active visitors tables.
  36.     */
  37.    function startSession(){
  38.       global $database;  //The database connection
  39.       session_start();   //Tell PHP to start the session
  40.  
  41.       /* Determine if user is logged in */
  42.       $this->logged_in = $this->checkLogin();
  43.  
  44.       /**
  45.        * Set guest value to users not logged in, and update
  46.        * active guests table accordingly.
  47.        */
  48.       if(!$this->logged_in){
  49.          $this->username = $_SESSION['username'] = GUEST_NAME;
  50.          $this->userlevel = GUEST_LEVEL;
  51.          $database->addActiveGuest($_SERVER['REMOTE_ADDR'], $this->time);
  52.       }
  53.       /* Update users last active timestamp */
  54.       else{
  55.          $database->addActiveUser($this->username, $this->time);
  56.       }
  57.      
  58.       /* Remove inactive visitors from database */
  59.       $database->removeInactiveUsers();
  60.       $database->removeInactiveGuests();
  61.      
  62.       /* Set referrer page */
  63.       if(isset($_SESSION['url'])){
  64.          $this->referrer = $_SESSION['url'];
  65.       }else{
  66.          $this->referrer = "/";
  67.       }
  68.  
  69.       /* Set current url */
  70.       $this->url = $_SESSION['url'] = $_SERVER['PHP_SELF'];
  71.    }
  72.  
  73.    /**
  74.     * checkLogin - Checks if the user has already previously
  75.     * logged in, and a session with the user has already been
  76.     * established. Also checks to see if user has been remembered.
  77.     * If so, the database is queried to make sure of the user's
  78.     * authenticity. Returns true if the user has logged in.
  79.     */
  80.    function checkLogin(){
  81.       global $database;  //The database connection
  82.       /* Check if user has been remembered */
  83.       if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookid'])){
  84.          $this->username = $_SESSION['username'] = $_COOKIE['cookname'];
  85.          $this->userid   = $_SESSION['userid']   = $_COOKIE['cookid'];
  86.       }
  87.  
  88.       /* Username and userid have been set and not guest */
  89.       if(isset($_SESSION['username']) && isset($_SESSION['userid']) &&
  90.          $_SESSION['username'] != GUEST_NAME){
  91.          /* Confirm that username and userid are valid */
  92.          if($database->confirmUserID($_SESSION['username'], $_SESSION['userid']) != 0){
  93.             /* Variables are incorrect, user not logged in */
  94.             unset($_SESSION['username']);
  95.             unset($_SESSION['userid']);
  96.             return false;
  97.          }
  98.  
  99.          /* User is logged in, set class variables */
  100.          $this->userinfo  = $database->getUserInfo($_SESSION['username']);
  101.          $this->username  = $this->userinfo['username'];
  102.          $this->userid    = $this->userinfo['userid'];
  103.          $this->userlevel = $this->userinfo['userlevel'];
  104.          return true;
  105.       }
  106.       /* User not logged in */
  107.       else{
  108.          return false;
  109.       }
  110.    }
  111.  
  112.    /**
  113.     * login - The user has submitted his username and password
  114.     * through the login form, this function checks the authenticity
  115.     * of that information in the database and creates the session.
  116.     * Effectively logging in the user if all goes well.
  117.     */
  118.    function login($subuser, $subpass, $subremember){
  119.       global $database, $form;  //The database and form object
  120.  
  121.       /* Username error checking */
  122.       $field = "user";  //Use field name for username
  123.       if(!$subuser || strlen($subuser = trim($subuser)) == 0){
  124.          $form->setError($field, "* Username not entered");
  125.       }
  126.       else{
  127.          /* Check if username is not alphanumeric */
  128.          if(!eregi("^([0-9a-z])*$", $subuser)){
  129.             $form->setError($field, "* Username not alphanumeric");
  130.          }
  131.       }
  132.  
  133.       /* Password error checking */
  134.       $field = "pass";  //Use field name for password
  135.       if(!$subpass){
  136.          $form->setError($field, "* Password not entered");
  137.       }
  138.      
  139.       /* Return if form errors exist */
  140.       if($form->num_errors > 0){
  141.          return false;
  142.       }
  143.  
  144.       /* Checks that username is in database and password is correct */
  145.       $subuser = stripslashes($subuser);
  146.       $result = $database->confirmUserPass($subuser, md5($subpass));
  147.  
  148.       /* Check error codes */
  149.       if($result == 1){
  150.          $field = "user";
  151.          $form->setError($field, "* Username not found");
  152.       }
  153.       else if($result == 2){
  154.          $field = "pass";
  155.          $form->setError($field, "* Invalid password");
  156.       }
  157.      
  158.       /* Return if form errors exist */
  159.       if($form->num_errors > 0){
  160.          return false;
  161.       }
  162.  
  163.       /* Username and password correct, register session variables */
  164.       $this->userinfo  = $database->getUserInfo($subuser);
  165.       $this->username  = $_SESSION['username'] = $this->userinfo['username'];
  166.       $this->userid    = $_SESSION['userid']   = $this->generateRandID();
  167.       $this->userlevel = $this->userinfo['userlevel'];
  168.      
  169.       /* Insert userid into database and update active users table */
  170.       $database->updateUserField($this->username, "userid", $this->userid);
  171.       $database->addActiveUser($this->username, $this->time);
  172.       $database->removeActiveGuest($_SERVER['REMOTE_ADDR']);
  173.  
  174.       /**
  175.        * This is the cool part: the user has requested that we remember that
  176.        * he's logged in, so we set two cookies. One to hold his username,
  177.        * and one to hold his random value userid. It expires by the time
  178.        * specified in constants.php. Now, next time he comes to our site, we will
  179.        * log him in automatically, but only if he didn't log out before he left.
  180.        */
  181.       if($subremember){
  182.          setcookie("cookname", $this->username, time()+COOKIE_EXPIRE, COOKIE_PATH);
  183.          setcookie("cookid",   $this->userid,   time()+COOKIE_EXPIRE, COOKIE_PATH);
  184.       }
  185.  
  186.       /* Login completed successfully */
  187.       return true;
  188.    }
  189.  
  190.    /**
  191.     * logout - Gets called when the user wants to be logged out of the
  192.     * website. It deletes any cookies that were stored on the users
  193.     * computer as a result of him wanting to be remembered, and also
  194.     * unsets session variables and demotes his user level to guest.
  195.     */
  196.    function logout(){
  197.       global $database;  //The database connection
  198.       /**
  199.        * Delete cookies - the time must be in the past,
  200.        * so just negate what you added when creating the
  201.        * cookie.
  202.        */
  203.       if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookid'])){
  204.          setcookie("cookname", "", time()-COOKIE_EXPIRE, COOKIE_PATH);
  205.          setcookie("cookid",   "", time()-COOKIE_EXPIRE, COOKIE_PATH);
  206.       }
  207.  
  208.       /* Unset PHP session variables */
  209.       unset($_SESSION['username']);
  210.       unset($_SESSION['userid']);
  211.  
  212.       /* Reflect fact that user has logged out */
  213.       $this->logged_in = false;
  214.      
  215.       /**
  216.        * Remove from active users table and add to
  217.        * active guests tables.
  218.        */
  219.       $database->removeActiveUser($this->username);
  220.       $database->addActiveGuest($_SERVER['REMOTE_ADDR'], $this->time);
  221.      
  222.       /* Set user level to guest */
  223.       $this->username  = GUEST_NAME;
  224.       $this->userlevel = GUEST_LEVEL;
  225.    }
  226.  
  227.    /**
  228.     * register - Gets called when the user has just submitted the
  229.     * registration form. Determines if there were any errors with
  230.     * the entry fields, if so, it records the errors and returns
  231.     * 1. If no errors were found, it registers the new user and
  232.     * returns 0. Returns 2 if registration failed.
  233.     */
  234.    function register($subuser, $subpass, $subemail){
  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.       /* Email error checking */
  293.       $field = "email";  //Use field name for email
  294.       if(!$subemail || strlen($subemail = trim($subemail)) == 0){
  295.          $form->setError($field, "* Email not entered");
  296.       }
  297.       else{
  298.          /* Check if valid email address */
  299.          $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
  300.                  ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
  301.                  ."\.([a-z]{2,}){1}$";
  302.          if(!eregi($regex,$subemail)){
  303.             $form->setError($field, "* Email invalid");
  304.          }
  305.          $subemail = stripslashes($subemail);
  306.       }
  307.  
  308.       /* Errors exist, have user correct them */
  309.       if($form->num_errors > 0){
  310.          return 1;  //Errors with form
  311.       }
  312.       /* No errors, add the new account to the */
  313.       else{
  314.          if($database->addNewUser($subuser, md5($subpass), $subemail)){
  315.             if(EMAIL_WELCOME){
  316.                $mailer->sendWelcome($subuser,$subemail,$subpass);
  317.             }
  318.             return 0;  //New user added succesfully
  319.          }else{
  320.             return 2;  //Registration attempt failed
  321.          }
  322.       }
  323.    }
  324.    
  325.     function SessionMasterRegister($subuser, $subpass, $subemail){
  326.      
  327.       global $database, $form, $mailer;  //The database, form and mailer object
  328.      
  329.       /* Username error checking */
  330.       $field = "user";  //Use field name for username
  331.       if(!$subuser || strlen($subuser = trim($subuser)) == 0){
  332.          $form->setError($field, "* Username not entered");
  333.       }
  334.       else{
  335.          /* Spruce up username, check length */
  336.          $subuser = stripslashes($subuser);
  337.          if(strlen($subuser) < 5){
  338.             $form->setError($field, "* Username below 5 characters");
  339.          }
  340.          else if(strlen($subuser) > 30){
  341.             $form->setError($field, "* Username above 30 characters");
  342.          }
  343.          /* Check if username is not alphanumeric */
  344.          else if(!eregi("^([0-9a-z])+$", $subuser)){
  345.             $form->setError($field, "* Username not alphanumeric");
  346.          }
  347.          /* Check if username is reserved */
  348.          else if(strcasecmp($subuser, GUEST_NAME) == 0){
  349.             $form->setError($field, "* Username reserved word");
  350.          }
  351.          /* Check if username is already in use */
  352.          else if($database->usernameTaken($subuser)){
  353.             $form->setError($field, "* Username already in use");
  354.          }
  355.          /* Check if username is banned */
  356.          else if($database->usernameBanned($subuser)){
  357.             $form->setError($field, "* Username banned");
  358.          }
  359.       }
  360.  
  361.       /* Password error checking */
  362.       $field = "pass";  //Use field name for password
  363.       if(!$subpass){
  364.          $form->setError($field, "* Password not entered");
  365.       }
  366.       else{
  367.          /* Spruce up password and check length*/
  368.          $subpass = stripslashes($subpass);
  369.          if(strlen($subpass) < 4){
  370.             $form->setError($field, "* Password too short");
  371.          }
  372.          /* Check if password is not alphanumeric */
  373.          else if(!eregi("^([0-9a-z])+$", ($subpass = trim($subpass)))){
  374.             $form->setError($field, "* Password not alphanumeric");
  375.          }
  376.          /**
  377.           * Note: I trimmed the password only after I checked the length
  378.           * because if you fill the password field up with spaces
  379.           * it looks like a lot more characters than 4, so it looks
  380.           * kind of stupid to report "password too short".
  381.           */
  382.       }
  383.      
  384.       /* Email error checking */
  385.       $field = "email";  //Use field name for email
  386.       if(!$subemail || strlen($subemail = trim($subemail)) == 0){
  387.          $form->setError($field, "* Email not entered");
  388.       }
  389.       else{
  390.          /* Check if valid email address */
  391.          $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
  392.                  ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
  393.                  ."\.([a-z]{2,}){1}$";
  394.          if(!eregi($regex,$subemail)){
  395.             $form->setError($field, "* Email invalid");
  396.          }
  397.          $subemail = stripslashes($subemail);
  398.       }
  399.  
  400.       /* Errors exist, have user correct them */
  401.       if($form->num_errors > 0){
  402.          return 1;  //Errors with form
  403.       }
  404.       /* No errors, add the new account to the */
  405.       else{
  406.       //THE NAME OF THE CURRENT USER THE PARENT...
  407.       $parent = $this->username;
  408.          if($database->addNewMaster($subuser, md5($subpass), $subemail, $parent)){
  409.             if(EMAIL_WELCOME){
  410.                $mailer->sendWelcome($subuser,$subemail,$subpass);
  411.             }
  412.             return 0;  //New user added succesfully
  413.          }else{
  414.             return 2;  //Registration attempt failed
  415.          }
  416.       }
  417.    }
  418.    
  419.    
  420.   function SessionMemberRegister($subuser, $subpass, $subemail){
  421.      
  422.       global $database, $form, $mailer;  //The database, form and mailer object
  423.      
  424.       /* Username error checking */
  425.       $field = "user";  //Use field name for username
  426.       if(!$subuser || strlen($subuser = trim($subuser)) == 0){
  427.          $form->setError($field, "* Username not entered");
  428.       }
  429.       else{
  430.          /* Spruce up username, check length */
  431.          $subuser = stripslashes($subuser);
  432.          if(strlen($subuser) < 5){
  433.             $form->setError($field, "* Username below 5 characters");
  434.          }
  435.          else if(strlen($subuser) > 30){
  436.             $form->setError($field, "* Username above 30 characters");
  437.          }
  438.          /* Check if username is not alphanumeric */
  439.          else if(!eregi("^([0-9a-z])+$", $subuser)){
  440.             $form->setError($field, "* Username not alphanumeric");
  441.          }
  442.          /* Check if username is reserved */
  443.          else if(strcasecmp($subuser, GUEST_NAME) == 0){
  444.             $form->setError($field, "* Username reserved word");
  445.          }
  446.          /* Check if username is already in use */
  447.          else if($database->usernameTaken($subuser)){
  448.             $form->setError($field, "* Username already in use");
  449.          }
  450.          /* Check if username is banned */
  451.          else if($database->usernameBanned($subuser)){
  452.             $form->setError($field, "* Username banned");
  453.          }
  454.       }
  455.  
  456.       /* Password error checking */
  457.       $field = "pass";  //Use field name for password
  458.       if(!$subpass){
  459.          $form->setError($field, "* Password not entered");
  460.       }
  461.       else{
  462.          /* Spruce up password and check length*/
  463.          $subpass = stripslashes($subpass);
  464.          if(strlen($subpass) < 4){
  465.             $form->setError($field, "* Password too short");
  466.          }
  467.          /* Check if password is not alphanumeric */
  468.          else if(!eregi("^([0-9a-z])+$", ($subpass = trim($subpass)))){
  469.             $form->setError($field, "* Password not alphanumeric");
  470.          }
  471.          /**
  472.           * Note: I trimmed the password only after I checked the length
  473.           * because if you fill the password field up with spaces
  474.           * it looks like a lot more characters than 4, so it looks
  475.           * kind of stupid to report "password too short".
  476.           */
  477.       }
  478.      
  479.       /* Email error checking */
  480.       $field = "email";  //Use field name for email
  481.       if(!$subemail || strlen($subemail = trim($subemail)) == 0){
  482.          $form->setError($field, "* Email not entered");
  483.       }
  484.       else{
  485.          /* Check if valid email address */
  486.          $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
  487.                  ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
  488.                  ."\.([a-z]{2,}){1}$";
  489.          if(!eregi($regex,$subemail)){
  490.             $form->setError($field, "* Email invalid");
  491.          }
  492.          $subemail = stripslashes($subemail);
  493.       }
  494.  
  495.       /* Errors exist, have user correct them */
  496.       if($form->num_errors > 0){
  497.          return 1;  //Errors with form
  498.       }
  499.       /* No errors, add the new account to the */
  500.       else{
  501.       //THE NAME OF THE CURRENT USER THE PARENT...
  502.       $parent = $this->username;
  503.          if($database->addNewMember($subuser, md5($subpass), $subemail, $parent)){
  504.             if(EMAIL_WELCOME){
  505.                $mailer->sendWelcome($subuser,$subemail,$subpass);
  506.             }
  507.             return 0;  //New user added succesfully
  508.          }else{
  509.             return 2;  //Registration attempt failed
  510.          }
  511.       }
  512.    }
  513.    
  514.    
  515.    function SessionAgentRegister($subuser, $subpass, $subemail){
  516.      
  517.       global $database, $form, $mailer;  //The database, form and mailer object
  518.      
  519.       /* Username error checking */
  520.       $field = "user";  //Use field name for username
  521.       if(!$subuser || strlen($subuser = trim($subuser)) == 0){
  522.          $form->setError($field, "* Username not entered");
  523.       }
  524.       else{
  525.          /* Spruce up username, check length */
  526.          $subuser = stripslashes($subuser);
  527.          if(strlen($subuser) < 5){
  528.             $form->setError($field, "* Username below 5 characters");
  529.          }
  530.          else if(strlen($subuser) > 30){
  531.             $form->setError($field, "* Username above 30 characters");
  532.          }
  533.          /* Check if username is not alphanumeric */
  534.          else if(!eregi("^([0-9a-z])+$", $subuser)){
  535.             $form->setError($field, "* Username not alphanumeric");
  536.          }
  537.          /* Check if username is reserved */
  538.          else if(strcasecmp($subuser, GUEST_NAME) == 0){
  539.             $form->setError($field, "* Username reserved word");
  540.          }
  541.          /* Check if username is already in use */
  542.          else if($database->usernameTaken($subuser)){
  543.             $form->setError($field, "* Username already in use");
  544.          }
  545.          /* Check if username is banned */
  546.          else if($database->usernameBanned($subuser)){
  547.             $form->setError($field, "* Username banned");
  548.          }
  549.       }
  550.  
  551.       /* Password error checking */
  552.       $field = "pass";  //Use field name for password
  553.       if(!$subpass){
  554.          $form->setError($field, "* Password not entered");
  555.       }
  556.       else{
  557.          /* Spruce up password and check length*/
  558.          $subpass = stripslashes($subpass);
  559.          if(strlen($subpass) < 4){
  560.             $form->setError($field, "* Password too short");
  561.          }
  562.          /* Check if password is not alphanumeric */
  563.          else if(!eregi("^([0-9a-z])+$", ($subpass = trim($subpass)))){
  564.             $form->setError($field, "* Password not alphanumeric");
  565.          }
  566.          /**
  567.           * Note: I trimmed the password only after I checked the length
  568.           * because if you fill the password field up with spaces
  569.           * it looks like a lot more characters than 4, so it looks
  570.           * kind of stupid to report "password too short".
  571.           */
  572.       }
  573.      
  574.       /* Email error checking */
  575.       $field = "email";  //Use field name for email
  576.       if(!$subemail || strlen($subemail = trim($subemail)) == 0){
  577.          $form->setError($field, "* Email not entered");
  578.       }
  579.       else{
  580.          /* Check if valid email address */
  581.          $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
  582.                  ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
  583.                  ."\.([a-z]{2,}){1}$";
  584.          if(!eregi($regex,$subemail)){
  585.             $form->setError($field, "* Email invalid");
  586.          }
  587.          $subemail = stripslashes($subemail);
  588.       }
  589.  
  590.       /* Errors exist, have user correct them */
  591.       if($form->num_errors > 0){
  592.          return 1;  //Errors with form
  593.       }
  594.       /* No errors, add the new account to the */
  595.       else{
  596.       //THE NAME OF THE CURRENT USER THE PARENT...
  597.       $parent = $this->username;
  598.          if($database->addNewAgent($subuser, md5($subpass), $subemail, $parent)){
  599.             if(EMAIL_WELCOME){
  600.                $mailer->sendWelcome($subuser,$subemail,$subpass);
  601.             }
  602.             return 0;  //New user added succesfully
  603.          }else{
  604.             return 2;  //Registration attempt failed
  605.          }
  606.       }
  607.    }
  608.    /**
  609.     * editAccount - Attempts to edit the user's account information
  610.     * including the password, which it first makes sure is correct
  611.     * if entered, if so and the new password is in the right
  612.     * format, the change is made. All other fields are changed
  613.     * automatically.
  614.     */
  615.    function editAccount($subcurpass, $subnewpass, $subemail){
  616.       global $database, $form;  //The database and form object
  617.       /* New password entered */
  618.       if($subnewpass){
  619.          /* Current Password error checking */
  620.          $field = "curpass";  //Use field name for current password
  621.          if(!$subcurpass){
  622.             $form->setError($field, "* Current Password not entered");
  623.          }
  624.          else{
  625.             /* Check if password too short or is not alphanumeric */
  626.             $subcurpass = stripslashes($subcurpass);
  627.             if(strlen($subcurpass) < 4 ||
  628.                !eregi("^([0-9a-z])+$", ($subcurpass = trim($subcurpass)))){
  629.                $form->setError($field, "* Current Password incorrect");
  630.             }
  631.             /* Password entered is incorrect */
  632.             if($database->confirmUserPass($this->username,md5($subcurpass)) != 0){
  633.                $form->setError($field, "* Current Password incorrect");
  634.             }
  635.          }
  636.          
  637.          /* New Password error checking */
  638.          $field = "newpass";  //Use field name for new password
  639.          /* Spruce up password and check length*/
  640.          $subpass = stripslashes($subnewpass);
  641.          if(strlen($subnewpass) < 4){
  642.             $form->setError($field, "* New Password too short");
  643.          }
  644.          /* Check if password is not alphanumeric */
  645.          else if(!eregi("^([0-9a-z])+$", ($subnewpass = trim($subnewpass)))){
  646.             $form->setError($field, "* New Password not alphanumeric");
  647.          }
  648.       }
  649.       /* Change password attempted */
  650.       else if($subcurpass){
  651.          /* New Password error reporting */
  652.          $field = "newpass";  //Use field name for new password
  653.          $form->setError($field, "* New Password not entered");
  654.       }
  655.      
  656.       /* Email error checking */
  657.       $field = "email";  //Use field name for email
  658.       if($subemail && strlen($subemail = trim($subemail)) > 0){
  659.          /* Check if valid email address */
  660.          $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
  661.                  ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
  662.                  ."\.([a-z]{2,}){1}$";
  663.          if(!eregi($regex,$subemail)){
  664.             $form->setError($field, "* Email invalid");
  665.          }
  666.          $subemail = stripslashes($subemail);
  667.       }
  668.      
  669.       /* Errors exist, have user correct them */
  670.       if($form->num_errors > 0){
  671.          return false;  //Errors with form
  672.       }
  673.      
  674.       /* Update password since there were no errors */
  675.       if($subcurpass && $subnewpass){
  676.          $database->updateUserField($this->username,"password",md5($subnewpass));
  677.       }
  678.      
  679.       /* Change Email */
  680.       if($subemail){
  681.          $database->updateUserField($this->username,"email",$subemail);
  682.       }
  683.      
  684.       /* Success! */
  685.       return true;
  686.    }
  687.    
  688.    /**
  689.     * isAdmin - Returns true if currently logged in user is
  690.     * an administrator, false otherwise.
  691.     */
  692.    function isAdmin(){
  693.       return ($this->userlevel == ADMIN_LEVEL ||
  694.               $this->username  == ADMIN_NAME);
  695.    }
  696.    
  697.     function isMaster(){
  698.       return ($this->userlevel == MASTER_LEVEL);
  699.    }
  700.    
  701.     function isAgent(){
  702.       return ($this->userlevel == AGENT_LEVEL);
  703.     }
  704.    
  705.     function isMember(){
  706.       return ($this->userlevel == AGENT_MEMBER_LEVEL);
  707.    }
  708.    
  709.  
  710.    /**
  711.     * generateRandID - Generates a string made up of randomized
  712.     * letters (lower and upper case) and digits and returns
  713.     * the md5 hash of it to be used as a userid.
  714.     */
  715.    function generateRandID(){
  716.       return md5($this->generateRandStr(16));
  717.    }
  718.    
  719.    /**
  720.     * generateRandStr - Generates a string made up of randomized
  721.     * letters (lower and upper case) and digits, the length
  722.     * is a specified parameter.
  723.     */
  724.    function generateRandStr($length){
  725.       $randstr = "";
  726.       for($i=0; $i<$length; $i++){
  727.          $randnum = mt_rand(0,61);
  728.          if($randnum < 10){
  729.             $randstr .= chr($randnum+48);
  730.          }else if($randnum < 36){
  731.             $randstr .= chr($randnum+55);
  732.          }else{
  733.             $randstr .= chr($randnum+61);
  734.          }
  735.       }
  736.       return $randstr;
  737.    }
  738. };
  739.  
  740.  
  741. /**
  742.  * Initialize session object - This must be initialized before
  743.  * the form object because the form uses session variables,
  744.  * which cannot be accessed unless the session has started.
  745.  */
  746. $session = new Session;
  747.  
  748. /* Initialize form object */
  749. $form = new Form;
  750.  
  751. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement