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