user.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <?php
  2. /**
  3. * Classe `user`
  4. *
  5. * Cette classe gère les utilisateurs et leurs actions.
  6. */
  7. class user
  8. {
  9. /**
  10. * Récupère les informations d'un utilisateur.
  11. *
  12. * @param int $userId L'ID de l'utilisateur.
  13. * @return array|bool Les informations de l'utilisateur ou FALSE en cas d'erreur.
  14. */
  15. public static function getUser(int $_id)
  16. {
  17. // Récupération des données de l'excel au format Json
  18. db::query("SELECT "
  19. . "" . DB_T_USER . ".id AS id, "
  20. . "" . DB_T_USER . ".email ,"
  21. . "" . DB_T_USER . ".prenom, "
  22. . "" . DB_T_USER . ".nom, "
  23. . "" . DB_T_USER . ".cree, "
  24. . "" . DB_T_USER . ".last_connect, "
  25. . "" . DB_T_USER . ".googleAuthenticator, "
  26. . "" . DB_T_USER . ".actif, "
  27. . "" . DB_T_USER . ".deleted, "
  28. . "" . DB_T_USER . ".id_type, "
  29. . "" . DB_T_TYPE_USER . ".type "
  30. . "FROM " . DB_T_USER . " "
  31. . "INNER JOIN " . DB_T_TYPE_USER . " ON " . DB_T_USER . ".id_type = " . DB_T_TYPE_USER . ".id "
  32. . "WHERE " . DB_T_USER . ".id = :id");
  33. db::bind(':id', $_id);
  34. $return = db::single();
  35. $return["tags"] = self::getTags($_id);
  36. return $return;
  37. }
  38. public static function getUsers() {
  39. // Récupération des données de l'excel au format Json
  40. db::query("SELECT "
  41. . "" . DB_T_USER . ".id, "
  42. . "" . DB_T_USER . ".email, "
  43. . "" . DB_T_USER . ".prenom, "
  44. . "" . DB_T_USER . ".nom, "
  45. . "" . DB_T_USER . ".cree, "
  46. . "" . DB_T_USER . ".last_connect, "
  47. . "" . DB_T_USER . ".googleAuthenticator, "
  48. . "" . DB_T_USER . ".actif, "
  49. . "" . DB_T_USER . ".id_type, "
  50. . "" . DB_T_TYPE_USER . ".type "
  51. . "FROM " . DB_T_USER . " "
  52. . "INNER JOIN " . DB_T_TYPE_USER . " ON " . DB_T_USER . ".id_type = " . DB_T_TYPE_USER . ".id "
  53. . "WHERE " . DB_T_USER . ".deleted = 0");
  54. $return = db::resultset();
  55. foreach ($return as $key => $users) {
  56. $return[$key] = $users;
  57. $return[$key]["tags"] = self::getTags($users["id"]);
  58. }
  59. return $return;
  60. }
  61. public static function getNameById(int $_id) {
  62. db::query("SELECT "
  63. . "CONCAT (" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS 'name' "
  64. . "FROM " . DB_T_USER . " "
  65. . "WHERE " . DB_T_USER . ".id = :id");
  66. db::bind(':id', $_id);
  67. return db::single()["name"];
  68. }
  69. public static function getMyGoogleAuthenticator(int $_id){
  70. db::query("SELECT "
  71. . "" . DB_T_USER . ".googleAuthenticatorSecret "
  72. . "FROM " . DB_T_USER . " "
  73. . "WHERE " . DB_T_USER . ".id = :id");
  74. db::bind(':id', $_id);
  75. return db::single()["googleAuthenticatorSecret"];
  76. }
  77. public static function checkGoogleAuthenticator(string $_email){
  78. db::query("SELECT "
  79. . "" . DB_T_USER . ".googleAuthenticator "
  80. . "FROM " . DB_T_USER . " "
  81. . "WHERE " . DB_T_USER . ".email = :email");
  82. db::bind(':email', $_email);
  83. $return = db::single();
  84. return (isset($return["googleAuthenticator"])) ? $return["googleAuthenticator"] : 0;
  85. }
  86. private static function insertJWT($_id_user, $_jwt){
  87. self::deleteJWTbyUSer($_id_user);
  88. db::query("INSERT INTO " . DB_T_JWT . " (id_user, md5, jwt) VALUES (:id_user, :md5, :jwt)");
  89. db::bind(':id_user', $_id_user);
  90. db::bind(':md5', md5($_jwt));
  91. db::bind(':jwt', $_jwt);
  92. try {
  93. db::execute();
  94. return TRUE;
  95. } catch (Exception $ex) {
  96. return FALSE;
  97. }
  98. }
  99. public static function deleteJWTbyUSer($_id_user){
  100. db::query("DELETE FROM " . DB_T_JWT . " WHERE id_user = :id_user");
  101. db::bind(':id_user', $_id_user);
  102. try {
  103. db::execute();
  104. return TRUE;
  105. } catch (Exception $ex) {
  106. return FALSE;
  107. }
  108. }
  109. public static function updateJWTbyMd5($_md5, $_jwt){
  110. db::query("UPDATE " . DB_T_JWT . " SET jwt = :jwt, md5 = :newmd5 WHERE md5 = :md5");
  111. db::bind(':md5', $_md5);
  112. db::bind(':jwt', $_jwt);
  113. db::bind(':newmd5', md5($_jwt));
  114. try {
  115. db::execute();
  116. return TRUE;
  117. } catch (Exception $ex) {
  118. return FALSE;
  119. }
  120. }
  121. public static function getInfosByJWT($_jwt){
  122. db::query("SELECT id_user, creer FROM " . DB_T_JWT . " WHERE md5 = :md5");
  123. db::bind(':md5', md5($_jwt));
  124. $row = db::single();
  125. if(isset($row["id_user"])){
  126. return $row;
  127. } else {
  128. return FALSE;
  129. }
  130. }
  131. public static function authenticator(array $_input)
  132. {
  133. db::query("SELECT id, email, password, prenom, nom, id_type, googleAuthenticator, googleAuthenticatorSecret, actif FROM " . DB_T_USER . " WHERE email = :email AND deleted = 0");
  134. db::bind(':email', $_input["email"]);
  135. $row = db::single();
  136. if (isset($row["id"])) {
  137. if ($row["actif"] == 0) {
  138. return [
  139. "status" => "error",
  140. "type" => "actif"
  141. ];
  142. } elseif (isset($_input["password"]) and md5($_input["password"]) == $row["password"]) {
  143. return [
  144. "status" => "success",
  145. "type" => "authent",
  146. "id" => $row["id"],
  147. "prenom" => $row["prenom"],
  148. "nom" => $row["nom"],
  149. "googleAuthenticator" => $row["googleAuthenticator"],
  150. "googleAuthenticatorSecret" => $row["googleAuthenticatorSecret"],
  151. "idType" => $row["id_type"],
  152. "email" => $row["email"],
  153. "actif" => $row["actif"],
  154. "iat" => time(),
  155. "exp" => time() + JWT_DURATION
  156. ];
  157. } else {
  158. return [
  159. "status" => "error",
  160. "type" => "authent"
  161. ];
  162. }
  163. } else {
  164. return [
  165. "status" => "error",
  166. "type" => "unknown"
  167. ];
  168. }
  169. }
  170. public static function connect(array $_input) {
  171. $connect = jwt::authenticate($_input);
  172. if ($connect["googleAuthenticator"] == 1 and DOMAIN_CONTROL != $_SERVER['SERVER_NAME']) {
  173. if (googleAuthenticator::verifyCode($connect["googleAuthenticatorSecret"], $_input["authenticator"], 1) == FALSE) {
  174. $connect["id"] = NULL;
  175. }
  176. }
  177. if (isset($connect["id"])) {
  178. if ($connect["status"] == "success") {
  179. $_SESSION["user"] = array(
  180. "id" => $connect["id"],
  181. "prenom" => $connect["prenom"],
  182. "nom" => $connect["nom"],
  183. "googleAuthenticator" => $connect["googleAuthenticator"],
  184. "idType" => $connect["idType"],
  185. "email" => $connect["email"],
  186. "actif" => $connect["actif"],
  187. "token" => $connect["token"]
  188. );
  189. self::updateLastConnect($connect["id"]);
  190. self::insertJWT($connect["id"], $connect["token"]);
  191. return TRUE;
  192. } else {
  193. if ($connect["type"] == "actif") {
  194. alert::recError("Votre compte est désactivé");
  195. return FALSE;
  196. } elseif ($connect["type"] == "authent") {
  197. alert::recError("Erreur d'authentification");
  198. return FALSE;
  199. } elseif ($connect["type"] == "unknown") {
  200. alert::recError("Erreur d'authentification");
  201. return FALSE;
  202. }
  203. }
  204. } else {
  205. alert::recError("Erreur d'authentification");
  206. return FALSE;
  207. }
  208. }
  209. private static function updateLastConnect(int $_id){
  210. db::query("UPDATE " . DB_T_USER . " SET `last_connect` = CURRENT_TIMESTAMP() WHERE id = :id");
  211. db::bind(':id', $_id);
  212. db::execute();
  213. }
  214. public static function add_user(array $_input){
  215. db::query("INSERT INTO " . DB_T_USER . " "
  216. . "(email, password, googleAuthenticator, googleAuthenticatorSecret, prenom, nom, id_type, actif) "
  217. . "VALUES (:email, :password, :googleAuthenticator, :googleAuthenticatorSecret, :prenom, :nom, :id_type, :actif)");
  218. db::bind(':email', $_input["email"]);
  219. db::bind(':password', md5($_input["password"]));
  220. db::bind(':prenom', $_input["prenom"]);
  221. db::bind(':nom', $_input["nom"]);
  222. db::bind(':googleAuthenticator', $_input["googleAuthenticator"]);
  223. db::bind(':googleAuthenticatorSecret', googleAuthenticator::createSecret());
  224. db::bind(':id_type', $_input["id_type"]);
  225. db::bind(':actif', $_input["actif"]);
  226. try {
  227. db::execute();
  228. $tags = tags::textToId($_input["tags"], 1);
  229. self::addTags(db::lastInsertId(), $tags);
  230. alert::recSuccess("La création a bien été prise en compte");
  231. } catch (Exception $ex) {
  232. alert::recError("Erreur lors de la création de l'utilisateur");
  233. header("Location: /add-user.html");
  234. exit();
  235. }
  236. }
  237. public static function lastUser(){
  238. db::query("SELECT MAX(id) AS id FROM ". DB_T_USER);
  239. return db::single()["id"];
  240. }
  241. public static function maj_user(array $_input){
  242. if($_input["password"] != ""){
  243. db::query("UPDATE " . DB_T_USER . " SET password = :password WHERE id = :id");
  244. db::bind(':password', md5($_input["password"]));
  245. db::bind(':id', $_input["id"]);
  246. try {
  247. db::execute();
  248. } catch (Exception $ex) {
  249. alert::recError("Erreur lors de la modification du mot de passe");
  250. header("Location: /user-" . $_input["id"] .".html");
  251. exit();
  252. }
  253. }
  254. if(self::getMyGoogleAuthenticator($_input["id"]) == NULL){
  255. db::query("UPDATE " . DB_T_USER . " SET googleAuthenticatorSecret = :googleAuthenticatorSecret WHERE id = :id");
  256. db::bind(':googleAuthenticatorSecret', googleAuthenticator::createSecret());
  257. db::bind(':id', $_input["id"]);
  258. try {
  259. db::execute();
  260. } catch (Exception $ex) {
  261. alert::recError("Erreur lors de la création du token de Google Authenticator");
  262. header("Location: /user-" . $_input["id"] .".html");
  263. exit();
  264. }
  265. }
  266. $tags = tags::textToId($_input["tags"], 1);
  267. self::addTags($_input["id"], $tags);
  268. db::query("UPDATE " . DB_T_USER . " SET
  269. email = :email,
  270. prenom = :prenom,
  271. nom = :nom,
  272. id_type = :id_type,
  273. googleAuthenticator = :googleAuthenticator,
  274. actif = :actif
  275. WHERE id = :id");
  276. db::bind(':email', $_input["email"]);
  277. db::bind(':prenom', $_input["prenom"]);
  278. db::bind(':nom', $_input["nom"]);
  279. db::bind(':googleAuthenticator', $_input["googleAuthenticator"]);
  280. db::bind(':id_type', $_input["id_type"]);
  281. db::bind(':actif', $_input["actif"]);
  282. db::bind(':id', $_input["id"]);
  283. try {
  284. db::execute();
  285. alert::recSuccess("La modification a bien été prise en compte");
  286. } catch (Exception $ex) {
  287. alert::recError("Erreur lors de la modification de l'utilisateur");
  288. header("Location: /user-" . $_input["id"] . ".html");
  289. exit();
  290. }
  291. }
  292. static public function getTags(float $_idUser){
  293. db::query("SELECT "
  294. . "" . DB_T_TAGS . ".label "
  295. . "FROM " . DB_T_USER_TAGS . " "
  296. . "INNER JOIN " . DB_T_TAGS . " ON " . DB_T_TAGS . ".id = " . DB_T_USER_TAGS . ".id_tags "
  297. . "WHERE " . DB_T_USER_TAGS . ".id_user = :id "
  298. . "ORDER BY " . DB_T_USER_TAGS . ".creer");
  299. db::bind(':id', $_idUser);
  300. $tmp = db::resultset();
  301. if(isset($tmp[0])){
  302. $return = NULL;
  303. foreach ($tmp as $value) {
  304. $return .= $value["label"].",";
  305. }
  306. $return = substr($return, 0, -1);
  307. return $return;
  308. } else {
  309. return NULL;
  310. }
  311. }
  312. static public function getIdTags(float $_idUser){
  313. db::query("SELECT "
  314. . "" . DB_T_USER_TAGS . ".id_tags "
  315. . "FROM " . DB_T_USER_TAGS . " "
  316. . "WHERE " . DB_T_USER_TAGS . ".id_user = :id "
  317. . "ORDER BY " . DB_T_USER_TAGS . ".creer");
  318. db::bind(':id', $_idUser);
  319. $tmp = db::resultset();
  320. if(isset($tmp[0])){
  321. $return = [];
  322. foreach ($tmp as $value) {
  323. $return[] = $value["id_tags"];
  324. }
  325. return $return;
  326. } else {
  327. return NULL;
  328. }
  329. }
  330. private static function addTags(float $_idUser, string $_tags = NULL)
  331. {
  332. db::query("DELETE FROM " . DB_T_USER_TAGS . " WHERE id_user = :id_user");
  333. db::bind(':id_user', $_idUser);
  334. db::execute();
  335. if($_tags != NULL){
  336. $tags = explode(",", $_tags);
  337. $sqlMaj = "";
  338. foreach ($tags as $tag) {
  339. $sqlMaj .= " (:id_user, ".$tag."),";
  340. }
  341. $sqlMaj = substr($sqlMaj, 0, -1);
  342. db::query("INSERT INTO " . DB_T_USER_TAGS . " (id_user, id_tags) VALUES" . $sqlMaj);
  343. db::bind(':id_user', $_idUser);
  344. try {
  345. db::execute();
  346. return TRUE;
  347. } catch (Exception $ex) {
  348. return FALSE;
  349. }
  350. }
  351. }
  352. public static function deleteUser(int $_id){
  353. db::query("UPDATE " . DB_T_USER . " SET deleted = 1 WHERE id = :id");
  354. db::bind(':id', $_id);
  355. try {
  356. db::execute();
  357. } catch (Exception $ex) {
  358. alert::recError("Erreur lors de la suppression");
  359. header("Location: /user-" . $_id .".html");
  360. exit();
  361. }
  362. }
  363. public static function restoreUser(int $_id){
  364. db::query("UPDATE " . DB_T_USER . " SET deleted = 0 WHERE id = :id");
  365. db::bind(':id', $_id);
  366. try {
  367. db::execute();
  368. } catch (Exception $ex) {
  369. alert::recError("Erreur lors de la restauration");
  370. header("Location: /user-" . $_id .".html");
  371. exit();
  372. }
  373. }
  374. static public function checkSecur(){
  375. db::query("SELECT googleAuthenticator FROM " . DB_T_USER . " WHERE id = :id");
  376. db::bind(':id', session::getId());
  377. return db::single()["googleAuthenticator"] == 1 ? TRUE : FALSE;
  378. }
  379. static public function printIsSecur(){
  380. if(ALERT_AUTHENTICATOR == TRUE){
  381. $_SESSION["CALLOUT"] ??= 0;
  382. if(self::checkSecur() == FALSE AND $_SESSION["CALLOUT"] < NB_ALERT_AUTHENTICATOR){
  383. $callout = [
  384. "type" => "danger",
  385. "size" => "tiny",
  386. "p" => "Pour sécuriser l'accès au CMS, il est fortement recommandé d'activer la double authentification (Google Authenticator) sur votre profil. Pour l'activer sur votre profil en <a href=\"/user.html\">cliquant ici</a>.",
  387. ];
  388. callout::print($callout);
  389. $_SESSION["CALLOUT"]++;
  390. }
  391. }
  392. }
  393. }