// Definir Base URL automaticamente (Ex: /gestao)
$basePath = dirname($_SERVER['SCRIPT_NAME']);
$basePath = str_replace(['\\', 'index.php'], ['/', ''], $basePath);
$basePath = rtrim($basePath, '/');
define('BASE_URL', $basePath);
// Ativar exibição de erros (Debug)
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Autoload do Composer (opcional)
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
}
// Simple Autoloader (if not using Composer yet)
spl_autoload_register(function ($class) {
$prefix = 'App\\';
$base_dir = __DIR__ . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR;
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) return;
$relative_class = substr($class, $len);
$file = $base_dir . str_replace('\\', DIRECTORY_SEPARATOR, $relative_class) . '.php';
if (file_exists($file)) {
require $file;
} else {
// Fallback: Tenta localizar o arquivo se as pastas estiverem em minúsculo (comum em Linux/Hostinger)
$parts = explode('\\', $relative_class);
$filename = array_pop($parts) . '.php';
$dir = implode(DIRECTORY_SEPARATOR, array_map('strtolower', $parts));
$file_fixed = $base_dir . (empty($dir) ? '' : $dir . DIRECTORY_SEPARATOR) . $filename;
if (file_exists($file_fixed)) {
require $file_fixed;
}
}
});
use App\Core\Router;
use App\Controllers\AuthController;
use App\Middleware\AuthMiddleware;
use App\Controllers\AdminController;
use App\Controllers\TenantController;
use App\Controllers\BillingController;
use App\Controllers\ClientController;
use App\Controllers\ContractController;
use App\Controllers\WebhookController;
$router = new Router();
// Guest Routes
$router->add('GET', '/', function() {
AuthMiddleware::isGuest();
$controller = new AuthController();
return $controller->login();
});
$router->add('GET', '/login', function() {
AuthMiddleware::isGuest();
$controller = new AuthController();
return $controller->login();
});
$router->add('GET', '/debug', function() {
echo "
Debug Info
";
echo "BASE_URL: " . BASE_URL . "
";
echo "SCRIPT_NAME: " . $_SERVER['SCRIPT_NAME'] . "
";
echo "REQUEST_URI: " . $_SERVER['REQUEST_URI'] . "
";
echo "PHP_SELF: " . $_SERVER['PHP_SELF'] . "
";
echo "Clean URI: " . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) . "
";
die();
});
$router->add('POST', '/login', function() {
$controller = new AuthController();
return $controller->login();
});
$router->add('GET', '/logout', [AuthController::class, 'logout']);
// Protected Routes
$router->add('GET', '/admin/dashboard', function() {
AuthMiddleware::check('master');
$controller = new AdminController();
return $controller->dashboard();
});
$router->add('POST', '/admin/tenant/create', function() {
AuthMiddleware::check('master');
$controller = new AdminController();
return $controller->createTenant();
});
$router->add('GET', '/dashboard', function() {
AuthMiddleware::check('admin');
$controller = new TenantController();
return $controller->dashboard();
});
$router->add('GET', '/settings', function() {
AuthMiddleware::check('admin');
$controller = new TenantController();
return $controller->settings();
});
$router->add('POST', '/settings/save', function() {
AuthMiddleware::check('admin');
$controller = new TenantController();
return $controller->saveSettings();
});
// Billing Routes
$router->add('GET', '/billing/create/{id}', [BillingController::class, 'create']);
$router->add('POST', '/billing/create/{id}', [BillingController::class, 'create']);
$router->add('GET', '/billing/show/{id}', [BillingController::class, 'show']);
// Webhook
$router->add('POST', '/webhook/mercadopago', [WebhookController::class, 'mercadopago']);
// Contract Routes
$router->add('POST', '/contracts/upload', [ContractController::class, 'upload']);
// Client Routes
$router->add('GET', '/clients', [ClientController::class, 'index']);
$router->add('GET', '/clients/create', [ClientController::class, 'create']);
$router->add('POST', '/clients/create', [ClientController::class, 'create']);
$router->add('GET', '/clients/edit/{id}', [ClientController::class, 'edit']);
$router->add('POST', '/clients/edit/{id}', [ClientController::class, 'edit']);
// Evolution API (Internal endpoints for the JS fetch)
$router->add('GET', '/api/evolution/status', function() {
AuthMiddleware::check('admin');
$tenantId = \App\Core\Session::get('tenant_id');
$evolutionModel = new \App\Models\EvolutionInstance();
$instance = $evolutionModel->findByTenant($tenantId);
if (!$instance) return json_encode(['connected' => false]);
$evolutionApi = new \App\Core\EvolutionAPI();
$status = $evolutionApi->getInstanceStatus($instance['instance_name']);
$isConnected = ($status['data']['instance']['state'] ?? '') === 'open';
// Update local DB status if changed
if ($isConnected != $instance['connected']) {
$evolutionModel->update($instance['id'], ['connected' => $isConnected]);
}
header('Content-Type: application/json');
echo json_encode(['connected' => $isConnected]);
});
$router->add('GET', '/api/evolution/connect', function() {
AuthMiddleware::check('admin');
$tenantId = \App\Core\Session::get('tenant_id');
$evolutionModel = new \App\Models\EvolutionInstance();
$instance = $evolutionModel->findByTenant($tenantId);
if (!$instance) return json_encode(['error' => 'No instance found']);
$evolutionApi = new \App\Core\EvolutionAPI();
$qr = $evolutionApi->getQRCode($instance['instance_name']);
header('Content-Type: application/json');
echo json_encode(['qrcode' => $qr['data']['code'] ?? null]);
});
$router->run();