<?php
	// HTTP/1.1 403 Forbidden
	$errMsg403 = "Forbidden";
	$errDesc403 = "You don't have permission to access " . $_SERVER['REQUEST_URI'] . " on this server.";

	// HTTP/1.1 404 Not Found
	$errMsg404 = "Not Found";
	$errDesc404 = "The requested URL " . $_SERVER['REQUEST_URI'] . " was not found on this server.";

	// HTTP/1.1 500 Internal Server Error
	$errMsg500 = "Internal Server Error";
	$errDesc500 = "Internal Server Error. " . $_SERVER['REQUEST_URI'] + "";

	function echoError($errMsg, $errDesc) {
		echo "<html><head><title>${errMsg}</title></head><body><h1>${errMsg}</h1><p>${errDesc}</p></body></html>";
	}

	$accept_lang = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : "";
	$filename = "index_en.html";
	if (preg_match("/^ja.*/", $accept_lang)) {
		$filename = "index_ja.html";
	}
	if (!file_exists($filename)) {
		echoError($errMsg404, $errDesc404);
		header('HTTP/1.1 404 Not Found');
		exit;
	}
	if(!is_file($filename)) {
		echoError($errMsg404, $errDesc404);
		header('HTTP/1.1 404 Not Found');
		exit;
	}
	if(!is_readable($filename)) {
		echoError($errMsg403, $errDesc403);
		header('HTTP/1.1 403 Forbidden');
		exit;
	}

	$stat = @stat($filename);

	header("Content-Type: text/html");
	header("Date: " . gmdate("D, d M Y H:i:s") . " GMT");
	if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $stat['mtime']) {
		header('Last-Modified: ' . gmdate("D, d M Y H:i:s", $stat['mtime']) . " GMT");
		header('HTTP/1.1 304 Not Modified');
		exit;
	}
	header('Last-Modified: ' . gmdate("D, d M Y H:i:s", $stat['mtime']) . " GMT");
	header('Accept-Ranges: bytes');
	header('Content-Length:' . $stat['size']);
	// PHPのバージョン情報を渡さないように、
	// X-Powered-By: PHP/5.3.6と出ないようにする。
	header_remove('X-Powered-By');

	$lines = file($filename);
	if ($lines === false) {
		echoError($errMsg500, $errDesc500);
		header('HTTP/1.1 500 Internal Server Error');
		exit;
	} else {
		header('HTTP/1.1 200 OK');
		foreach ($lines as $line) {
			echo $line;
		}
		exit;
	}

?>


