passwordforgotten.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /* Copyright (C) 2007-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2008-2012 Regis Houssin <regis.houssin@inodbox.com>
  4. * Copyright (C) 2008-2011 Juanjo Menent <jmenent@2byte.es>
  5. * Copyright (C) 2014 Teddy Andreotti <125155@supinfo.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/user/passwordforgotten.php
  22. * \brief Page to ask a new password
  23. */
  24. define("NOLOGIN",1); // This means this output page does not require to be logged.
  25. require '../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/usergroups.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  29. if (! empty($conf->ldap->enabled)) require_once DOL_DOCUMENT_ROOT.'/core/class/ldap.class.php';
  30. // Load translation files required by page
  31. $langs->loadLangs(array('errors', 'users', 'companies', 'ldap', 'other'));
  32. // Security check
  33. if (! empty($conf->global->MAIN_SECURITY_DISABLEFORGETPASSLINK))
  34. {
  35. header("Location: ".DOL_URL_ROOT.'/');
  36. exit;
  37. }
  38. $action=GETPOST('action', 'alpha');
  39. $mode=$dolibarr_main_authentication;
  40. if (! $mode) $mode='http';
  41. $username = GETPOST('username','alpha');
  42. $passwordhash = GETPOST('passwordhash','alpha');
  43. $conf->entity = (GETPOST('entity','int') ? GETPOST('entity','int') : 1);
  44. // Instantiate hooks of thirdparty module only if not already define
  45. $hookmanager->initHooks(array('passwordforgottenpage'));
  46. if (GETPOST('dol_hide_leftmenu','alpha') || ! empty($_SESSION['dol_hide_leftmenu'])) $conf->dol_hide_leftmenu=1;
  47. if (GETPOST('dol_hide_topmenu','alpha') || ! empty($_SESSION['dol_hide_topmenu'])) $conf->dol_hide_topmenu=1;
  48. if (GETPOST('dol_optimize_smallscreen','alpha') || ! empty($_SESSION['dol_optimize_smallscreen'])) $conf->dol_optimize_smallscreen=1;
  49. if (GETPOST('dol_no_mouse_hover','alpha') || ! empty($_SESSION['dol_no_mouse_hover'])) $conf->dol_no_mouse_hover=1;
  50. if (GETPOST('dol_use_jmobile','alpha') || ! empty($_SESSION['dol_use_jmobile'])) $conf->dol_use_jmobile=1;
  51. /**
  52. * Actions
  53. */
  54. // Validate new password
  55. if ($action == 'validatenewpassword' && $username && $passwordhash)
  56. {
  57. $edituser = new User($db);
  58. $result=$edituser->fetch('',$_GET["username"]);
  59. if ($result < 0)
  60. {
  61. $message = '<div class="error">'.$langs->trans("ErrorLoginDoesNotExists",$username).'</div>';
  62. }
  63. else
  64. {
  65. if (dol_verifyHash($edituser->pass_temp, $passwordhash))
  66. {
  67. $newpassword=$edituser->setPassword($user,$edituser->pass_temp,0);
  68. dol_syslog("passwordforgotten.php new password for user->id=".$edituser->id." validated in database");
  69. header("Location: ".DOL_URL_ROOT.'/');
  70. exit;
  71. }
  72. else
  73. {
  74. $langs->load("errors");
  75. $message = '<div class="error">'.$langs->trans("ErrorFailedToValidatePasswordReset").'</div>';
  76. }
  77. }
  78. }
  79. // Action modif mot de passe
  80. if ($action == 'buildnewpassword' && $username)
  81. {
  82. $sessionkey = 'dol_antispam_value';
  83. $ok=(array_key_exists($sessionkey, $_SESSION) === true && (strtolower($_SESSION[$sessionkey]) == strtolower($_POST['code'])));
  84. // Verify code
  85. if (! $ok)
  86. {
  87. $message = '<div class="error">'.$langs->trans("ErrorBadValueForCode").'</div>';
  88. }
  89. else
  90. {
  91. $edituser = new User($db);
  92. $result=$edituser->fetch('',$username,'',1);
  93. if ($result <= 0 && $edituser->error == 'USERNOTFOUND')
  94. {
  95. $message = '<div class="error">'.$langs->trans("ErrorLoginDoesNotExists",$username).'</div>';
  96. $username='';
  97. }
  98. else
  99. {
  100. if (! $edituser->email)
  101. {
  102. $message = '<div class="error">'.$langs->trans("ErrorLoginHasNoEmail").'</div>';
  103. }
  104. else
  105. {
  106. $newpassword=$edituser->setPassword($user,'',1);
  107. if ($newpassword < 0)
  108. {
  109. // Failed
  110. $message = '<div class="error">'.$langs->trans("ErrorFailedToChangePassword").'</div>';
  111. }
  112. else
  113. {
  114. // Success
  115. if ($edituser->send_password($user,$newpassword,1) > 0)
  116. {
  117. $message = '<div class="ok">'.$langs->trans("PasswordChangeRequestSent",$edituser->login,dolObfuscateEmail($edituser->email)).'</div>';
  118. $username='';
  119. }
  120. else
  121. {
  122. $message.= '<div class="error">'.$edituser->error.'</div>';
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }
  129. /**
  130. * View
  131. */
  132. $dol_url_root = DOL_URL_ROOT;
  133. // Title
  134. $title='Dolibarr '.DOL_VERSION;
  135. if (! empty($conf->global->MAIN_APPLICATION_TITLE)) $title=$conf->global->MAIN_APPLICATION_TITLE;
  136. // Select templates
  137. if (file_exists(DOL_DOCUMENT_ROOT."/theme/".$conf->theme."/tpl/passwordforgotten.tpl.php"))
  138. {
  139. $template_dir = DOL_DOCUMENT_ROOT."/theme/".$conf->theme."/tpl/";
  140. }
  141. else
  142. {
  143. $template_dir = DOL_DOCUMENT_ROOT."/core/tpl/";
  144. }
  145. if (! $username) $focus_element = 'username';
  146. else $focus_element = 'password';
  147. // Send password button enabled ?
  148. $disabled='disabled';
  149. if (preg_match('/dolibarr/i',$mode)) $disabled='';
  150. if (! empty($conf->global->MAIN_SECURITY_ENABLE_SENDPASSWORD)) $disabled=''; // To force button enabled
  151. // Show logo (search in order: small company logo, large company logo, theme logo, common logo)
  152. $width=0;
  153. $rowspan=2;
  154. $urllogo=DOL_URL_ROOT.'/theme/login_logo.png';
  155. if (! empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small))
  156. {
  157. $urllogo=DOL_URL_ROOT.'/viewimage.php?cache=1&amp;modulepart=mycompany&amp;file='.urlencode('logos/thumbs/'.$mysoc->logo_small);
  158. }
  159. elseif (! empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo))
  160. {
  161. $urllogo=DOL_URL_ROOT.'/viewimage.php?cache=1&amp;modulepart=mycompany&amp;file='.urlencode('logos/'.$mysoc->logo);
  162. $width=128;
  163. }
  164. elseif (is_readable(DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/img/dolibarr_logo.png'))
  165. {
  166. $urllogo=DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/dolibarr_logo.png';
  167. }
  168. elseif (is_readable(DOL_DOCUMENT_ROOT.'/theme/dolibarr_logo.png'))
  169. {
  170. $urllogo=DOL_URL_ROOT.'/theme/dolibarr_logo.png';
  171. }
  172. // Security graphical code
  173. if (function_exists("imagecreatefrompng") && ! $disabled)
  174. {
  175. $captcha = 1;
  176. $captcha_refresh = img_picto($langs->trans("Refresh"),'refresh','id="captcha_refresh_img"');
  177. }
  178. // Execute hook getPasswordForgottenPageOptions (for table)
  179. $parameters=array('entity' => GETPOST('entity','int'));
  180. $hookmanager->executeHooks('getPasswordForgottenPageOptions',$parameters); // Note that $action and $object may have been modified by some hooks
  181. if (is_array($hookmanager->resArray) && ! empty($hookmanager->resArray)) {
  182. $morelogincontent = $hookmanager->resArray; // (deprecated) For compatibility
  183. } else {
  184. $morelogincontent = $hookmanager->resPrint;
  185. }
  186. // Execute hook getPasswordForgottenPageExtraOptions (eg for js)
  187. $parameters=array('entity' => GETPOST('entity','int'));
  188. $reshook = $hookmanager->executeHooks('getPasswordForgottenPageExtraOptions',$parameters); // Note that $action and $object may have been modified by some hooks.
  189. $moreloginextracontent = $hookmanager->resPrint;
  190. include $template_dir.'passwordforgotten.tpl.php'; // To use native PHP