<?php
// check for pending hash
$userId = (int) $_REQUEST['u'];
$passwordHash = $_REQUEST['h'];
$user = UserPeer::loadUserByPasswordResetHash($passwordHash);
if (!$user)
{
    coreFunctions::redirect(WEB_ROOT);
}

// check user id passed is valid
if ($user->id != $userId)
{
    coreFunctions::redirect(WEB_ROOT);
}

// 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_reset_title_page_description_left", ""));
define("TITLE_DESCRIPTION_RIGHT", t("forgot_password_reset_title_page_description_right", ""));
$success = false;

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

// process forgot password, this is a non-ajax fallback so rarely used
if ((int) $_REQUEST['submitme'])
{
    // validation
    $password = trim($_REQUEST['password']);
    $confirmPassword = trim($_REQUEST['confirmPassword']);
    if (!strlen($password))
    {
        notification::setError(t("please_enter_your_password", "Please enter your new password"));
    }
    elseif ((strlen($password) < 6) || (strlen($password) > 20))
    {
        notification::setError(t("password_length_incorrect", "Password should be between 6 - 20 characters in length"));
    }
    elseif (validation::containsInvalidCharacters(strtolower($password), 'abcdefghijklmnopqrstuvwxyz1234567890@~#!-_£$&*()^%}{()'))
    {
        notification::setError(t("password_contains_illegal_characters", "Password contains invalid characters, please choose another."));
    }
    elseif ($password != $confirmPassword)
    {
        notification::setError(t("password_confirmation_does_not_match", "Your password confirmation does not match"));
    }

    // create the account
    if (!notification::isErrors())
    {
        // update password
        $db = Database::getDatabase(true);
        $db->query('UPDATE users SET passwordResetHash = "", password = :password WHERE id = :id', array('password' => Password::createHash($password), 'id' => $userId));

        // success
        coreFunctions::redirect(WEB_ROOT . "/login." . 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_reset_intro_text", "Set your new password below to access your account."); ?></p>
                <!-- progress bar indicator -->
                <div class="login-progressbar-indicator">
                    <h3>1%</h3>
                    <span><?php echo t('forgot_password_setting_password', 'setting password...'); ?></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();
}
?>
                <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_reset" action="forgot_password_reset.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-lock"></i>
                            </div>
                            <input type="password" class="form-control" name="password" id="password" placeholder="<?php echo UCWords(t("password", "Password")); ?>" autocomplete="off" />
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="input-group">
                            <div class="input-group-addon">
                                <i class="entypo-lock"></i>
                            </div>
                            <input type="password" class="form-control" name="confirmPassword" id="confirmPassword" placeholder="<?php echo UCWords(t("confirm_password", "Confirm Password")); ?>" 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("update_password", "update password")); ?>
                        </button>
                    </div>
                    <div class="form-group">&nbsp;</div>
                    <input type="hidden" value="1" name="submitme"/>
                    <input type="hidden" value="<?php echo (int) $_REQUEST['u']; ?>" name="u" id="u"/>
                    <input type="hidden" value="<?php echo validation::safeOutputToScreen($_REQUEST['h']); ?>" name="h" id="h"/>
                </form>
                <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>