<?php

/**
 * Class file for manager authentication.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package clearFusionCMS
 * @copyright Copyright (c) 2012 - 2015 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

/**
 * cfBackendForgotpwdAction Class
 *
 * Class to implement the forgot password action.
 *
 * @package clearFusionCMS
 */
class cfBackendForgotpwdAction extends cfBackendControllerAction {
	/**
	 * Process the action.
	 */
	function process() {
		$this->tpl->setPlaceholder('loginuri', $this->assemble('', 'login'));

		$form = new flForm($this->request, 'resetform', $this->assemble('', 'forgotpwd'), true);

		$username = new flFormElementText($form, 'username');
		$username->setLabel(__('Username', 'cfBackend'))
				->setRequired()
				->autocompleteOff()
				->addValidator(new flFormValidatorRequired())
				->addValidator(new flFormValidatorMaxLength(100));

		$email = new flFormElementText($form, 'email');
		$email->setLabel(__('Email Address', 'cfBackend'))
				->setRequired()
				->autocompleteOff()
				->addValidator(new flFormValidatorRequired())
				->addValidator(new flFormValidatorEmail());

		$submit = new flFormElementSubmit($form, 'forgotpwd');
		$submit->setCaption(__('Reset Password', 'cfBackend'))
			->setCSS('primary');

		$form->focusElement($username);

		$msg = '';
		if($form->isSubmitted() && $form->isValid()) {

			$userMgr = clearFusionCMS::getUserMgr();
			$uid1 = $userMgr->find($username->getValue(), array('username'), true);
			$uid2 = $userMgr->find($email->getValue(), array('email'), true);

			if(count($uid1) == 1 && count($uid2) == 1 && $uid1[0] == $uid2[0]) {
				$mailer = clearFusionCMS::getMailer();
				if($mailer->connect()) {
					$cfg = new flConfigDb('cfSystemEmails', $this->db);
					$doc = str_replace(array(
						'[[+domain]]', '[[+hash]]'
					), array(
						$this->request->getServerName(), $userMgr->getVerificationHash($uid1[0])
					), $cfg->pwdResetEmail);

					$subject = str_replace('[[+domain]]', $this->request->getServerName(), $cfg->pwdResetSubject);

					$mailer->addRecipient($email->getValue())
							->setSubject($subject)
							->setTextBody($doc);

					$mailer->send();
					$mailer->close();

					// Go to step 2
					$this->response->redirectTo($this->assemble('', 'resetpwd', array(), true));
				}
				else
					$msg = $this->alertMessage('Error', __('Unable to connect to the mail server, please try again.', 'cfBackend'));
			}
			else
				$msg = $this->alertMessage('Error', __('Either the username or the email address is wrong. Please verify the information and try again.', 'cfBackend'));
		}

		$this->response->setBody($this->tpl->parseDocument('managerForgotPwdPage', 'backend_' . $this->request->getSelf(),
			array(
				'message' => $msg,
				'page_title' => __('Forgot Password', 'cfBackend'),
				'page_desc' => __('Please enter your username and registered email address below, password reset instructions will then be emailed to you.', 'cfBackend'),
				'login' => __('Return to Login Page', 'cfBackend'),
				'content' => $form->render($this->tpl->getCodeBuffer()),
				'mgr_lang' => fusionLib::getLocale(true)
			)))
			->sendResponse();
	}
}
