Raiju/admin/api/dashboard.php
2026-10-25 06:05:42 +03:00

79 lines
3.9 KiB
PHP

<?php
if (!defined('RAIJU_ADMIN_DIR')) { http_response_code(404); exit; }
// Authenticated dispatcher supplies shared helpers and request context.
switch ($action) {
case 'system-status':
if (!isAdmin()) jsonResponse(false, null, 'Forbidden');
require_once RAIJU_ADMIN_DIR . '/../includes/system-status.php';
jsonResponse(true, raijuSystemStatus((string)($_GET['scheme'] ?? '')));
break;
case 'security-check':
if (!isAdmin()) jsonResponse(false, null, 'Forbidden');
require_once RAIJU_ADMIN_DIR . '/../includes/security-check.php';
// The probe requests this site over HTTP for a few seconds at most:
// release the session lock so other dashboard requests keep working,
// and always remove the probe files, even if the browser disconnects.
session_write_close();
ignore_user_abort(true);
jsonResponse(true, raijuSecurityStatus((string)($_POST['scheme'] ?? ''), true, ($_POST['force'] ?? '') === '1'));
break;
case 'seo-check':
if (!isAdmin()) jsonResponse(false, null, 'Forbidden');
require_once RAIJU_ADMIN_DIR . '/../includes/seo-check.php';
// Three requests to the own site, a few seconds at most: do not hold the session lock meanwhile.
session_write_close();
ignore_user_abort(true);
jsonResponse(true, raijuSeoCheckStatus((string)($_POST['scheme'] ?? ''), true, ($_POST['force'] ?? '') === '1'));
break;
case 'keepalive':
if (!validateCsrfToken()) {
jsonResponse(false, null, 'Invalid CSRF token');
}
jsonResponse(true, ['time' => time()], 'Session refreshed');
break;
case 'dashboard-overview':
$analyticsPeriod = $_GET['analytics_period'] ?? 'days';
$analyticsCount = isset($_GET['analytics_count']) ? (int)$_GET['analytics_count'] : 30;
if (!in_array($analyticsPeriod, ['days', 'months', 'years'], true)) {
$analyticsPeriod = 'days';
}
if ($analyticsPeriod === 'months' && $analyticsCount <= 0) {
$analyticsCount = 12;
} elseif ($analyticsPeriod === 'years' && $analyticsCount < 0) {
$analyticsCount = 0;
} elseif ($analyticsPeriod === 'days' && $analyticsCount <= 0) {
$analyticsCount = 30;
}
$pagesPath = defined('CONTENT_PATH') ? CONTENT_PATH : dirname(RAIJU_ADMIN_DIR) . '/content/pages/';
$newsPath = dirname(RAIJU_ADMIN_DIR) . '/content/news/';
$pageCount = count(glob($pagesPath . '[a-z][a-z]_*.json') ?: []);
$newsCount = is_dir($newsPath) ? count(glob($newsPath . '*.json') ?: []) : 0;
$response = [
'pages' => $pageCount,
'news' => $newsCount,
'status' => dashboardStatusOverview($pagesPath, $newsPath),
'analytics' => raijuAnalyticsSummary($analyticsPeriod, $analyticsCount),
];
$settings = is_file(SETTINGS_PATH) ? json_decode(file_get_contents(SETTINGS_PATH), true) : [];
$response['analytics']['state'] = ($response['analytics']['state'] ?? '') === 'error' ? 'error'
: (!raijuAnalyticsEnabled() ? 'disabled' : (empty($response['analytics']['periodViews']) ? 'empty' : 'ready'));
if (dashboardAiModuleEnabled()) {
$aiSettings = raijuAiLoadSettings(true);
// Same shape as load-ai-settings, so the form rendered from the overview is complete
if (isAdmin() && function_exists('raijuAgentStatusForAdmin')) $aiSettings['agentStatus'] = raijuAgentStatusForAdmin();
$response['ai'] = [
'settings' => $aiSettings,
'usage' => raijuAiUsageSummary()
];
}
jsonResponse(true, $response);
break;
// ============================================================
// CONTENT MANAGEMENT
// ============================================================
}