user.class.php 16 KB

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