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

// 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, file, hosting"));

// 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() . '/account_home.' . 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 SITE_IMAGE_PATH; ?>/main_logo.png" alt="<?php echo SITE_CONFIG_SITE_NAME; ?>" />
                </a>
                <p class="description"><?php echo t("login_intro_text", "Please enter your username and password below to login."); ?></p>
                <!-- 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">
                <?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; ?>
                <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" />
                        </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" />
                        </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="form-group">&nbsp;</div>
                    <?php
                    // include any plugin includes
                    pluginHelper::includeAppends('login_login_box.php');
                    ?>
                    <input type="hidden" value="1" name="submitme"/>
                </form>
                <div class="login-bottom-links">
                    <a href="<?php echo coreFunctions::getCoreSitePath(); ?>/forgot_password.<?php echo SITE_CONFIG_PAGE_EXTENSION; ?>" class="link"><?php echo UCWords(t("forgot_password", "forgot password")); ?>?</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>