dolibarr  16.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  *
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 
26 define("NOLOGIN", 1); // This means this output page does not require to be logged.
27 
28 require '../main.inc.php';
29 require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/usergroups.lib.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
32 if (!empty($conf->ldap->enabled)) {
33  require_once DOL_DOCUMENT_ROOT.'/core/class/ldap.class.php';
34 }
35 
36 // Load translation files required by page
37 $langs->loadLangs(array('errors', 'users', 'companies', 'ldap', 'other'));
38 
39 // Security check
40 if (!empty($conf->global->MAIN_SECURITY_DISABLEFORGETPASSLINK)) {
41  header("Location: ".DOL_URL_ROOT.'/');
42  exit;
43 }
44 
45 $action = GETPOST('action', 'aZ09');
46 $mode = $dolibarr_main_authentication;
47 if (!$mode) {
48  $mode = 'http';
49 }
50 
51 $username = GETPOST('username', 'alphanohtml');
52 $passworduidhash = GETPOST('passworduidhash', 'alpha');
53 $conf->entity = (GETPOST('entity', 'int') ? GETPOST('entity', 'int') : 1);
54 
55 // Instantiate hooks of thirdparty module only if not already define
56 $hookmanager->initHooks(array('passwordforgottenpage'));
57 
58 
59 if (GETPOST('dol_hide_leftmenu', 'alpha') || !empty($_SESSION['dol_hide_leftmenu'])) {
60  $conf->dol_hide_leftmenu = 1;
61 }
62 if (GETPOST('dol_hide_topmenu', 'alpha') || !empty($_SESSION['dol_hide_topmenu'])) {
63  $conf->dol_hide_topmenu = 1;
64 }
65 if (GETPOST('dol_optimize_smallscreen', 'alpha') || !empty($_SESSION['dol_optimize_smallscreen'])) {
66  $conf->dol_optimize_smallscreen = 1;
67 }
68 if (GETPOST('dol_no_mouse_hover', 'alpha') || !empty($_SESSION['dol_no_mouse_hover'])) {
69  $conf->dol_no_mouse_hover = 1;
70 }
71 if (GETPOST('dol_use_jmobile', 'alpha') || !empty($_SESSION['dol_use_jmobile'])) {
72  $conf->dol_use_jmobile = 1;
73 }
74 
75 
80 $parameters = array('username' => $username);
81 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
82 if ($reshook < 0) {
83  $message = $hookmanager->error;
84 }
85 
86 if (empty($reshook)) {
87  // Validate new password
88  if ($action == 'validatenewpassword' && $username && $passworduidhash) {
89  $edituser = new User($db);
90  $result = $edituser->fetch('', $username, '', 0, $conf->entity);
91  if ($result < 0) {
92  $message = '<div class="error">'.dol_escape_htmltag($langs->trans("ErrorLoginDoesNotExists", $username)).'</div>';
93  } else {
94  global $dolibarr_main_instance_unique_id;
95 
96  //print $edituser->pass_temp.'-'.$edituser->id.'-'.$dolibarr_main_instance_unique_id.' '.$passworduidhash;
97  if ($edituser->pass_temp && dol_verifyHash($edituser->pass_temp.'-'.$edituser->id.'-'.$dolibarr_main_instance_unique_id, $passworduidhash)) {
98  // Clear session
99  unset($_SESSION['dol_login']);
100  $_SESSION['dol_loginmesg'] = $langs->transnoentitiesnoconv('NewPasswordValidated'); // Save message for the session page
101 
102  $newpassword = $edituser->setPassword($user, $edituser->pass_temp, 0);
103  dol_syslog("passwordforgotten.php new password for user->id=".$edituser->id." validated in database");
104 
105  header("Location: ".DOL_URL_ROOT.'/');
106  exit;
107  } else {
108  $langs->load("errors");
109  $message = '<div class="error">'.$langs->trans("ErrorFailedToValidatePasswordReset").'</div>';
110  }
111  }
112  }
113  // Action modif mot de passe
114  if ($action == 'buildnewpassword' && $username) {
115  $sessionkey = 'dol_antispam_value';
116  $ok = (array_key_exists($sessionkey, $_SESSION) === true && (strtolower($_SESSION[$sessionkey]) == strtolower(GETPOST('code'))));
117 
118  // Verify code
119  if (!$ok) {
120  $message = '<div class="error">'.$langs->trans("ErrorBadValueForCode").'</div>';
121  } else {
122  $isanemail = preg_match('/@/', $username);
123 
124  $edituser = new User($db);
125  $result = $edituser->fetch('', $username, '', 1, $conf->entity);
126  if ($result == 0 && $isanemail) {
127  $result = $edituser->fetch('', '', '', 1, $conf->entity, $username);
128  }
129 
130  if ($result <= 0 && $edituser->error == 'USERNOTFOUND') {
131  $message = '<div class="warning paddingtopbottom'.(empty($conf->global->MAIN_LOGIN_BACKGROUND) ? '' : ' backgroundsemitransparent boxshadow').'">';
132  if (!$isanemail) {
133  $message .= $langs->trans("IfLoginExistPasswordRequestSent");
134  } else {
135  $message .= $langs->trans("IfEmailExistPasswordRequestSent");
136  }
137  $message .= '</div>';
138  $username = '';
139  } else {
140  if (!$edituser->email) {
141  $message = '<div class="error">'.$langs->trans("ErrorLoginHasNoEmail").'</div>';
142  } else {
143  $newpassword = $edituser->setPassword($user, '', 1);
144  if ($newpassword < 0) {
145  // Failed
146  $message = '<div class="error">'.$langs->trans("ErrorFailedToChangePassword").'</div>';
147  } else {
148  // Success
149  if ($edituser->send_password($user, $newpassword, 1) > 0) {
150  $message = '<div class="warning paddingtopbottom'.(empty($conf->global->MAIN_LOGIN_BACKGROUND) ? '' : ' backgroundsemitransparent boxshadow').'">';
151  if (!$isanemail) {
152  $message .= $langs->trans("IfLoginExistPasswordRequestSent");
153  } else {
154  $message .= $langs->trans("IfEmailExistPasswordRequestSent");
155  }
156  //$message .= $langs->trans("PasswordChangeRequestSent", $edituser->login, dolObfuscateEmail($edituser->email));
157  $message .= '</div>';
158  $username = '';
159  } else {
160  $message .= '<div class="error">'.$edituser->error.'</div>';
161  }
162  }
163  }
164  }
165  }
166  }
167 }
168 
169 
174 $dol_url_root = DOL_URL_ROOT;
175 
176 // Title
177 $title = 'Dolibarr '.DOL_VERSION;
178 if (!empty($conf->global->MAIN_APPLICATION_TITLE)) {
179  $title = $conf->global->MAIN_APPLICATION_TITLE;
180 }
181 
182 // Select templates
183 if (file_exists(DOL_DOCUMENT_ROOT."/theme/".$conf->theme."/tpl/passwordforgotten.tpl.php")) {
184  $template_dir = DOL_DOCUMENT_ROOT."/theme/".$conf->theme."/tpl/";
185 } else {
186  $template_dir = DOL_DOCUMENT_ROOT."/core/tpl/";
187 }
188 
189 if (!$username) {
190  $focus_element = 'username';
191 } else {
192  $focus_element = 'password';
193 }
194 
195 // Send password button enabled ?
196 $disabled = 'disabled';
197 if (preg_match('/dolibarr/i', $mode)) {
198  $disabled = '';
199 }
200 if (!empty($conf->global->MAIN_SECURITY_ENABLE_SENDPASSWORD)) {
201  $disabled = ''; // To force button enabled
202 }
203 
204 // Show logo (search in order: small company logo, large company logo, theme logo, common logo)
205 $width = 0;
206 $rowspan = 2;
207 $urllogo = DOL_URL_ROOT.'/theme/common/login_logo.png';
208 if (!empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small)) {
209  $urllogo = DOL_URL_ROOT.'/viewimage.php?cache=1&amp;modulepart=mycompany&amp;file='.urlencode('logos/thumbs/'.$mysoc->logo_small);
210 } elseif (!empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo)) {
211  $urllogo = DOL_URL_ROOT.'/viewimage.php?cache=1&amp;modulepart=mycompany&amp;file='.urlencode('logos/'.$mysoc->logo);
212  $width = 128;
213 } elseif (is_readable(DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/img/dolibarr_logo.svg')) {
214  $urllogo = DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/dolibarr_logo.svg';
215 } elseif (is_readable(DOL_DOCUMENT_ROOT.'/theme/dolibarr_logo.svg')) {
216  $urllogo = DOL_URL_ROOT.'/theme/dolibarr_logo.svg';
217 }
218 
219 // Security graphical code
220 if (function_exists("imagecreatefrompng") && !$disabled) {
221  $captcha = 1;
222  $captcha_refresh = img_picto($langs->trans("Refresh"), 'refresh', 'id="captcha_refresh_img"');
223 }
224 
225 // Execute hook getPasswordForgottenPageOptions (for table)
226 $parameters = array('entity' => GETPOST('entity', 'int'));
227 $hookmanager->executeHooks('getPasswordForgottenPageOptions', $parameters); // Note that $action and $object may have been modified by some hooks
228 if (is_array($hookmanager->resArray) && !empty($hookmanager->resArray)) {
229  $morelogincontent = $hookmanager->resArray; // (deprecated) For compatibility
230 } else {
231  $morelogincontent = $hookmanager->resPrint;
232 }
233 
234 // Execute hook getPasswordForgottenPageExtraOptions (eg for js)
235 $parameters = array('entity' => GETPOST('entity', 'int'));
236 $reshook = $hookmanager->executeHooks('getPasswordForgottenPageExtraOptions', $parameters); // Note that $action and $object may have been modified by some hooks.
237 $moreloginextracontent = $hookmanager->resPrint;
238 
239 include $template_dir.'passwordforgotten.tpl.php'; // To use native PHP
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_verifyHash
dol_verifyHash($chain, $hash, $type='0')
Compute a hash and compare it to the given one For backward compatibility reasons,...
Definition: security.lib.php:151
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
User
Class to manage Dolibarr users.
Definition: user.class.php:44
$dol_url_root
if($reshook< 0) if(empty($reshook)) $dol_url_root
View.
Definition: passwordforgotten.php:174
$parameters
if(GETPOST('dol_hide_leftmenu', 'alpha')||!empty($_SESSION['dol_hide_leftmenu'])) if(GETPOST('dol_hide_topmenu', 'alpha')||!empty($_SESSION['dol_hide_topmenu'])) if(GETPOST('dol_optimize_smallscreen', 'alpha')||!empty($_SESSION['dol_optimize_smallscreen'])) if(GETPOST('dol_no_mouse_hover', 'alpha')||!empty($_SESSION['dol_no_mouse_hover'])) if(GETPOST('dol_use_jmobile', 'alpha')||!empty($_SESSION['dol_use_jmobile'])) $parameters
Actions.
Definition: passwordforgotten.php:80