user.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. "apiSession" => $connect["session_id"],
  170. "id" => $connect["id"],
  171. "prenom" => $connect["prenom"],
  172. "nom" => $connect["nom"],
  173. "googleAuthenticator" => $connect["googleAuthenticator"],
  174. "idType" => $connect["idType"],
  175. "email" => $connect["email"],
  176. "actif" => $connect["actif"],
  177. "token" => $connect["token"]
  178. );
  179. self::updateLastConnect($connect["id"]);
  180. self::insertJWT($connect["id"], $connect["token"]);
  181. return TRUE;
  182. } else {
  183. if ($connect["type"] == "actif") {
  184. alert::recError("Votre compte est désactivé");
  185. return FALSE;
  186. } elseif ($connect["type"] == "authent") {
  187. alert::recError("Erreur d'authentification");
  188. return FALSE;
  189. } elseif ($connect["type"] == "unknown") {
  190. alert::recError("Erreur d'authentification");
  191. return FALSE;
  192. }
  193. }
  194. } else {
  195. alert::recError("Erreur d'authentification");
  196. return FALSE;
  197. }
  198. }
  199. private static function updateLastConnect(int $_id){
  200. db::query("UPDATE " . DB_T_USER . " SET `last_connect` = CURRENT_TIMESTAMP() WHERE id = :id");
  201. db::bind(':id', $_id);
  202. db::execute();
  203. }
  204. public static function add_user(array $_input){
  205. db::query("INSERT INTO " . DB_T_USER . " "
  206. . "(email, password, googleAuthenticator, googleAuthenticatorSecret, prenom, nom, id_type, actif) "
  207. . "VALUES (:email, :password, :googleAuthenticator, :googleAuthenticatorSecret, :prenom, :nom, :id_type, :actif)");
  208. db::bind(':email', $_input["email"]);
  209. db::bind(':password', md5($_input["password"]));
  210. db::bind(':prenom', $_input["prenom"]);
  211. db::bind(':nom', $_input["nom"]);
  212. db::bind(':googleAuthenticator', $_input["googleAuthenticator"]);
  213. db::bind(':googleAuthenticatorSecret', googleAuthenticator::createSecret());
  214. db::bind(':id_type', $_input["id_type"]);
  215. db::bind(':actif', $_input["actif"]);
  216. try {
  217. db::execute();
  218. $tags = tags::textToId($_input["tags"], 1);
  219. self::addTags(db::lastInsertId(), $tags);
  220. alert::recSuccess("La création a bien été prise en compte");
  221. } catch (Exception $ex) {
  222. alert::recError("Erreur lors de la création de l'utilisateur");
  223. header("Location: /add-user.html");
  224. exit();
  225. }
  226. }
  227. public static function lastUser(){
  228. db::query("SELECT MAX(id) AS id FROM ". DB_T_USER);
  229. return db::single()["id"];
  230. }
  231. public static function maj_user(array $_input){
  232. if($_input["password"] != ""){
  233. db::query("UPDATE " . DB_T_USER . " SET password = :password WHERE id = :id");
  234. db::bind(':password', md5($_input["password"]));
  235. db::bind(':id', $_input["id"]);
  236. try {
  237. db::execute();
  238. } catch (Exception $ex) {
  239. alert::recError("Erreur lors de la modification du mot de passe");
  240. header("Location: /user-" . $_input["id"] .".html");
  241. exit();
  242. }
  243. }
  244. if(self::getMyGoogleAuthenticator($_input["id"]) == NULL){
  245. db::query("UPDATE " . DB_T_USER . " SET googleAuthenticatorSecret = :googleAuthenticatorSecret WHERE id = :id");
  246. db::bind(':googleAuthenticatorSecret', googleAuthenticator::createSecret());
  247. db::bind(':id', $_input["id"]);
  248. try {
  249. db::execute();
  250. } catch (Exception $ex) {
  251. alert::recError("Erreur lors de la création du token de Google Authenticator");
  252. header("Location: /user-" . $_input["id"] .".html");
  253. exit();
  254. }
  255. }
  256. $tags = tags::textToId($_input["tags"], 1);
  257. self::addTags($_input["id"], $tags);
  258. db::query("UPDATE " . DB_T_USER . " SET
  259. email = :email,
  260. prenom = :prenom,
  261. nom = :nom,
  262. id_type = :id_type,
  263. googleAuthenticator = :googleAuthenticator,
  264. actif = :actif
  265. WHERE id = :id");
  266. db::bind(':email', $_input["email"]);
  267. db::bind(':prenom', $_input["prenom"]);
  268. db::bind(':nom', $_input["nom"]);
  269. db::bind(':googleAuthenticator', $_input["googleAuthenticator"]);
  270. db::bind(':id_type', $_input["id_type"]);
  271. db::bind(':actif', $_input["actif"]);
  272. db::bind(':id', $_input["id"]);
  273. try {
  274. db::execute();
  275. alert::recSuccess("La modification a bien été prise en compte");
  276. } catch (Exception $ex) {
  277. alert::recError("Erreur lors de la modification de l'utilisateur");
  278. header("Location: /user-" . $_input["id"] . ".html");
  279. exit();
  280. }
  281. }
  282. static public function getTags(float $_idUser){
  283. db::query("SELECT "
  284. . "" . DB_T_TAGS . ".label "
  285. . "FROM " . DB_T_USER_TAGS . " "
  286. . "INNER JOIN " . DB_T_TAGS . " ON " . DB_T_TAGS . ".id = " . DB_T_USER_TAGS . ".id_tags "
  287. . "WHERE " . DB_T_USER_TAGS . ".id_user = :id "
  288. . "ORDER BY " . DB_T_USER_TAGS . ".creer");
  289. db::bind(':id', $_idUser);
  290. $tmp = db::resultset();
  291. if(isset($tmp[0])){
  292. $return = NULL;
  293. foreach ($tmp as $value) {
  294. $return .= $value["label"].",";
  295. }
  296. $return = substr($return, 0, -1);
  297. return $return;
  298. } else {
  299. return NULL;
  300. }
  301. }
  302. static public function getIdTags(float $_idUser){
  303. db::query("SELECT "
  304. . "" . DB_T_USER_TAGS . ".id_tags "
  305. . "FROM " . DB_T_USER_TAGS . " "
  306. . "WHERE " . DB_T_USER_TAGS . ".id_user = :id "
  307. . "ORDER BY " . DB_T_USER_TAGS . ".creer");
  308. db::bind(':id', $_idUser);
  309. $tmp = db::resultset();
  310. if(isset($tmp[0])){
  311. $return = [];
  312. foreach ($tmp as $value) {
  313. $return[] = $value["id_tags"];
  314. }
  315. return $return;
  316. } else {
  317. return NULL;
  318. }
  319. }
  320. private static function addTags(float $_idUser, string $_tags = NULL)
  321. {
  322. db::query("DELETE FROM " . DB_T_USER_TAGS . " WHERE id_user = :id_user");
  323. db::bind(':id_user', $_idUser);
  324. db::execute();
  325. if($_tags != NULL){
  326. $tags = explode(",", $_tags);
  327. $sqlMaj = "";
  328. foreach ($tags as $tag) {
  329. $sqlMaj .= " (:id_user, ".$tag."),";
  330. }
  331. $sqlMaj = substr($sqlMaj, 0, -1);
  332. db::query("INSERT INTO " . DB_T_USER_TAGS . " (id_user, id_tags) VALUES" . $sqlMaj);
  333. db::bind(':id_user', $_idUser);
  334. try {
  335. db::execute();
  336. return TRUE;
  337. } catch (Exception $ex) {
  338. return FALSE;
  339. }
  340. }
  341. }
  342. public static function deleteUser(int $_id){
  343. db::query("UPDATE " . DB_T_USER . " SET deleted = 1 WHERE id = :id");
  344. db::bind(':id', $_id);
  345. try {
  346. db::execute();
  347. } catch (Exception $ex) {
  348. alert::recError("Erreur lors de la suppression");
  349. header("Location: /user-" . $_id .".html");
  350. exit();
  351. }
  352. }
  353. public static function restoreUser(int $_id){
  354. db::query("UPDATE " . DB_T_USER . " SET deleted = 0 WHERE id = :id");
  355. db::bind(':id', $_id);
  356. try {
  357. db::execute();
  358. } catch (Exception $ex) {
  359. alert::recError("Erreur lors de la restauration");
  360. header("Location: /user-" . $_id .".html");
  361. exit();
  362. }
  363. }
  364. static public function checkSecur(){
  365. db::query("SELECT googleAuthenticator FROM " . DB_T_USER . " WHERE id = :id");
  366. db::bind(':id', session::getId());
  367. return db::single()["googleAuthenticator"] == 1 ? TRUE : FALSE;
  368. }
  369. static public function printIsSecur(){
  370. if(ALERT_AUTHENTICATOR == TRUE){
  371. $_SESSION["CALLOUT"] ??= 0;
  372. if(self::checkSecur() == FALSE AND $_SESSION["CALLOUT"] < NB_ALERT_AUTHENTICATOR){
  373. $callout = [
  374. "type" => "danger",
  375. "size" => "tiny",
  376. "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>.",
  377. ];
  378. callout::print($callout);
  379. $_SESSION["CALLOUT"]++;
  380. }
  381. }
  382. }
  383. }