hardcode profile ids for inno clan
[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 = "data/$request.json";
22 $last = file_exists($target) ? filemtime($target) : 0;
23
24 if ($last < time() - 3600) {
25         $recipe = NULL;
26         if (is_numeric($request)) {
27                 $recipe = $request;
28         }
29         elseif ($request == 'inno') {
30                 #TODO: get ids from existing data
31                 $recipe = "$request 2138280 6531490";
32         }
33         elseif (!$last) {
34                 error("Unknown profile request <q>$request</q>", 400);
35         }
36
37         if ($recipe) {
38                 system("./getsc2clan $recipe | sponge $target", $exitcode);
39                 if ($exitcode and !$last) {
40                         error("No results for profile $request from Blizzard", 503);
41                 }
42         }
43 }
44 if (!filesize($target)) {
45         error("No clan data for <q>$request</q>", 404);
46 }
47 require('widget.html');