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]); }