Files
MoYuBan/api/stats.php
2025-11-19 09:31:41 +08:00

158 lines
6.4 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];
$total = ['visits' => 0, 'video_plays' => 0, 'picture_shows' => 0, 'uv' => 0, 'bandwidth_in' => 0, 'bandwidth_out' => 0];
foreach ($daily as $d) {
if (!is_array($d)) continue;
$total['visits'] += intval($d['visits'] ?? 0);
$total['video_plays'] += intval($d['video_plays'] ?? 0);
$total['picture_shows'] += intval($d['picture_shows'] ?? 0);
$total['bandwidth_in'] += intval($d['bandwidth_in'] ?? 0);
$total['bandwidth_out'] += intval($d['bandwidth_out'] ?? 0);
}
// 累计唯一访客:合并所有 uv/<date>.json 的 IP
$ips = [];
if (is_dir($uvRoot)) {
$list = @scandir($uvRoot) ?: [];
foreach ($list as $f) {
if ($f === '.' || $f === '..') continue;
$p = $uvRoot . DIRECTORY_SEPARATOR . $f;
if (!is_file($p)) continue;
$rawIps = @file_get_contents($p);
$decodedIps = json_decode($rawIps, true);
if (is_array($decodedIps)) {
foreach ($decodedIps as $ip) { if (is_string($ip)) { $ips[$ip] = true; } }
}
}
}
$total['uv'] = count($ips);
echo json_encode(['success' => true, 'today' => $todayData, 'total' => $total, '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);
// 计算累计总数并返回
$total = ['visits' => 0, 'video_plays' => 0, 'picture_shows' => 0, 'uv' => 0, 'bandwidth_in' => 0, 'bandwidth_out' => 0];
if (file_exists($dailyPath)) {
$rawDaily = @file_get_contents($dailyPath);
$decoded = json_decode($rawDaily, true);
if (is_array($decoded)) {
foreach ($decoded as $d) {
if (!is_array($d)) continue;
$total['visits'] += intval($d['visits'] ?? 0);
$total['video_plays'] += intval($d['video_plays'] ?? 0);
$total['picture_shows'] += intval($d['picture_shows'] ?? 0);
$total['bandwidth_in'] += intval($d['bandwidth_in'] ?? 0);
$total['bandwidth_out'] += intval($d['bandwidth_out'] ?? 0);
}
}
}
// 累计 UV
$ips = [];
if (is_dir($uvRoot)) {
$list = @scandir($uvRoot) ?: [];
foreach ($list as $f) {
if ($f === '.' || $f === '..') continue;
$p = $uvRoot . DIRECTORY_SEPARATOR . $f;
if (!is_file($p)) continue;
$rawIps = @file_get_contents($p);
$decodedIps = json_decode($rawIps, true);
if (is_array($decodedIps)) {
foreach ($decodedIps as $ip) { if (is_string($ip)) { $ips[$ip] = true; } }
}
}
}
$total['uv'] = count($ips);
echo json_encode(['success' => true, 'today' => $daily[$today], 'total' => $total], 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; }
}