2
0

user.class.php 15 KB

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