<?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_file_manager_top.inc.php');
?>
<body class="page-body login-page login-form-fall">
    <div class="login-container">
        <div class="login-header login-caret">
            <div class="login-content">
                <a href="<?php echo coreFunctions::getCoreSitePath(); ?>/index.<?php echo SITE_CONFIG_PAGE_EXTENSION; ?>" class="logo">
                    <img src="<?php echo SITE_IMAGE_PATH; ?>/main_logo.png" alt="<?php echo SITE_CONFIG_SITE_NAME; ?>" />
                </a>
                <p class="description"><?php echo t("forgot_password_intro_text", "Enter your email address below to receive further instructions on how to reset your account password."); ?></p>
                <!-- progress bar indicator -->
                <div class="login-progressbar-indicator">
                    <h3>1%</h3>
                    <span><?php echo t('forgot_password_requesting_reset', 'requesting reset...'); ?></span>
                </div>
            </div>
        </div>
        <div class="login-progressbar">
            <div></div>
        </div>
        <div class="login-form">
            <div class="login-content">
                <?php
                if (notification::isErrors())
                {
                    echo notification::outputErrors();
                }
                ?>
                <?php if (isset($_REQUEST['s'])): ?>
                    <div class="alert alert-success">
                        <i class="entypo-check"></i> <?php echo t("forgot_password_sent_intro_text", "An email has been sent with further instructions on how to reset your password. Please check your email inbox.", array('EMAIL_ADDRESS' => validation::safeOutputToScreen($_REQUEST['emailAddress']))); ?>
                    </div>
                <?php else: ?>
                    <div class="form-login-error">
                        <h3><?php echo t("error", "Error"); ?></h3>
                        <p id="error-message-container"></p>
                    </div>
                    <form method="post" role="form" id="form_forgot_password" action="forgot_password.html" autocomplete="off">
                        <!-- fix for chrome auto complete not working -->
                        <input style="display:none"><input type="password" style="display:none">
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-addon">
                                    <i class="entypo-mail"></i>
                                </div>
                                <input type="text" class="form-control" name="emailAddress" id="emailAddress" placeholder="<?php echo UCWords(t("email_address", "email address")); ?>" autocomplete="off" />
                            </div>
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-info btn-block btn-login">
                                <i class="entypo-right-open-mini"></i>
                                <?php echo UCWords(t("request_reset", "request reset")); ?>
                            </button>
                        </div>
                        <div class="form-group">&nbsp;</div>
                        <input type="hidden" value="1" name="submitme"/>
                    </form>
                <?php endif; ?>
                <div class="login-bottom-links">
                    <a href="<?php echo coreFunctions::getCoreSitePath(); ?>/login.<?php echo SITE_CONFIG_PAGE_EXTENSION; ?>" class="link"><i class="entypo-lock"></i> <?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>
                    <br />
                    <a href="<?php echo coreFunctions::getCoreSitePath(); ?>/terms.<?php echo SITE_CONFIG_PAGE_EXTENSION; ?>"><?php echo t('term_and_conditions', 'terms and conditions'); ?></a>&nbsp;&nbsp;|&nbsp;&nbsp;<?php echo t("copyright", "copyright"); ?> &copy; <?php echo date("Y"); ?> - <a href="<?php echo coreFunctions::getCoreSitePath(); ?>"><?php echo SITE_CONFIG_SITE_NAME; ?></a>
                </div>
            </div>
        </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>
</body>
</html>