Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class User
- {
- //POLA KLASY
- private $conn;
- private $table_name = "Users";
- public $idUser;
- public $userLogin;
- public $userPassword;
- public $userEmail;
- public $userActive;
- public $idFamily;
- public $userType;
- public $userActivated;
- //KONSTRUKTOR
- public function __construct($db) {
- $this->conn = $db;
- }
- //METODY KLASY
- function create() {
- $query = "INSERT INTO {$this->table_name}
- SET
- userLogin = :userLogin,
- userPassword = :userPassword,
- userEmail = :userEmail,
- userActive = 0,
- idFamily = :idFamily,
- userType = :userType,
- userActivated = 0";
- //sprawdzic co dokladnie robi prepare
- $stmt = $this->conn->prepare($query);
- //filtrowanie zmiennych obiektu
- $this->userLogin=htmlspecialchars(strip_tags($this->userLogin));
- $this->userPassword=htmlspecialchars(strip_tags($this->userPassword));
- $this->userEmail=htmlspecialchars(strip_tags($this->userEmail));
- $this->idFamily=htmlspecialchars(strip_tags($this->idFamily));
- $this->userType=htmlspecialchars(strip_tags($this->userType));
- if(empty($this->userLogin)) {
- http_response_code(400);
- echo json_encode(array("message" => "Wprowadz login."));
- return false;
- }
- //wstawianie zmiennych obiketu do zapytania
- $password_hash = password_hash($this->userPassword, PASSWORD_BCRYPT);
- $stmt->bindParam(':userLogin', $this->userLogin);
- $stmt->bindParam(':userPassword', $password_hash);
- $stmt->bindParam(':userEmail', $this->userEmail);
- $stmt->bindParam(':idFamily', $this->idFamily);
- $stmt->bindParam(':userType', $this->userType);
- //jesli zapytanie sie wykona poprawnie zwroc true
- if($stmt->execute()) {
- $this->idUser = $this->conn->lastInsertId();
- return true;
- }
- return false;
- }
- function read() {
- $query = "SELECT *
- FROM {$this->table_name}
- WHERE idUser = ?";
- $stmt = $this->conn->prepare($query);
- $stmt->bindParam(1, $this->idUser);
- if($stmt->execute()) {
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
- $this->userLogin = $row['userLogin'];
- $this->userPassword = $row['userPassword'];
- $this->userEmail = $row['userEmail'];
- $this->idFamily = $row['idFamily'];
- $this->userType = $row['userType'];
- $this->userActivated = $row['userActivated'];
- return true;
- }
- return false;
- }
- function update() {
- $query = "UPDATE . $this->table_name .
- SET
- userName = :";
- }
- function delete() {
- }
- function emailExists() {
- $query = "SELECT userEmail
- FROM {$this->table_name}
- WHERE userEmail = ?
- LIMIT 0,1";
- $stmt = $this->conn->prepare( $query );
- $this->userEmail=htmlspecialchars(strip_tags($this->userEmail));
- $stmt->bindParam(1, $this->userEmail);
- $stmt->execute();
- $num = $stmt->rowCount();
- if($num>0) return true;
- else return false;
- }
- function loginExists() {
- $query = "SELECT idUser, userLogin
- FROM {$this->table_name}
- WHERE userLogin = ?
- LIMIT 0,1";
- $stmt = $this->conn->prepare( $query );
- $this->userLogin=htmlspecialchars(strip_tags($this->userLogin));
- $stmt->bindParam(1, $this->userLogin);
- $stmt->execute();
- $num = $stmt->rowCount();
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
- if($num>0) {
- $this->idUser = $row['idUser'];
- return true;
- }
- else return false;
- }
- function activate() {
- $query = "UPDATE {$this->table_name}
- SET userActivated = 1
- WHERE idUser = :idUser";
- $stmt = $this->conn->prepare( $query );
- $this->idUser=htmlspecialchars(strip_tags($this->idUser));
- $stmt->bindParam(':idUser', $this->idUser);
- if($stmt->execute()) return true;
- else return false;
- }
- function login() {
- $query = "UPDATE {$this->table_name}
- SET userActive = 1
- WHERE idUser = :idUser";
- $stmt = $this->conn->prepare( $query );
- $this->idUser=htmlspecialchars(strip_tags($this->idUser));
- $stmt->bindParam(':idUser', $this->idUser);
- if($stmt->execute()) return true;
- else return false;
- }
- function logout() {
- $query = "UPDATE {$this->table_name}
- SET userActive = 0
- WHERE idUser = :idUser";
- $stmt = $this->conn->prepare( $query );
- $this->idUser=htmlspecialchars(strip_tags($this->idUser));
- $stmt->bindParam(':idUser', $this->idUser);
- if($stmt->execute()) return true;
- else return false;
- }
- }
Add Comment
Please, Sign In to add comment