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