Files
manjapro_v6/load/check_voucher.php
2026-08-20 18:39:37 +07:00

183 lines
4.6 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
include "/www/wwwroot/bilspro/config/connect.php";
include "/www/wwwroot/bilspro/config/routeros_api.class.php";
// ============================
// PARAMETER CLI
// ============================
$m_ip = $argv[1] ?? '';
$m_user = $argv[2] ?? '';
$m_pass = $argv[3] ?? '';
$m_port = $argv[4] ?? '';
$idsm = $argv[5] ?? '';
$nama = $argv[6] ?? 'unknown';
// ============================
// AMANKAN NAMA FILE LOG
// ============================
$nama_safe = preg_replace('/[^A-Za-z0-9_\-]/', '_', $nama);
$logFile = "/www/wwwroot/bilspro/load/logs/voucher_{$nama_safe}.log";
// ============================
// FUNCTION LOG (ANTI CORRUPT)
// ============================
function writeLog($msg)
{
global $logFile;
$line = "[" . date('Y-m-d H:i:s') . "] $msg\n";
file_put_contents($logFile, $line, FILE_APPEND | LOCK_EX);
}
// ============================
// API
// ============================
$API = new RouterosAPI();
$API->debug = false;
$API->timeout = 3;
// ============================
// CONNECT
// ============================
if (!$API->connect2($m_ip, $m_user, $m_pass, $m_port)) {
writeLog("❌ Gagal konek ke {$m_ip}");
exit;
}
writeLog("✅ Connected ke {$m_ip}");
// ============================
// 🔥 AMBIL HOTSPOT SEKALI SAJA
// ============================
$hotspot = $API->comm('/ip/hotspot/user/print');
$userMap = [];
if (is_array($hotspot)) {
foreach ($hotspot as $u) {
$userMap[$u['name']] = $u;
}
}
// ============================
// LOOP DATA DB
// ============================
$batchSize = 200;
$offset = 0;
do {
$stmt = $pdo->prepare("
SELECT id, create_time, voucher, durasi
FROM v_load_cek_voucher
WHERE id_setting_mikrotik = :idsm
LIMIT :limit OFFSET :offset
");
$stmt->bindValue(':idsm', $idsm, PDO::PARAM_INT);
$stmt->bindValue(':limit', $batchSize, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (!$rows) break;
foreach ($rows as $r) {
$id = $r['id'];
$voucher = $r['voucher'];
$durasi = $r['durasi'];
$interval = convertDurationToInterval($durasi);
if (!$interval) {
writeLog("⛔ {$voucher} durasi invalid");
continue;
}
// ============================
// CEK DARI CACHE
// ============================
if (isset($userMap[$voucher])) {
$comment = $userMap[$voucher]['comment'] ?? '';
if ($comment) {
$activetime = processCommentDate($comment);
if (!$activetime) {
writeLog("❌ {$voucher} tanggal invalid");
continue;
}
$tgl = new DateTime($activetime);
$tgl->add($interval);
$exp = $tgl->format('Y-m-d H:i:s');
updateVoucher($pdo, $id, $activetime, $exp);
writeLog("✅ {$voucher} aktif, exp: {$exp}");
} else {
markVoucherChecked($pdo, $id);
writeLog("ℹ️ {$voucher} tanpa comment");
}
} else {
$aktif = $r['create_time'];
$tgl = new DateTime($aktif);
$tgl->add($interval);
$exp = $tgl->format('Y-m-d H:i:s');
updateVoucher($pdo, $id, $aktif, $exp);
writeLog("⚠️ {$voucher} tidak ada di MT");
}
usleep(50000); // biar halus
}
$offset += $batchSize;
} while (count($rows) == $batchSize);
$API->disconnect();
writeLog("✅ SELESAI {$m_ip}");
// ============================
// HELPER
// ============================
function convertDurationToInterval($d)
{
if (substr($d, -1) == 'H') return new DateInterval("PT" . rtrim($d, 'H') . "H");
if (substr($d, -1) == 'D') return new DateInterval("P" . rtrim($d, 'D') . "D");
if (substr($d, -1) == 'M') return new DateInterval("PT" . rtrim($d, 'M') . "M");
return false;
}
function processCommentDate($c)
{
$d = substr($c, 6);
if (substr($d, 0, 4) == date('Y')) return $d;
$date = date_create_from_format("M/d/Y H:i:s", $d);
return $date ? date_format($date, "Y-m-d H:i:s") : null;
}
function updateVoucher($pdo, $id, $a, $e)
{
$stmt = $pdo->prepare("UPDATE ganerate_voucher SET active_time=?, expaired_time=? WHERE id=?");
$stmt->execute([$a, $e, $id]);
}
function markVoucherChecked($pdo, $id)
{
$stmt = $pdo->prepare("UPDATE ganerate_voucher SET status_cek='1' WHERE id=?");
$stmt->execute([$id]);
}