integraçao site
This commit is contained in:
38
app/Controllers/IntegrationController.php
Normal file
38
app/Controllers/IntegrationController.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Models\Order;
|
||||
use App\Models\Domain;
|
||||
use App\Models\Setting;
|
||||
use App\Utils\View;
|
||||
|
||||
class IntegrationController
|
||||
{
|
||||
public function stats()
|
||||
{
|
||||
$orderModel = new Order();
|
||||
$domainModel = new Domain();
|
||||
$settingModel = new Setting();
|
||||
|
||||
// Orders metrics
|
||||
$totalOrders = $orderModel->countAll();
|
||||
$recentOrders = $orderModel->countRecent(7); // Last 7 days
|
||||
|
||||
// Domain metrics
|
||||
$blockedDomains = $domainModel->countBlocked();
|
||||
|
||||
// Last Update - Try specific key first, fallback to now if not set yet
|
||||
$lastUpdate = $settingModel->get('last_rpz_update');
|
||||
if (!$lastUpdate) {
|
||||
$lastUpdate = date('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
View::json([
|
||||
'orders_total' => $totalOrders,
|
||||
'orders_recent' => $recentOrders,
|
||||
'domains_blocked' => $blockedDomains,
|
||||
'last_update' => $lastUpdate
|
||||
]);
|
||||
}
|
||||
}
|
||||
23
app/Middleware/IntegrationMiddleware.php
Normal file
23
app/Middleware/IntegrationMiddleware.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Middleware;
|
||||
|
||||
use App\Utils\View;
|
||||
|
||||
class IntegrationMiddleware
|
||||
{
|
||||
public function handle()
|
||||
{
|
||||
$headers = getallheaders();
|
||||
$tokenHeader = $headers['X-Integration-Token'] ?? $headers['x-integration-token'] ?? $_SERVER['HTTP_X_INTEGRATION_TOKEN'] ?? '';
|
||||
|
||||
$validToken = $_ENV['INTEGRATION_TOKEN'] ?? null;
|
||||
|
||||
if (empty($validToken) || $tokenHeader !== $validToken) {
|
||||
View::json(['error' => 'Unauthorized'], 401);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -12,4 +12,18 @@ class Order extends Model
|
||||
$stmt->execute();
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
public function countAll()
|
||||
{
|
||||
$stmt = $this->conn->query("SELECT COUNT(*) as total FROM orders");
|
||||
return $stmt->fetch()['total'];
|
||||
}
|
||||
|
||||
public function countRecent($days = 7)
|
||||
{
|
||||
$stmt = $this->conn->prepare("SELECT COUNT(*) as total FROM orders WHERE created_at >= DATE_SUB(NOW(), INTERVAL :days DAY)");
|
||||
$stmt->bindValue(':days', $days, \PDO::PARAM_INT);
|
||||
$stmt->execute();
|
||||
return $stmt->fetch()['total'];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,6 +101,10 @@ $router->post('/api/auth/login', [\App\Controllers\ApiAuthController::class, 'lo
|
||||
$router->get('/api/v1/domains', [\App\Controllers\ApiController::class, 'domains']);
|
||||
$router->addMiddleware(\App\Middleware\ApiMiddleware::class);
|
||||
|
||||
// Integration Routes
|
||||
$router->get('/api/integration/stats', [\App\Controllers\IntegrationController::class, 'stats']);
|
||||
$router->addMiddleware(\App\Middleware\IntegrationMiddleware::class);
|
||||
|
||||
// Client Routes
|
||||
$router->get('/client/dashboard', [\App\Controllers\ClientDashboardController::class, 'index']);
|
||||
$router->addMiddleware(\App\Middleware\ClientMiddleware::class);
|
||||
|
||||
Reference in New Issue
Block a user