user.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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_USER . ".tags, "
  19. . "" . DB_T_USER_TYPE . ".type "
  20. . "FROM " . DB_T_USER . " "
  21. . "INNER JOIN " . DB_T_USER_TYPE . " ON " . DB_T_USER . ".id_type = " . DB_T_USER_TYPE . ".id "
  22. . "WHERE " . DB_T_USER . ".id = :id");
  23. db::bind(':id', $_id);
  24. $return = db::single();
  25. $return["tags"] = tags::idToTtext($return["tags"]);
  26. return $return;
  27. }
  28. public static function getUsers() {
  29. // Récupération des données de l'excel au format Json
  30. db::query("SELECT "
  31. . "" . DB_T_USER . ".id AS id, "
  32. . "" . DB_T_USER . ".email ,"
  33. . "" . DB_T_USER . ".prenom, "
  34. . "" . DB_T_USER . ".nom, "
  35. . "" . DB_T_USER . ".cree, "
  36. . "" . DB_T_USER . ".last_connect, "
  37. . "" . DB_T_USER . ".googleAuthenticator, "
  38. . "" . DB_T_USER . ".actif, "
  39. . "" . DB_T_USER . ".id_type, "
  40. . "" . DB_T_USER . ".tags, "
  41. . "" . DB_T_USER_TYPE . ".type "
  42. . "FROM " . DB_T_USER . " "
  43. . "INNER JOIN " . DB_T_USER_TYPE . " ON " . DB_T_USER . ".id_type = " . DB_T_USER_TYPE . ".id "
  44. . "WHERE " . DB_T_USER . ".deleted = 0");
  45. $return = db::resultset();
  46. foreach ($return as $key => $users) {
  47. $return[$key] = $users;
  48. $return[$key]["tags"] = tags::idToTtext($users["tags"]);
  49. }
  50. return $return;
  51. }
  52. public static function getNameById(int $_id) {
  53. db::query("SELECT "
  54. . "CONCAT (" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS 'name' "
  55. . "FROM " . DB_T_USER . " "
  56. . "WHERE " . DB_T_USER . ".id = :id");
  57. db::bind(':id', $_id);
  58. return db::single()["name"];
  59. }
  60. public static function getMyGoogleAuthenticator(int $_id){
  61. db::query("SELECT "
  62. . "" . DB_T_USER . ".googleAuthenticatorSecret "
  63. . "FROM " . DB_T_USER . " "
  64. . "WHERE " . DB_T_USER . ".id = :id");
  65. db::bind(':id', $_id);
  66. return db::single()["googleAuthenticatorSecret"];
  67. }
  68. public static function checkGoogleAuthenticator(string $_email){
  69. db::query("SELECT "
  70. . "" . DB_T_USER . ".googleAuthenticator "
  71. . "FROM " . DB_T_USER . " "
  72. . "WHERE " . DB_T_USER . ".email = :email");
  73. db::bind(':email', $_email);
  74. $return = db::single();
  75. return (isset($return["googleAuthenticator"])) ? $return["googleAuthenticator"] : 0;
  76. }
  77. public static function connect(array $_input) {
  78. $return = NULL;
  79. if (isset($_input["email"]) AND isset($_input["password"])) {
  80. db::query("SELECT id, email, password, prenom, nom, id_type, googleAuthenticator, googleAuthenticatorSecret, actif FROM " . DB_T_USER . " WHERE email = :email AND deleted = 0");
  81. db::bind(':email', $_input["email"]);
  82. $row = db::single();
  83. if($row["googleAuthenticator"] == 1 AND DOMAIN_CONTROL != $_SERVER['SERVER_NAME']){
  84. if(googleAuthenticator::verifyCode($row["googleAuthenticatorSecret"], $_input["authenticator"], 1) == FALSE){
  85. $row["id"] = NULL;
  86. }
  87. }
  88. if (isset($row["id"])) {
  89. if ($row["actif"] == 0) {
  90. alert::recError("Votre compte est désactivé");
  91. return FALSE;
  92. } elseif (isset($_input["password"]) AND md5($_input["password"]) == $row["password"]) {
  93. $_SESSION["user"] = array(
  94. "id" => $row["id"],
  95. "prenom" => $row["prenom"],
  96. "nom" => $row["nom"],
  97. "googleAuthenticator" => $row["googleAuthenticator"],
  98. "idType" => $row["id_type"],
  99. "email" => $row["email"],
  100. "actif" => $row["actif"]
  101. );
  102. self::updateLastConnect($row["id"]);
  103. return TRUE;
  104. } else {
  105. alert::recError("Erreur d'authentification");
  106. return FALSE;
  107. }
  108. } else {
  109. alert::recError("Erreur d'authentification");
  110. return FALSE;
  111. }
  112. } else {
  113. alert::recError("Erreur d'authentification");
  114. return FALSE;
  115. }
  116. }
  117. private static function updateLastConnect(int $_id){
  118. db::query("UPDATE " . DB_T_USER . " SET `last_connect` = CURRENT_TIMESTAMP() WHERE id = :id");
  119. db::bind(':id', $_id);
  120. db::execute();
  121. }
  122. public static function add_user(array $_input){
  123. $tags = tags::textToId($_input("tags"));
  124. db::query("INSERT INTO " . DB_T_USER . " "
  125. . "(email, password, googleAuthenticator, googleAuthenticatorSecret, prenom, nom, id_type, tags, actif) "
  126. . "VALUES (:email, :password, :googleAuthenticator, :googleAuthenticatorSecret, :prenom, :nom, :id_type, :tags, :actif)");
  127. db::bind(':email', $_input["email"]);
  128. db::bind(':password', md5($_input["password"]));
  129. db::bind(':prenom', $_input["prenom"]);
  130. db::bind(':nom', $_input["nom"]);
  131. db::bind(':googleAuthenticator', $_input["googleAuthenticator"]);
  132. db::bind(':googleAuthenticatorSecret', googleAuthenticator::createSecret());
  133. db::bind(':id_type', $_input["id_type"]);
  134. db::bind(':tags', $tags);
  135. db::bind(':actif', $_input["actif"]);
  136. try {
  137. db::execute();
  138. alert::recSuccess("La création a bien été prise en compte");
  139. } catch (Exception $ex) {
  140. alert::recError("Erreur lors de la création de l'utilisateur");
  141. header("Location: /add-user.html");
  142. exit();
  143. }
  144. }
  145. public static function lastUser(){
  146. db::query("SELECT MAX(id) AS id FROM ". DB_T_USER);
  147. return db::single()["id"];
  148. }
  149. public static function maj_user(array $_input){
  150. if($_input["password"] != ""){
  151. db::query("UPDATE " . DB_T_USER . " SET password = :password WHERE id = :id");
  152. db::bind(':password', md5($_input["password"]));
  153. db::bind(':id', $_input["id"]);
  154. try {
  155. db::execute();
  156. } catch (Exception $ex) {
  157. alert::recError("Erreur lors de la modification du mot de passe");
  158. header("Location: /user-" . $_input["id"] .".html");
  159. exit();
  160. }
  161. }
  162. if(self::getMyGoogleAuthenticator($_input["id"]) == NULL){
  163. db::query("UPDATE " . DB_T_USER . " SET googleAuthenticatorSecret = :googleAuthenticatorSecret WHERE id = :id");
  164. db::bind(':googleAuthenticatorSecret', googleAuthenticator::createSecret());
  165. db::bind(':id', $_input["id"]);
  166. try {
  167. db::execute();
  168. } catch (Exception $ex) {
  169. alert::recError("Erreur lors de la création du token de Google Authenticator");
  170. header("Location: /user-" . $_input["id"] .".html");
  171. exit();
  172. }
  173. }
  174. $tags = tags::textToId($_input["tags"]);
  175. db::query("UPDATE " . DB_T_USER . " SET
  176. email = :email,
  177. prenom = :prenom,
  178. nom = :nom,
  179. id_type = :id_type,
  180. tags = :tags,
  181. googleAuthenticator = :googleAuthenticator,
  182. actif = :actif
  183. WHERE id = :id");
  184. db::bind(':email', $_input["email"]);
  185. db::bind(':prenom', $_input["prenom"]);
  186. db::bind(':nom', $_input["nom"]);
  187. db::bind(':googleAuthenticator', $_input["googleAuthenticator"]);
  188. db::bind(':id_type', $_input["id_type"]);
  189. db::bind(':tags', $tags);
  190. db::bind(':actif', $_input["actif"]);
  191. db::bind(':id', $_input["id"]);
  192. try {
  193. db::execute();
  194. alert::recSuccess("La modification a bien été prise en compte");
  195. } catch (Exception $ex) {
  196. alert::recError("Erreur lors de la modification de l'utilisateur");
  197. header("Location: /user-" . $_input["id"] . ".html");
  198. exit();
  199. }
  200. }
  201. public static function deleteUser(int $_id){
  202. db::query("UPDATE " . DB_T_USER . " SET deleted = 1 WHERE id = :id");
  203. db::bind(':id', $_id);
  204. try {
  205. db::execute();
  206. } catch (Exception $ex) {
  207. alert::recError("Erreur lors de la suppression");
  208. header("Location: /user-" . $_id .".html");
  209. exit();
  210. }
  211. }
  212. public static function restoreUser(int $_id){
  213. db::query("UPDATE " . DB_T_USER . " SET deleted = 0 WHERE id = :id");
  214. db::bind(':id', $_id);
  215. try {
  216. db::execute();
  217. } catch (Exception $ex) {
  218. alert::recError("Erreur lors de la restauration");
  219. header("Location: /user-" . $_id .".html");
  220. exit();
  221. }
  222. }
  223. }