return not found response for missing requests
[sc2-widget] / widget.php
1 <?php
2 function error($message, $status = 500)
3 {
4         http_response_code($status);
5         if ($page = @fopen('widget.html', 'r')) {
6                 # copy static page contents until page body
7                 while (!feof($page)) {
8                         print $line = fgets($page);
9                         if (preg_match('/<body\b/', $line)) break;
10                 }
11         }
12         else {
13                 print '<html><body>';
14         }
15         print "<p>$message</p>\n";
16         print "</body>\n</html>\n";
17         exit;
18 }
19
20 $request = trim($_SERVER['PATH_INFO'], '/');
21 $target = "$request.json";
22 if (!file_exists($target)) {
23         if (!is_numeric($request)) {
24                 error("Unknown profile request <q>$request</q>", 400);
25         }
26         system("./getsc2clan $request | sponge $target", $exitcode);
27         if ($exitcode) {
28                 error("No results for profile id $request from Blizzard", 503);
29         }
30 }
31 if (!filesize($target)) {
32         error("No clan data for <q>$request</q>", 404);
33 }
34 require('widget.html');