<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$content = file_get_contents("php://input");



include('class/bot.php');
include('class/db.php');

$bot = new bot("7679298225:AAE298D-vssqO7qqdMlevIVm724HCVdrVls", $content);
$db = new db();



function getExpiredVIP($db)
{
    $now = time();
    $sql = "select * from `user` expire_time where expire_time < " . $now . " and `status` = 'vip' ";
    $stmt = $db->conn->prepare($sql);
    $stmt->execute();
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $getter = $stmt->fetchAll();
    $numrowfusers = $stmt->rowCount();
    if ($numrowfusers == 0) {
        return 'nobody';
    }


    return $getter;
}

function getExpiredMonthly($db)
{
    $now = time();
    $sql = "select * from `user` expire_time where expire_time < " . $now . " and `status` = 'monthly' ";
    $stmt = $db->conn->prepare($sql);
    $stmt->execute();
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $getter = $stmt->fetchAll();
    $numrowfusers = $stmt->rowCount();
    if ($numrowfusers == 0) {
        return 'nobody';
    }


    return $getter;
}


$expired = getExpired($db);
foreach ($expired as $singleGetter) {


$text = "📌 مدت استفاده‌ی یک‌ماهه‌ شما به پایان رسیده
برای تمدید عضویت فقط کافیه:
1️⃣ اسکرین‌شات کد معرفتون رو دوباره ارسال کنید
2️⃣ درخواست ورود به کانال بدید

✅ در صورت انجام معامله، عضویت شما برای یک ماه دیگه تمدید میشه.";

    $bot->sendmessage(urlencode($text), $singleGetter['chatid'], '');
    $bot->banMember($singleGetter['chatid'], $singleGetter['channel_id']);
    print($singleGetter['chatid'] . 'in channel' . $channelName . 'ended' );


    $sql = "update `user` set `status`='inactive'  where chatid='" . $singleGetter['chatid'] . "' ";
    $db->conn->exec($sql);

}





function oneDayAlertMonthly($db)
{
    $oneDayLater = time() + (3600 * 24 * 3);
    $sql = "select * from `user` where monthly_alert='no' and  expire_time < $oneDayLater and `status` = 'active'";
    // print($sql);
    $stmt = $db->conn->prepare($sql);
    $stmt->execute();
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $getter = $stmt->fetchAll();
    $numrowfusers = $stmt->rowCount();
    if ($numrowfusers == 0) {
        return 'nobody';
    }


    return $getter;

    
}


$twoDayAlertFree = oneDayAlertMonthly($db);
foreach ($twoDayAlertFree as $singleGetter) {
    $channelName = $singleGetter['channel_name'];

$text = "
از عضویت شما 3روز مانده لطفا مجدد برای عضویت  اسکرین شات که قبلاً ارسال کردید بفرستید
";

    $bot->sendmessage(urlencode($text), $singleGetter['chatid'], '');
    print('2 day alert' . "<br />");

    $sql = "update `user` set forex_notif='yes'  where chatid='" . $singleGetter['chatid'] . "' ";
    $db->conn->exec($sql);

}





function threeDayAlertCrypto($db)
{
    $threeDayLater = time() + (3600 * 24 * 3);
    $sql = "select * from `user` where crypto_notif='no' and  expire_time_crypto < $threeDayLater and `status_crypto` = 'active'";
    // print($sql);
    $stmt = $db->conn->prepare($sql);
    $stmt->execute();
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $getter = $stmt->fetchAll();
    $numrowfusers = $stmt->rowCount();
    if ($numrowfusers == 0) {
        return 'nobody';
    }


    return $getter;

    
}


$twoDayAlertFree = threeDayAlertCrypto($db);
foreach ($twoDayAlertFree as $singleGetter) {
    $channelName = $singleGetter['channel_name'];

$text = "
از عضویت شما 3روز مانده لطفا مجدد برای عضویت  اسکرین شات که قبلاً ارسال کردید بفرستید
";

    $bot->sendmessage(urlencode($text), $singleGetter['chatid'], '');
    print('2 day alert' . "<br />");

    $sql = "update `user` set expire_time_crypto='yes'  where chatid='" . $singleGetter['chatid'] . "' ";
    $db->conn->exec($sql);

}







?>