Advertisement
Guest User

login controller

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