Advertisement
Guest User

Untitled

a guest
Mar 30th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.93 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * To change this license header, choose License Headers in Project Properties.
  5.  * To change this template file, choose Tools | Templates
  6.  * and open the template in the editor.
  7.  */
  8.  
  9. class SigninController extends Controller {
  10.    
  11.     public function _init() {
  12.         global $config;
  13.         if (!$_SESSION['branch']) {
  14.             $model = new Model_EmmaBranch();
  15.             $branch = $model->getBranch($config['branch']);
  16.             $_SESSION['branch'] = $branch;
  17.         }
  18.         $this->layout->name = "Login";
  19.         $this->layout->title = "Login";
  20.         if ($_SESSION['user']) {
  21.             $this->_redirect(BASE_URL);
  22.         } else {
  23.             $user = $_COOKIE['user'];
  24.             if ($user) {
  25.                 $model = new Model_Azuser();
  26.                 $json = base64_decode($user);
  27.                 $arr = json_decode($json);
  28.                 $client = $model->getByEmail($arr->email);
  29.                 if ($client['password'] == $arr->password) {
  30.                     $_SESSION['user'] = $client;
  31.                     $this->_redirect(BASE_URL);
  32.                 } else {
  33.                     setcookie('user', "", 0, "/");
  34.                 }
  35.             }
  36.         }
  37.     }
  38.    
  39.     public function indexAction() {
  40.         if ($_POST) {
  41.             $email = trim($_POST['email']);
  42.             $password = trim($_POST['password']); //6 - 100 ky tu
  43.             $remember = trim($_POST['remember']);
  44.  
  45.             $r = -1; //chua thiet lap
  46.  
  47.             if ($r == -1) {
  48.                 $model = new Model_Azuser();
  49.                 $client = $model->getByEmail($email);
  50.                 //$client = $model->getByEmail($email);
  51.                 if (!$client) {
  52.                     //Wrong email or password
  53.                     $r = 2;
  54.                     $msg = 'Wrong email or password.';
  55.                 }
  56.             }
  57.  
  58.             if ($r == -1) {
  59.                 //check password
  60.                 if ($client['password'] != md5($password)) {
  61.                     //Wrong email or password
  62.                     $r = 2;
  63.                     $msg = 'Wrong email or password.';
  64.                 }
  65.             }
  66.             if ($r == -1) {
  67.                 $_SESSION['user'] = $client;
  68.                 //$model->update(array('lastVisitTime' => time()), $email);
  69.                 $r = 0;
  70.                 if($remember == "true"){
  71.                     $json = json_encode(array(
  72.                         'email' => $client['email'],
  73.                         'password' => $client['password']
  74.                     ));
  75.                     setcookie('user', base64_encode($json), time() + (60 * 60 * 24 * 7), "/");
  76.                 }
  77.             }
  78.  
  79.             if ($this->_isAjaxRequest()) {
  80.                 $this->setNoRender();
  81.  
  82.                 $res = array(
  83.                     'r' => $r,
  84.                     'msg' => $msg,
  85.                 );
  86.                 echo json_encode($res);
  87.             }
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement