dolibarr/htdocs/user/passwordforgotten.php

240 lines
8.9 KiB
PHP
Raw Normal View History

<?php
2012-09-02 09:27:06 +02:00
/* Copyright (C) 2007-2011 Laurent Destailleur <eldy@users.sourceforge.net>
2018-10-27 14:43:12 +02:00
* Copyright (C) 2008-2012 Regis Houssin <regis.houssin@inodbox.com>
2012-09-02 09:27:06 +02:00
* Copyright (C) 2008-2011 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2014 Teddy Andreotti <125155@supinfo.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2009-03-13 14:12:43 +01:00
/**
* \file htdocs/user/passwordforgotten.php
2011-07-08 17:50:28 +02:00
* \brief Page to ask a new password
2009-03-13 14:12:43 +01:00
*/
define("NOLOGIN", 1); // This means this output page does not require to be logged.
2008-01-20 22:53:43 +01:00
require '../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/usergroups.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
2021-02-26 13:18:40 +01:00
if (!empty($conf->ldap->enabled)) {
require_once DOL_DOCUMENT_ROOT.'/core/class/ldap.class.php';
}
2018-05-26 16:24:54 +02:00
// Load translation files required by page
$langs->loadLangs(array('errors', 'users', 'companies', 'ldap', 'other'));
// Security check
2021-02-26 13:18:40 +01:00
if (!empty($conf->global->MAIN_SECURITY_DISABLEFORGETPASSLINK)) {
header("Location: ".DOL_URL_ROOT.'/');
exit;
}
2009-12-29 12:30:41 +01:00
2020-09-16 19:39:50 +02:00
$action = GETPOST('action', 'aZ09');
$mode = $dolibarr_main_authentication;
2021-02-26 13:18:40 +01:00
if (!$mode) {
$mode = 'http';
}
2020-09-16 19:39:50 +02:00
$username = GETPOST('username', 'alphanohtml');
$passworduidhash = GETPOST('passworduidhash', 'alpha');
2019-02-16 10:29:15 +01:00
$conf->entity = (GETPOST('entity', 'int') ? GETPOST('entity', 'int') : 1);
2012-09-07 17:23:16 +02:00
// Instantiate hooks of thirdparty module only if not already define
2012-09-02 09:27:06 +02:00
$hookmanager->initHooks(array('passwordforgottenpage'));
2013-04-05 18:09:05 +02:00
2021-02-26 13:18:40 +01:00
if (GETPOST('dol_hide_leftmenu', 'alpha') || !empty($_SESSION['dol_hide_leftmenu'])) {
$conf->dol_hide_leftmenu = 1;
}
if (GETPOST('dol_hide_topmenu', 'alpha') || !empty($_SESSION['dol_hide_topmenu'])) {
$conf->dol_hide_topmenu = 1;
}
if (GETPOST('dol_optimize_smallscreen', 'alpha') || !empty($_SESSION['dol_optimize_smallscreen'])) {
$conf->dol_optimize_smallscreen = 1;
}
if (GETPOST('dol_no_mouse_hover', 'alpha') || !empty($_SESSION['dol_no_mouse_hover'])) {
$conf->dol_no_mouse_hover = 1;
}
if (GETPOST('dol_use_jmobile', 'alpha') || !empty($_SESSION['dol_use_jmobile'])) {
$conf->dol_use_jmobile = 1;
}
2013-04-05 18:09:05 +02:00
/**
* Actions
*/
$parameters = array('username' => $username);
$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
if ($reshook < 0) {
2021-02-26 13:18:40 +01:00
$message = $hookmanager->error;
}
if (empty($reshook)) {
2021-02-26 13:18:40 +01:00
// Validate new password
if ($action == 'validatenewpassword' && $username && $passworduidhash) {
2021-02-26 13:18:40 +01:00
$edituser = new User($db);
$result = $edituser->fetch('', $username);
2021-02-26 13:18:40 +01:00
if ($result < 0) {
$message = '<div class="error">'.dol_escape_htmltag($langs->trans("ErrorLoginDoesNotExists", $username)).'</div>';
} else {
global $dolibarr_main_instance_unique_id;
//print $edituser->pass_temp.'-'.$edituser->id.'-'.$dolibarr_main_instance_unique_id.' '.$passworduidhash;
if ($edituser->pass_temp && dol_verifyHash($edituser->pass_temp.'-'.$edituser->id.'-'.$dolibarr_main_instance_unique_id, $passworduidhash)) {
2021-02-26 13:18:40 +01:00
// Clear session
unset($_SESSION['dol_login']);
2021-08-20 12:38:44 +02:00
$_SESSION['dol_loginmesg'] = $langs->transnoentitiesnoconv('NewPasswordValidated'); // Save message for the session page
2021-02-26 13:18:40 +01:00
$newpassword = $edituser->setPassword($user, $edituser->pass_temp, 0);
dol_syslog("passwordforgotten.php new password for user->id=".$edituser->id." validated in database");
2021-02-26 13:18:40 +01:00
header("Location: ".DOL_URL_ROOT.'/');
exit;
} else {
$langs->load("errors");
$message = '<div class="error">'.$langs->trans("ErrorFailedToValidatePasswordReset").'</div>';
}
}
}
// Action modif mot de passe
if ($action == 'buildnewpassword' && $username) {
$sessionkey = 'dol_antispam_value';
2022-02-22 23:44:56 +01:00
$ok = (array_key_exists($sessionkey, $_SESSION) === true && (strtolower($_SESSION[$sessionkey]) == strtolower(GETPOST('code'))));
2021-02-26 13:18:40 +01:00
// Verify code
if (!$ok) {
$message = '<div class="error">'.$langs->trans("ErrorBadValueForCode").'</div>';
} else {
$isanemail = preg_match('/@/', $username);
$edituser = new User($db);
$result = $edituser->fetch('', $username, '', 1);
if ($result == 0 && $isanemail) {
$result = $edituser->fetch('', '', '', 1, -1, $username);
}
if ($result <= 0 && $edituser->error == 'USERNOTFOUND') {
2021-07-05 18:56:24 +02:00
$message = '<div class="warning paddingtopbottom'.(empty($conf->global->MAIN_LOGIN_BACKGROUND) ? '' : ' backgroundsemitransparent boxshadow').'">';
2021-02-26 13:18:40 +01:00
if (!$isanemail) {
$message .= $langs->trans("IfLoginExistPasswordRequestSent");
} else {
$message .= $langs->trans("IfEmailExistPasswordRequestSent");
}
$message .= '</div>';
$username = '';
} else {
if (!$edituser->email) {
$message = '<div class="error">'.$langs->trans("ErrorLoginHasNoEmail").'</div>';
} else {
$newpassword = $edituser->setPassword($user, '', 1);
if ($newpassword < 0) {
// Failed
$message = '<div class="error">'.$langs->trans("ErrorFailedToChangePassword").'</div>';
} else {
// Success
if ($edituser->send_password($user, $newpassword, 1) > 0) {
2021-07-05 18:56:24 +02:00
$message = '<div class="warning paddingtopbottom'.(empty($conf->global->MAIN_LOGIN_BACKGROUND) ? '' : ' backgroundsemitransparent boxshadow').'">';
2021-02-26 13:18:40 +01:00
if (!$isanemail) {
$message .= $langs->trans("IfLoginExistPasswordRequestSent");
} else {
$message .= $langs->trans("IfEmailExistPasswordRequestSent");
}
//$message .= $langs->trans("PasswordChangeRequestSent", $edituser->login, dolObfuscateEmail($edituser->email));
$message .= '</div>';
$username = '';
} else {
$message .= '<div class="error">'.$edituser->error.'</div>';
}
}
}
}
}
}
}
2011-07-08 17:50:28 +02:00
/**
* View
*/
2011-07-08 17:50:28 +02:00
$dol_url_root = DOL_URL_ROOT;
// Title
$title = 'Dolibarr '.DOL_VERSION;
2021-02-26 13:18:40 +01:00
if (!empty($conf->global->MAIN_APPLICATION_TITLE)) {
$title = $conf->global->MAIN_APPLICATION_TITLE;
}
// Select templates
2021-02-26 13:18:40 +01:00
if (file_exists(DOL_DOCUMENT_ROOT."/theme/".$conf->theme."/tpl/passwordforgotten.tpl.php")) {
$template_dir = DOL_DOCUMENT_ROOT."/theme/".$conf->theme."/tpl/";
2020-05-21 15:05:19 +02:00
} else {
$template_dir = DOL_DOCUMENT_ROOT."/core/tpl/";
}
2021-02-26 13:18:40 +01:00
if (!$username) {
$focus_element = 'username';
} else {
$focus_element = 'password';
}
// Send password button enabled ?
$disabled = 'disabled';
2021-02-26 13:18:40 +01:00
if (preg_match('/dolibarr/i', $mode)) {
$disabled = '';
}
if (!empty($conf->global->MAIN_SECURITY_ENABLE_SENDPASSWORD)) {
$disabled = ''; // To force button enabled
}
// Show logo (search in order: small company logo, large company logo, theme logo, common logo)
$width = 0;
$rowspan = 2;
$urllogo = DOL_URL_ROOT.'/theme/common/login_logo.png';
2021-02-26 13:18:40 +01:00
if (!empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small)) {
$urllogo = DOL_URL_ROOT.'/viewimage.php?cache=1&amp;modulepart=mycompany&amp;file='.urlencode('logos/thumbs/'.$mysoc->logo_small);
2021-02-26 13:18:40 +01:00
} elseif (!empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo)) {
$urllogo = DOL_URL_ROOT.'/viewimage.php?cache=1&amp;modulepart=mycompany&amp;file='.urlencode('logos/'.$mysoc->logo);
$width = 128;
2021-02-26 13:18:40 +01:00
} elseif (is_readable(DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/img/dolibarr_logo.svg')) {
2020-05-15 12:43:48 +02:00
$urllogo = DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/dolibarr_logo.svg';
2021-02-26 13:18:40 +01:00
} elseif (is_readable(DOL_DOCUMENT_ROOT.'/theme/dolibarr_logo.svg')) {
2020-05-15 12:43:48 +02:00
$urllogo = DOL_URL_ROOT.'/theme/dolibarr_logo.svg';
}
// Security graphical code
2021-02-26 13:18:40 +01:00
if (function_exists("imagecreatefrompng") && !$disabled) {
$captcha = 1;
$captcha_refresh = img_picto($langs->trans("Refresh"), 'refresh', 'id="captcha_refresh_img"');
}
// Execute hook getPasswordForgottenPageOptions (for table)
$parameters = array('entity' => GETPOST('entity', 'int'));
$hookmanager->executeHooks('getPasswordForgottenPageOptions', $parameters); // Note that $action and $object may have been modified by some hooks
if (is_array($hookmanager->resArray) && !empty($hookmanager->resArray)) {
$morelogincontent = $hookmanager->resArray; // (deprecated) For compatibility
} else {
$morelogincontent = $hookmanager->resPrint;
}
// Execute hook getPasswordForgottenPageExtraOptions (eg for js)
$parameters = array('entity' => GETPOST('entity', 'int'));
$reshook = $hookmanager->executeHooks('getPasswordForgottenPageExtraOptions', $parameters); // Note that $action and $object may have been modified by some hooks.
$moreloginextracontent = $hookmanager->resPrint;
2012-09-02 09:27:06 +02:00
include $template_dir.'passwordforgotten.tpl.php'; // To use native PHP