Advertisement
Guest User

Untitled

a guest
Sep 15th, 2017
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.89 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Process.php
  4.  *
  5.  * The Process class is meant to simplify the task of processing
  6.  * user submitted forms, redirecting the user to the correct
  7.  * pages if errors are found, or if form is successful, either
  8.  * way. Also handles the logout procedure.
  9.  *
  10.  */
  11. include("include/session.php");
  12.  
  13. class Process
  14. {
  15.    /* Class constructor */
  16.    function Process(){
  17.       global $session;
  18.       /* User submitted login form */
  19.       if(isset($_POST['sublogin'])){
  20.          $this->procLogin();
  21.       }
  22.       /* User submitted registration form */
  23.       else if(isset($_POST['subjoin'])){
  24.          $this->procRegister();
  25.       }
  26.       /* User submitted forgot password form */
  27.       else if(isset($_POST['subforgot'])){
  28.          $this->procForgotPass();
  29.       }
  30.       /* User submitted edit account form */
  31.       else if(isset($_POST['subedit'])){
  32.          $this->procEditAccount();
  33.       }
  34.     else if(isset($_POST['videdit'])){
  35.          $this->procEditVid();
  36.       }
  37.       /**
  38.        * The only other reason user should be directed here
  39.        * is if he wants to logout, which means user is
  40.        * logged in currently.
  41.        */
  42.       else if($session->logged_in){
  43.        
  44.          $this->procLogout();
  45.       }
  46.       /**
  47.        * Should not get here, which means user is viewing this page
  48.        * by mistake and therefore is redirected.
  49.        */
  50.        else{
  51.           header("Location: main.php");
  52.          
  53.        }
  54.    }
  55.  
  56.    /**
  57.     * procLogin - Processes the user submitted login form, if errors
  58.     * are found, the user is redirected to correct the information,
  59.     * if not, the user is effectively logged in to the system.
  60.     */
  61.    function procLogin(){
  62.       global $session, $form;
  63.      
  64.       /* Login attempt */
  65.       $retval = $session->login($_POST['user'], $_POST['pass'], isset($_POST['remember']));
  66.      
  67.       /* Login successful */
  68.       if($retval){
  69.          header("Location: ".$session->referrer);
  70.       }
  71.       /* Login failed */
  72.       else{
  73.          $_SESSION['value_array'] = $_POST;
  74.          $_SESSION['error_array'] = $form->getErrorArray();
  75.          header("Location: ".$session->referrer);
  76.       }
  77.    }
  78.    
  79.    /**
  80.     * procLogout - Simply attempts to log the user out of the system
  81.     * given that there is no logout form to process.
  82.     */
  83.    function procLogout(){
  84.       global $session;
  85.      
  86.       $retval = $session->logout();
  87.       header("Location: main.php");
  88.    }
  89.    
  90.    /**
  91.     * procRegister - Processes the user submitted registration form,
  92.     * if errors are found, the user is redirected to correct the
  93.     * information, if not, the user is effectively registered with
  94.     * the system and an email is (optionally) sent to the newly
  95.     * created user.
  96.     */
  97.    function procRegister(){
  98.       global $session, $form;
  99.       /* Convert username to all lowercase (by option) */
  100.      
  101.       if(ALL_LOWERCASE){
  102.          $_POST['user'] = strtolower($_POST['user']);
  103.       }
  104.       /* Registration attempt */
  105.       $retval = $session->register($_POST['user'], $_POST['pass'], $_POST['email']);
  106.      
  107.       /* Registration Successful */
  108.       if($retval == 0){
  109.          $_SESSION['reguname'] = $_POST['user'];
  110.          $_SESSION['regsuccess'] = true;
  111.          header("Location: ".$session->referrer);
  112.       }
  113.       /* Error found with form */
  114.       else if($retval == 1){
  115.          $_SESSION['value_array'] = $_POST;
  116.          $_SESSION['error_array'] = $form->getErrorArray();
  117.          header("Location: ".$session->referrer);
  118.       }
  119.       /* Registration attempt failed */
  120.       else if($retval == 2){
  121.          $_SESSION['reguname'] = $_POST['user'];
  122.          $_SESSION['regsuccess'] = false;
  123.          header("Location: ".$session->referrer);
  124.       }
  125.    }
  126.    
  127.    /**
  128.     * procForgotPass - Validates the given username then if
  129.     * everything is fine, a new password is generated and
  130.     * emailed to the address the user gave on sign up.
  131.     */
  132.    function procForgotPass(){
  133.       global $database, $session, $mailer, $form;
  134.       /* Username error checking */
  135.      
  136.       $subuser = $_POST['user'];
  137.       $field = "user";  //Use field name for username
  138.       if(!$subuser || strlen($subuser = trim($subuser)) == 0){
  139.          $form->setError($field, "* Username not entered<br>");
  140.       }
  141.       else{
  142.          /* Make sure username is in database */
  143.          $subuser = stripslashes($subuser);
  144.          if(strlen($subuser) < 5 || strlen($subuser) > 30 ||
  145.             !eregi("^([0-9a-z])+$", $subuser) ||
  146.             (!$database->usernameTaken($subuser))){
  147.             $form->setError($field, "* Username does not exist<br>");
  148.          }
  149.       }
  150.      
  151.       /* Errors exist, have user correct them */
  152.       if($form->num_errors > 0){
  153.          $_SESSION['value_array'] = $_POST;
  154.          $_SESSION['error_array'] = $form->getErrorArray();
  155.       }
  156.       /* Generate new password and email it to user */
  157.       else{
  158.          /* Generate new password */
  159.          $newpass = $session->generateRandStr(8);
  160.          
  161.          /* Get email of user */
  162.          $usrinf = $database->getUserInfo($subuser);
  163.          $email  = $usrinf['email'];
  164.          
  165.          /* Attempt to send the email with new password */
  166.          if($mailer->sendNewPass($subuser,$email,$newpass)){
  167.             /* Email sent, update database */
  168.             $database->updateUserField($subuser, "password", md5($newpass));
  169.             $_SESSION['forgotpass'] = true;
  170.          }
  171.          /* Email failure, do not change password */
  172.          else{
  173.             $_SESSION['forgotpass'] = false;
  174.          }
  175.       }
  176.      
  177.       header("Location: ".$session->referrer);
  178.    }
  179.    
  180.    /**
  181.     * procEditAccount - Attempts to edit the user's account
  182.     * information, including the password, which must be verified
  183.     * before a change is made.
  184.     */
  185.    function procEditAccount(){
  186.       global $session, $form;
  187.       /* Account edit attempt */
  188.      
  189.       $retval = $session->editAccount($_POST['curpass'], $_POST['newpass'], $_POST['email']);
  190.  
  191.       /* Account edit successful */
  192.       if($retval){
  193.          $_SESSION['useredit'] = true;
  194.          header("Location: ".$session->referrer);
  195.       }
  196.       /* Error found with form */
  197.       else{
  198.          $_SESSION['value_array'] = $_POST;
  199.          $_SESSION['error_array'] = $form->getErrorArray();
  200.          header("Location: ".$session->referrer);
  201.       }
  202.    }
  203.    
  204.    
  205.    function procEditVid(){
  206.     global $session,$form;
  207.  
  208.    $retval=$session->addavideo($_POST['link']);
  209.    
  210.    if($retval){
  211.          $_SESSION['videoedit'] = true;
  212.          
  213.          header("Location: ".$session->referrer);
  214.       }
  215.      
  216.       else{
  217.          $_SESSION['value_array'] = $_POST;
  218.          $_SESSION['error_array'] = $form->getErrorArray();
  219.          header("Location: ".$session->referrer);
  220.       }
  221.  
  222.     }
  223.    
  224.    
  225.    
  226.    
  227. };
  228.  
  229. /* Initialize process */
  230. $process = new Process;
  231.  
  232. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement