OSDN Git Service

e0a85f8c61989ddff407b33442fec61c4d024f89
[hengband/web.git] / score / dump_file.inc
1 <?php
2 class DumpFile
3 {
4     public function __construct($score_id) {
5         $this->score_id = $score_id;
6     }
7
8
9     public function get_filename($dir, $ext)
10     {
11         $dirname = sprintf("%s/%d", $dir, floor($this->score_id / 1000) * 1000);
12         return sprintf("%s/%d.%s.gz", $dirname, $this->score_id, $ext);
13     }
14
15
16     public function show($dir, $ext, $content_type)
17     {
18         $filename = $this->get_filename($dir, $ext);
19
20         if (!file_exists($filename)) {
21             http_response_code(404);
22             return;
23         }
24
25         $contents = file_get_contents($filename);
26         $etag = md5($contents);
27
28         if ($etag === filter_input(INPUT_SERVER, 'HTTP_IF_NONE_MATCH')) {
29             http_response_code(304);
30             return;
31         }
32
33         $content_encoding = self::get_content_encoding();
34         header("Etag: ".$etag);
35         header("Content-Type: ".$content_type);
36
37         if ($content_encoding !== FALSE) {
38             header("Content-Encoding: ".$content_encoding);
39             echo $contents;
40         } else {
41             echo gzdecode($contents);
42         }
43     }
44
45
46     public function save($dir, $ext, $contents)
47     {
48         if ($outputs === FALSE) return;
49
50         umask(2); // Group書き込み権限許可
51
52         $filename = $this->get_filename($dir, $ext);
53         $dirname = dirname($filename);
54
55         if (!file_exists($dirname)) {
56             mkdir($dirname, 02775, TRUE);
57         }
58
59         $zp = gzopen($filename, "w9");
60
61         foreach ($contents as $line) {
62             gzwrite($zp, $line);
63             gzwrite($zp, "\n");
64         }
65
66         gzclose($zp);
67     }
68
69
70     public function exists($dir, $ext)
71     {
72         $dirname = sprintf("%s/%d", $dir, floor($this->score_id / 1000) * 1000);
73         $filename = sprintf("%s/%d.%s.gz", $dirname, $this->score_id, $ext);
74
75         return file_exists($filename);
76     }
77
78
79     /**
80      * キャラクタダンプの死ぬ直前のメッセージもしくは勝利メッセージを取得する
81      *
82      * @return array 死ぬ直前のメッセージもしくは勝利メッセージを1行1要素にした文字列の配列
83      */
84     public function get_last_message()
85     {
86         $zp = gzopen($this->get_filename('dumps', 'txt'), 'r');
87         if ($zp === FALSE) return [];
88
89         $in_message = FALSE;
90         $result = [];
91         while (!gzeof($zp)) {
92             $line = gzgets($zp);
93
94             if (preg_match('/^\s*\[(.*)\]\s*$/u', $line, $matches)) {
95                 if ($matches[1] == '*勝利*メッセージ' ||
96                     $matches[1] == '死ぬ直前のメッセージ') {
97                     $in_message = TRUE;
98                 }
99                 else if ($in_message) {
100                     break;
101                 }
102             }
103
104             if ($in_message) {
105                 $result[] = rtrim($line, "\n");
106             }
107         }
108
109         return $result;
110     }
111
112     private static function browser_accept_encodings()
113     {
114         $accept_encoding = filter_input(INPUT_SERVER, 'HTTP_ACCEPT_ENCODING');
115         if ($accept_encoding == NULL) return [];
116
117         return array_map('trim', explode(",", $accept_encoding));
118     }
119
120
121     private static function get_content_encoding()
122     {
123         $supported_gzip_encodings = array_intersect(
124             self::browser_accept_encodings(),
125             ['gzip', 'x-gzip']);
126
127         return count($supported_gzip_encodings) > 0 ?
128             $supported_gzip_encodings[0] : FALSE;
129     }
130
131 }