user.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. public static function connect(array $_input) {
  76. $return = NULL;
  77. if (isset($_input["email"]) AND isset($_input["password"])) {
  78. db::query("SELECT id, email, password, prenom, nom, id_type, googleAuthenticator, googleAuthenticatorSecret, actif FROM " . DB_T_USER . " WHERE email = :email AND deleted = 0");
  79. db::bind(':email', $_input["email"]);
  80. $row = db::single();
  81. if($row["googleAuthenticator"] == 1 AND DOMAIN_CONTROL != $_SERVER['SERVER_NAME']){
  82. if(googleAuthenticator::verifyCode($row["googleAuthenticatorSecret"], $_input["authenticator"], 1) == FALSE){
  83. $row["id"] = NULL;
  84. }
  85. }
  86. if (isset($row["id"])) {
  87. if ($row["actif"] == 0) {
  88. alert::recError("Votre compte est désactivé");
  89. return FALSE;
  90. } elseif (isset($_input["password"]) AND md5($_input["password"]) == $row["password"]) {
  91. $_SESSION["user"] = array(
  92. "id" => $row["id"],
  93. "prenom" => $row["prenom"],
  94. "nom" => $row["nom"],
  95. "googleAuthenticator" => $row["googleAuthenticator"],
  96. "idType" => $row["id_type"],
  97. "email" => $row["email"],
  98. "actif" => $row["actif"]
  99. );
  100. self::updateLastConnect($row["id"]);
  101. return TRUE;
  102. } else {
  103. alert::recError("Erreur d'authentification");
  104. return FALSE;
  105. }
  106. } else {
  107. alert::recError("Erreur d'authentification");
  108. return FALSE;
  109. }
  110. } else {
  111. alert::recError("Erreur d'authentification");
  112. return FALSE;
  113. }
  114. }
  115. private static function updateLastConnect(int $_id){
  116. db::query("UPDATE " . DB_T_USER . " SET `last_connect` = CURRENT_TIMESTAMP() WHERE id = :id");
  117. db::bind(':id', $_id);
  118. db::execute();
  119. }
  120. public static function add_user(array $_input){
  121. db::query("INSERT INTO " . DB_T_USER . " "
  122. . "(email, password, googleAuthenticator, googleAuthenticatorSecret, prenom, nom, id_type, tags, actif) "
  123. . "VALUES (:email, :password, :googleAuthenticator, :googleAuthenticatorSecret, :prenom, :nom, :id_type, :tags, :actif)");
  124. db::bind(':email', $_input["email"]);
  125. db::bind(':password', md5($_input["password"]));
  126. db::bind(':prenom', $_input["prenom"]);
  127. db::bind(':nom', $_input["nom"]);
  128. db::bind(':googleAuthenticator', $_input["googleAuthenticator"]);
  129. db::bind(':googleAuthenticatorSecret', googleAuthenticator::createSecret());
  130. db::bind(':id_type', $_input["id_type"]);
  131. db::bind(':actif', $_input["actif"]);
  132. try {
  133. db::execute();
  134. $tags = tags::textToId($_input["tags"], 1);
  135. self::addTags(db::lastInsertId(), $tags);
  136. alert::recSuccess("La création a bien été prise en compte");
  137. } catch (Exception $ex) {
  138. alert::recError("Erreur lors de la création de l'utilisateur");
  139. header("Location: /add-user.html");
  140. exit();
  141. }
  142. }
  143. public static function lastUser(){
  144. db::query("SELECT MAX(id) AS id FROM ". DB_T_USER);
  145. return db::single()["id"];
  146. }
  147. public static function maj_user(array $_input){
  148. if($_input["password"] != ""){
  149. db::query("UPDATE " . DB_T_USER . " SET password = :password WHERE id = :id");
  150. db::bind(':password', md5($_input["password"]));
  151. db::bind(':id', $_input["id"]);
  152. try {
  153. db::execute();
  154. } catch (Exception $ex) {
  155. alert::recError("Erreur lors de la modification du mot de passe");
  156. header("Location: /user-" . $_input["id"] .".html");
  157. exit();
  158. }
  159. }
  160. if(self::getMyGoogleAuthenticator($_input["id"]) == NULL){
  161. db::query("UPDATE " . DB_T_USER . " SET googleAuthenticatorSecret = :googleAuthenticatorSecret WHERE id = :id");
  162. db::bind(':googleAuthenticatorSecret', googleAuthenticator::createSecret());
  163. db::bind(':id', $_input["id"]);
  164. try {
  165. db::execute();
  166. } catch (Exception $ex) {
  167. alert::recError("Erreur lors de la création du token de Google Authenticator");
  168. header("Location: /user-" . $_input["id"] .".html");
  169. exit();
  170. }
  171. }
  172. $tags = tags::textToId($_input["tags"], 1);
  173. self::addTags($_input["id"], $tags);
  174. db::query("UPDATE " . DB_T_USER . " SET
  175. email = :email,
  176. prenom = :prenom,
  177. nom = :nom,
  178. id_type = :id_type,
  179. googleAuthenticator = :googleAuthenticator,
  180. actif = :actif
  181. WHERE id = :id");
  182. db::bind(':email', $_input["email"]);
  183. db::bind(':prenom', $_input["prenom"]);
  184. db::bind(':nom', $_input["nom"]);
  185. db::bind(':googleAuthenticator', $_input["googleAuthenticator"]);
  186. db::bind(':id_type', $_input["id_type"]);
  187. db::bind(':actif', $_input["actif"]);
  188. db::bind(':id', $_input["id"]);
  189. try {
  190. db::execute();
  191. alert::recSuccess("La modification a bien été prise en compte");
  192. } catch (Exception $ex) {
  193. alert::recError("Erreur lors de la modification de l'utilisateur");
  194. header("Location: /user-" . $_input["id"] . ".html");
  195. exit();
  196. }
  197. }
  198. static public function getTags(float $_idUser){
  199. db::query("SELECT "
  200. . "" . DB_T_TAGS . ".label "
  201. . "FROM " . DB_T_USER_TAGS . " "
  202. . "INNER JOIN " . DB_T_TAGS . " ON " . DB_T_TAGS . ".id = " . DB_T_USER_TAGS . ".id_tags "
  203. . "WHERE " . DB_T_USER_TAGS . ".id_user = :id "
  204. . "ORDER BY " . DB_T_USER_TAGS . ".creer");
  205. db::bind(':id', $_idUser);
  206. $tmp = db::resultset();
  207. if(isset($tmp[0])){
  208. $return = NULL;
  209. foreach ($tmp as $value) {
  210. $return .= $value["label"].",";
  211. }
  212. $return = substr($return, 0, -1);
  213. return $return;
  214. } else {
  215. return NULL;
  216. }
  217. }
  218. static public function getIdTags(float $_idUser){
  219. db::query("SELECT "
  220. . "" . DB_T_USER_TAGS . ".id_tags "
  221. . "FROM " . DB_T_USER_TAGS . " "
  222. . "WHERE " . DB_T_USER_TAGS . ".id_user = :id "
  223. . "ORDER BY " . DB_T_USER_TAGS . ".creer");
  224. db::bind(':id', $_idUser);
  225. $tmp = db::resultset();
  226. if(isset($tmp[0])){
  227. $return = [];
  228. foreach ($tmp as $value) {
  229. $return[] = $value["id_tags"];
  230. }
  231. return $return;
  232. } else {
  233. return NULL;
  234. }
  235. }
  236. private static function addTags(float $_idUser, string $_tags = NULL)
  237. {
  238. db::query("DELETE FROM " . DB_T_USER_TAGS . " WHERE id_user = :id_user");
  239. db::bind(':id_user', $_idUser);
  240. db::execute();
  241. if($_tags != NULL){
  242. $tags = explode(",", $_tags);
  243. $sqlMaj = "";
  244. foreach ($tags as $tag) {
  245. $sqlMaj .= " (:id_user, ".$tag."),";
  246. }
  247. $sqlMaj = substr($sqlMaj, 0, -1);
  248. db::query("INSERT INTO " . DB_T_USER_TAGS . " (id_user, id_tags) VALUES" . $sqlMaj);
  249. db::bind(':id_user', $_idUser);
  250. try {
  251. db::execute();
  252. return TRUE;
  253. } catch (Exception $ex) {
  254. return FALSE;
  255. }
  256. }
  257. }
  258. public static function deleteUser(int $_id){
  259. db::query("UPDATE " . DB_T_USER . " SET deleted = 1 WHERE id = :id");
  260. db::bind(':id', $_id);
  261. try {
  262. db::execute();
  263. } catch (Exception $ex) {
  264. alert::recError("Erreur lors de la suppression");
  265. header("Location: /user-" . $_id .".html");
  266. exit();
  267. }
  268. }
  269. public static function restoreUser(int $_id){
  270. db::query("UPDATE " . DB_T_USER . " SET deleted = 0 WHERE id = :id");
  271. db::bind(':id', $_id);
  272. try {
  273. db::execute();
  274. } catch (Exception $ex) {
  275. alert::recError("Erreur lors de la restauration");
  276. header("Location: /user-" . $_id .".html");
  277. exit();
  278. }
  279. }
  280. static public function checkSecur(){
  281. db::query("SELECT googleAuthenticator FROM " . DB_T_USER . " WHERE id = :id");
  282. db::bind(':id', session::getId());
  283. return db::single()["googleAuthenticator"] == 1 ? TRUE : FALSE;
  284. }
  285. static public function printIsSecur(){
  286. if(ALERT_AUTHENTICATOR == TRUE){
  287. $_SESSION["CALLOUT"] ??= 0;
  288. if(self::checkSecur() == FALSE AND $_SESSION["CALLOUT"] < NB_ALERT_AUTHENTICATOR){
  289. $callout = [
  290. "type" => "danger",
  291. "size" => "tiny",
  292. "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>.",
  293. ];
  294. callout::print($callout);
  295. $_SESSION["CALLOUT"]++;
  296. }
  297. }
  298. }
  299. }