2016-07-07 15:33:38 +02:00

150 lines
6.8 KiB
PHP

<?php
/**
* Description of Lost Password
*
* @company Antadis
*/
class lostpasswordlostpasswordModuleFrontController extends ModuleFrontController {
public $current_step = 0;
/**
* Assign template vars related to page content
* @see FrontController::initContent()
*/
public function initContent(){
parent::initContent();
if($this->current_step == 1) {
$this->context->smarty->assign(array('confirmation' => 1));
} elseif($this->current_step == 2) {
$this->context->smarty->assign(array('confirmation' => 2));
} elseif($this->current_step == 3) {
$this->context->smarty->assign(array('confirmation' => 3));
}
$this->setTemplate('lostpassword.tpl');
}
public function setMedia()
{
parent::setMedia();
$this->context->controller->addJS(_MODULE_DIR_.$this->module->name.'/js/'.$this->module->name.'.js');
}
/**
* Start forms process
* @see FrontController::postProcess()
*/
public function postProcess(){
if (Tools::isSubmit('submitEmail'))
{
if (!($email = trim(Tools::getValue('email'))) || !Validate::isEmail($email)) {
$this->errors[] = Tools::displayError('Invalid email address.');
} else {
$customer = new Customer();
$customer->getByemail($email);
if (!Validate::isLoadedObject($customer))
$this->errors[] = Tools::displayError('There is no account registered for this email address.');
elseif (!$customer->active)
$this->errors[] = Tools::displayError('You cannot regenerate the password for this account.');
else{
$mail_params = array(
'{email}' => $customer->email,
'{lastname}' => $customer->lastname,
'{firstname}' => $customer->firstname,
'{url}' => $this->context->link->getModuleLink('lostpassword', 'lostpassword', array('submit' => 'submitResetFromUrl', 'token'=>$customer->secure_key, 'id_customer'=>(int)$customer->id))
);
if (Mail::Send($this->context->language->id, 'lostpassword', Mail::l('Réinitialiser son mot de passe', (int)$this->context->language->id), $mail_params, $customer->email, ($customer->firstname.' '.$customer->lastname), null, null, null, null, dirname(__FILE__).'/../../mails/')) {
$this->context->smarty->assign(array('confirmation' => 1, 'customer_email' => $customer->email));
$this->current_step = 1;
} else {
$this->errors[] = Tools::displayError('An error occurred while sending the email.');
}
}
}
if(!empty($this->errors)) {
$this->current_step = 0;
}
}
elseif (($submit = Tools::getValue('submit')) && ($token = Tools::getValue('token')) && ($id_customer = (int)Tools::getValue('id_customer')))
{
$email = Db::getInstance()->getValue('SELECT `email` FROM '._DB_PREFIX_.'customer c WHERE c.`secure_key` = \''.pSQL($token).'\' AND c.id_customer = '.(int)$id_customer);
if ($email)
{
$customer = new Customer();
$customer->getByemail($email);
if (!Validate::isLoadedObject($customer)) {
$this->errors[] = Tools::displayError('Customer account not found');
} elseif (!$customer->active) {
$this->errors[] = Tools::displayError('You cannot regenerate the password for this account.');
} else {
$this->context->smarty->assign(
array(
'confirmation' => 2,
'token_for_reset' => $token,
'id_customer' => $id_customer
)
);
$this->current_step = 2;
}
} else {
$this->errors[] = Tools::displayError('Customer account not found');
}
if(!empty($this->errors)) {
$this->current_step = 0;
}
}
elseif (Tools::isSubmit('submitResetPassword') && ($token = Tools::getValue('token')) && ($id_customer = (int)Tools::getValue('id_customer')))
{
$email = Db::getInstance()->getValue('SELECT `email` FROM '._DB_PREFIX_.'customer c WHERE c.`secure_key` = \''.pSQL($token).'\' AND c.`id_customer` = '.(int)$id_customer);
if ($email)
{
$password = Tools::getValue('password');
$passConf = Tools::getValue('passwordConf');
if($password !== $passConf)
$this->errors[] = Tools::displayError('Passwords are different.');
$customer = new Customer($id_customer);
if (!Validate::isLoadedObject($customer)) {
$this->errors[] = Tools::displayError('Customer account not found');
} elseif (!$customer->active) {
$this->errors[] = Tools::displayError('You cannot reset the password for this account.');
}
if (empty($this->errors)) {
$customer->passwd = Tools::encrypt($password);
$customer->last_passwd_gen = date('Y-m-d H:i:s', time());
if ($customer->update())
{
Hook::exec('actionPasswordRenew', array('customer' => $customer, 'password' => $password));
$this->context->smarty->assign(array('confirmation' => 3, 'customer_email' => $customer->email));
$this->current_step = 3;
}
else
$this->errors[] = Tools::displayError('An error occurred with your account. Please report this issue using the contact form.');
}
} else {
$this->errors[] = Tools::displayError('We cannot reset your password with the data you\'ve submitted. token = '.$token.' id = '.$id_customer);
}
if(!empty($this->errors)) {
$this->current_step = 2;
$this->context->smarty->assign(
array(
'confirmation' => 2,
'token_for_reset' => $token,
'id_customer' => $id_customer
)
);
}
}
elseif (Tools::getValue('token') || Tools::getValue('id_customer') || Tools::getValue('submit'))
$this->errors[] = Tools::displayError('We cannot reset your password with the data you\'ve submitted.');
}
}