Update dari aaPanel

This commit is contained in:
WD - Dev
2025-07-15 00:18:35 +07:00
parent 84c697516e
commit 759b6d9047
13 changed files with 259 additions and 101 deletions
+135 -73
View File
@@ -2,127 +2,189 @@
ini_set('display_errors', 1);
error_reporting(E_ALL);
// =======================
// Include necessary files
// =======================
include "/www/wwwroot/bilspro/config/connect.php";
include "/www/wwwroot/bilspro/config/routeros_api.class.php";
// include "../config/connect.php";
// include "../config/routeros_api.class.php";
// ============================
// Get parameters from CLI args
// ============================
$m_ip = $argv[1]; // IP address
$m_user = $argv[2]; // Username
$m_pass = $argv[3]; // Password
$m_port = $argv[4]; // Port
$idsm = $argv[5]; // Mikrotik setting ID
$nama = $argv[6]; // Nama Mikrotik
// Get parameters from command line arguments
$m_ip = $argv[1]; // IP address
$m_user = $argv[2]; // Username
$m_pass = $argv[3]; // Password
$m_port = $argv[4]; // Port
$idsm = $argv[5]; // Mikrotik setting ID
// ====================
// Log file location
// ====================
$logFile = "/www/wwwroot/bilspro/load/logs/voucher_{$nama}_log.txt";
// Create new RouterosAPI instance
// ================================
// Function: Write to log file
// ================================
function writeLog($message)
{
global $logFile;
$date = date("Y-m-d H:i:s");
$newLine = "[$date] $message\n";
// Append new log
file_put_contents($logFile, $newLine, FILE_APPEND);
// Keep log file max 1000 lines
$lines = file($logFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$totalLines = count($lines);
if ($totalLines > 1000) {
$lines = array_slice($lines, -1000); // Keep only last 1000
file_put_contents($logFile, implode("\n", $lines) . "\n");
}
}
// ==========================
// Create RouterOS API object
// ==========================
$API = new RouterosAPI();
$API->debug = false;
// Connect to the router API
// =======================
// Connect to RouterOS API
// =======================
if ($API->connect2($m_ip, $m_user, $m_pass, $m_port)) {
echo "\n<b>Connected to Router: {$m_ip}</b>\n";
echo "✅ Berhasil terhubung ke Router: {$m_ip}\n";
writeLog("[✅] Berhasil terhubung ke Router: {$m_ip}");
// Query vouchers that need to be checked
$ambilv1 = $pdo->prepare("SELECT id, create_time, voucher, durasi FROM v_load_cek_voucher WHERE id_setting_mikrotik = :idsm AND status_cek IS NULL LIMIT 75");
$ambilv1->execute([':idsm' => $idsm]);
$batchSize = 200; // Batasi 200 voucher per batch
$offset = 0;
while ($cekv1 = $ambilv1->fetch(PDO::FETCH_ASSOC)) {
$iddatav1 = $cekv1['id'];
$datav1 = $cekv1['voucher'];
$durasi = $cekv1['durasi'];
do {
$ambilv1 = $pdo->prepare("
SELECT id, create_time, voucher, durasi
FROM v_load_cek_voucher
WHERE id_setting_mikrotik = :idsm
LIMIT :limit OFFSET :offset
");
$ambilv1->bindValue(':idsm', $idsm, PDO::PARAM_INT);
$ambilv1->bindValue(':limit', $batchSize, PDO::PARAM_INT);
$ambilv1->bindValue(':offset', $offset, PDO::PARAM_INT);
$ambilv1->execute();
// Convert the 'durasi' to a valid DateInterval format
$inter = convertDurationToInterval($durasi);
if (!$inter) {
echo "Invalid duration format: $durasi.\n";
continue; // Skip this voucher if the duration is invalid
}
$rows = $ambilv1->fetchAll(PDO::FETCH_ASSOC);
if (!$rows) break;
// Check if the voucher exists in the hotspot users
$cek = $API->comm('/ip/hotspot/user/print', ["?name" => $datav1]);
foreach ($rows as $cekv1) {
$iddatav1 = $cekv1['id'];
$datav1 = $cekv1['voucher'];
$durasi = $cekv1['durasi'];
if ($cek) {
// If voucher exists, check the comment
if (isset($cek[0]['comment']) && $cek[0]['comment'] != '') {
$activetime = processCommentDate($cek[0]['comment']);
$Tgl = new DateTime($activetime);
// ================================
// Convert duration to DateInterval
// ================================
$inter = convertDurationToInterval($durasi);
if (!$inter) {
$status = "Durasi tidak valid ($durasi)";
echo "{$datav1} => $status\n";
writeLog("[⛔] {$datav1} => $status");
continue;
}
// =====================================
// Check if voucher exists on Hotspot
// =====================================
$cek = $API->comm('/ip/hotspot/user/print', ["?name" => $datav1]);
if ($cek) {
// If voucher exists and has comment
if (!empty($cek[0]['comment'])) {
$activetime = processCommentDate($cek[0]['comment']);
$Tgl = new DateTime($activetime);
$Tgl->add($inter);
$exp = $Tgl->format('Y-m-d H:i:s');
updateVoucher($pdo, $iddatav1, $activetime, $exp);
$status = "Voucher aktif & diperbarui. Expired: $exp";
echo "{$datav1} => $status\n";
writeLog("[✅] {$datav1} => $status");
} else {
markVoucherChecked($pdo, $iddatav1);
$status = "Voucher ditemukan tanpa komentar, ditandai sudah dicek";
echo "{$datav1} => $status\n";
writeLog("[️] {$datav1} => $status");
}
} else {
// If voucher does not exist
$aktif = $cekv1['create_time'];
$Tgl = new DateTime($aktif);
$Tgl->add($inter);
$exp = $Tgl->format('Y-m-d H:i:s');
echo "{$datav1} comment->{$cek[0]['comment']} active->{$activetime} exp->{$exp} => Exists\n";
// Update the database with active and expired time
updateVoucher($pdo, $iddatav1, $activetime, $exp);
} else {
// Mark the voucher as checked
markVoucherChecked($pdo, $iddatav1);
updateVoucher($pdo, $iddatav1, $aktif, $exp);
$status = "Voucher tidak ditemukan, dianggap aktif sejak $aktif, expired: $exp";
echo "{$datav1} => $status\n";
writeLog("[⚠️] {$datav1} => $status");
}
} else {
// If voucher does not exist, calculate expiration time based on creation date
$aktif = $cekv1['create_time'];
$Tgl = new DateTime($aktif);
$Tgl->add($inter);
$exp = $Tgl->format('Y-m-d H:i:s');
echo "{$datav1} active->{$aktif} exp->{$exp} => Does not exist\n";
// Update the database with active and expired time
updateVoucher($pdo, $iddatav1, $aktif, $exp);
}
echo "{$cekv1['voucher']}\n";
}
$offset += $batchSize;
} while (count($rows) == $batchSize);
echo "✅ Selesai memproses semua voucher untuk Router {$m_ip}\n";
writeLog("[✅] Selesai memproses semua voucher untuk Router {$m_ip}");
} else {
echo "Unable to connect to router: {$m_ip}\n";
$msg = "Gagal terhubung ke Router: {$m_ip}";
echo "$msg\n";
writeLog("[❌] $msg");
}
// Disconnect the API session
// =======================
// Disconnect API session
// =======================
$API->disconnect();
// Function to convert duration string to DateInterval
function convertDurationToInterval($durasi) {
// ============================================
// Helper Functions
// ============================================
function convertDurationToInterval($durasi)
{
if (substr($durasi, -1) == 'H') {
return new DateInterval("PT" . rtrim($durasi, 'H') . "H"); // For hours
return new DateInterval("PT" . rtrim($durasi, 'H') . "H");
} elseif (substr($durasi, -1) == 'D') {
return new DateInterval("P" . rtrim($durasi, 'D') . "D"); // For days
return new DateInterval("P" . rtrim($durasi, 'D') . "D");
} elseif (substr($durasi, -1) == 'M') {
return new DateInterval("PT" . rtrim($durasi, 'M') . "M"); // For minutes
return new DateInterval("PT" . rtrim($durasi, 'M') . "M");
} else {
return false; // Invalid duration format
return false;
}
}
// Function to process the comment and extract valid activation time
function processCommentDate($comment) {
function processCommentDate($comment)
{
$modifiedDateTime = substr($comment, 6);
if (substr($modifiedDateTime, 0, 4) == date('Y')) {
return $modifiedDateTime;
} else {
$date = date_create_from_format("M/d/Y H:i:s", $modifiedDateTime);
if ($date !== false) {
return date_format($date, "Y-m-d H:i:s");
} else {
echo "Invalid date format: {$modifiedDateTime}\n";
return null;
}
return $date ? date_format($date, "Y-m-d H:i:s") : null;
}
}
// Function to update the voucher in the database
function updateVoucher($pdo, $iddatav1, $activetime, $exp) {
function updateVoucher($pdo, $iddatav1, $activetime, $exp)
{
$updateQuery = "UPDATE ganerate_voucher SET active_time = :activetime, expaired_time = :exp WHERE id = :id";
$stmt = $pdo->prepare($updateQuery);
$stmt->execute([
':activetime' => $activetime,
':exp' => $exp,
':id' => $iddatav1
':exp' => $exp,
':id' => $iddatav1
]);
}
// Function to mark voucher as checked in the database
function markVoucherChecked($pdo, $iddatav1) {
function markVoucherChecked($pdo, $iddatav1)
{
$updateQuery = "UPDATE ganerate_voucher SET status_cek = '1' WHERE id = :id";
$stmt = $pdo->prepare($updateQuery);
$stmt->execute([':id' => $iddatav1]);