index.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. class Post {
  3. //db backend stuff
  4. private $conn;
  5. private $table = 'users';
  6. //post properties
  7. public $id;
  8. public $rol;
  9. public $name;
  10. public $password;
  11. public $softDelete;
  12. public $createdAt;
  13. public $identify;
  14. public $hash;
  15. //constructor with db connection
  16. public function __construct($db) {
  17. $this->conn = $db;
  18. }
  19. //create user
  20. public function create() {
  21. $query = '';
  22. //create query
  23. $query .= 'INSERT INTO ';
  24. $query .= "{$this->table} ";
  25. $query .= 'SET ';
  26. $query .= 'rol = :rol, ';
  27. $query .= 'name = :name, ';
  28. $query .= 'password = :password ';
  29. //prepare statement
  30. $stmt = $this->conn->prepare($query);
  31. //clean data
  32. $this->rol = htmlspecialchars(strip_tags($this->rol));
  33. $this->name = htmlspecialchars(strip_tags($this->name));
  34. $this->password = htmlspecialchars(strip_tags($this->password));
  35. //sha512 will be build in the client
  36. $security_database_password = crypt($this->password, '$6$');
  37. //binding of parameters
  38. $stmt->bindParam(':rol', $this->rol);
  39. $stmt->bindParam(':name', $this->name);
  40. $stmt->bindParam(':password', $security_database_password);
  41. //execute the query
  42. if ($stmt->execute()) {
  43. return true;
  44. }
  45. //print error if something goes wrong
  46. printf("Error %s. \n", $stmt->error);
  47. return false;
  48. }
  49. //read users
  50. public function read() {
  51. $query = '';
  52. //create query
  53. $query .= 'SELECT ';
  54. $query .= 'id, ';
  55. $query .= 'rol, ';
  56. $query .= 'name, ';
  57. $query .= 'password, ';
  58. $query .= 'softDelete, ';
  59. $query .= 'createdAt ';
  60. $query .= "FROM {$this->table} ";
  61. $query .= 'ORDER BY ';
  62. $query .= 'createdAt DESC';
  63. //prepare statement
  64. $stmt = $this->conn->prepare($query);
  65. //execute query
  66. $stmt->execute();
  67. return $stmt;
  68. }
  69. //update users
  70. public function update_rol() {
  71. $query = '';
  72. //update query
  73. $query .= 'UPDATE ';
  74. $query .= "{$this->table} ";
  75. $query .= 'SET ';
  76. $query .= 'rol = :rol ';
  77. $query .= 'WHERE id = :id ';
  78. //prepare statement
  79. $stmt = $this->conn->prepare($query);
  80. //clean data
  81. $this->id = htmlspecialchars(strip_tags($this->id));
  82. $this->rol = htmlspecialchars(strip_tags($this->rol));
  83. //binding of parameters
  84. $stmt->bindParam(':id', $this->id);
  85. $stmt->bindParam(':rol', $this->rol);
  86. //execute the query
  87. if ($stmt->execute()) {
  88. return true;
  89. }
  90. //print error if something goes wrong
  91. printf("Error %s. \n", $stmt->error);
  92. return false;
  93. }
  94. public function update_softDelete() {
  95. $query = '';
  96. //update query
  97. $query .= 'UPDATE ';
  98. $query .= "{$this->table} ";
  99. $query .= 'SET ';
  100. $query .= 'softDelete = :softDelete ';
  101. $query .= 'WHERE id = :id ';
  102. //prepare statement
  103. $stmt = $this->conn->prepare($query);
  104. //clean data
  105. $this->id = htmlspecialchars(strip_tags($this->id));
  106. $this->softDelete = htmlspecialchars(strip_tags($this->softDelete));
  107. //binding of parameters
  108. $stmt->bindParam(':id', $this->id);
  109. $stmt->bindParam(':softDelete', $this->softDelete);
  110. //execute the query
  111. if ($stmt->execute()) {
  112. return true;
  113. }
  114. //print error if something goes wrong
  115. printf("Error %s. \n", $stmt->error);
  116. return false;
  117. }
  118. //update password users
  119. public function update_password() {
  120. $query = '';
  121. //update query
  122. $query .= 'UPDATE ';
  123. $query .= "{$this->table} ";
  124. $query .= 'SET ';
  125. $query .= 'password = :password ';
  126. $query .= 'WHERE id = :id ';
  127. //prepare statement
  128. $stmt = $this->conn->prepare($query);
  129. //clean data
  130. $this->id = htmlspecialchars(strip_tags($this->id));
  131. $this->password = htmlspecialchars(strip_tags($this->password));
  132. //sha512 will be build in the client
  133. $security_database_password = crypt($this->password, '$6$');
  134. //binding of parameters
  135. $stmt->bindParam(':id', $this->id);
  136. $stmt->bindParam(':password', $security_database_password);
  137. //execute the query
  138. if ($stmt->execute()) {
  139. return true;
  140. }
  141. //print error if something goes wrong
  142. printf("Error %s. \n", $stmt->error);
  143. return false;
  144. }
  145. //soft delete user
  146. public function soft_delete() {
  147. $query = '';
  148. //soft delete query
  149. $query .= 'UPDATE ';
  150. $query .= "{$this->table} ";
  151. $query .= 'SET ';
  152. $query .= 'softDelete = :softDelete ';
  153. $query .= 'WHERE id = :id ';
  154. //prepare statement
  155. $stmt = $this->conn->prepare($query);
  156. //clean data
  157. $this->id = htmlspecialchars(strip_tags($this->id));
  158. $this->softDelete = htmlspecialchars(strip_tags($this->softDelete));
  159. //binding of parameters
  160. $stmt->bindParam(':id', $this->id);
  161. $stmt->bindParam(':softDelete', $this->softDelete);
  162. //execute the query
  163. if ($stmt->execute()) {
  164. return true;
  165. }
  166. //print error if something goes wrong
  167. printf("Error %s. \n", $stmt->error);
  168. return false;
  169. }
  170. //user identification
  171. public function identify() {
  172. $query = '';
  173. //create query
  174. $query .= 'SELECT ';
  175. $query .= 'name, ';
  176. $query .= 'password, ';
  177. $query .= 'softDelete ';
  178. $query .= "FROM {$this->table} ";
  179. $query .= 'WHERE name = ? ';
  180. //prepare statement
  181. $stmt = $this->conn->prepare($query);
  182. //binding param
  183. $stmt->bindParam(1, $this->identify);
  184. //execute query
  185. $stmt->execute();
  186. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  187. if (!is_array($row)) {
  188. return false;
  189. }
  190. if ($row['softDelete'] == 1) {
  191. return false;
  192. }
  193. if (hash_equals($row['password'], crypt($this->hash, $row['password']))) {
  194. return true;
  195. }
  196. return false;
  197. }
  198. //is duplicate user
  199. public function is_new_user() {
  200. $query = '';
  201. //create query
  202. $query .= 'SELECT ';
  203. $query .= 'name ';
  204. $query .= "FROM {$this->table} ";
  205. $query .= 'WHERE name = ? ';
  206. //prepare statement
  207. $stmt = $this->conn->prepare($query);
  208. //binding param
  209. $stmt->bindParam(1, $this->name);
  210. //execute query
  211. $stmt->execute();
  212. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  213. if (!is_array($row)) {
  214. return true;
  215. }
  216. if ($row['name']) {
  217. return false;
  218. }
  219. return false;
  220. }
  221. //is super user
  222. public function is_super_user() {
  223. $query = '';
  224. //create query
  225. $query .= 'SELECT ';
  226. $query .= 'rol, ';
  227. $query .= 'softDelete ';
  228. $query .= "FROM {$this->table} ";
  229. $query .= 'WHERE name = ? ';
  230. //prepare statement
  231. $stmt = $this->conn->prepare($query);
  232. //binding param
  233. $stmt->bindParam(1, $this->identify);
  234. //execute query
  235. $stmt->execute();
  236. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  237. if (!is_array($row)) {
  238. return false;
  239. }
  240. if ($row['softDelete'] == 1) {
  241. return false;
  242. }
  243. if ($row['rol'] == 1) {
  244. return false;
  245. }
  246. return true;
  247. }
  248. //read single user
  249. public function read_single() {
  250. $query = '';
  251. //create query
  252. $query .= 'SELECT ';
  253. $query .= 'id, ';
  254. $query .= 'rol, ';
  255. $query .= 'name, ';
  256. $query .= 'password, ';
  257. $query .= 'softDelete, ';
  258. $query .= 'createdAt ';
  259. $query .= "FROM {$this->table} ";
  260. $query .= 'WHERE id = ? ';
  261. //prepare statement
  262. $stmt = $this->conn->prepare($query);
  263. //binding param
  264. $stmt->bindParam(1, $this->id);
  265. //execute query
  266. $stmt->execute();
  267. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  268. $this->rol = $row['rol'];
  269. $this->name = $row['name'];
  270. $this->softDelete = $row['softDelete'];
  271. return $stmt;
  272. }
  273. }
  274. ?>