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

// if already logged in revert to account home
if($Auth->loggedIn())
{
	coreFunctions::redirect(coreFunctions::getCoreSitePath() . '/index.' . SITE_CONFIG_PAGE_EXTENSION);
}

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

// page settings
define("PAGE_NAME", t("login_page_name", "Login"));
define("PAGE_DESCRIPTION", t("login_meta_description", "Login to your account"));
define("PAGE_KEYWORDS", t("login_meta_keywords", "login, register, image, picture, pic, img, hosting, sharing, upload, storage, site, website"));
define("HTML_ELEMENT_CLASS", "login-page-wrapper");

// 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
    $loginUsername = trim($_REQUEST['username']);
    $loginPassword = trim($_REQUEST['password']);

    // check user isn't banned from logging in
    $bannedIp = bannedIP::getBannedIPData();
    if ($bannedIp)
    {
        if ($bannedIp['banType'] == 'Login')
        {
            notification::setError(t("login_ip_banned", "You have been temporarily blocked from logging in due to too many failed login attempts. Please try again [[[EXPIRY_TIME]]].", array('EXPIRY_TIME' => ($bannedIp['banExpiry'] != null ? coreFunctions::formatDate($bannedIp['banExpiry']) : t('later', 'later')))));
        }
    }

    // initial validation
    if (notification::isErrors() == false)
    {
        if (!strlen($loginUsername))
        {
            // log failure
            Auth::logFailedLoginAttempt(coreFunctions::getUsersIPAddress(), $loginUsername);

            notification::setError(t("please_enter_your_username", "Please enter your username"));
        }
        elseif (!strlen($loginPassword))
        {
            // log failure
            Auth::logFailedLoginAttempt(coreFunctions::getUsersIPAddress(), $loginUsername);

            notification::setError(t("please_enter_your_password", "Please enter your password"));
        }
    }

    if (notification::isErrors() == false)
    {
        $rs = $Auth->login($loginUsername, $loginPassword);
        if ($rs)
        {
            // if we know the file
            if (isset($_REQUEST['loginShortUrl']))
            {
                // download file
                $file = file::loadByShortUrl(trim($_REQUEST['loginShortUrl']));
                if ($file)
                {
                    coreFunctions::redirect($file->getFullShortUrl());
                }
            }

            // successful login
            coreFunctions::redirect(coreFunctions::getCoreSitePath() . '/index.' . SITE_CONFIG_PAGE_EXTENSION);
        }
        else
        {
            // login failed
            notification::setError(t("username_and_password_is_invalid", "Your username and password are invalid"));
        }
    }
}
// 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('login_logging_in', 'logging in...'); ?></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 str_replace('pageErrors', 'alert alert-danger', notification::outputErrors());
					}
					?>
					<div class="form-login-error">
						<h3><?php echo t("error", "Error"); ?></h3>
						<p id="error-message-container"><?php echo t("username_and_password_is_invalid", "Your username and password are invalid"); ?></p>
					</div>
					<?php if (isset($_REQUEST['s'])): ?>
						<div class="alert alert-success">
							<i class="entypo-check"></i> <?php echo t("forgot_password_reset_confirm_intro_text_login_below", "Your password has been reset. You can now login to the site below."); ?>
						</div>
					<?php endif; ?>
					<p class="description"><?php echo t("login_intro_text", "Please enter your username and password below to login."); ?></p>
					<form method="post" role="form" id="form_login" action="login.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-user"></i>
								</div>
								<input type="text" class="form-control" name="username" id="username" placeholder="<?php echo UCWords(t("username", "username")); ?>" autocomplete="off" value="<?php echo coreFunctions::inDemoMode()?'admin':''; ?>"/>
							</div>
						</div>
						<div class="form-group">
							<div class="input-group">
								<div class="input-group-addon">
									<i class="entypo-key"></i>
								</div>
								<input type="password" class="form-control" name="password" id="password" placeholder="<?php echo UCWords(t("password", "password")); ?>" autocomplete="off"  value="<?php echo coreFunctions::inDemoMode()?'password':''; ?>"/>
							</div>
						</div>
						<div class="form-group">
							<button type="submit" class="btn btn-primary btn-block btn-login">
								<i class="entypo-login"></i>
								<?php echo UCWords(t("login", "login")); ?>
							</button>
						</div>
						
						<div class="login-bottom-links">
							<?php if(SITE_CONFIG_ENABLE_USER_REGISTRATION == 'yes'): ?><i class="entypo-user"></i> <a href="<?php echo coreFunctions::getCoreSitePath(); ?>/register.<?php echo SITE_CONFIG_PAGE_EXTENSION; ?>" class="link"><?php echo UCWords(t("register", "register")); ?></a>&nbsp;&nbsp;&nbsp;<?php endif; ?><a href="<?php echo coreFunctions::getCoreSitePath(); ?>/forgot_password.<?php echo SITE_CONFIG_PAGE_EXTENSION; ?>" class="link"><i class="entypo-info"></i> <?php echo UCWords(t("forgot_password", "forgot password")); ?>?</a>
						</div>
						
						<?php
						// include any plugin includes
						pluginHelper::includeAppends('login_login_box.php');
						?>
						<input type="hidden" value="1" name="submitme"/>
					</form>
				</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>