dolibarr 24.0.3
passwordforgotten.php
Go to the documentation of this file.
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 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
7 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
28define("NOLOGIN", 1); // This means this output page does not require to be logged.
29
30// Load Dolibarr environment
31require '../main.inc.php';
32require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
33require_once DOL_DOCUMENT_ROOT.'/core/lib/usergroups.lib.php';
34require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
35if (isModEnabled('ldap')) {
36 require_once DOL_DOCUMENT_ROOT.'/core/class/ldap.class.php';
37}
38
49// Load translation files required by page
50$langs->loadLangs(array('errors', 'users', 'companies', 'ldap', 'other'));
51
52// Security check
53if (getDolGlobalString('MAIN_SECURITY_DISABLEFORGETPASSLINK')) {
54 header("Location: ".DOL_URL_ROOT.'/');
55 exit;
56}
57
58$action = GETPOST('action', 'aZ09');
59$mode = $dolibarr_main_authentication;
60if (!$mode) {
61 $mode = 'http';
62}
63
64$username = GETPOST('username', 'alphanohtml');
65$passworduidhash = GETPOST('passworduidhash', 'alpha');
66$setnewpassword = GETPOST('setnewpassword', 'aZ09');
67
68$conf->entity = (GETPOSTINT('entity') ? GETPOSTINT('entity') : 1);
69
70// Instantiate hooks of thirdparty module only if not already define
71$hookmanager->initHooks(array('passwordforgottenpage'));
72
73
74if (GETPOST('dol_hide_leftmenu', 'alpha') || !empty($_SESSION['dol_hide_leftmenu'])) {
75 $conf->dol_hide_leftmenu = 1;
76}
77if (GETPOST('dol_hide_topmenu', 'alpha') || !empty($_SESSION['dol_hide_topmenu'])) {
78 $conf->dol_hide_topmenu = 1;
79}
80if (GETPOST('dol_optimize_smallscreen', 'alpha') || !empty($_SESSION['dol_optimize_smallscreen'])) {
81 $conf->dol_optimize_smallscreen = 1;
82}
83if (GETPOST('dol_no_mouse_hover', 'alpha') || !empty($_SESSION['dol_no_mouse_hover'])) {
84 $conf->dol_no_mouse_hover = 1;
85}
86if (GETPOST('dol_use_jmobile', 'alpha') || !empty($_SESSION['dol_use_jmobile'])) {
87 $conf->dol_use_jmobile = 1;
88}
89
90
91/*
92 * Actions
93 */
94
95$parameters = array('username' => $username);
96$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
97if ($reshook < 0) {
98 $message = $hookmanager->error;
99} else {
100 $message = '';
101}
102
103if (empty($reshook)) {
104 // Validate new password
105 if ($action == 'validatenewpassword' && $username && $passworduidhash) { // Test on permission not required here. Security is managed by $passworduihash
106 $edituser = new User($db);
107 $result = $edituser->fetch(0, $username, '', 0, $conf->entity);
108 if ($result < 0) {
109 $message = '<div class="error">'.dol_escape_htmltag($langs->trans("ErrorTechnicalError")).'</div>';
110 } else {
111 global $conf;
112
113 //print $edituser->pass_temp.'-'.$edituser->id.'-'.$conf->file->instance_unique_id.' '.$passworduidhash;
114 if ($edituser->pass_temp && dol_verifyHash($edituser->pass_temp.'-'.$edituser->id.'-'.$conf->file->instance_unique_id, $passworduidhash)) {
115 // Clear session
116 unset($_SESSION['dol_login']);
117 $_SESSION['dol_loginmesg'] = '<!-- warning -->'.$langs->transnoentitiesnoconv('NewPasswordValidated'); // Save message for the session page
118
119 $newpassword = $edituser->setPassword($user, $edituser->pass_temp, 0);
120 dol_syslog("passwordforgotten.php new password for user->id=".$edituser->id." validated in database");
121
122 header("Location: ".DOL_URL_ROOT.'/?username='.urlencode($edituser->login));
123 exit;
124 } else {
125 $langs->load("errors");
126 $message = '<div class="error">'.$langs->trans("ErrorFailedToValidatePasswordReset").'</div>';
127 }
128 }
129 }
130
131 // Action to set a temporary password and send email for reset
132 if ($action == 'buildnewpassword' && $username) { // Test on permission not required here. This action is done anonymously.
133 $sessionkey = 'dol_antispam_value';
134 $ok = (array_key_exists($sessionkey, $_SESSION) && (strtolower($_SESSION[$sessionkey]) === strtolower(GETPOST('code'))));
135 unset($_SESSION[$sessionkey]); // A code can be used only once. The page shows a new image, so a new code is set.
136
137 // Verify code
138 if (!$ok) {
139 dol_syslog('Bad value for code, password reset refused', LOG_NOTICE);
140
141 $message = '<div class="error">'.$langs->trans("ErrorBadValueForCode").'</div>';
142 } else {
143 $isanemail = preg_match('/@/', $username);
144
145 $edituser = new User($db);
146 $result = $edituser->fetch(0, $username, '', 1, $conf->entity);
147 if ($result == 0 && $isanemail) {
148 $result = $edituser->fetch(0, '', '', 1, $conf->entity, $username);
149 }
150
151 // If the user does not have the right to change his own password
152 if ($result > 0) {
153 $edituser->loadRights('user');
154 if (!$edituser->hasRight('user', 'self', 'password')) {
155 $result = 0;
156 $edituser->error = 'USERNOTALLOWEDTOCHANGEPASS';
157 }
158 }
159
160 // Set the message to show (must be the same if login/email exists or not to avoid to guess them.
161 $messagewarning = '<div class="warning paddingtopbottom'.(!getDolGlobalString('MAIN_LOGIN_BACKGROUND') ? '' : ' backgroundsemitransparent boxshadow').'">';
162 if (!$isanemail) {
163 $messagewarning .= $langs->trans("IfLoginExistPasswordRequestSent");
164 } else {
165 $messagewarning .= $langs->trans("IfEmailExistPasswordRequestSent");
166 }
167 $messagewarning .= '</div>';
168
169 if ($result <= 0 && ($edituser->error == 'USERNOTFOUND' || $edituser->error == 'USERNOTALLOWEDTOCHANGEPASS')) {
170 usleep(20000); // add delay to simulate setPassword() and send_password() actions delay (0.02s)
171 $message .= $messagewarning;
172 $username = '';
173 } else {
174 if (empty($edituser->email)) {
175 usleep(20000); // add delay to simulate setPassword() and send_password() actions delay (0.02s)
176 $message .= $messagewarning;
177 } else {
178 $newpassword = $edituser->setPassword($user, '', 1);
179 if (is_int($newpassword) && $newpassword < 0) {
180 // Technical failure
181 $message = '<div class="error">'.$langs->trans("ErrorFailedToChangePassword").'</div>';
182 } else {
183 // Success to set temporary password, send email
184 if ($edituser->send_password($user, $newpassword, 1) > 0) {
185 $message .= $messagewarning;
186 $username = '';
187 } else {
188 // Technical failure
189 $message .= '<div class="error">'.$edituser->error.'</div>';
190 }
191 }
192 }
193 }
194 }
195 }
196}
197
198
199/*
200 * View
201 */
202
203$dol_url_root = DOL_URL_ROOT;
204
205$appli = constant('DOL_APPLICATION_TITLE');
206$applicustom = getDolGlobalString('MAIN_APPLICATION_TITLE');
207if ($applicustom) {
208 $appli = (preg_match('/^\+/', $applicustom) ? $appli : '').$applicustom;
209} else {
210 $appli .= " ".DOL_VERSION;
211}
212
213// Title
214$title = $appli; // $title is used in .tpl file
215
216// Select templates dir
217$template_dir = '';
218if (!empty($conf->modules_parts['tpl'])) { // Using this feature slow down application
219 $dirtpls = array_merge($conf->modules_parts['tpl'], array('/core/tpl/'));
220 foreach ($dirtpls as $reldir) {
221 $tmp = dol_buildpath($reldir.'passwordforgotten.tpl.php');
222 if (file_exists($tmp)) {
223 $template_dir = preg_replace('/passwordforgotten\.tpl\.php$/', '', $tmp);
224 break;
225 }
226 }
227} elseif (file_exists(DOL_DOCUMENT_ROOT."/theme/".$conf->theme."/tpl/passwordforgotten.tpl.php")) {
228 $template_dir = DOL_DOCUMENT_ROOT."/theme/".$conf->theme."/tpl/";
229} else {
230 $template_dir = DOL_DOCUMENT_ROOT."/core/tpl/";
231}
232
233if (!$username) {
234 $focus_element = 'username';
235} else {
236 $focus_element = 'password';
237}
238
239// Show logo (search in order: small company logo, large company logo, theme logo, common logo)
240$width = 0;
241$rowspan = 2;
242$urllogo = DOL_URL_ROOT.'/theme/common/login_logo.png';
243if (!empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small)) {
244 $urllogo = DOL_URL_ROOT.'/viewimage.php?cache=1&amp;modulepart=mycompany&amp;file='.urlencode('logos/thumbs/'.$mysoc->logo_small);
245} elseif (!empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo)) {
246 $urllogo = DOL_URL_ROOT.'/viewimage.php?cache=1&amp;modulepart=mycompany&amp;file='.urlencode('logos/'.$mysoc->logo);
247 $width = 128;
248} elseif (is_readable(DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/img/dolibarr_logo.svg')) {
249 $urllogo = DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/dolibarr_logo.svg';
250} elseif (is_readable(DOL_DOCUMENT_ROOT.'/theme/dolibarr_logo.svg')) {
251 $urllogo = DOL_URL_ROOT.'/theme/dolibarr_logo.svg';
252}
253
254// Send password button enabled ?
255$disabled = 'disabled';
256if (preg_match('/dolibarr/i', $mode)) {
257 $disabled = '';
258}
259if (getDolGlobalString('MAIN_SECURITY_ENABLE_SENDPASSWORD')) {
260 $disabled = ''; // To force button enabled
261}
262
263// Security graphical code
264$captcha = '';
265if (!$disabled) {
266 $captcha = getDolGlobalString('MAIN_SECURITY_ENABLECAPTCHA_HANDLER', 'standard');
267}
268
269// Execute hook getPasswordForgottenPageOptions (for table)
270$parameters = array('entity' => GETPOSTINT('entity'));
271$hookmanager->executeHooks('getPasswordForgottenPageOptions', $parameters); // Note that $action and $object may have been modified by some hooks
272if (is_array($hookmanager->resArray) && !empty($hookmanager->resArray)) {
273 $morelogincontent = $hookmanager->resArray; // (deprecated) For compatibility
274} else {
275 $morelogincontent = $hookmanager->resPrint;
276}
277
278// Execute hook getPasswordForgottenPageExtraOptions (eg for js)
279$parameters = array('entity' => GETPOSTINT('entity'));
280$reshook = $hookmanager->executeHooks('getPasswordForgottenPageExtraOptions', $parameters); // Note that $action and $object may have been modified by some hooks.
281$moreloginextracontent = $hookmanager->resPrint;
282
283if (empty($setnewpassword)) {
284 include $template_dir.'passwordforgotten.tpl.php'; // To use native PHP
285} else {
286 include $template_dir.'passwordreset.tpl.php'; // To use native PHP
287}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to manage Dolibarr users.
global $mysoc
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_verifyHash($chain, $hash, $type='0')
Compute a hash and compare it to the given one For backward compatibility reasons,...