-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathindex.php
More file actions
84 lines (70 loc) · 2.86 KB
/
index.php
File metadata and controls
84 lines (70 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
declare(strict_types=1);
// load functions
require_once "../vendor/autoload.php";
require_once "stats.php";
require_once "card.php";
require_once "cache.php";
// load .env
$dotenv = \Dotenv\Dotenv::createImmutable(dirname(__DIR__, 1));
$dotenv->safeLoad();
// if environment variables are not loaded, display error
if (!isset($_SERVER["TOKEN"])) {
$message = file_exists(dirname(__DIR__ . "../.env", 1))
? "Missing token in config. Check Contributing.md for details."
: ".env was not found. Check Contributing.md for details.";
renderOutput($message, 500);
}
// set cache to refresh once per day (24 hours)
$cacheSeconds = CACHE_DURATION;
header("Expires: " . gmdate("D, d M Y H:i:s", time() + $cacheSeconds) . " GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: public, max-age=$cacheSeconds");
// redirect to demo site if user is not given
if (!isset($_REQUEST["user"])) {
header("Location: demo/");
exit();
}
try {
// get streak stats for user given in query string
$user = preg_replace("/[^a-zA-Z0-9\-]/", "", $_REQUEST["user"]);
$startingYear = isset($_REQUEST["starting_year"]) ? intval($_REQUEST["starting_year"]) : null;
$mode = isset($_GET["mode"]) ? $_GET["mode"] : null;
$excludeDaysRaw = $_GET["exclude_days"] ?? "";
// Build cache options based on request parameters
$cacheOptions = [
"starting_year" => $startingYear,
"mode" => $mode,
"exclude_days" => $excludeDaysRaw,
];
// Check if cache is disabled
$useCache = !isset($_SERVER["DISABLE_CACHE"]) || strtolower($_SERVER["DISABLE_CACHE"]) !== "true";
// Check for cached stats first (24 hour cache) unless cache is disabled
$cachedStats = $useCache ? getCachedStats($user, $cacheOptions) : null;
if ($cachedStats !== null) {
// Use cached stats - instant response!
$stats = $cachedStats;
} else {
// Fetch fresh data from GitHub API
$contributionGraphs = getContributionGraphs($user, $startingYear);
$contributions = getContributionDates($contributionGraphs);
if ($mode === "weekly") {
$stats = getWeeklyContributionStats($contributions);
} else {
// split and normalize excluded days
$excludeDays = normalizeDays(explode(",", $excludeDaysRaw));
$stats = getContributionStats($contributions, $excludeDays);
}
// Cache the stats for 24 hours unless cache is disabled
if ($useCache) {
setCachedStats($user, $cacheOptions, $stats);
}
}
renderOutput($stats);
} catch (InvalidArgumentException | AssertionError $error) {
error_log("Error {$error->getCode()}: {$error->getMessage()}");
if ($error->getCode() >= 500) {
error_log($error->getTraceAsString());
}
renderOutput($error->getMessage(), $error->getCode());
}