<?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
 */

/**
 * cfBackendResetpwdAction Class
 *
 * Password reset action class.
 *
 * @package clearFusionCMS
 */
class cfBackendResetpwdAction extends cfBackendControllerAction {
	/**
	 * Process the action.
	 */
	function process() {
		$this->tpl->setPlaceholder('loginuri', $this->assemble('', 'login'));

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

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

		$auth = new flFormElementText($form, 'authcode');
		$auth->setLabel(__('Authorization Code', 'cfBackend'))
				->setRequired()
				->autocompleteOff()
				->addValidator(new flFormValidatorRequired());

		$pwd = new flFormElementPassword($form, 'pwd');
		$pwd->setLabel(__('New Password', 'cfBackend'))
			->setRequired()
			->autocompleteOff()
			->setDescription(__('The password must be at least 8 characters long.', 'cfBackend'))
			->addValidator(new flFormValidatorRequired())
			->addValidator(new flFormValidatorMinLength(8));

		$pwd2 = new flFormElementPassword($form, 'pwd2');
		$pwd2->setLabel(__('Confirm Password', 'cfBackend'))
			->setRequired()
			->autocompleteOff()
			->addValidator(new flFormValidatorSameAs($pwd));

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

		$form->focusElement($username);

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

			$userMgr = clearFusionCMS::getUserMgr();
			$uid = $userMgr->find($username->getValue(), array('username'), true);
			if(!count($uid) || $auth->getValue() != $userMgr->getVerificationHash($uid[0]))
				$msg = $this->alertMessage('Error', __('Unable to reset your password as one or more pieces of information are incorrect.', 'cfBackend'));
			else {
				$userMgr->setPassword($uid[0], $pwd->getValue());
				$msg = $this->alertMessage('Done', __('Your password has been set to the new value, please use the link below to return to the login page.', 'cfBackend'));
				$show = false;
			}
		}

		$this->response->setBody($this->tpl->parseDocument('managerResetPwdPage', 'backend_' . $this->request->getSelf(),
			array(
				'message' => $msg,
				'page_title' => __('Reset Password', 'cfBackend'),
				'page_desc' => __('An authorization code has been sent to your registered email address, please complete the form below and include the authorization code from the email.', 'cfBackend'),
				'login' => __('Return to Login Page', 'cfBackend'),
				'content' => $show ? $form->render($this->tpl->getCodeBuffer()) : ''
			)))
			->sendResponse();
	}
}
