2 function error($message, $status = 500)
4 http_response_code($status);
5 if ($page = @fopen('widget.html', 'r')) {
6 # copy static page contents until page body
8 print $line = fgets($page);
9 if (preg_match('/<body\b/', $line)) break;
15 print "<p>$message</p>\n";
16 print "</body>\n</html>\n";
20 $request = trim($_SERVER['PATH_INFO'], '/');
21 $target = "data/$request.json";
22 $last = file_exists($target) ? filemtime($target) : 0;
24 if ($last < time() - 3600) {
26 if (is_numeric($request)) {
30 if ($data = json_decode(file_get_contents($target), true)) {
31 $profiles = array_column($data['members'], 'profileId');
32 #TODO: reduce profiles by checking ladder presence
33 $recipe = join(' ', array_merge([$request], $profiles));
37 error("Unknown profile request <q>$request</q>", 400);
41 system("./getsc2clan $recipe | sponge $target", $exitcode);
42 if ($exitcode and !$last) {
43 error("No results for profile $request from Blizzard", 503);
47 if (!filesize($target)) {
48 error("No clan data for <q>$request</q>", 404);
50 require('widget.html');