diff --git a/about.html b/about.html index b40233f..7d52b3d 100644 --- a/about.html +++ b/about.html @@ -23,7 +23,7 @@ - + + diff --git a/stats.php b/stats.php new file mode 100644 index 0000000..578ac4a --- /dev/null +++ b/stats.php @@ -0,0 +1,238 @@ + $data) { + foreach (array_keys($data['profiles'] ?? []) as $p) { + $all_profiles[$p] = true; + } +} +// Sort: preferred order first, then any extras alphabetically +$ordered = []; +foreach ($profile_order as $p) { + if (isset($all_profiles[$p])) $ordered[] = $p; +} +foreach (array_keys($all_profiles) as $p) { + if (!in_array($p, $ordered, true)) $ordered[] = $p; +} + +$profile_labels = [ + 'basic' => 'Basic', + 'home' => 'Home', + 'vehicle' => 'Vehicle', + 'farming' => 'Farming', + 'outdoors' => 'Places', + 'alltemps' => 'Temps', + 'showall' => 'Show All', + 'custom' => 'Custom', +]; + +$total_visits = array_sum(array_column($tracking, 'visits')); +$profile_totals = []; +foreach ($ordered as $p) { + $profile_totals[$p] = array_sum(array_map(fn($d) => $d['profiles'][$p] ?? 0, $tracking)); +} +?> + + + + + + SunScope — Stats + + + + + + +
+ +
Usage stats
+
+ +
+
+
Total visits
+
+
+
+
Days tracked
+
+
+ +
+
clicks
+
+
+ +
+ + +
No tracking data yet.
+ +
+ + + + + + + + + + + + $data): ?> + + + + + + + + + + + + + + + + + + +
DateVisits
Total
+
+ + +
← Back to SunScope
+ + + diff --git a/track.php b/track.php new file mode 100644 index 0000000..eee8d57 --- /dev/null +++ b/track.php @@ -0,0 +1,72 @@ + 'Method not allowed']); + exit; +} + +$body = json_decode(file_get_contents('php://input'), true); +$event = $body['event'] ?? ''; +$profile = $body['profile'] ?? ''; + +if (!in_array($event, ['visit', 'profile'], true)) { + http_response_code(400); + echo json_encode(['error' => 'Invalid event']); + exit; +} + +if ($event === 'profile' && $profile === '') { + http_response_code(400); + echo json_encode(['error' => 'Missing profile']); + exit; +} + +$data_dir = __DIR__ . '/data'; +$data_file = $data_dir . '/tracking.json'; + +if (!is_dir($data_dir)) { + mkdir($data_dir, 0755, true); + // Block direct web access to the data folder + file_put_contents($data_dir . '/.htaccess', "Deny from all\n"); +} + +$fp = fopen($data_file, 'c+'); +if (!$fp) { + http_response_code(500); + echo json_encode(['error' => 'Could not open tracking file']); + exit; +} + +flock($fp, LOCK_EX); + +$size = fstat($fp)['size']; +$tracking = []; +if ($size > 0) { + $tracking = json_decode(fread($fp, $size), true) ?? []; +} + +$day = date('Y-m-d'); +if (!isset($tracking[$day])) { + $tracking[$day] = ['visits' => 0, 'profiles' => []]; +} + +if ($event === 'visit') { + $tracking[$day]['visits']++; +} else { + $tracking[$day]['profiles'][$profile] = ($tracking[$day]['profiles'][$profile] ?? 0) + 1; +} + +ftruncate($fp, 0); +rewind($fp); +fwrite($fp, json_encode($tracking, JSON_PRETTY_PRINT)); + +flock($fp, LOCK_UN); +fclose($fp); + +echo json_encode(['ok' => true]);