PageRenderTime 34ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/main/user/user_password.php

http://github.com/FSB/Fire-Soft-Board-2
PHP | 343 lines | 196 code | 46 blank | 101 comment | 46 complexity | 86e38987096830315596236e44770d92 MD5 | raw file
  1. <?php
  2. /**
  3. * Fire-Soft-Board version 2
  4. *
  5. * @package FSB2
  6. * @author Genova <genova@fire-soft-board.com>
  7. * @version $Id$
  8. * @license http://opensource.org/licenses/gpl-2.0.php GNU GPL 2
  9. */
  10. /**
  11. * On affiche le module
  12. *
  13. * @var bool
  14. */
  15. $show_this_module = true;
  16. // Necessite le fichier de langue lg_forum_register.php pour le test du mot de passe
  17. Fsb::$session->load_lang('lg_forum_register');
  18. /**
  19. * Module d'utilisateur permettant au membre de modifier son login et son mot de passe
  20. * Notez qu'il faut connaitre obligatoirement le login et le mot de passe pour avoir le droit de
  21. * changer un des deux (ou meme les deux).
  22. */
  23. class Page_user_password extends Fsb_model
  24. {
  25. /**
  26. * Ancien pseudonyme
  27. *
  28. * @var string
  29. */
  30. public $old_login;
  31. /**
  32. * Ancien mot de passe
  33. *
  34. * @var string
  35. */
  36. public $old_password;
  37. /**
  38. * Nouveau Pseudonyme
  39. *
  40. * @var string
  41. */
  42. public $new_login;
  43. /**
  44. * Nouveau mot de passe
  45. *
  46. * @var string
  47. */
  48. public $new_password;
  49. /**
  50. * Confirmation du mot de passe
  51. *
  52. * @var string
  53. */
  54. public $new_password_confirm;
  55. /**
  56. * Nouvelle adresse Email
  57. *
  58. * @var string
  59. */
  60. public $new_email;
  61. /**
  62. * Donnée du mot de passe
  63. *
  64. * @var string
  65. */
  66. public $pwd_data;
  67. /**
  68. * Mise a jour de l'adresse Email ?
  69. *
  70. * @var bool
  71. */
  72. public $update_email = false;
  73. /**
  74. * Peut-yon changer l'adresse Email ?
  75. *
  76. * @var bool
  77. */
  78. public $can_update_email = false;
  79. /**
  80. * Erreurs
  81. *
  82. * @var array
  83. */
  84. public $errstr = array();
  85. /**
  86. * Constructeur
  87. */
  88. public function __construct()
  89. {
  90. $this->old_login = trim(Http::request('old_login', 'post'));
  91. $this->old_password = trim(Http::request('old_password', 'post'));
  92. $this->new_login = trim(Http::request('new_login', 'post'));
  93. $this->new_password = trim(Http::request('new_password', 'post'));
  94. $this->new_password_confirm = trim(Http::request('new_password_confirm', 'post'));
  95. $this->new_email = trim(Http::request('new_email', 'post'));
  96. if ((Fsb::$cfg->get('register_type') == 'confirm' || Fsb::$cfg->get('register_type') == 'admin' || Fsb::$cfg->get('register_type') == 'both') && Fsb::$session->auth() < MODOSUP)
  97. {
  98. $this->can_update_email = true;
  99. }
  100. if (Http::request('test_password', 'post'))
  101. {
  102. $this->test_password();
  103. }
  104. else if (Http::request('submit', 'post'))
  105. {
  106. $this->check_form();
  107. if (!count($this->errstr))
  108. {
  109. $this->submit_form();
  110. }
  111. }
  112. $this->password_form();
  113. }
  114. /**
  115. * Affiche le formulaire de modification de mot de passe
  116. */
  117. public function password_form()
  118. {
  119. if (count($this->errstr))
  120. {
  121. Fsb::$tpl->set_switch('error');
  122. }
  123. if ($this->can_update_email)
  124. {
  125. Fsb::$tpl->set_switch('email_explain');
  126. }
  127. Fsb::$tpl->set_file('user/user_password.html');
  128. Fsb::$tpl->set_vars(array(
  129. 'CONTENT' => Html::make_errstr($this->errstr),
  130. 'OLD_LOGIN' => $this->old_login,
  131. 'OLD_PASSWORD' => $this->old_password,
  132. 'NEW_LOGIN' => $this->new_login,
  133. 'NEW_PASSWORD' => $this->new_password,
  134. 'NEW_PASSWORD_CONFIRM' => $this->new_password_confirm,
  135. 'NEW_EMAIL' => ($this->new_email) ? $this->new_email : Fsb::$session->data['u_email'],
  136. ));
  137. }
  138. /**
  139. * Verifie les donnees envoyees par le formulaire
  140. */
  141. public function check_form()
  142. {
  143. $this->update_email = ($this->new_email && strtolower($this->new_email) != strtolower(Fsb::$session->data['u_email'])) ? true : false;
  144. // Il faut entrer son login et son mot de passe actuel
  145. if (!$this->old_login)
  146. {
  147. $this->errstr[] = Fsb::$session->lang('user_password_need_login');
  148. }
  149. if (!$this->old_password)
  150. {
  151. $this->errstr[] = Fsb::$session->lang('user_password_need_password');
  152. }
  153. // On verifie si la clef login / mot de passe est correct
  154. $sql = 'SELECT *
  155. FROM ' . SQL_PREFIX . 'users_password
  156. WHERE u_id = ' . Fsb::$session->id();
  157. $this->pwd_data = Fsb::$db->request($sql);
  158. if (strtolower($this->old_login) !== strtolower($this->pwd_data['u_login']) || Password::hash($this->old_password, $this->pwd_data['u_algorithm'], $this->pwd_data['u_use_salt']) !== $this->pwd_data['u_password'])
  159. {
  160. $this->errstr[] = Fsb::$session->lang('user_password_bad_login');
  161. }
  162. // On verifie si les deux nouveaux mots de passe sont les meme
  163. if (($this->new_password || $this->new_password_confirm) && $this->new_password !== $this->new_password_confirm)
  164. {
  165. $this->errstr[] = Fsb::$session->lang('user_password_dif');
  166. }
  167. // Validite de l'adresse Email
  168. if ($this->update_email && !User::email_valid($this->new_email))
  169. {
  170. $this->errstr[] = Fsb::$session->lang('user_email_format');
  171. }
  172. // Existance du login
  173. if ($this->new_login && strtolower($this->new_login) != strtolower($this->pwd_data['u_login']) && User::login_exists($this->new_login))
  174. {
  175. $this->errstr[] = Fsb::$session->lang('user_login_exists');
  176. }
  177. // Existance de l'adresse Email
  178. if ($this->update_email && User::email_exists($this->new_email))
  179. {
  180. $this->errstr[] = Fsb::$session->lang('user_email_exists');
  181. }
  182. }
  183. /**
  184. * Soumet les donnees envoyees par le formulaire
  185. */
  186. public function submit_form()
  187. {
  188. // Nouveau login
  189. $update_array = array();
  190. $update_pwd_array = array();
  191. if ($this->new_login)
  192. {
  193. $update_pwd_array['u_login'] = $this->new_login;
  194. }
  195. // Nouveau mot de passe
  196. if ($this->new_password && $this->new_password_confirm)
  197. {
  198. $update_pwd_array['u_password'] = Password::hash($this->new_password, $this->pwd_data['u_algorithm'], $this->pwd_data['u_use_salt']);
  199. }
  200. // Si une nouvelle adresse Email est entree, et que la configuration necessite une validation, on desactive le compte et on envoie
  201. // un Email avec un nouveau code de validation.
  202. $logout = false;
  203. if ($this->update_email && $this->can_update_email)
  204. {
  205. $confirm_hash = md5(rand(0, time()));
  206. $mail = new Notify_mail();
  207. $mail->AddAddress($this->new_email);
  208. $mail->Subject = Fsb::$session->lang('user_email_change');
  209. $mail->set_file(ROOT . 'lang/' . Fsb::$session->data['u_language'] . '/mail/update_email.txt');
  210. $mail->set_vars(array(
  211. 'FORUM_NAME' => Fsb::$cfg->get('forum_name'),
  212. 'NICKNAME' => Fsb::$session->data['u_nickname'],
  213. 'U_CONFIRM' => Fsb::$cfg->get('fsb_path') . '/index.' . PHPEXT . '?p=login&id=' . Fsb::$session->id() . '&confirm=' . urlencode($confirm_hash),
  214. 'U_FORUM' => Fsb::$cfg->get('fsb_path'),
  215. ));
  216. $result = $mail->Send();
  217. $mail->SmtpClose();
  218. // On ne fait le controle de validation que si l'Email a pu etre envoye
  219. if ($result)
  220. {
  221. $update_array['u_activated'] = false;
  222. $update_array['u_confirm_hash'] = $confirm_hash;
  223. $update_array['u_email'] = $this->new_email;
  224. $logout = true;
  225. Log::user(Fsb::$session->id(), 'update_email');
  226. }
  227. }
  228. else if ($this->update_email)
  229. {
  230. $update_array['u_email'] = $this->new_email;
  231. }
  232. // Mise a jour
  233. if ($update_array)
  234. {
  235. Fsb::$db->update('users', $update_array, 'WHERE u_id = ' . Fsb::$session->id());
  236. }
  237. if ($update_pwd_array)
  238. {
  239. // Regeneration de la clef d'auto connexion
  240. $prefix = '';
  241. foreach ($update_pwd_array AS $v)
  242. {
  243. $prefix .= $v;
  244. }
  245. $update_pwd_array['u_autologin_key'] = Password::generate_autologin_key($v . Fsb::$session->id());
  246. Fsb::$db->update('users_password', $update_pwd_array, 'WHERE u_id = ' . Fsb::$session->id());
  247. }
  248. Log::user(Fsb::$session->id(), 'update_password_info');
  249. // Deconnexion ?
  250. if ($logout)
  251. {
  252. Fsb::$session->logout();
  253. Http::redirect(ROOT . 'index.' . PHPEXT);
  254. }
  255. Display::message('user_profil_submit', ROOT . 'index.' . PHPEXT . '?p=profile&amp;module=password', 'forum_profil');
  256. }
  257. /**
  258. * Evalue la robustesse du mot de passe
  259. */
  260. public function test_password()
  261. {
  262. if (!$this->new_password)
  263. {
  264. return ;
  265. }
  266. $password = new Password();
  267. $result = $password->grade($this->new_password);
  268. $result_errstr = array();
  269. if ($result >= 3)
  270. {
  271. $result_str = Fsb::$session->lang('register_test_high');
  272. }
  273. else
  274. {
  275. // Mot de passe faible (ou moyennement faible), on evalue les besoins du mot de passe en fonction
  276. // des parametres calcule par la classe Password() (taille, caracteres speciaux, moyenne).
  277. $result_str = ($result <= 1) ? Fsb::$session->lang('register_test_low') : Fsb::$session->lang('register_test_middle');
  278. if ($password->grade_data['len'] < 3)
  279. {
  280. $result_errstr[] = Fsb::$session->lang('register_test_len');
  281. }
  282. if ($password->grade_data['char_type'] < 3)
  283. {
  284. $result_errstr[] = Fsb::$session->lang('register_test_type');
  285. }
  286. if ($password->grade_data['average'] < 3)
  287. {
  288. $result_errstr[] = Fsb::$session->lang('register_test_average');
  289. }
  290. }
  291. Fsb::$tpl->set_switch('test_password');
  292. Fsb::$tpl->set_vars(array(
  293. 'PASSWORD_RESULT' => $result_str . Html::make_errstr($result_errstr),
  294. ));
  295. }
  296. }
  297. /* EOF */