Advertisement
Guest User

Untitled

a guest
May 19th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.79 KB | None | 0 0
  1. <?php
  2. namespace Auth;
  3.  
  4. use Connect\ConnectDB;
  5.  
  6. class User
  7. {
  8.     private $id;
  9.     private $username;
  10.     private $db;
  11.     private $user_id;
  12.  
  13.     private $db_host = "localhost";
  14.     private $db_name = "u0003130_jew";
  15.     private $db_user = "root";
  16.     private $db_pass = "";
  17.  
  18.     private $is_authorized = false;
  19.  
  20.     public function __construct($username = null, $password = null)
  21.     {
  22.         $this->username = $username;
  23.         $this->connectDb($this->db_name, $this->db_user, $this->db_pass, $this->db_host);
  24.     }
  25.  
  26.     public function __destruct()
  27.     {
  28.         $this->db = null;
  29.     }
  30.  
  31.     public static function isAuthorized()
  32.     {
  33.         if (!empty($_SESSION["user_id"])) {
  34.             return (bool) $_SESSION["user_id"];
  35.         }
  36.         return false;
  37.     }
  38.  
  39.     public function passwordHash($password, $iterations = 10)
  40.     {
  41.         $options = [
  42.             'cost' => $iterations,
  43.         ];
  44.         return password_hash($password,PASSWORD_BCRYPT ,$options);
  45.     }
  46.  
  47.  
  48.     public function verify($password, $hash){
  49.         return password_verify($password, $hash);
  50.     }
  51.     public function authorize($username, $password, $remember=false)
  52.     {
  53.         $query = "select id, username,password from users where
  54.            username = :username limit 1";
  55.         $sth = $this->db->prepare($query);
  56.  
  57.  
  58.  
  59.  
  60.         $sth->execute(
  61.             array(
  62.                 ":username" => $username
  63.             )
  64.         );
  65.         $this->user = $sth->fetch();
  66.  
  67.         if(!$this->user){
  68.             $this->is_authorized = false;
  69.             return false;
  70.         }
  71.         if(!$this->verify($password,$this->user['password'])){
  72.             $this->is_authorized = false;
  73.             return false;
  74.         }
  75.         $this->is_authorized = true;
  76.         $this->user_id = $this->user['id'];
  77.         $this->saveSession($remember);
  78.         return true;
  79.  
  80.     }
  81.  
  82.     public function logout()
  83.     {
  84.         if (!empty($_SESSION["user_id"])) {
  85.             unset($_SESSION["user_id"]);
  86.             setcookie("sid","");
  87.         }
  88.     }
  89.  
  90.     public function saveSession($remember = false, $http_only = true, $days = 7)
  91.     {
  92.         $_SESSION["user_id"] = $this->user_id;
  93.  
  94.         if ($remember) {
  95.             // Save session id in cookies
  96.             $sid = session_id();
  97.  
  98.             $expire = time() + $days * 24 * 3600;
  99.             $domain = ""; // default domain
  100.             $secure = false;
  101.             $path = "/";
  102.  
  103.             $cookie = setcookie("sid", $sid, $expire, $path, $domain, $secure, $http_only);
  104.         }
  105.     }
  106.  
  107.     public function create($username, $password) {
  108.  
  109.  
  110.         $query = "insert into users (username, password)
  111.            values (:username, :password)";
  112.         $hash = $this->passwordHash($password);
  113.         $sth = $this->db->prepare($query);
  114.  
  115.         try {
  116.             $this->db->beginTransaction();
  117.             $result = $sth->execute(
  118.                 array(
  119.                     ':username' => $username,
  120.                     ':password' => $hash
  121.  
  122.                 )
  123.             );
  124.             $this->db->commit();
  125.         } catch (\PDOException $e) {
  126.             $this->db->rollback();
  127.             echo "Database error: " . $e->getMessage();
  128.             die();
  129.         }
  130.  
  131.         if (!$result) {
  132.             $info = $sth->errorInfo();
  133.             printf("Database error %d %s", $info[1], $info[2]);
  134.             die();
  135.         }
  136.  
  137.         return $result;
  138.     }
  139.  
  140.     public function connectdb($db_name, $db_user, $db_pass, $db_host)
  141.     {
  142.         try {
  143.             $this->db = new \pdo("mysql:host=$db_host;dbname=$db_name;charset=utf-8", $db_user, $db_pass);
  144.         } catch (\pdoexception $e) {
  145.             echo "database error: " . $e->getmessage();
  146.             die();
  147.         }
  148.  
  149.  
  150.         return $this;
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement