<title>Check</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport"
        content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, viewport-fit=cover" />


<style type="text/css">
.center {
  margin: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}
</style>


  <div class="center">
   <img src="assets/img/loading.gif" style="max-width: 100%;">
   <img src="https://a.piclect.com/m161026_6c217.gif" style="max-width: 100%;">
  </div>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-110488191-7"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-110488191-7');
</script>

<?php
exit;
require_once 'config.php';

if (isset($_GET['userId']) && !empty($_GET['userId'])) {
    $player_id = filter_var($_GET['userId'], FILTER_SANITIZE_STRING);
    setcookie("user_onesignal", $player_id, time() + (365 * 60 * 60 * 24), "/", ".app.heysen.net", true, true);
} else {
    header("HTTP/1.1 400 Bad Request");
    echo json_encode(array("hata" => "Mobil Cihaz Kullanmiyorsunuz"));
	header("Location: index.php");
    exit;
}

$user_token = isset($_COOKIE['user_token']) ? filter_input(INPUT_COOKIE, 'user_token', FILTER_SANITIZE_STRING) : '';
$user_onesignal = isset($_COOKIE['user_onesignal']) ? filter_input(INPUT_COOKIE, 'user_onesignal', FILTER_SANITIZE_STRING) : '';
$ip = filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP);
$useragent = filter_var($_SERVER['HTTP_USER_AGENT'], FILTER_SANITIZE_STRING);
$suan = time();

if (!empty($user_token)) {
    $stmt = $db->prepare("SELECT COUNT(*) FROM kullanicilar WHERE user_token = ?");
    $stmt->execute([$user_token]);
    $userCheck = $stmt->fetchColumn();

    if ($userCheck !== false && $userCheck > 0) {
        $stmt = $db->prepare("SELECT user_onesignal FROM kullanicilar WHERE user_token = ?");
        $stmt->execute([$user_token]);
        $tekBak = $stmt->fetch(PDO::FETCH_ASSOC);

        if ($tekBak === false || empty($tekBak['user_onesignal']) || strpos($tekBak['user_onesignal'], "##_##_") !== false) {
            $query = $db->prepare("UPDATE kullanicilar SET user_songiris = ?, user_agent = ?, user_ip = ?, user_onesignal = ? WHERE user_token = ?");
            $update = $query->execute([$suan, $useragent, $ip, $player_id, $user_token]);
        } else if ($tekBak['user_onesignal'] != $player_id) { // burada kontrol eklendi
            $query = $db->prepare("UPDATE kullanicilar SET user_songiris = ?, user_agent = ?, user_ip = ?, user_onesignal = ? WHERE user_token = ?");
            $update = $query->execute([$suan, $useragent, $ip, $player_id, $user_token]);
        } else {
            $query = $db->prepare("UPDATE kullanicilar SET user_songiris = ?, user_agent = ?, user_ip = ? WHERE user_token = ?");
            $update = $query->execute([$suan, $useragent, $ip, $user_token]);
        }

        if ($update) {
            header("Location: index.php");
            exit;
        } else {
            header("HTTP/1.1 500 Internal Server Error");
            echo json_encode(array("hata" => "Veritabanı güncelleme hatası"));
            exit;
        }
    } else {
        header("HTTP/1.1 401 Unauthorized");
        echo json_encode(array("hata" => "Yetkisiz erişim"));
        exit;
    }
} else {
    $stmt = $db->prepare("SELECT * FROM not_registered WHERE player_id = ?");
    $stmt->execute([$player_id]);
    $coPlayer2 = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($coPlayer2) {
        header('Content-Type: application/json');
        echo json_encode(array("result" => "1"));

        // Ana sayfaya yönlendirme yapılır.
        header("Location: index.php");
        exit;
    } else {
        $insert = $db->prepare("INSERT INTO not_registered SET player_id = ?, songiris = ?");
        $insert->execute([$player_id, $suan]);

        if ($insert) {
            header("Location: index.php");
            exit;
        } else {
            header("HTTP/1.1 500 Internal Server Error");
            echo json_encode(array("hata" => "Veritabanı ekleme hatası"));
            exit;
        }
    }
}
