passwordforgotten.php 9.1 KB

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