<?php 
include '../../config.php';

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$rand = rand(0,4);

if($rand!=0) die();

$zaman = time();

$sonaktif = time()-24*60*60;

function randBot(){
	global $db;
	$query = $db->query("SELECT * FROM kullanicilar WHERE user_bot = 1 ORDER BY RAND() LIMIT 1", PDO::FETCH_ASSOC);
	foreach($query as $veri){
		$bot_id = $veri['user_id'];
	}
	return $bot_id;
}


$onesignalArr = array();
$bot_id = randBot();

$query = $db->query("SELECT * FROM kullanicilar WHERE user_songiris >= $sonaktif AND user_bot = 0 AND user_cinsiyet = 'erkek' AND user_id != 663793", PDO::FETCH_ASSOC);

foreach($query as $row){

	$query = $db->query("SELECT * FROM ziyaretler WHERE u_to = '{$row['user_id']}' ORDER BY id DESC")->fetch(PDO::FETCH_ASSOC);

	$zamanfark = $query['time']+0;

	if(empty($query) OR time()>=$zamanfark){

		$varMi = $db->query("SELECT COUNT(*) FROM ziyaretler WHERE u_from = {$bot_id} AND u_to = {$row['user_id']}")->fetchColumn();      

		if($varMi==0){

			$query = $db->prepare("INSERT INTO ziyaretler SET
				u_from = ?,
				u_to = ?,
				time = ?");

			$insert = $query->execute(array(
				$bot_id, $row['user_id'], time()
			));


if(strlen($row['user_onesignal'])==36){

$onesignalArr[] = $row['user_onesignal'];

}


		}
	}


}

//print_r($onesignalArr);

$rand = rand(0,1);

if($rand!=0) die();


$botBilgisi = $db->query("SELECT * FROM kullanicilar WHERE user_id = '{$bot_id}'")->fetch(PDO::FETCH_ASSOC);




$bot_resim = "https://app.heysen.net/".$botBilgisi['user_picture'];

$bot_ismi = explode(" ",$botBilgisi['user_fullname']);

$bildirim_icerik = "👀".$bot_ismi[0]." Profilinize Baktı👀";


      $heading = array(
            "en" => "👄1 Yeni Profil Ziyareçisi!👄"
        );
    
        $content = array(
            "en" => $bildirim_icerik
        );
    
        $fields = array(
            'app_id' => "ad1b405f-217b-47b5-b59b-770d801cb89b",
            'include_player_ids' => $onesignalArr, 
            'data' => array("foo" => "bar"),
            'contents' => $content,
            'ttl' => 2700,
            'headings' => $heading,
            'priority' => 10,
            'large_icon' => $bot_resim,
            'url' => 'https://'.$_SERVER['SERVER_NAME'].'/hesabim.php?visitor=1' 
        );
    

    // 'url' => 'https://'.$_SERVER['SERVER_NAME'].'/mesajlar.php?id='.$bot['user_id']

        $fields = json_encode($fields);
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        $response = curl_exec($ch);
        curl_close($ch);

echo $response;   
echo "<br><br>";     








?>

<head>
  <!-- <meta http-equiv="refresh" content="5">
 --></head>