Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.82 KB | None | 0 0
  1. <?php
  2.  
  3. class User
  4. {
  5.     function __construct()
  6.     {
  7.         $this->db = new MyPDO("mysql:host=" . DBHOST . ";dbname=" . DBNAME . ";charset=" . DBCHARSET, DBUSER, DBPASS, DBPARAMS);
  8.     }
  9.  
  10.     public function validateEmail($email){
  11.         $email = filter_var($email, FILTER_SANITIZE_EMAIL);
  12.         return filter_var($email, FILTER_VALIDATE_EMAIL);
  13.     }
  14.  
  15.     private function guidv4($data) {
  16.         // obtained from StackOverflow
  17.         // http://stackoverflow.com/questions/2040240/php-function-to-generate-v4-uuid
  18.         // usage:  echo guidv4(openssl_random_pseudo_bytes(16));
  19.  
  20.         assert(strlen($data) == 16);
  21.  
  22.         $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
  23.         $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
  24.  
  25.         return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  26.     }
  27.  
  28.     private function checkPasswordLength($password){
  29.         return strlen($password);
  30.     }
  31.  
  32.     private function doPasswordsMatch($password, $verify) {
  33.          return $password == $verify;
  34.     }
  35.  
  36.     private function hashPassword($password){
  37.         return password_hash($password, PASSWORD_DEFAULT);
  38.     }
  39.  
  40.     private function doesUserExist($email){
  41.         return $this->db->run("SELECT id FROM users WHERE email = ?", [$email])->rowCount();
  42.  
  43.     }
  44.  
  45.     public function getUserValidationKey($email){
  46.         return $this->db->run("SELECT validation_key FROM users WHERE email = ?", [$email])->fetch();
  47.     }
  48.  
  49.     public function validateUser($email, $password, $verify){
  50.               if ($this->doesUserExist($email) == 1) {
  51.             $error = "email already exists";
  52.         } elseif ($this->validateEmail($email) === false) {
  53.             $error = "email not valid";
  54.         } elseif ($this->doPasswordsMatch($password, $verify) === false) {
  55.             $error = "passwords didn't match";
  56.         } elseif ($this->checkPasswordLength($password) < 6) {
  57.             $error = "password must be 6 or more characters";
  58.         }
  59.  
  60.         if (isset($error)) {
  61.             $_SESSION['message']        = $error;
  62.             $_SESSION['message-type']   = 'error';
  63.             return false;
  64.         }
  65.         return true;
  66.     }
  67.  
  68.     public function activateUserByKey($key) {
  69.         return $this->db->run("UPDATE users SET validated = 1 WHERE validation_key = ?", [$key])->rowCount();
  70.     }
  71.  
  72.  
  73.     public function createUser($email, $password){
  74.         $email    = $this->validateEmail($email);
  75.         $hash     = $this->hashPassword($password);
  76.         $uuid     = $this->guidv4(openssl_random_pseudo_bytes(16));
  77.  
  78.         if($this->db->run("INSERT INTO users (email, password, validation_key) VALUES (?, ?, ?)", [$email, $hash, $uuid])) {
  79.             return true;
  80.         }
  81.  
  82.         // should log an error somewhere if this fails
  83.         return false;
  84.     }
  85.  
  86.  
  87.     public function userLogin($username, $password) {
  88.         $user = $this->db->run("SELECT id, email, name, password, is_admin FROM users WHERE email = ? LIMIT 1", [$username])->fetch();
  89.  
  90.         if (password_verify($password, $user['password']) == true) {
  91.             $_SESSION['user_id'] = $user['id'];
  92.             $_SESSION['name'] = $user['name'];
  93.             $_SESSION['email'] = $user['email'];
  94.             $_SESSION['is_admin'] = $user['is_admin'];
  95.             $_SESSION['message'] = "You have been logged in";
  96.             $_SESSION['message-type'] = "info";
  97.             $result = true;
  98.         } else {
  99.             $_SESSION['user_id'] = 0;
  100.             $_SESSION['name'] = 0;
  101.             $_SESSION['email'] = 0;
  102.             $_SESSION['is_admin'] = 0;
  103.             $_SESSION['message'] = "The username/password combination you provided could not be found";
  104.             $_SESSION['message-type'] = "error";
  105.             $result = false;
  106.         }
  107.         return $result;
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement