Advertisement
Guest User

Untitled

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