<?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);
}

// load theme functions
$themeObj = themeHelper::getLoadedInstance();

// 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, image, picture, pic, img, hosting, sharing, upload, storage, site, website"));
define("HTML_ELEMENT_CLASS", "login-page-wrapper");

$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 ($password != $confirmPassword)
    {
        notification::setError(t("password_confirmation_does_not_match", "Your password confirmation does not match"));
    }
	else
	{
		$passValid = passwordPolicy::validatePassword($password);
		if(is_array($passValid))
		{
			notification::setError(implode('<br/>', $passValid));
		}
	}

    // 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 $themeObj->getMainLogoUrl(); ?>" alt="<?php echo SITE_CONFIG_SITE_NAME; ?>" />
                </a>
                <!-- 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">
				<div class="login-main-box">
					<?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>
					<p class="description"><?php echo t("forgot_password_reset_intro_text", "Set your new password below to access your account."); ?></p>
					<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>
					</div>
				</div>
            </div>
			<div class="footer-login-links">
				<?php echo t("copyright", "copyright"); ?> &copy; <?php echo date("Y"); ?> - <a href="<?php echo coreFunctions::getCoreSitePath(); ?>"><?php echo SITE_CONFIG_SITE_NAME; ?></a>&nbsp;&nbsp;|&nbsp;&nbsp;Powered by <a href="https://mfscripts.com/ucloud/overview.html">uCloud</a>
			</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; ?>/cloudable-api.js"></script>
    <script src="<?php echo SITE_JS_PATH; ?>/jquery.validate.min.js"></script>
    <script src="<?php echo SITE_JS_PATH; ?>/cloudable-login.js"></script>
    <script src="<?php echo SITE_JS_PATH; ?>/custom.js"></script>
</body>
</html>