<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.4
 * @ Decoder version: 1.0.2
 * @ Release: 10/08/2022
 */

// Decoded file for php version 74.
define("IN_SITE", true);
require_once __DIR__ . "/../libs/db.php";
require_once __DIR__ . "/../config.php";
require_once __DIR__ . "/../libs/lang.php";
require_once __DIR__ . "/../libs/helper.php";
$CMSNT = new DB();
header("Content-Type: application/json");
if($_SERVER["REQUEST_METHOD"] !== "POST") {
    exit(json_encode(["status" => "error", "msg" => __("Method not allowed")]));
}
if($CMSNT->site("cronjob_debug") == 1) {
    if(!($server = $CMSNT->get_row(" SELECT * FROM `cron_server` WHERE `status` = 1 "))) {
        exit(json_encode(["status" => "error", "msg" => __("Không tìm thấy server cho chế độ debug")]));
    }
} elseif(!($server = $CMSNT->get_row(" SELECT * FROM `cron_server` WHERE `ip` = '" . myip() . "' AND `status` = 1 "))) {
    exit(json_encode(["status" => "error", "msg" => __("Địa chỉ IP không được cấp phép")]));
}
$json_body = file_get_contents("php://input");
$data = json_decode($json_body, true);
if(!isset($data["tasks"]) || !is_array($data["tasks"])) {
    exit(json_encode(["status" => "error", "msg" => __("Dữ liệu không hợp lệ hoặc thiếu \"tasks\"")]));
}
foreach ($data["tasks"] as $task) {
    $id_cron = isset($task["id"]) ? (int) $task["id"] : 0;
    $status = isset($task["status"]) ? $task["status"] : NULL;
    $timeout = isset($task["timeout"]) ? $task["timeout"] : NULL;
    $output = isset($task["output"]) ? $task["output"] : NULL;
    if($id_cron <= 0) {
    } else {
        $cron = $CMSNT->get_row("SELECT * FROM `cron_jobs` WHERE `id` = '" . $id_cron . "' ");
        if(!$cron) {
        } else {
            if($output && 100 < mb_strlen($output)) {
                $output = mb_substr($output, 0, 100) . "...";
            }
            $isUpdate = $CMSNT->update("cron_jobs", ["last_run" => time(), "output" => $output, "code" => $status, "timeout" => $timeout], " `id` = '" . $cron["id"] . "' ");
            if($isUpdate) {
                $CMSNT->insert("cron_history", ["cron_job_id" => $cron["id"], "server_id" => $server["id"], "run_at" => gettime(), "code" => $status, "output" => $output, "timeout" => $timeout]);
                $CMSNT->query("\n            DELETE FROM `cron_history` \n            WHERE `cron_job_id` = '" . $cron["id"] . "'\n            AND `run_at` NOT IN (\n                SELECT `run_at` FROM (\n                    SELECT `run_at`\n                    FROM `cron_history`\n                    WHERE `cron_job_id` = '" . $cron["id"] . "'\n                    ORDER BY `run_at` DESC\n                    LIMIT 100\n                ) AS tmp\n            )\n        ");
            }
        }
    }
}
exit(json_encode(["status" => "success", "msg" => __("Batch update thành công")]));

?>