Advertisement
Guest User

Untitled

a guest
Nov 1st, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.31 KB | None | 0 0
  1. class Login
  2. {
  3.     private $_id;
  4.     private $_username;
  5.     private $_password;
  6.     private $_passmd5;
  7.  
  8.     private $_errors;
  9.     private $_access;
  10.     private $_login;
  11.     private $_token;
  12.  
  13.     public function __construct()
  14.     {
  15.         $this->_errors = [];
  16.         $this->_login  = isset($_POST['login']) ? 1 : 0;
  17.  
  18.         $this->_access   = 0;
  19.         $this->_token     = (!isset($_POST['token']) ? '' : $_POST['token']);
  20.         $this->_id       = 0;
  21.         $this->_username = ($this->_login) ? $this->filter($_POST['username']) : $_SESSION['username'];
  22.         $this->_password = ($this->_login) ? $this->filter($_POST['password']) : '';
  23.         $this->_passmd5  = ($this->_login) ? md5($this->_password) : $_SESSION['password'];
  24.     }
  25.  
  26.     public function isLoggedIn()
  27.     {
  28.         ($this->_login) ? $this->verifyPost() : $this->verifySession();
  29.  
  30.         return $this->_access;
  31.     }
  32.  
  33.     public function filter($var)
  34.     {
  35.         //Strips everything what isnt character or number
  36.         return preg_replace('/[^a-zA-Z0-9]/', '', $var);
  37.     }
  38.  
  39.     public function verifyPost()
  40.     {
  41.         try {
  42.             if (!$this->isTokenValid()) {
  43.                 throw new Exception('Invalid form submission');
  44.             }
  45.  
  46.             if (!$this->isDataValid()) {
  47.                 throw new Exception('Nepareizi dati');
  48.             }
  49.  
  50.             if (!$this->verifyDatabase()) {
  51.                 throw new Exception('Nepareizs Lietotajvards/Parole');
  52.             }
  53.  
  54.             $this->_access = 1;
  55.             $this->registerSession();
  56.         } catch (Exception $e) {
  57.             $this->_errors[] = $e->getMessage();
  58.         }
  59.     }
  60.  
  61.  
  62.     public function verifySession()
  63.     {
  64.         if ($this->sessionExist() && $this->verifyDatabase()) {
  65.             $this->_access = 1;
  66.         }
  67.     }
  68.  
  69.     public function verifyDatabase()
  70.     {
  71.         $dsn      = 'mysql:dbname=login;host=127.0.0.1';
  72.         $user     = 'root';
  73.         $password = 'pass';
  74.  
  75.         $dbh = new PDO($dsn, $user, $password);
  76.  
  77.         $sth = $dbh->prepare('SELECT `userid` FROM user WHERE username = :username
  78.                        AND password = :password');
  79.  
  80.         $sth->execute([':username' => $this->_username,
  81.             'password' => $this->_passmd5]);
  82.  
  83.         $id = $sth->fetch(PDO::FETCH_ASSOC);
  84.  
  85.         if ($sth->rowCount()) {
  86.             $this->_id = $id;
  87.  
  88.  
  89.             return true;
  90.         } else {
  91.             return false;
  92.         }
  93.  
  94.     }
  95.  
  96.  
  97.     public function isDataValid()
  98.     {
  99.         return (preg_match('/^[a-zA-Z0-9]{5,12}$/', $this->_username) && preg_match('/^[a-zA-Z0-9]{5,12}$/', $this->_password)) ? 1 : 0;
  100.     }
  101.  
  102.  
  103.     public function isTokenValid()
  104.     {
  105.         return (!isset($_SESSION['token']) || $this->_token != $_SESSION['token']) ? 0 : 1;
  106.     }
  107.  
  108.     public function registerSession()
  109.     {
  110.         $_SESSION['userid']   = $this->_id;
  111.         $_SESSION['username'] = $this->_username;
  112.         $_SESSION['password'] = $this->_passmd5;
  113.     }
  114.  
  115.     public function sessionExist()
  116.     {
  117.         return (isset($_SESSION['username']) && isset($_SESSION['password']) ? 1 : 0);
  118.     }
  119.  
  120.     public function showErrors()
  121.     {
  122.         echo '<h3>Errors</h3>';
  123.  
  124.  
  125.         foreach ( $this->_errors as $key => $value) {
  126.  
  127.             echo $value;
  128.  
  129.         }
  130.  
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement