action.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. //Récuperation d'une liste d'utilisateurs en attente de validation
  3. Action::register('subscribe_search',function(&$response){
  4. global $myUser,$_;
  5. User::check_access('subscribe','read');
  6. });
  7. //Ajout ou modification d'élément
  8. Action::register('subscribe_save',function(&$response){
  9. global $myUser,$_,$conf;
  10. Plugin::callHook('subscribe_validate',array());
  11. if(empty($_['firstname'])) throw new Exception('Le champ "Prénom" est obligatoire');
  12. if(empty($_['name'])) throw new Exception('Le champ "Nom" est obligatoire');
  13. $_['mail'] = mb_strtolower(trim($_['mail']));
  14. if(empty($_['mail'])) throw new Exception('Le champ "Email" est obligatoire');
  15. if(!check_mail($_['mail'])) throw new Exception('Le format du champ "Email" est invalide');
  16. if(empty($_['mail_confirm']) || $_['mail'] != $_['mail_confirm']) throw new Exception('Champs "Email" et "Confirmation email" non similaires');
  17. //Récupération du domaine
  18. preg_match("/.*@(.*)/i", $_['mail'], $matches);
  19. if(!empty($conf->get('subscribe_enable_blacklist'))) {
  20. $blacklist = array();
  21. foreach(Dictionary::bySlug('subscribe_domain_blacklist', true) as $domain)
  22. $blacklist[] = trim($domain->label);
  23. if(!isset($matches[1]) || in_array($matches[1], $blacklist)) throw new Exception("Le domaine utilisé pour l'adresse mail n'est pas autorisé");
  24. }
  25. if(!empty($conf->get('subscribe_enable_whitelist'))) {
  26. $whitelist = array();
  27. foreach(Dictionary::bySlug('subscribe_domain_whitelist', true) as $domain)
  28. $whitelist[] = trim($domain->label);
  29. if(!isset($matches[1]) || !in_array($matches[1], $whitelist)) throw new Exception("Le domaine utilisé pour l'adresse mail n'est pas autorisé");
  30. }
  31. if(empty($_['password_initialisation'])) throw new Exception("Mot de passe obligatoire");
  32. if(!empty($conf->get('password_forbidden_char')) && preg_match('|['.preg_quote(htmlspecialchars_decode($conf->get('password_forbidden_char'))).']|i', htmlspecialchars_decode($_['password_initialisation']), $match))
  33. throw new Exception("Caractère ".$match[0]." interdit dans le mot de passe");
  34. $_['password_initialisation'] = trim($_['password_initialisation']);
  35. $_['password_confirm'] = trim($_['password_confirm']);
  36. if(empty($_['password_confirm']) || $_['password_initialisation'] != $_['password_confirm']) throw new Exception("Mot de passe et confirmation non similaires");
  37. $passwordErrors = User::check_password_format(html_entity_decode($_['password_initialisation']));
  38. if(count($passwordErrors)!=0) throw new Exception("Le format de mot de passe ne respecte pas les conditions suivantes : <br>".implode("<br>",$passwordErrors));
  39. if($conf->get('subscribe_enable_captcha')){
  40. //Vérification du code captcha
  41. if(!isset($_['captcha']) || (empty($_['captcha']) && $_['captcha'] != 0)) throw new Exception('Veuillez remplir le champ anti-robots');
  42. subscribe_captcha($_['captcha']);
  43. }
  44. if(empty($conf->get('subscribe_firm'))) throw new Exception("Module non configuré (établissement), merci de contacter un administrateur");
  45. if(empty($conf->get('subscribe_rank')) && empty($conf->get('connected_default_rank'))) throw new Exception("Module non configuré (rang), merci de contacter un administrateur");
  46. $user = new User;
  47. $user->firstname = ucfirst(mb_strtolower($_['firstname']));
  48. $user->name = mb_strtoupper($_['name']);
  49. $user->login = $_['mail'];
  50. if($conf->get('subscribe_short_login')) $user->login = preg_replace('/^([^@]+)@.*\.[a-z]{2,6}/im', '$1', $user->login);
  51. $user->mail = $_['mail'];
  52. if($_['password_initialisation']==$user->mail) throw new Exception("Le mot de passe ne peut pas être identique à l'adresse mail");
  53. if($_['password_initialisation']==$user->login) throw new Exception("Le mot de passe ne peut pas être identique à l'identifiant");
  54. foreach(User::getAll(array('right'=>false, 'force'=>true)) as $existingUser){
  55. if($existingUser->mail == trim($user->mail)) throw new Exception("Un utilisateur existe déjà avec cette adresse email, ".(!empty($conf->get('password_allow_lost'))?"veuillez utiliser le système de mot de passe oublié":"veuillez contacter un administrateur"));
  56. if($existingUser->login == $user->login) throw new Exception("Un utilisateur existe déjà avec cet identifiant, ".(!empty($conf->get('password_allow_lost'))?"veuillez utiliser le système de mot de passe oublié":"veuillez contacter un administrateur"));
  57. }
  58. $user->state = $conf->get('subscribe_disable_mail') ? User::ACTIVE : 'registered';
  59. $user->password = User::password_encrypt($_['password_initialisation']);
  60. $user->preference('passwordTime',time());
  61. $token = base64_encode(sha1($user->login.mt_rand(0,1000).time()));
  62. $user->token = $token;
  63. $user->save();
  64. //clear du captcha utilisé
  65. subscribe_captcha('off');
  66. //validation par mail si non désactivé
  67. if(!$conf->get('subscribe_disable_mail')){
  68. $expDays = intval($conf->get('subscribe_mail_expire'));
  69. $parameters = array('token' => $user->token,'step' => 1,);
  70. $link = ROOT_URL.'/index.php?module=subscribe&page=validation&token='.base64_encode(json_encode($parameters));
  71. $mail = new Mail();
  72. $mail->title = "Inscription sur ".ROOT_URL;
  73. $mail->expeditor = !empty($conf->get('subscribe_reply_mail')) ? $conf->get('subscribe_reply_mail') : "no-reply@core.fr";
  74. $mail->reply = !empty($conf->get('subscribe_reply_mail')) ? $conf->get('subscribe_reply_mail') : "no-reply@core.fr";
  75. $mail->template(__DIR__.SLASH.'mail.subscription.html',array(
  76. 'link' => $link,
  77. 'url' => ROOT_URL,
  78. 'name' => $user->name,
  79. 'firstname' => $user->firstname,
  80. 'accountExpiration' => !empty($conf->get('subscribe_registered_expire')) ? intval($conf->get('subscribe_registered_expire')) : '',
  81. 'expDays' => $expDays,
  82. 'expDate' => !empty($expDays) ? date("d/m/Y à H:i", strtotime($expDays." days")) : ''
  83. ),true);
  84. $mail->recipients['to'][] = $user->mail;
  85. $mail->send();
  86. } else {
  87. $response['login'] = $user->login;
  88. }
  89. if(isset($_SESSION['users_rights'])) unset($_SESSION['users_rights']);
  90. if(isset($_SESSION['users_norights'])) unset($_SESSION['users_norights']);
  91. Log::put("Inscription de l'utilisateur ".$user->toText(),'Utilisateur');
  92. Plugin::callHook('subscribe_save',array(&$fields));
  93. });
  94. //Édition d'utilisateur en attente de validation
  95. Action::register('subscribe_edit',function(&$response){
  96. global $myUser,$_;
  97. User::check_access('subscribe','delete');
  98. });
  99. //Suppression d'utilisateur en attente de validation
  100. Action::register('subscribe_delete',function(&$response){
  101. global $myUser,$_;
  102. User::check_access('subscribe','delete');
  103. });
  104. //Sauvegarde des configurations de subscribe
  105. Action::register('subscribe_setting_save',function(&$response){
  106. global $myUser,$_,$conf;
  107. User::check_access('subscribe','configure');
  108. foreach(Configuration::setting('subscribe') as $key=>$value){
  109. if(!is_array($value)) continue;
  110. $allowed[] = $key;
  111. }
  112. foreach ($_['fields'] as $key => $value)
  113. if(in_array($key, $allowed)){
  114. if($key == 'subscribe_mail_expire' && (!is_numeric($value) || $value<0)) $value = 0;
  115. $conf->put($key,$value);
  116. }
  117. });
  118. //Récupération card des règles de mot de passe
  119. Action::register('subscribe_password_card',function(&$response){
  120. global $_,$conf;
  121. $selectedFormats = json_decode($conf->get('password_format'),true);
  122. $passwordInput = isset($_['password']) ? trim($_['password']) : '';
  123. $formats = array();
  124. foreach (User::password_formats() as $format)
  125. $formats[$format['pattern']] = $format;
  126. ob_start();
  127. require_once(__DIR__.SLASH.'card.subscribe.password.php');
  128. $stream = ob_get_clean();
  129. $response['content'] = $stream;
  130. });