OSDN Git Service

[add]ダンプ表示用コード
authorHabu <habu@users.sourceforge.jp>
Thu, 15 Mar 2018 00:57:03 +0000 (09:57 +0900)
committerHabu <habu@users.sourceforge.jp>
Thu, 15 Mar 2018 01:38:21 +0000 (10:38 +0900)
ファイルとして保存されているキャラクタダンプとスクリーンショットの表示

dump_file.inc [new file with mode: 0644]
show_dump.php [new file with mode: 0644]
show_screen.php [new file with mode: 0644]

diff --git a/dump_file.inc b/dump_file.inc
new file mode 100644 (file)
index 0000000..3881839
--- /dev/null
@@ -0,0 +1,47 @@
+<?php
+class DumpFile
+{
+    public function __construct($score_id) {
+        $this->score_id = $score_id;
+    }
+
+    public function show($dir, $ext, $content_type)
+    {
+        $dirname = sprintf("%s/%d", $dir, floor($this->score_id / 1000) * 1000);
+        $filename = sprintf("%s/%d.%s.gz", $dirname, $this->score_id, $ext);
+
+        if (!file_exists($filename)) {
+            http_response_code(404);
+            return;
+        }
+
+        $content_encoding = self::get_content_encoding();
+
+        header("Content-Type: ".$content_type);
+        if ($content_encoding !== FALSE) {
+            header("Content-Encoding: ".$content_encoding);
+            readfile($filename);
+        } else {
+            readgzfile($filename);
+        }
+    }
+
+    private static function browser_accept_encodings()
+    {
+        if (!isset($_SERVER["HTTP_ACCEPT_ENCODING"])) return [];
+
+        return array_map('trim', explode(",", $_SERVER["HTTP_ACCEPT_ENCODING"]));
+    }
+
+
+    private static function get_content_encoding()
+    {
+        $supported_gzip_encodings = array_intersect(
+            self::browser_accept_encodings(),
+            ['gzip', 'x-gzip']);
+
+        return count($supported_gzip_encodings) > 0 ?
+            $supported_gzip_encodings[0] : FALSE;
+    }
+
+}
diff --git a/show_dump.php b/show_dump.php
new file mode 100644 (file)
index 0000000..4a49b01
--- /dev/null
@@ -0,0 +1,10 @@
+<?php
+//ini_set('display_errors', 'On');
+
+ini_set('log_errors', 'On');
+ini_set('error_log', 'errors/'.pathinfo(__FILE__, PATHINFO_FILENAME).'.log');
+
+include "dump_file.inc";
+
+$dump_file = new DumpFile($_GET['score_id']);
+$dump_file->show('dumps', 'txt', 'text/plain; charset=UTF-8');
diff --git a/show_screen.php b/show_screen.php
new file mode 100644 (file)
index 0000000..c35da69
--- /dev/null
@@ -0,0 +1,10 @@
+<?php
+//ini_set('display_errors', 'On');
+
+ini_set('log_errors', 'On');
+ini_set('error_log', 'errors/'.pathinfo(__FILE__, PATHINFO_FILENAME).'.log');
+
+include "dump_file.inc";
+
+$dump_file = new DumpFile($_GET['score_id']);
+$dump_file->show('screens', 'html', 'text/html; charset=UTF-8');