passwordforgotten.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 <https://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. // Load Dolibarr environment
  26. require '../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/usergroups.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  30. if (isModEnabled('ldap')) {
  31. require_once DOL_DOCUMENT_ROOT.'/core/class/ldap.class.php';
  32. }
  33. // Load translation files required by page
  34. $langs->loadLangs(array('errors', 'users', 'companies', 'ldap', 'other'));
  35. // Security check
  36. if (getDolGlobalString('MAIN_SECURITY_DISABLEFORGETPASSLINK')) {
  37. header("Location: ".DOL_URL_ROOT.'/');
  38. exit;
  39. }
  40. $action = GETPOST('action', 'aZ09');
  41. $mode = $dolibarr_main_authentication;
  42. if (!$mode) {
  43. $mode = 'http';
  44. }
  45. $username = GETPOST('username', 'alphanohtml');
  46. $passworduidhash = GETPOST('passworduidhash', 'alpha');
  47. $setnewpassword = GETPOST('setnewpassword', 'aZ09');
  48. $conf->entity = (GETPOST('entity', 'int') ? GETPOST('entity', 'int') : 1);
  49. // Instantiate hooks of thirdparty module only if not already define
  50. $hookmanager->initHooks(array('passwordforgottenpage'));
  51. if (GETPOST('dol_hide_leftmenu', 'alpha') || !empty($_SESSION['dol_hide_leftmenu'])) {
  52. $conf->dol_hide_leftmenu = 1;
  53. }
  54. if (GETPOST('dol_hide_topmenu', 'alpha') || !empty($_SESSION['dol_hide_topmenu'])) {
  55. $conf->dol_hide_topmenu = 1;
  56. }
  57. if (GETPOST('dol_optimize_smallscreen', 'alpha') || !empty($_SESSION['dol_optimize_smallscreen'])) {
  58. $conf->dol_optimize_smallscreen = 1;
  59. }
  60. if (GETPOST('dol_no_mouse_hover', 'alpha') || !empty($_SESSION['dol_no_mouse_hover'])) {
  61. $conf->dol_no_mouse_hover = 1;
  62. }
  63. if (GETPOST('dol_use_jmobile', 'alpha') || !empty($_SESSION['dol_use_jmobile'])) {
  64. $conf->dol_use_jmobile = 1;
  65. }
  66. /**
  67. * Actions
  68. */
  69. $parameters = array('username' => $username);
  70. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  71. if ($reshook < 0) {
  72. $message = $hookmanager->error;
  73. } else $message = '';
  74. if (empty($reshook)) {
  75. // Validate new password
  76. if ($action == 'validatenewpassword' && $username && $passworduidhash) {
  77. $edituser = new User($db);
  78. $result = $edituser->fetch('', $username, '', 0, $conf->entity);
  79. if ($result < 0) {
  80. $message = '<div class="error">'.dol_escape_htmltag($langs->trans("ErrorTechnicalError")).'</div>';
  81. } else {
  82. global $conf;
  83. //print $edituser->pass_temp.'-'.$edituser->id.'-'.$conf->file->instance_unique_id.' '.$passworduidhash;
  84. if ($edituser->pass_temp && dol_verifyHash($edituser->pass_temp.'-'.$edituser->id.'-'.$conf->file->instance_unique_id, $passworduidhash)) {
  85. // Clear session
  86. unset($_SESSION['dol_login']);
  87. $_SESSION['dol_loginmesg'] = '<!-- warning -->'.$langs->transnoentitiesnoconv('NewPasswordValidated'); // Save message for the session page
  88. $newpassword = $edituser->setPassword($user, $edituser->pass_temp, 0);
  89. dol_syslog("passwordforgotten.php new password for user->id=".$edituser->id." validated in database");
  90. header("Location: ".DOL_URL_ROOT.'/');
  91. exit;
  92. } else {
  93. $langs->load("errors");
  94. $message = '<div class="error">'.$langs->trans("ErrorFailedToValidatePasswordReset").'</div>';
  95. }
  96. }
  97. }
  98. // Action to set a temporary password and send email for reset
  99. if ($action == 'buildnewpassword' && $username) {
  100. $sessionkey = 'dol_antispam_value';
  101. $ok = (array_key_exists($sessionkey, $_SESSION) === true && (strtolower($_SESSION[$sessionkey]) == strtolower(GETPOST('code'))));
  102. // Verify code
  103. if (!$ok) {
  104. $message = '<div class="error">'.$langs->trans("ErrorBadValueForCode").'</div>';
  105. } else {
  106. $isanemail = preg_match('/@/', $username);
  107. $edituser = new User($db);
  108. $result = $edituser->fetch('', $username, '', 1, $conf->entity);
  109. if ($result == 0 && $isanemail) {
  110. $result = $edituser->fetch('', '', '', 1, $conf->entity, $username);
  111. }
  112. // Set the message to show (must be the same if login/email exists or not
  113. // to avoid to guess them.
  114. $messagewarning = '<div class="warning paddingtopbottom'.(!getDolGlobalString('MAIN_LOGIN_BACKGROUND') ? '' : ' backgroundsemitransparent boxshadow').'">';
  115. if (!$isanemail) {
  116. $messagewarning .= $langs->trans("IfLoginExistPasswordRequestSent");
  117. } else {
  118. $messagewarning .= $langs->trans("IfEmailExistPasswordRequestSent");
  119. }
  120. $messagewarning .= '</div>';
  121. if ($result <= 0 && $edituser->error == 'USERNOTFOUND') {
  122. usleep(20000); // add delay to simulate setPassword() and send_password() actions delay (0.02s)
  123. $message .= $messagewarning;
  124. $username = '';
  125. } else {
  126. if (empty($edituser->email)) {
  127. usleep(20000); // add delay to simulate setPassword() and send_password() actions delay (0.02s)
  128. $message .= $messagewarning;
  129. } else {
  130. $newpassword = $edituser->setPassword($user, '', 1);
  131. if (is_int($newpassword) && $newpassword < 0) {
  132. // Technical failure
  133. $message = '<div class="error">'.$langs->trans("ErrorFailedToChangePassword").'</div>';
  134. } else {
  135. // Success
  136. if ($edituser->send_password($user, $newpassword, 1) > 0) {
  137. $message .= $messagewarning;
  138. $username = '';
  139. } else {
  140. // Technical failure
  141. $message .= '<div class="error">'.$edituser->error.'</div>';
  142. }
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }
  149. /**
  150. * View
  151. */
  152. $dol_url_root = DOL_URL_ROOT;
  153. // Title
  154. $title = 'Dolibarr '.DOL_VERSION;
  155. if (getDolGlobalString('MAIN_APPLICATION_TITLE')) {
  156. $title = $conf->global->MAIN_APPLICATION_TITLE;
  157. }
  158. // Select templates
  159. if (file_exists(DOL_DOCUMENT_ROOT."/theme/".$conf->theme."/tpl/passwordforgotten.tpl.php")) {
  160. $template_dir = DOL_DOCUMENT_ROOT."/theme/".$conf->theme."/tpl/";
  161. } else {
  162. $template_dir = DOL_DOCUMENT_ROOT."/core/tpl/";
  163. }
  164. if (!$username) {
  165. $focus_element = 'username';
  166. } else {
  167. $focus_element = 'password';
  168. }
  169. // Send password button enabled ?
  170. $disabled = 'disabled';
  171. if (preg_match('/dolibarr/i', $mode)) {
  172. $disabled = '';
  173. }
  174. if (getDolGlobalString('MAIN_SECURITY_ENABLE_SENDPASSWORD')) {
  175. $disabled = ''; // To force button enabled
  176. }
  177. // Show logo (search in order: small company logo, large company logo, theme logo, common logo)
  178. $width = 0;
  179. $rowspan = 2;
  180. $urllogo = DOL_URL_ROOT.'/theme/common/login_logo.png';
  181. if (!empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small)) {
  182. $urllogo = DOL_URL_ROOT.'/viewimage.php?cache=1&amp;modulepart=mycompany&amp;file='.urlencode('logos/thumbs/'.$mysoc->logo_small);
  183. } elseif (!empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo)) {
  184. $urllogo = DOL_URL_ROOT.'/viewimage.php?cache=1&amp;modulepart=mycompany&amp;file='.urlencode('logos/'.$mysoc->logo);
  185. $width = 128;
  186. } elseif (is_readable(DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/img/dolibarr_logo.svg')) {
  187. $urllogo = DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/dolibarr_logo.svg';
  188. } elseif (is_readable(DOL_DOCUMENT_ROOT.'/theme/dolibarr_logo.svg')) {
  189. $urllogo = DOL_URL_ROOT.'/theme/dolibarr_logo.svg';
  190. }
  191. // Security graphical code
  192. if (function_exists("imagecreatefrompng") && !$disabled) {
  193. $captcha = 1;
  194. $captcha_refresh = img_picto($langs->trans("Refresh"), 'refresh', 'id="captcha_refresh_img"');
  195. }
  196. // Execute hook getPasswordForgottenPageOptions (for table)
  197. $parameters = array('entity' => GETPOST('entity', 'int'));
  198. $hookmanager->executeHooks('getPasswordForgottenPageOptions', $parameters); // Note that $action and $object may have been modified by some hooks
  199. if (is_array($hookmanager->resArray) && !empty($hookmanager->resArray)) {
  200. $morelogincontent = $hookmanager->resArray; // (deprecated) For compatibility
  201. } else {
  202. $morelogincontent = $hookmanager->resPrint;
  203. }
  204. // Execute hook getPasswordForgottenPageExtraOptions (eg for js)
  205. $parameters = array('entity' => GETPOST('entity', 'int'));
  206. $reshook = $hookmanager->executeHooks('getPasswordForgottenPageExtraOptions', $parameters); // Note that $action and $object may have been modified by some hooks.
  207. $moreloginextracontent = $hookmanager->resPrint;
  208. if (empty($setnewpassword)) {
  209. include $template_dir.'passwordforgotten.tpl.php'; // To use native PHP
  210. } else {
  211. include $template_dir.'passwordreset.tpl.php'; // To use native PHP
  212. }