X-Git-Url: http://git.sourceforge.jp/view?p=hengband%2Fweb.git;a=blobdiff_plain;f=dump_file.inc;h=e0a85f8c61989ddff407b33442fec61c4d024f89;hp=3881839d6de1235e1950c1c17ebbb13590311d08;hb=cad7b585c34bce34621affec52efd48d2c56e001;hpb=8ecfa11e6d22e40b9a675adf35e0e5706fdfb512 diff --git a/dump_file.inc b/dump_file.inc index 3881839..e0a85f8 100644 --- a/dump_file.inc +++ b/dump_file.inc @@ -5,32 +5,116 @@ class DumpFile $this->score_id = $score_id; } - public function show($dir, $ext, $content_type) + + public function get_filename($dir, $ext) { $dirname = sprintf("%s/%d", $dir, floor($this->score_id / 1000) * 1000); - $filename = sprintf("%s/%d.%s.gz", $dirname, $this->score_id, $ext); + return sprintf("%s/%d.%s.gz", $dirname, $this->score_id, $ext); + } + + + public function show($dir, $ext, $content_type) + { + $filename = $this->get_filename($dir, $ext); if (!file_exists($filename)) { http_response_code(404); return; } - $content_encoding = self::get_content_encoding(); + $contents = file_get_contents($filename); + $etag = md5($contents); + if ($etag === filter_input(INPUT_SERVER, 'HTTP_IF_NONE_MATCH')) { + http_response_code(304); + return; + } + + $content_encoding = self::get_content_encoding(); + header("Etag: ".$etag); header("Content-Type: ".$content_type); + if ($content_encoding !== FALSE) { header("Content-Encoding: ".$content_encoding); - readfile($filename); + echo $contents; } else { - readgzfile($filename); + echo gzdecode($contents); + } + } + + + public function save($dir, $ext, $contents) + { + if ($outputs === FALSE) return; + + umask(2); // Group書き込み権限許可 + + $filename = $this->get_filename($dir, $ext); + $dirname = dirname($filename); + + if (!file_exists($dirname)) { + mkdir($dirname, 02775, TRUE); + } + + $zp = gzopen($filename, "w9"); + + foreach ($contents as $line) { + gzwrite($zp, $line); + gzwrite($zp, "\n"); } + + gzclose($zp); + } + + + public function exists($dir, $ext) + { + $dirname = sprintf("%s/%d", $dir, floor($this->score_id / 1000) * 1000); + $filename = sprintf("%s/%d.%s.gz", $dirname, $this->score_id, $ext); + + return file_exists($filename); + } + + + /** + * キャラクタダンプの死ぬ直前のメッセージもしくは勝利メッセージを取得する + * + * @return array 死ぬ直前のメッセージもしくは勝利メッセージを1行1要素にした文字列の配列 + */ + public function get_last_message() + { + $zp = gzopen($this->get_filename('dumps', 'txt'), 'r'); + if ($zp === FALSE) return []; + + $in_message = FALSE; + $result = []; + while (!gzeof($zp)) { + $line = gzgets($zp); + + if (preg_match('/^\s*\[(.*)\]\s*$/u', $line, $matches)) { + if ($matches[1] == '*勝利*メッセージ' || + $matches[1] == '死ぬ直前のメッセージ') { + $in_message = TRUE; + } + else if ($in_message) { + break; + } + } + + if ($in_message) { + $result[] = rtrim($line, "\n"); + } + } + + return $result; } private static function browser_accept_encodings() { - if (!isset($_SERVER["HTTP_ACCEPT_ENCODING"])) return []; + $accept_encoding = filter_input(INPUT_SERVER, 'HTTP_ACCEPT_ENCODING'); + if ($accept_encoding == NULL) return []; - return array_map('trim', explode(",", $_SERVER["HTTP_ACCEPT_ENCODING"])); + return array_map('trim', explode(",", $accept_encoding)); }