<?php

include 'config.php';

$zaman2 = date('H',time()+10800);

echo $zaman2;
echo "<br>";

if($zaman2=="02" OR $zaman2=="03" OR $zaman2=="05" OR $zaman2=="06" OR $zaman2=="07" OR $zaman2=="08"){
    echo "BU SAATTE BEĞENİ ATILMAZ";
    exit;
}



$zaman = time();


function botSec(){ // json olarak herhangi bir botu çekiyoruz

    global $db;

    $query = $db->query("SELECT * FROM kullanicilar WHERE user_bot = 1 AND user_send = 0 ORDER BY RAND() LIMIT 1", PDO::FETCH_ASSOC);

    foreach ($query as $veri) {
        return $veri['user_id'];
    }

}

function botSecLike(){ // json olarak herhangi bir botu çekiyoruz

    global $db;

    $query = $db->query("SELECT * FROM kullanicilar WHERE user_bot = 1 AND user_like = 0 ORDER BY RAND() LIMIT 1", PDO::FETCH_ASSOC);

    foreach ($query as $veri) {
        return $veri['user_id'];
    }

}

function kullaniciCek(){ // array olarak onesignal_id alıyoruz

    global $db;

    $suan = time();
    $yirmidort = time()-172800;

    $query = $db->query("SELECT * FROM kullanicilar WHERE user_bot = 0 AND user_cinsiyet = 'erkek' AND user_songiris >= $yirmidort AND user_onesignal IS NOT NULL ORDER BY id DESC", PDO::FETCH_ASSOC);

    $userListArr = array();
    $useridArr = array();

    foreach ($query as $veri) {
        if(strlen($veri['user_onesignal'])==36){
        $userListArr[] = $veri['user_onesignal'];
        $useridArr[] = $veri['user_id'];
    }
    }
    return $userListArr;
    return $useridArr;

}


function idCek(){ // array olarak onesignal_id alıyoruz

    global $db;

    $suan = time();
    $yirmidort = time()-172800;


    $query = $db->query("SELECT * FROM kullanicilar WHERE user_bot = 0 AND user_cinsiyet = 'erkek' AND user_songiris >= $yirmidort AND user_onesignal IS NOT NULL ORDER BY id DESC", PDO::FETCH_ASSOC);

    $useridArr = array();

    foreach ($query as $veri) {
        if(strlen($veri['user_onesignal'])==36){
        $useridArr[] = $veri['user_id'];
    }
    }
    return $useridArr;

}

function update_settings($time1,$time2){
    global $db;

    $update_settings = $db->prepare("UPDATE settings SET like_time = ?, like_cron = ? WHERE id = ?");
    $update_settings->execute([$time1,$time2, 1]);
}


$setting = $db->prepare("SELECT like_time,like_cron FROM settings WHERE id = ?");
$setting->execute([1]);
$settings = $setting->fetch(PDO::FETCH_ASSOC);

if(time()<$settings['like_cron']) die();

$rand = rand(100,200);

update_settings(time(), time()+$rand*60);



echo "BİLDİRİM AT<br/>";

$botGel = botSecLike();
$userList = kullaniciCek();
$userIdList = idCek();

$bot = $db->query("SELECT * FROM kullanicilar WHERE user_id='$botGel'")->fetch(PDO::FETCH_ASSOC);

$bot_isim = $bot['user_fullname'];
$bot_resim = "https://".$_SERVER['SERVER_NAME']."/".$bot['user_picture'];


foreach ($userIdList as $veri) {
    
            $query = $db->prepare("INSERT INTO begeniler SET
            begenen_id = ?,
            begenilen_id = ?,
            zaman = ?
        ");

        $insert = $query->execute(array(
             $bot['user_id'], $veri, $zaman
        ));

}




        $content = array(
            "en" => "💌 ".$bot_isim." sizi beğendi."
        );
    
        $heading = array(
            "en" => "🔷Yeni Beğeniniz Var!🔷"
        );
    
        $fields = array(
            'app_id' => "69302624-ee42-4571-9f00-38d6f1248d5d",
            'include_player_ids' => $userList,
            'data' => array("foo" => "bar"),
            'contents' => $content,
            'headings' => $heading,
            'large_icon' => $bot_resim,
            'url' => 'https://'.$_SERVER['SERVER_NAME'].'/profil.php?id='.$bot['user_id']
        );
    

    // '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);
        

$jsonObj = json_decode($response, true);

$iletimSayi = $jsonObj['recipients'];

// $error_goster = $jsonObj['errors']['0'];



$query = $db->prepare("UPDATE kullanicilar SET
user_like = :yeni_send
WHERE user_id = :user_id");
$update = $query->execute(array(
     "yeni_send" => 1,
     "user_id" => $bot['user_id']
));



$ciktim = count($userList)." Kişiden {$iletimSayi} Tanesine Beğeni Gönderildi.";

  $array = array("result" => "1", "cevap" => $ciktim);
  echo json_encode($array); // başarılı üyelik
























