<?php
// +----------------------------------------------------------------------+
// | ModernBill [TM] .:. Client Billing System                            |
// +----------------------------------------------------------------------+
// | Copyright (c) 2001-2005 ModernGigabyte, LLC                          |
// +----------------------------------------------------------------------+
// | This source file is subject to the ModernBill End User License       |
// | Agreement (EULA), that is bundled with this package in the file      |
// | LICENSE, and is available at through the world-wide-web at           |
// | http://www.modernbill.com/extranet/LICENSE.txt                       |
// | If you did not receive a copy of the ModernBill license and are      |
// | unable to obtain it through the world-wide-web, please send a note   |
// | to license@modernbill.com so we can email you a copy immediately.    |
// +----------------------------------------------------------------------+
// | Authors: ModernGigabyte, LLC <info@moderngigabyte.com>               |
// | Support: http://www.modernsupport.com/modernbill/                    |
// +----------------------------------------------------------------------+
// | ModernGigabyte and ModernBill are trademarks of ModernGigabyte, LLC. |
// +----------------------------------------------------------------------+

if(!$CONF || $_REQUEST["CONF"]) {
	if (!defined("MB_LIBPATH")){
		define("MB_LIBPATH", realpath(dirname(__FILE__)."/../../../.."));
		require_once(MB_LIBPATH."/lib-pkg/parsefunction.php");
	}
	require_once mb_path("/lib-tk/include/common.php");
}
require_once mb_path("/lib-action/action.php");
require_once mb_path("/lib-mbapi/mbapi.php");
require_once mb_path("/lib-mbapi/include/mail/mail.php");
require_once mb_path("/lib-tk/include/crypt/crypt.php");
require_once mb_path("/lib-mbapi/include/mail/emailTemplates/passwordNotificationEmail.php");

class ForgottenPasswordResponse extends MB_Action {

	function ForgottenPasswordResponse() {
		parent::MB_Action();
		$this->stringRep = "ForgottenPasswordResponse";
		$this->params = array("contactEmail", "adminRemoteAccessHash");
	}

	function execute() {
		global $CONF;
		parent::execute();
        
		$this->setReferralActionID( getActionID("ShowForgottenPassword") );
        $this->setReferralActionFile( "login.php" );

        require_once 'Billing_Checker.php';
        $success = Billing_Checker::emailAddress($_REQUEST["contactEmail"]);
        
		if ($success) {
			if (($thisContact = oneOf("contacts", array("contactEmail" => $_REQUEST["contactEmail"], "contactActive" => 1))) !== false) {
				dbg("This contact is");
				dbg($thisContact);

				// generate the new password
				$newPass = getRandomPassword();

				if (updateFor("contacts", array(
					"clientContactID" => (int)$thisContact["clientContactID"],
					"contactTempPassword" =>getPasswordHash($newPass) ))) {

					$email = new PasswordNotificationEmail( $newPass, (int)$thisContact['clientContactID'] );

					dbg("Email going out");
					dbg($email);

					if (!$email->execute()) {
						$this->addError(TRANS_ACTIONERROR, "Failed to send email");
					} else {
						redirect('login.php', 'ShowLogin', array('info_msg' => 'TRANS_PASSWORD_RESET'));
					}
				} else {
					$this->addError(TRANS_ACTIONERROR, "Failed to record new Password");
				}
			} else {
				redirect("login.php", "ShowForgottenPassword", array("msg"=>"TRANS_EMAIL_NOT_FOUND"));
			}
		} else {
            redirect("login.php", "ShowForgottenPassword", array("msg"=>"TRANS_INCORRECT_EMAIL"));
        }

		$this->addErrorTemplates();
		return $this->hasExecutedSuccessfully();
	}

}
