user.class.php 14 KB

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