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