<?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();
if($_SERVER["REQUEST_METHOD"] == "POST") {
    if($CMSNT->site("cronjob_debug") == 1) {
        if(!($server = $CMSNT->get_row(" SELECT * FROM `cron_server` WHERE `status` = 1 "))) {
            exit(json_encode(["status" => "error", "msg" => __("Địa chỉ IP không được cấp phép")]));
        }
    } 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")]));
    }
    if(empty($_POST["id"])) {
        exit(json_encode(["status" => "error", "msg" => __("Thiếu id")]));
    }
    if(!($cron = $CMSNT->get_row(" SELECT * FROM `cron_jobs` WHERE `id` = '" . check_string($_POST["id"]) . "' "))) {
        exit(json_encode(["status" => "error", "msg" => __("id không tồn tại trong hệ thống")]));
    }
    $output = !empty($_POST["output"]) ? check_string($_POST["output"]) : NULL;
    if($output !== NULL && 100 < strlen($output)) {
        $output = substr($output, 0, 100) . "...";
    }
    $code = !empty($_POST["status"]) ? check_string($_POST["status"]) : NULL;
    $timeout = !empty($_POST["timeout"]) ? check_string($_POST["timeout"]) : NULL;
    $isUpdate = $CMSNT->update("cron_jobs", ["last_run" => time(), "output" => $output, "code" => $code, "timeout" => $timeout], " `id` = '" . $cron["id"] . "' ");
    if($isUpdate) {
        $CMSNT->query("DELETE FROM `cron_history` WHERE `cron_job_id` = '" . $cron["id"] . "' AND `run_at` NOT IN (SELECT `run_at` FROM (SELECT `run_at` FROM `cron_history` WHERE `cron_job_id` = '" . $cron["id"] . "' ORDER BY `run_at` DESC LIMIT 100) AS tmp)");
        $CMSNT->insert("cron_history", ["cron_job_id" => $cron["id"], "server_id" => $server["id"], "run_at" => gettime(), "code" => $code, "output" => $output, "timeout" => $timeout]);
        exit(json_encode(["status" => "success", "msg" => __("Cập nhật thành công")]));
    }
}

?>