mirror of
https://github.com/mivodev/mivo.git
synced 2026-01-26 13:31:56 +07:00
Initial Release v1.0.0: Full feature set with Docker automation, Nginx/Alpine stack
This commit is contained in:
74
app/Controllers/ApiController.php
Normal file
74
app/Controllers/ApiController.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Core\Controller;
|
||||
use App\Libraries\RouterOSAPI;
|
||||
|
||||
use App\Models\Config;
|
||||
use App\Helpers\EncryptionHelper;
|
||||
|
||||
class ApiController extends Controller {
|
||||
|
||||
public function getInterfaces() {
|
||||
// Only allow POST
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'Method Not Allowed']);
|
||||
return;
|
||||
}
|
||||
|
||||
// Get JSON Input
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
$ip = $input['ip'] ?? '';
|
||||
$user = $input['user'] ?? '';
|
||||
$pass = $input['password'] ?? '';
|
||||
$id = $input['id'] ?? null;
|
||||
$port = $input['port'] ?? 8728; // Default port
|
||||
|
||||
// Fallback to stored password if empty and ID provided (Edit Mode)
|
||||
if (empty($pass) && !empty($id)) {
|
||||
$configModel = new Config();
|
||||
$session = $configModel->getSessionById($id);
|
||||
if ($session && !empty($session['password'])) {
|
||||
$pass = EncryptionHelper::decrypt($session['password']);
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($ip) || empty($user)) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'IP Address and Username are required']);
|
||||
return;
|
||||
}
|
||||
|
||||
$api = new RouterOSAPI();
|
||||
// $api->debug = true; // Enable for debugging
|
||||
$api->port = (int)$port;
|
||||
|
||||
if ($api->connect($ip, $user, $pass)) {
|
||||
$api->write('/interface/print');
|
||||
$read = $api->read(false);
|
||||
$interfaces = $api->parseResponse($read);
|
||||
$api->disconnect();
|
||||
|
||||
$list = [];
|
||||
foreach ($interfaces as $iface) {
|
||||
if (isset($iface['name'])) {
|
||||
$list[] = $iface['name'];
|
||||
}
|
||||
}
|
||||
|
||||
// Return success
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'interfaces' => $list
|
||||
]);
|
||||
} else {
|
||||
http_response_code(500);
|
||||
echo json_encode([
|
||||
'error' => 'Connection failed. Check IP, User, Password, or connectivity.'
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user