html headers before wrapper errors
authorMischa POSLAWSKY <perl@shiar.org>
Wed, 22 May 2019 16:42:06 +0000 (18:42 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Wed, 22 May 2019 22:00:49 +0000 (00:00 +0200)
widget.php

index e5aa32617a8872cb5f7f091c6ad9b60ebc01ba92..eec063774ea0932f227c4d48f5f02f935a82a49d 100644 (file)
@@ -1,12 +1,30 @@
 <?php
+function error($message, $status = 500)
+{
+       http_response_code($status);
+       if ($page = @fopen('widget.html', 'r')) {
+               # copy static page contents until page body
+               while (!feof($page)) {
+                       print $line = fgets($page);
+                       if (preg_match('/<body\b/', $line)) break;
+               }
+       }
+       else {
+               print '<html><body>';
+       }
+       print "<p>$message</p>\n";
+       print "</body>\n</html>\n";
+       exit;
+}
+
 $request = trim($_SERVER['PATH_INFO'], '/');
 if (!file_exists("$request.json")) {
        if (!is_numeric($request)) {
-               die("unknown profile request $request");
+               error("Unknown profile request <q>$request</q>", 400);
        }
        system("./getsc2clan $request | sponge $request.json", $exitcode);
        if ($exitcode) {
-               die("no results for profile id $request from Blizzard");
+               error("No results for profile id $request from Blizzard", 503);
        }
 }
 require('widget.html');