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

101 lines
3.6 KiB
PHP

<?php
include "/www/wwwroot/bilspro/config/connect.php";
// Set Waktu & Error Reporting
date_default_timezone_set('Asia/Jakarta');
error_reporting(E_ALL);
ini_set('display_errors', 1);
// 1. Ambil data dari tabel setting_mikrotik
$stmt = $pdo->prepare("SELECT id, hsa, nama FROM setting_mikrotik WHERE hsa != '' AND status = 'Connected'");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (!$rows) {
echo "Tidak ada data HSA untuk diproses.\n";
exit;
}
foreach ($rows as $row) {
$id_mikrotik = $row['id'];
$hsa_raw = rtrim($row['hsa'], "|");
$entries = explode('|', $hsa_raw);
// Mapping data Hotspot (Voucher => RawTime)
$hotspot_active = [];
foreach ($entries as $entry) {
$parts = explode('_', $entry);
if (count($parts) >= 2) {
// Bersihkan karakter titik yang sering muncul di MikroTik lama
$cleanTime = str_replace('.', ' ', trim($parts[1]));
$hotspot_active[$parts[0]] = $cleanTime;
}
}
// 2. Ambil voucher yang belum aktif
$stmtPel = $pdo->prepare("SELECT id, voucher, durasi FROM v_ganerate_voucher WHERE id_setting_mikrotik = :id AND active_time IS NULL");
$stmtPel->execute([':id' => $id_mikrotik]);
$vouchers = $stmtPel->fetchAll(PDO::FETCH_ASSOC);
if (empty($vouchers)) continue;
echo "---[ " . date('Y-m-d H:i:s') . " ]---\n";
echo "--- Mengolah MikroTik: {$row['nama']} (" . count($vouchers) . " voucher) ---\n";
$pdo->beginTransaction();
try {
$stmtUpdate = $pdo->prepare("UPDATE ganerate_voucher SET active_time = :at, expaired_time = :et WHERE id = :id");
$countUpdate = 0;
foreach ($vouchers as $v) {
$kode = $v['voucher'];
if (isset($hotspot_active[$kode])) {
$rawTime = $hotspot_active[$kode];
$activetime = null;
// LOGIKA PARSING WAKTU MULTI-FORMAT
if (preg_match('/^\d{4}-\d{2}-\d{2}/', $rawTime)) {
// Format: 2026-03-13 21:15:25
$activetime = $rawTime;
} else {
// Format: mar/13/2026 21:12:57 atau may/25/2024
$dateObj = date_create($rawTime);
if ($dateObj) {
$activetime = $dateObj->format("Y-m-d H:i:s");
}
}
if ($activetime) {
// Hitung Masa Aktif
$durasi = strtoupper($v['durasi']);
$unit = substr($durasi, -1);
$val = rtrim($durasi, $unit);
// Interval Format: Jam (H) -> PT...H, Hari (D) -> P...D, Bulan (M) -> P...M
$intervalStr = ($unit == 'H') ? "PT{$val}H" : "P{$val}{$unit}";
try {
$tgl = new DateTime($activetime);
$tgl->add(new DateInterval($intervalStr));
$expired = $tgl->format('Y-m-d H:i:s');
$stmtUpdate->execute([
':at' => $activetime,
':et' => $expired,
':id' => $v['id']
]);
$countUpdate++;
} catch (Exception $e) {
continue;
}
}
}
}
$pdo->commit();
echo "Sukses Update: $countUpdate voucher.\n";
} catch (Exception $e) {
$pdo->rollBack();
echo "Gagal pada MikroTik ini: " . $e->getMessage() . "\n";
}
}