Files

147 lines
6.1 KiB
PHP

<?php
// Pastikan untuk menonaktifkan tampilan error
ini_set('display_errors', 1);
error_reporting(E_ALL);
// Termasuk file koneksi Mikrotik dan sesi
include '../config/connect.php';
include '../config/routeros_api.class.php';
include '../config/tanggal_indo.php';
session_start();
// Fungsi untuk menangani respon JSON
function sendResponse($success, $message) {
// Kirim respons JSON dengan status dan pesan
echo json_encode(['success' => $success, 'message' => $message]);
exit;
}
// Menyimpan data ke session
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Simpan data POST ke session (termasuk data Mikrotik jika ada)
foreach ($_POST as $key => $value) {
if (!empty($value)) {
$_SESSION[$key] = $value; // Menyimpan data POST ke session
}
}
$mikrotikValid = true; // Flag untuk validasi koneksi Mikrotik
// Cek apakah ada data Mikrotik yang diperlukan
if (isset($_POST['ipa'], $_POST['usm'], $_POST['psw'], $_POST['pam'])) {
try {
$API = new RouterosAPI();
$API->debug = false;
// Ambil data koneksi Mikrotik dari $_POST
$ip_address = $_POST['ipa']; // IP Mikrotik
$username = $_POST['usm']; // Username Mikrotik
$password = $_POST['psw']; // Password Mikrotik
$port_api = $_POST['pam']; // Port API Mikrotik
// Menghubungkan ke API Mikrotik menggunakan data yang dikirim melalui POST
if ($API->connect2($ip_address, $username, $password, $port_api)) {
sendResponse(true, 'Data Form Komplit, Koneksi ke Mikrotik berhasil.');
} else {
$mikrotikValid = false;
sendResponse(false, 'Koneksi ke Mikrotik gagal, silakan perbaiki koneksi.');
}
} catch (Exception $e) {
$mikrotikValid = false;
sendResponse(false, 'Terjadi kesalahan saat menghubungkan ke Mikrotik: ' . $e->getMessage());
}
}
// Jika ada data selain Mikrotik, cek validasi untuk jenis dan profile
if (isset($_POST['jenis'], $_POST['profile'])) {
try {
$API = new RouterosAPI();
$API->debug = false;
// Mengambil data koneksi Mikrotik dari session jika sudah ada
$ip_address = $_SESSION['ipa'] ?? ''; // IP Mikrotik
$username = $_SESSION['usm'] ?? ''; // Username Mikrotik
$password = $_SESSION['psw'] ?? ''; // Password Mikrotik
$port_api = $_SESSION['pam'] ?? 8728; // Default port API Mikrotik
// Cek apakah koneksi Mikrotik valid
if ($API->connect2($ip_address, $username, $password, $port_api)) {
// Cek status jenis layanan dan profile
$profileName = $_POST['profile'];
if ($_POST['jenis'] == 'PPPOE') {
// Cek profil PPPOE di Mikrotik
$profiles = $API->comm('/ppp/profile/print');
$found = false;
foreach ($profiles as $profile) {
if ($profile['name'] === $profileName) {
$found = true;
break;
}
}
if ($found) {
sendResponse(true, 'Data Form Komplit, Profile PPPOE di Mikrotik sesuai.');
} else {
sendResponse(false, 'Profil PPPOE tidak ditemukan di Mikrotik.');
}
} else {
// Cek profil Hotspot di Mikrotik
$profiles = $API->comm('/ip/hotspot/user/profile/print');
$found = false;
$profileId = null;
foreach ($profiles as $profile) {
if ($profile['name'] === $profileName) {
$profileId = $profile['.id'];
$found = true;
break;
}
}
if ($found) {
$status = ($_POST['jenishs'] == 1) ? 1 : NULL;
if ($status == 1) {
// Format interval durasi jika diperlukan
$interval = intervalFormat($_POST['durasi']);
// Skrip On Login yang akan dipasang di profil Hotspot
$onLoginScript = ':local nama $user; :local tanggal [/system clock get date]; :local waktu [/system clock get time]; :if ([/ip hotspot user get [find name=$nama] comment] = "") do={/ip hotspot user set [find name=$nama] comment="Login.$tanggal $waktu"; :log warning ("User " . $nama . " berhasil login.")} else={:log warning ("User " . $nama . " berhasil melakukan login ulang.")}; :if ([/system schedule find name=$nama] = "") do={/system schedule add name=$nama interval='.$interval.' on-event="/ip hotspot active remove [find user=$nama]; /ip hotspot user remove [find name=$nama]; /system schedule remove [find name=$nama]" start-date=$tanggal start-time=$waktu;}';
// Update skrip On Login
$response = $API->comm('/ip/hotspot/user/profile/set', [
'.id' => $profileId,
'on-login' => $onLoginScript,
]);
}
sendResponse(true, 'Data Form Komplit, Profile Hotspot di Mikrotik sesuai.');
} else {
sendResponse(false, 'Profil Hotspot tidak ditemukan di Mikrotik.');
}
}
} else {
$mikrotikValid = false;
sendResponse(false, 'Koneksi ke Mikrotik gagal, silakan perbaiki koneksi.');
}
} catch (Exception $e) {
$mikrotikValid = false;
sendResponse(false, 'Terjadi kesalahan saat menghubungkan ke Mikrotik: ' . $e->getMessage());
}
}
// Kirimkan respon sukses jika data berhasil disimpan
sendResponse(true, 'Data Form Komplit.');
} else {
sendResponse(false, 'Permintaan tidak valid.');
}
?>