toDateString(); if ($data = Cache::get($key)) { return $data; } return Cache::lock('activity:v1:refresh', 20)->block(2, function () use ($key) { if ($data = Cache::get($key)) { return $data; } if (Cache::has('activity:v1:failed')) { throw new RuntimeException('Unavailable'); } try { $token = ActivityCredential::find('gitea-parsa')?->token; if (!$token) { throw new RuntimeException('Unavailable'); } // Fixed upstream and identity; no user-controlled URLs, paths or query strings. $response = Http::withToken($token, 'token')->acceptJson()->connectTimeout(3)->timeout(10) ->withOptions(['allow_redirects' => false])->get('https://git.parsaaghayi.ir/api/v1/users/parsa/heatmap'); if (!$response->successful() || strlen($response->body()) > 2000000) { throw new RuntimeException('Unavailable'); } $data = $this->aggregate($response->json(), CarbonImmutable::now('Asia/Tehran')); Cache::put($key, $data, 300); return $data; } catch (\Throwable $e) { // Never propagate/log upstream exceptions: request context can contain credentials. Cache::put('activity:v1:failed', true, 30); throw new RuntimeException('Activity unavailable'); } }); } public function aggregate(mixed $rows, CarbonImmutable $now): array { if (!is_array($rows) || !array_is_list($rows) || count($rows) > 40000) { throw new RuntimeException('Invalid source'); } $today = $now->setTimezone('Asia/Tehran')->startOfDay(); $start = $today->subDays(364); $counts = []; $seen = []; foreach ($rows as $row) { if (!is_array($row) || !isset($row['timestamp'], $row['contributions']) || !is_int($row['timestamp']) || !is_int($row['contributions']) || $row['timestamp'] < 0 || $row['contributions'] < 0 || $row['contributions'] > 1000000 || $row['timestamp'] > $now->timestamp + 900) { throw new RuntimeException('Invalid source'); } $ts = $row['timestamp']; $n = $row['contributions']; if (isset($seen[$ts])) { if ($seen[$ts] !== $n) { throw new RuntimeException('Conflicting bucket'); } continue; } $seen[$ts] = $n; $date = CarbonImmutable::createFromTimestamp($ts, 'Asia/Tehran')->toDateString(); if ($date >= $start->toDateString() && $date <= $today->toDateString()) { $counts[$date] = ($counts[$date] ?? 0) + $n; } } $days = []; $months = []; $active = 0; $total = 0; $longest = 0; $run = 0; for ($day = $start; $day <= $today; $day = $day->addDay()) { $date = $day->toDateString(); $n = $counts[$date] ?? 0; $days[] = ['date' => $date, 'count' => $n]; $month = $day->format('Y-m'); $months[$month] = ($months[$month] ?? 0) + $n; $total += $n; $active += (int) ($n > 0); $run = $n > 0 ? $run + 1 : 0; $longest = max($longest, $run); } return ['timezone' => 'Asia/Tehran', 'from' => $start->toDateString(), 'to' => $today->toDateString(), 'updated_at' => $now->toIso8601String(), 'days' => $days, 'stats' => ['total' => $total, 'active_days' => $active, 'daily_average' => round($total / 365, 2), 'longest_streak' => $longest], 'months' => array_map(fn ($m, $n) => ['month' => $m, 'count' => $n], array_keys($months), array_values($months))]; } }