You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
4.2 KiB
PHTML
99 lines
4.2 KiB
PHTML
|
2 months ago
|
<?php
|
||
|
|
|
||
|
|
namespace App\Controllers\Admin;
|
||
|
|
|
||
|
|
use App\Controllers\AdminController;
|
||
|
|
use App\Services\SiteAnalyticsService;
|
||
|
|
use App\Services\SiteSecurityEventService;
|
||
|
|
use App\Services\AdminDataMasker;
|
||
|
|
use App\Services\AdminPermission;
|
||
|
|
|
||
|
|
class SiteAnalyticsController extends AdminController
|
||
|
|
{
|
||
|
|
public function index($request, $response, $args)
|
||
|
|
{
|
||
|
|
$tableError = '';
|
||
|
|
try {
|
||
|
|
if (AdminPermission::isSuperAdmin()) {
|
||
|
|
SiteAnalyticsService::ensureTables();
|
||
|
|
} elseif (!SiteAnalyticsService::tablesAvailable()) {
|
||
|
|
throw new \RuntimeException('网站统计数据表尚未创建');
|
||
|
|
}
|
||
|
|
} catch (\Throwable $exception) {
|
||
|
|
$tableError = AdminDataMasker::sensitiveText('日志数据库连接或统计表不可用:' . $exception->getMessage());
|
||
|
|
}
|
||
|
|
|
||
|
|
list($from, $to) = SiteAnalyticsService::normalizeDateRange('', '');
|
||
|
|
return $this->view()
|
||
|
|
->assign('site_analytics_from', $from)
|
||
|
|
->assign('site_analytics_to', $to)
|
||
|
|
->assign('site_analytics_table_error', $tableError)
|
||
|
|
->display('admin/site_analytics/index.tpl');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function data($request, $response, $args)
|
||
|
|
{
|
||
|
|
$query = $request->getQueryParams();
|
||
|
|
try {
|
||
|
|
list($from, $to) = SiteAnalyticsService::normalizeDateRange(
|
||
|
|
isset($query['from']) ? $query['from'] : '',
|
||
|
|
isset($query['to']) ? $query['to'] : ''
|
||
|
|
);
|
||
|
|
$report = SiteAnalyticsService::report($from, $to, 80, AdminPermission::isSuperAdmin());
|
||
|
|
if (isset($report['rankings']) && is_array($report['rankings'])) {
|
||
|
|
foreach ($report['rankings'] as $dimension => &$rows) {
|
||
|
|
if (!is_array($rows)) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
foreach ($rows as &$row) {
|
||
|
|
if (!is_array($row) || !isset($row['value'])) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
$row['value'] = $dimension === 'ip_addresses'
|
||
|
|
? AdminDataMasker::ip($row['value'])
|
||
|
|
: AdminDataMasker::sensitiveText($row['value']);
|
||
|
|
}
|
||
|
|
unset($row);
|
||
|
|
}
|
||
|
|
unset($rows);
|
||
|
|
}
|
||
|
|
$report = AdminDataMasker::sanitizeArray($report);
|
||
|
|
return $this->json($response, 1, '获取成功', ['data' => $report]);
|
||
|
|
} catch (\Throwable $exception) {
|
||
|
|
return $this->json($response->withStatus(503), 0, AdminDataMasker::sensitiveText('统计数据暂不可用:' . $exception->getMessage()));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function security($request, $response, $args)
|
||
|
|
{
|
||
|
|
$query = $request->getQueryParams();
|
||
|
|
try {
|
||
|
|
$report = SiteSecurityEventService::report(
|
||
|
|
isset($query['from']) ? $query['from'] : '',
|
||
|
|
isset($query['to']) ? $query['to'] : '',
|
||
|
|
[
|
||
|
|
'risk' => isset($query['risk']) ? $query['risk'] : '',
|
||
|
|
'status' => isset($query['status']) ? $query['status'] : 0,
|
||
|
|
'actor' => isset($query['actor']) ? $query['actor'] : '',
|
||
|
|
'keyword' => isset($query['keyword']) ? $query['keyword'] : '',
|
||
|
|
],
|
||
|
|
isset($query['page']) ? $query['page'] : 1,
|
||
|
|
50,
|
||
|
|
AdminPermission::isSuperAdmin()
|
||
|
|
);
|
||
|
|
$report = AdminDataMasker::sanitizeArray($report);
|
||
|
|
return $this->json($response, 1, '获取成功', ['data' => $report]);
|
||
|
|
} catch (\Throwable $exception) {
|
||
|
|
return $this->json($response->withStatus(503), 0, AdminDataMasker::sensitiveText('异常访问数据暂不可用:' . $exception->getMessage()));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private function json($response, $ret, $msg, array $extra = [])
|
||
|
|
{
|
||
|
|
$payload = array_merge(['ret' => (int) $ret, 'msg' => (string) $msg], $extra);
|
||
|
|
$response = $response->withHeader('Content-Type', 'application/json; charset=UTF-8');
|
||
|
|
$response->getBody()->write(json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
|
||
|
|
return $response;
|
||
|
|
}
|
||
|
|
}
|