Guest User

Untitled

a guest
Apr 27th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.17 KB | None | 0 0
  1. <?php
  2. class User
  3. {
  4.     //POLA KLASY
  5.     private $conn;
  6.     private $table_name = "Users";
  7.  
  8.     public $idUser;
  9.     public $userLogin;
  10.     public $userPassword;
  11.     public $userEmail;
  12.     public $userActive;
  13.     public $idFamily;
  14.     public $userType;
  15.     public $userActivated;
  16.     //KONSTRUKTOR
  17.     public function __construct($db) {
  18.         $this->conn = $db;
  19.     }
  20.     //METODY KLASY
  21.     function create() {
  22.         $query = "INSERT INTO {$this->table_name}
  23.                SET
  24.                    userLogin = :userLogin,
  25.                    userPassword = :userPassword,
  26.                    userEmail = :userEmail,
  27.                    userActive = 0,
  28.                    idFamily = :idFamily,
  29.                    userType = :userType,
  30.                    userActivated = 0";
  31.         //sprawdzic co dokladnie robi prepare
  32.         $stmt = $this->conn->prepare($query);
  33.         //filtrowanie zmiennych obiektu
  34.         $this->userLogin=htmlspecialchars(strip_tags($this->userLogin));
  35.         $this->userPassword=htmlspecialchars(strip_tags($this->userPassword));
  36.         $this->userEmail=htmlspecialchars(strip_tags($this->userEmail));
  37.         $this->idFamily=htmlspecialchars(strip_tags($this->idFamily));
  38.         $this->userType=htmlspecialchars(strip_tags($this->userType));
  39.         if(empty($this->userLogin)) {
  40.             http_response_code(400);
  41.             echo json_encode(array("message" => "Wprowadz login."));
  42.             return false;
  43.         }
  44.         //wstawianie zmiennych obiketu do zapytania
  45.         $password_hash = password_hash($this->userPassword, PASSWORD_BCRYPT);
  46.         $stmt->bindParam(':userLogin', $this->userLogin);
  47.         $stmt->bindParam(':userPassword', $password_hash);
  48.         $stmt->bindParam(':userEmail', $this->userEmail);
  49.         $stmt->bindParam(':idFamily', $this->idFamily);
  50.         $stmt->bindParam(':userType', $this->userType);
  51.         //jesli zapytanie sie wykona poprawnie zwroc true
  52.         if($stmt->execute()) {
  53.             $this->idUser = $this->conn->lastInsertId();
  54.             return true;
  55.         }
  56.         return false;
  57.     }
  58.  
  59.     function read() {
  60.         $query = "SELECT *
  61.                FROM {$this->table_name}
  62.                WHERE idUser = ?";
  63.         $stmt = $this->conn->prepare($query);
  64.         $stmt->bindParam(1, $this->idUser);
  65.         if($stmt->execute()) {
  66.             $row = $stmt->fetch(PDO::FETCH_ASSOC);
  67.             $this->userLogin = $row['userLogin'];
  68.             $this->userPassword = $row['userPassword'];
  69.             $this->userEmail = $row['userEmail'];
  70.             $this->idFamily = $row['idFamily'];
  71.             $this->userType = $row['userType'];
  72.             $this->userActivated = $row['userActivated'];
  73.             return true;
  74.         }
  75.         return false;
  76.     }
  77.  
  78.     function update() {
  79.         $query = "UPDATE . $this->table_name .
  80.                SET
  81.                    userName = :";
  82.     }
  83.  
  84.     function delete() {
  85.  
  86.     }
  87.    
  88.     function emailExists() {
  89.         $query = "SELECT userEmail
  90.                FROM {$this->table_name}
  91.                WHERE userEmail = ?
  92.                LIMIT 0,1";
  93.         $stmt = $this->conn->prepare( $query );
  94.         $this->userEmail=htmlspecialchars(strip_tags($this->userEmail));
  95.         $stmt->bindParam(1, $this->userEmail);
  96.         $stmt->execute();
  97.         $num = $stmt->rowCount();
  98.         if($num>0) return true;
  99.         else return false;
  100.     }
  101.  
  102.     function loginExists() {
  103.         $query = "SELECT idUser, userLogin
  104.                FROM {$this->table_name}
  105.                WHERE userLogin = ?
  106.                LIMIT 0,1";
  107.         $stmt = $this->conn->prepare( $query );
  108.         $this->userLogin=htmlspecialchars(strip_tags($this->userLogin));
  109.         $stmt->bindParam(1, $this->userLogin);
  110.         $stmt->execute();
  111.         $num = $stmt->rowCount();
  112.         $row = $stmt->fetch(PDO::FETCH_ASSOC);
  113.         if($num>0) {
  114.             $this->idUser = $row['idUser'];
  115.             return true;
  116.         }
  117.         else return false;
  118.     }
  119.  
  120.     function activate() {
  121.         $query = "UPDATE {$this->table_name}
  122.                SET userActivated = 1
  123.                WHERE idUser = :idUser";
  124.         $stmt = $this->conn->prepare( $query );
  125.         $this->idUser=htmlspecialchars(strip_tags($this->idUser));
  126.         $stmt->bindParam(':idUser', $this->idUser);
  127.         if($stmt->execute()) return true;
  128.         else return false;
  129.     }
  130.  
  131.     function login() {
  132.         $query = "UPDATE {$this->table_name}
  133.                SET userActive = 1
  134.                WHERE idUser = :idUser";
  135.         $stmt = $this->conn->prepare( $query );
  136.         $this->idUser=htmlspecialchars(strip_tags($this->idUser));
  137.         $stmt->bindParam(':idUser', $this->idUser);
  138.         if($stmt->execute()) return true;
  139.         else return false;
  140.     }
  141.  
  142.     function logout() {
  143.         $query = "UPDATE {$this->table_name}
  144.                SET userActive = 0
  145.                WHERE idUser = :idUser";
  146.         $stmt = $this->conn->prepare( $query );
  147.         $this->idUser=htmlspecialchars(strip_tags($this->idUser));
  148.         $stmt->bindParam(':idUser', $this->idUser);
  149.         if($stmt->execute()) return true;
  150.         else return false;
  151.     }
  152. }
Add Comment
Please, Sign In to add comment