CommunityID/modules/users/controllers/RecoverpasswordController.php

122 lines
4.5 KiB
PHP
Raw Normal View History

2019-07-17 20:08:50 +00:00
<?php
/*
* @copyright Copyright (C) 2005-2010 Keyboard Monkeys Ltd. http://www.kb-m.com
2019-07-17 20:08:50 +00:00
* @license http://creativecommons.org/licenses/BSD/ BSD License
* @author Keyboard Monkey Ltd
* @since CommunityID 0.9
* @package CommunityID
* @packager Keyboard Monkeys
*/
2019-07-17 20:16:19 +00:00
class Users_RecoverpasswordController extends CommunityID_Controller_Action
2019-07-17 20:08:50 +00:00
{
public function init()
{
parent::init();
2019-07-17 20:16:19 +00:00
if ($this->user->role != Users_Model_User::ROLE_ADMIN && $this->underMaintenance) {
2019-07-17 20:08:50 +00:00
return $this->_redirectForMaintenance();
}
}
public function indexAction()
{
$appSession = Zend_Registry::get('appSession');
if (isset($appSession->recoverPasswordForm)) {
$this->view->form = $appSession->recoverPasswordForm;
unset($appSession->recoverPasswordForm);
} else {
2019-07-17 20:16:19 +00:00
$this->view->form = new Users_Form_RecoverPassword();
2019-07-17 20:08:50 +00:00
}
$this->_helper->actionStack('index', 'login', 'users');
}
public function sendAction()
{
2019-07-17 20:16:19 +00:00
$form = new Users_Form_RecoverPassword();
2019-07-17 20:08:50 +00:00
$formData = $this->_request->getPost();
$form->populate($formData);
if (!$form->isValid($formData)) {
$appSession = Zend_Registry::get('appSession');
$appSession->recoverPasswordForm = $form;
return $this->_forward('index');
}
2019-07-17 20:16:19 +00:00
$users = new Users_Model_Users();
2019-07-17 20:08:50 +00:00
$user = $users->getUserWithEmail($form->getValue('email'));
if (!$user) {
$form->email->addError($this->view->translate('This E-mail is not registered in the system'));
$appSession = Zend_Registry::get('appSession');
$appSession->recoverPasswordForm = $form;
return $this->_forward('index');
}
2019-07-17 20:16:19 +00:00
$user->token = Users_Model_User::generateToken();
2019-07-17 20:08:50 +00:00
$user->save();
$file = CommunityID_Resources::getResourcePath('passwordreset_mail.txt');
2019-07-17 20:08:50 +00:00
$emailTemplate = file_get_contents($file);
$emailTemplate = str_replace('{userName}', $user->getFullName(), $emailTemplate);
$emailTemplate = str_replace('{IP}', $_SERVER['REMOTE_ADDR'], $emailTemplate);
// $_SERVER['SCRIPT_URI'] is not always available
$URI = self::getProtocol() . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
2019-07-17 20:08:50 +00:00
preg_match('#(.*)/users/recoverpassword#', $URI, $matches);
$emailTemplate = str_replace('{passwordResetURL}',
$matches[1] . '/users/recoverpassword/reset?token=' . $user->token,
$emailTemplate);
$this->_sendMail($user->email, $this->view->translate('Community-ID password reset'), $emailTemplate);
$this->_helper->FlashMessenger->addMessage($this->view->translate('Password reset E-mail has been sent'));
$this->_redirect('');
}
public function resetAction()
{
2019-07-17 20:16:19 +00:00
$users = new Users_Model_Users();
2019-07-17 20:08:50 +00:00
$user = $users->getUserWithToken($this->_getParam('token'));
if (!$user) {
$this->_helper->FlashMessenger->addMessage($this->view->translate('Wrong Token'));
2019-07-17 20:08:50 +00:00
$this->_redirect('');
return;
}
$newPassword = $user->generateRandomPassword();
$user->setClearPassword($newPassword);
// reset token
2019-07-17 20:16:19 +00:00
$user->token = Users_Model_User::generateToken();
2019-07-17 20:08:50 +00:00
$user->save();
$file = CommunityID_Resources::getResourcePath('passwordreset2_mail.txt');
2019-07-17 20:08:50 +00:00
$emailTemplate = file_get_contents($file);
$emailTemplate = str_replace('{userName}', $user->getFullName(), $emailTemplate);
$emailTemplate = str_replace('{password}', $newPassword, $emailTemplate);
$this->_sendMail($user->email, $this->view->translate('Community-ID password reset'), $emailTemplate);
$this->_helper->FlashMessenger->addMessage($this->view->translate('You\'ll receive your new password via E-mail'));
$this->_redirect('');
}
private function _sendMail($to, $subject, $body)
{
if (strtolower($this->_config->email->transport) == 'smtp') {
Zend_Mail::setDefaultTransport(new Zend_Mail_Transport_Smtp($this->_config->email->host, $this->_config->email->toArray()));
} else {
Zend_Mail::setDefaultTransport(new Zend_Mail_Transport_Sendmail());
}
$mail = new Zend_Mail('utf-8');
$mail->setBodyText($body);
$mail->setFrom($this->_config->email->supportemail);
$mail->addTo($to);
$mail->setSubject($subject);
$mail->send();
}
}