integraçao site

This commit is contained in:
HalbeBruno
2025-12-17 12:33:42 -03:00
parent fca10b13c1
commit 72b7d8ccd7
572 changed files with 1646 additions and 27 deletions

View 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
]);
}
}

View 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;
}
}

View File

@@ -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'];
}
}

View File

@@ -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);