Advertisement
Guest User

Untitled

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