Advertisement
Guest User

Untitled

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