prefer get parameter for apache redirects
[sc2-widget] / widget.php
index 736a7b1305b04eb8ee3f11bc4a409d44c572ca3f..c99611140d654aca1a66b0add7a697e63d0c7a9b 100644 (file)
@@ -17,15 +17,31 @@ function error($message, $status = 500)
        exit;
 }
 
-$request = trim($_SERVER['PATH_INFO'], '/');
-$target = "$request.json";
-if (!file_exists($target)) {
-       if (!is_numeric($request)) {
+$request = trim($_GET['clan'] ?? $_SERVER['PATH_INFO'], '/');
+$target = "data/$request.json";
+$last = file_exists($target) ? lstat($target)['mtime'] : 0;
+
+if ($last < time() - 3600) {
+       $recipe = NULL;
+       if (is_numeric($request)) {
+               $recipe = $request;
+       }
+       elseif ($last) {
+               if ($data = json_decode(file_get_contents($target), true)) {
+                       $profiles = array_column($data['members'], 'profileId');
+                       #TODO: reduce profiles by checking ladder presence
+                       $recipe = join(' ', array_merge([$request], $profiles));
+               }
+       }
+       else {
                error("Unknown profile request <q>$request</q>", 400);
        }
-       system("./getsc2clan $request | sponge $target", $exitcode);
-       if ($exitcode) {
-               error("No results for profile id $request from Blizzard", 503);
+
+       if ($recipe) {
+               system("./getsc2clan $recipe | sponge $target", $exitcode);
+               if ($exitcode and !$last) {
+                       error("No results for profile $request from Blizzard", 503);
+               }
        }
 }
 if (!filesize($target)) {