<?php
// local template functions
require_once(SITE_TEMPLATES_PATH . '/partial/_template_functions.inc.php');

// page settings
define("PAGE_NAME", t("forgot_password_page_name", "Forgot Password"));
define("PAGE_DESCRIPTION", t("forgot_password_meta_description", "Forgot account password"));
define("PAGE_KEYWORDS", t("forgot_password_meta_keywords", "forgot, password, account, short, url, user"));
define("TITLE_DESCRIPTION_LEFT", t("forgot_password_title_page_description_left", ""));
define("TITLE_DESCRIPTION_RIGHT", t("forgot_password_title_page_description_right", ""));

// top navigation
require_once(SITE_TEMPLATES_PATH . '/partial/_navigation_header.inc.php');

// login user, this is a non-ajax fallback so rarely used
if ((int) $_REQUEST['submitme'])
{
    // clear any expired IPs
    bannedIP::clearExpiredBannedIps();

    // do login
    $emailAddress = trim($_REQUEST['emailAddress']);

    // initial validation
    if (notification::isErrors() == false)
    {
        if (!strlen($emailAddress))
        {
            // log failure
            notification::setError(t("please_enter_your_email_address", "Please enter the account email address"));
        }
    }

    if (notification::isErrors() == false)
    {
        $checkEmail = UserPeer::loadUserByEmailAddress($emailAddress);
        if (!$checkEmail)
        {
            // username exists
            notification::setError(t("account_not_found", "Account with that email address not found"));
        }
    }

    // reset password
    if (!notification::isErrors())
    {
        $userAccount = UserPeer::loadUserByEmailAddress($emailAddress);
        if ($userAccount)
        {
            // create password reset hash
            $resetHash = UserPeer::createPasswordResetHash($userAccount->id);

            $subject = t('forgot_password_email_subject', 'Password reset instructions for account on [[[SITE_NAME]]]', array('SITE_NAME' => SITE_CONFIG_SITE_NAME));

            $replacements = array(
                'FIRST_NAME' => $userAccount->firstname,
                'SITE_NAME' => SITE_CONFIG_SITE_NAME,
                'WEB_ROOT' => WEB_ROOT,
                'USERNAME' => $username,
                'PAGE_EXTENSION' => SITE_CONFIG_PAGE_EXTENSION,
                'ACCOUNT_ID' => $userAccount->id,
                'RESET_HASH' => $resetHash
            );
            $defaultContent = "Dear [[[FIRST_NAME]]],<br/><br/>";
            $defaultContent .= "We've received a request to reset your password on [[[SITE_NAME]]] for account [[[USERNAME]]]. Follow the url below to set a new account password:<br/><br/>";
            $defaultContent .= "<a href='[[[WEB_ROOT]]]/forgot_password_reset.[[[PAGE_EXTENSION]]]?u=[[[ACCOUNT_ID]]]&h=[[[RESET_HASH]]]'>[[[WEB_ROOT]]]/forgot_password_reset.[[[PAGE_EXTENSION]]]?u=[[[ACCOUNT_ID]]]&h=[[[RESET_HASH]]]</a><br/><br/>";
            $defaultContent .= "If you didn't request a password reset, just ignore this email and your existing password will continue to work.<br/><br/>";
            $defaultContent .= "Regards,<br/>";
            $defaultContent .= "[[[SITE_NAME]]] Admin";
            $htmlMsg = t('forgot_password_email_content', $defaultContent, $replacements);

            coreFunctions::sendHtmlEmail($emailAddress, $subject, $htmlMsg, SITE_CONFIG_DEFAULT_EMAIL_ADDRESS_FROM, strip_tags(str_replace("<br/>", "\n", $htmlMsg)));
            coreFunctions::redirect(WEB_ROOT . "/forgot_password." . SITE_CONFIG_PAGE_EXTENSION . "?s=1");
        }
    }
}
// header top
require_once(SITE_TEMPLATES_PATH . '/partial/_header.inc.php');
?>
<!-- Container -->
            <div class="container">
            	<div class="white-space space-big"></div>
                <div class="row">
                    <div class="col-md-6">
                    	<h3 class="fancy-title"><span> Information</span></h3>
                        <p>
						<?php echo t("forgot_password_intro_text", "Enter your email address below to receive further instructions on how to reset your account password."); ?>
						</p>
                        <div class="row">	
                        	<div class="col-sm-6">
                            	<ul class="list-default fa-ul">
                        			<li><a href="<?php echo coreFunctions::getCoreSitePath(); ?>/login.<?php echo SITE_CONFIG_PAGE_EXTENSION; ?>" class="link"><?php echo UCWords(t("login_form", "login form")); ?></a>&nbsp;&nbsp;&nbsp;<a href="<?php echo coreFunctions::getCoreSitePath(); ?>/register.<?php echo SITE_CONFIG_PAGE_EXTENSION; ?>" class="link"><?php echo UCWords(t("register", "register")); ?></a></li>                          
                        		</ul>
                            </div>
                        	<div class="col-sm-6">
                            	<ul class="list-default fa-ul">
                        			<li><a href="<?php echo coreFunctions::getCoreSitePath(); ?>/terms.<?php echo SITE_CONFIG_PAGE_EXTENSION; ?>"><?php echo t('term_and_conditions', 'terms and conditions'); ?></a></li>
                            	</ul>
                            </div>
                        </div>
                        <div class="white-space space-small"></div>
                    </div>
					
					<div class="col-md-6">
                    	<h3 class="fancy-title"><span>Reset Password</span></h3>
						<?php
                if (notification::isErrors())
                {
                    echo notification::outputErrors();
                }
                ?>
                
                        <!-- Form -->
						 <form method="post" role="form" id="form_forgot_password" action="forgot_password.html" autocomplete="off">
							 
							<div class="form-group">
								<div class="col-sm-12">
									<input type="text" class="form-control" name="emailAddress" id="emailAddress" placeholder="<?php echo UCWords(t("email_address", "email address")); ?>" autocomplete="off" />
								</div>

								<div class="col-sm-12">
									<button type="submit" class="btn btn-primary btn-block btn-login">
                                
                                <?php echo UCWords(t("request_reset", "request reset")); ?>
                            </button>
								</div>
							</div>
						</form>                        
                        <!-- /Form --> 
                                               
                        <div class="white-space space-small"></div>
                    </div>
					
				</div>               
			
            	<div class="white-space space-medium"></div>
        </div>

          
    <!-- Bottom Scripts -->
    <script src="<?php echo SITE_JS_PATH; ?>/gsap/main-gsap.js"></script>
    <script src="<?php echo SITE_JS_PATH; ?>/bootstrap.js"></script>
    <script src="<?php echo SITE_JS_PATH; ?>/joinable.js"></script>
    <script src="<?php echo SITE_JS_PATH; ?>/resizeable.js"></script>
    <script src="<?php echo SITE_JS_PATH; ?>/flow-api.js"></script>
    <script src="<?php echo SITE_JS_PATH; ?>/jquery.validate.min.js"></script>
    <script src="<?php echo SITE_JS_PATH; ?>/flow-login.js"></script>
    <script src="<?php echo SITE_JS_PATH; ?>/custom.js"></script>
<?php

// include footer

require_once(SITE_TEMPLATES_PATH . '/partial/_footer.inc.php');

?>