Advertisement
Guest User

login controller

a guest
Feb 23rd, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.83 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Login Controller
  5.  * @author: Rogier Fischer
  6.  * @version: 1.2
  7.  * @last edit: 19-12-2015
  8. **/
  9.  
  10. class login_controller extends Controller {
  11.  
  12.  
  13.     /**
  14.      * CONSTRUCTOR
  15.      *   performs the complete logic of this page
  16.      **/
  17.     public function __construct() {
  18.  
  19.  
  20.         // If a user is already logged in, we redirect them to the homepage
  21.         if(User::session_exists()) {
  22.  
  23.             Auth::redirect('/' . Language::l() . '/account/home');
  24.  
  25.         }
  26.  
  27.         try {
  28.  
  29.             if($_SERVER['REQUEST_METHOD'] === 'POST') {
  30.  
  31.                 // security, spam prevention and CSRF
  32.                 Auth::decodeFormNames($_POST, 'login');
  33.  
  34.                 // validate login form
  35.                 if($this->validateForm()) {
  36.  
  37.                     // load user model
  38.                     $this->model = $this->loadModel('login');
  39.  
  40.  
  41.                     // perform login logic
  42.                     $this->login();
  43.  
  44.  
  45.                     // security, reset PHP session
  46.                     session_regenerate_id();
  47.  
  48.  
  49.                     // after login, send user to previous page
  50.                     if(isset($_GET['next'])) {
  51.  
  52.                         Auth::redirect('/' . Auth::esc(str_replace('-', '/', $_GET['next'])));
  53.  
  54.                     }
  55.                     else {
  56.  
  57.                         Auth::redirect('/' . Language::l() . '/account/home');
  58.  
  59.                     }
  60.  
  61.                 }
  62.  
  63.             }
  64.  
  65.         }
  66.         catch(Exception $e) {
  67.  
  68.             $error = $e->getMessage();
  69.  
  70.             // check wether there exists an translation for the given error
  71.             if(Language::g($error) === false) {
  72.  
  73.                 $error = 'error_unknown';
  74.  
  75.             }
  76.  
  77.             $this->loadView('login/content', [
  78.                 'error' => Language::g($error)
  79.             ]);
  80.  
  81.         }
  82.  
  83.         $this->loadView('login/content');
  84.  
  85.     }
  86.  
  87.     private function validateForm() {
  88.  
  89.         // initialize form class which handles basic security like XSS and CSRF
  90.         $form = new Form();
  91.  
  92.         // define expected fields, 'csrf-token' is also an expected field.
  93.         // The Form class does not accept more or less POST defined in the fields variable.
  94.         $fields = array(
  95.             'email' => array(
  96.                 'default_error_message' => 'email_invalid',
  97.                 'validators' => array(
  98.                     'email' => array(
  99.                         'error_message' => 'email_incorrect'
  100.                     )
  101.                 )
  102.             ),
  103.             'password' => array(
  104.                 'default_error_message' => 'password_invalid',
  105.                 'validators' => array(
  106.                     'minLength' => array(
  107.                         'helper' => 6,
  108.                         'error_message' => 'password_short'
  109.                     )
  110.                 )
  111.             ),
  112.             'two-fa-code' => array(
  113.                 'default_error_message' => 'auth_invalid',
  114.                 'validators' => array(
  115.                     'maxLength' => array(
  116.                         'helper' => 6,
  117.                         'error_message' => 'auth_incorrect'
  118.                     )
  119.                 )
  120.             )
  121.         );
  122.  
  123.  
  124.         // check wether the currect _POST variable complies with the field variable
  125.         try {
  126.  
  127.             $form->isValidPost($fields);
  128.  
  129.         }
  130.         catch(Exception $e) {
  131.  
  132.             throw new Exception($e->getMessage());
  133.  
  134.         }
  135.  
  136.         return true;
  137.  
  138.     }
  139.  
  140.     private function login() {
  141.  
  142.         // search user row
  143.         $user_row = $this->model->searchUserByEmail($_POST['email']);
  144.  
  145.         // unknown email
  146.         if(!$user_row) {
  147.  
  148.             throw new UserException('login_incorrect');
  149.  
  150.         }
  151.  
  152.         // 2 factor authentication
  153.         if($user_row['user_2fa_enabled']) {
  154.  
  155.             if(!isset($_POST['two-fa-code'])) {
  156.  
  157.                 throw new UserException('illegal_2fa_code');
  158.  
  159.             }
  160.  
  161.             if(!googleAuthenticator::verifyCode($user_row['user_2fa_secret'], $_POST['two-fa-code'], 2)) {
  162.  
  163.                 throw new UserException('illegal_2fa_code');
  164.  
  165.             }
  166.  
  167.         }
  168.  
  169.         // not activated
  170.         if(!$user_row['user_activated']) {
  171.  
  172.             throw new UserException('account_not_activated');
  173.  
  174.         }
  175.  
  176.         // save password hashing
  177.         $salt = Auth::getSalt($user_row['user_password']);
  178.         $password = Auth::hash_password($_POST['password'], $salt);
  179.  
  180.         // create session and save it
  181.         if($password === $user_row['user_password']) {
  182.  
  183.             $token = Auth::createCode(64);
  184.  
  185.             $_SESSION['user']['session_token'] = $token;
  186.             $_SESSION['user']['session_exists'] = true;
  187.             $_SESSION['user']['data'] = $user_row;
  188.  
  189.             $this->model->saveSession($user_row['user_id'], $token);
  190.  
  191.         }
  192.         else {
  193.  
  194.             throw new UserException('login_incorrect');
  195.  
  196.         }
  197.  
  198.     }
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement