123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- <?php
- class Post {
- //db backend stuff
- private $conn;
- private $table = 'users';
- //post properties
- public $id;
- public $rol;
- public $name;
- public $password;
- public $softDelete;
- public $createdAt;
- public $identify;
- public $hash;
- //constructor with db connection
- public function __construct($db) {
- $this->conn = $db;
- }
- //create user
- public function create() {
- $query = '';
- //create query
- $query .= 'INSERT INTO ';
- $query .= "{$this->table} ";
- $query .= 'SET ';
- $query .= 'rol = :rol, ';
- $query .= 'name = :name, ';
- $query .= 'password = :password ';
- //prepare statement
- $stmt = $this->conn->prepare($query);
- //clean data
- $this->rol = htmlspecialchars(strip_tags($this->rol));
- $this->name = htmlspecialchars(strip_tags($this->name));
- $this->password = htmlspecialchars(strip_tags($this->password));
- //sha512 will be build in the client
- $security_database_password = crypt($this->password, '$6$');
- //binding of parameters
- $stmt->bindParam(':rol', $this->rol);
- $stmt->bindParam(':name', $this->name);
- $stmt->bindParam(':password', $security_database_password);
- //execute the query
- if ($stmt->execute()) {
- return true;
- }
- //print error if something goes wrong
- printf("Error %s. \n", $stmt->error);
- return false;
- }
- //read users
- public function read() {
- $query = '';
- //create query
- $query .= 'SELECT ';
- $query .= 'id, ';
- $query .= 'rol, ';
- $query .= 'name, ';
- $query .= 'password, ';
- $query .= 'softDelete, ';
- $query .= 'createdAt ';
- $query .= "FROM {$this->table} ";
- $query .= 'ORDER BY ';
- $query .= 'createdAt DESC';
- //prepare statement
- $stmt = $this->conn->prepare($query);
- //execute query
- $stmt->execute();
- return $stmt;
- }
- //update users
- public function update_rol() {
- $query = '';
- //update query
- $query .= 'UPDATE ';
- $query .= "{$this->table} ";
- $query .= 'SET ';
- $query .= 'rol = :rol ';
- $query .= 'WHERE id = :id ';
- //prepare statement
- $stmt = $this->conn->prepare($query);
- //clean data
- $this->id = htmlspecialchars(strip_tags($this->id));
- $this->rol = htmlspecialchars(strip_tags($this->rol));
- //binding of parameters
- $stmt->bindParam(':id', $this->id);
- $stmt->bindParam(':rol', $this->rol);
- //execute the query
- if ($stmt->execute()) {
- return true;
- }
- //print error if something goes wrong
- printf("Error %s. \n", $stmt->error);
- return false;
- }
- public function update_softDelete() {
- $query = '';
- //update query
- $query .= 'UPDATE ';
- $query .= "{$this->table} ";
- $query .= 'SET ';
- $query .= 'softDelete = :softDelete ';
- $query .= 'WHERE id = :id ';
- //prepare statement
- $stmt = $this->conn->prepare($query);
- //clean data
- $this->id = htmlspecialchars(strip_tags($this->id));
- $this->softDelete = htmlspecialchars(strip_tags($this->softDelete));
- //binding of parameters
- $stmt->bindParam(':id', $this->id);
- $stmt->bindParam(':softDelete', $this->softDelete);
- //execute the query
- if ($stmt->execute()) {
- return true;
- }
- //print error if something goes wrong
- printf("Error %s. \n", $stmt->error);
- return false;
- }
- //update password users
- public function update_password() {
- $query = '';
- //update query
- $query .= 'UPDATE ';
- $query .= "{$this->table} ";
- $query .= 'SET ';
- $query .= 'password = :password ';
- $query .= 'WHERE id = :id ';
- //prepare statement
- $stmt = $this->conn->prepare($query);
- //clean data
- $this->id = htmlspecialchars(strip_tags($this->id));
- $this->password = htmlspecialchars(strip_tags($this->password));
- //sha512 will be build in the client
- $security_database_password = crypt($this->password, '$6$');
- //binding of parameters
- $stmt->bindParam(':id', $this->id);
- $stmt->bindParam(':password', $security_database_password);
- //execute the query
- if ($stmt->execute()) {
- return true;
- }
- //print error if something goes wrong
- printf("Error %s. \n", $stmt->error);
- return false;
- }
- //soft delete user
- public function soft_delete() {
- $query = '';
- //soft delete query
- $query .= 'UPDATE ';
- $query .= "{$this->table} ";
- $query .= 'SET ';
- $query .= 'softDelete = :softDelete ';
- $query .= 'WHERE id = :id ';
- //prepare statement
- $stmt = $this->conn->prepare($query);
- //clean data
- $this->id = htmlspecialchars(strip_tags($this->id));
- $this->softDelete = htmlspecialchars(strip_tags($this->softDelete));
- //binding of parameters
- $stmt->bindParam(':id', $this->id);
- $stmt->bindParam(':softDelete', $this->softDelete);
- //execute the query
- if ($stmt->execute()) {
- return true;
- }
- //print error if something goes wrong
- printf("Error %s. \n", $stmt->error);
- return false;
- }
- //user identification
- public function identify() {
- $query = '';
- //create query
- $query .= 'SELECT ';
- $query .= 'name, ';
- $query .= 'password, ';
- $query .= 'softDelete ';
- $query .= "FROM {$this->table} ";
- $query .= 'WHERE name = ? ';
- //prepare statement
- $stmt = $this->conn->prepare($query);
- //binding param
- $stmt->bindParam(1, $this->identify);
- //execute query
- $stmt->execute();
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
- if (!is_array($row)) {
- return false;
- }
- if ($row['softDelete'] == 1) {
- return false;
- }
- if (hash_equals($row['password'], crypt($this->hash, $row['password']))) {
- return true;
- }
- return false;
- }
- //is duplicate user
- public function is_new_user() {
- $query = '';
- //create query
- $query .= 'SELECT ';
- $query .= 'name ';
- $query .= "FROM {$this->table} ";
- $query .= 'WHERE name = ? ';
- //prepare statement
- $stmt = $this->conn->prepare($query);
- //binding param
- $stmt->bindParam(1, $this->name);
- //execute query
- $stmt->execute();
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
- if (!is_array($row)) {
- return true;
- }
- if ($row['name']) {
- return false;
- }
- return false;
- }
- //is super user
- public function is_super_user() {
- $query = '';
- //create query
- $query .= 'SELECT ';
- $query .= 'rol, ';
- $query .= 'softDelete ';
- $query .= "FROM {$this->table} ";
- $query .= 'WHERE name = ? ';
- //prepare statement
- $stmt = $this->conn->prepare($query);
- //binding param
- $stmt->bindParam(1, $this->identify);
- //execute query
- $stmt->execute();
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
- if (!is_array($row)) {
- return false;
- }
- if ($row['softDelete'] == 1) {
- return false;
- }
- if ($row['rol'] == 1) {
- return false;
- }
- return true;
- }
- //read single user
- public function read_single() {
- $query = '';
- //create query
- $query .= 'SELECT ';
- $query .= 'id, ';
- $query .= 'rol, ';
- $query .= 'name, ';
- $query .= 'password, ';
- $query .= 'softDelete, ';
- $query .= 'createdAt ';
- $query .= "FROM {$this->table} ";
- $query .= 'WHERE id = ? ';
- //prepare statement
- $stmt = $this->conn->prepare($query);
- //binding param
- $stmt->bindParam(1, $this->id);
- //execute query
- $stmt->execute();
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
- $this->rol = $row['rol'];
- $this->name = $row['name'];
- $this->softDelete = $row['softDelete'];
- return $stmt;
- }
- }
- ?>
|