Files
MoYuBan/api/stats.php

100 lines
4.0 KiB
PHP

<?php
header('Content-Type: application/json; charset=utf-8');
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
$statsRoot = __DIR__ . '/../stats';
if (!is_dir($statsRoot)) { @mkdir($statsRoot, 0777, true); }
$eventsRoot = $statsRoot . '/events';
if (!is_dir($eventsRoot)) { @mkdir($eventsRoot, 0777, true); }
$uvRoot = $statsRoot . '/uv';
if (!is_dir($uvRoot)) { @mkdir($uvRoot, 0777, true); }
$dailyPath = $statsRoot . '/daily.json';
$today = (new DateTime('now'))->format('Y-m-d');
if ($method === 'GET') {
$daily = [];
if (file_exists($dailyPath)) {
$rawDaily = @file_get_contents($dailyPath);
$decoded = json_decode($rawDaily, true);
if (is_array($decoded)) { $daily = $decoded; }
}
$keys = array_keys($daily);
sort($keys);
$last7 = array_slice($keys, max(0, count($keys) - 7));
$out = [];
foreach ($last7 as $k) { $out[$k] = $daily[$k]; }
$todayData = $daily[$today] ?? ['visits' => 0, 'video_plays' => 0, 'picture_shows' => 0, 'uv' => 0, 'bandwidth_in' => 0, 'bandwidth_out' => 0];
echo json_encode(['success' => true, 'today' => $todayData, 'daily' => $out], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
exit;
}
$raw = file_get_contents('php://input');
$data = json_decode($raw, true);
$event = isset($data['event']) ? trim((string)$data['event']) : '';
$page = isset($data['page']) ? trim((string)$data['page']) : '';
$url = isset($data['url']) ? trim((string)$data['url']) : '';
$title = isset($data['title']) ? trim((string)$data['title']) : '';
$allowed = ['visit','video_play','picture_show','bandwidth'];
if (!in_array($event, $allowed, true)) {
echo json_encode(['success' => false, 'message' => 'invalid event']);
exit;
}
$daily = [];
if (file_exists($dailyPath)) {
$rawDaily = @file_get_contents($dailyPath);
$decoded = json_decode($rawDaily, true);
if (is_array($decoded)) { $daily = $decoded; }
}
if (!isset($daily[$today]) || !is_array($daily[$today])) {
$daily[$today] = ['visits' => 0, 'video_plays' => 0, 'picture_shows' => 0, 'uv' => 0, 'bandwidth_in' => 0, 'bandwidth_out' => 0];
}
if ($event === 'visit') { $daily[$today]['visits']++; }
if ($event === 'video_play') { $daily[$today]['video_plays']++; }
if ($event === 'picture_show') { $daily[$today]['picture_shows']++; }
if ($event === 'visit') {
$uvPath = $uvRoot . '/' . $today . '.json';
$ips = [];
if (file_exists($uvPath)) {
$rawIps = @file_get_contents($uvPath);
$decodedIps = json_decode($rawIps, true);
if (is_array($decodedIps)) { $ips = $decodedIps; }
}
$ip = $_SERVER['REMOTE_ADDR'] ?? '';
if ($ip !== '') {
if (!in_array($ip, $ips, true)) { $ips[] = $ip; }
}
@file_put_contents($uvPath, json_encode($ips, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), LOCK_EX);
$daily[$today]['uv'] = count($ips);
}
@file_put_contents($dailyPath, json_encode($daily, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT), LOCK_EX);
$eventsPath = $eventsRoot . '/' . $today . '.json';
$events = [];
if (file_exists($eventsPath)) {
$rawEvents = @file_get_contents($eventsPath);
$decoded = json_decode($rawEvents, true);
if (is_array($decoded)) { $events = $decoded; }
}
$events[] = [
'type' => $event,
'page' => $page,
'url' => $url,
'title' => $title,
'direction' => $direction ?? '',
'bytes' => $bytes ?? 0,
'ip' => $_SERVER['REMOTE_ADDR'] ?? '',
'ua' => $_SERVER['HTTP_USER_AGENT'] ?? '',
'ts' => (new DateTime('now'))->format(DateTime::ATOM),
];
@file_put_contents($eventsPath, json_encode($events, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT), LOCK_EX);
echo json_encode(['success' => true, 'today' => $daily[$today]], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$direction = isset($data['direction']) ? (string)$data['direction'] : '';
$bytes = isset($data['bytes']) ? intval($data['bytes']) : 0;
if ($event === 'bandwidth' && $bytes > 0) {
if ($direction === 'in') { $daily[$today]['bandwidth_in'] += $bytes; }
else if ($direction === 'out') { $daily[$today]['bandwidth_out'] += $bytes; }
}