<?php
function sendToContact($arrContact)
{
// Generate new recovery token
$arrContact['contact_password_lost'] = \Framework\Random::randomString(10, true, true, false);
\Components\Customers\Contacts::$m_bIsSecurityDisabled = true;
\Components\Customers\Contacts::saveColumn($arrContact['contact_id'], 'contact_password_lost', $arrContact['contact_password_lost']);
\Components\Customers\Contacts::$m_bIsSecurityDisabled = false;
// Register variables
\Framework\DOM\CurrentContext::setVariable('contact', $arrContact);
// Send the email
$strSiteTitle = \Components\Settings\ConfigurationItems::getValue('\Components\Settings\MyAccount', 'account_title');
$pEmail = new \Framework\Mail\Message();
$pEmail->setTo($arrContact['contact_email']);
$pEmail->setFrom('noreply@' . \Components\Settings\Domains::getPrimary());
$pEmail->setFromName($strSiteTitle);
$pEmail->setSubject($strSiteTitle . ' - Lost Password');
$pEmail->setContentHTMLBodyFromPage('/community/lost_password/email/');
$pEmail->send();
}
function lostPassword($pForm)
{
$strEmail = $pForm->getFormItem('forgot_contact_email')->getValue();
if(!$strEmail)
{
$pException = new \Framework\Exceptions\Validation();
$pException->setTitle('Please fill in this field');
throw $pException;
}
$arrFilter = array();
$arrFilter[] = array('contact_email', '=', $strEmail);
$arrFilter[] = 'OR';
$arrFilter[] = array('contact_username', '=', $strEmail);
$arrProperties = array();
$arrProperties['filter'] = $arrFilter;
$arrContacts = \Components\Customers\Contacts::getAll($arrProperties);
$bSent = false;
// Try to find a contact that does have a password set
foreach($arrContacts as $arrContact)
{
// If the password was not set, skip
if(!\Components\Customers\Contacts\Passwords::getIsSetForContact($arrContact['contact_id']))
{
continue;
}
sendToContact($arrContact);
$bSent = true;
}
// Fall back to a contact that does not have a password
if(!$bSent && count($arrContacts))
{
sendToContact($arrContacts[0]);
$bSent = true;
}
// If we didnt send an email, throw a validation error
if(!$bSent)
{
$pException = new \Framework\Exceptions\Validation();
$pException->setTitle('Sorry, we do not recognise that e-mail address or username');
throw $pException;
}
}
?>
|