<?php
	require("../__common__/__define_common.php");
	require("../__common__/include-common-all.php");
	if (!defined("LOGIN_COOKIE_EXPIRE")) {
		define("LOGIN_COOKIE_EXPIRE", (3600 * 24 * 365));	// ログイン setcookie 保存時間
	}
	if ($_GET['fs'] <> '') {
		$fontsize = $_GET['fs'];
	} elseif ($_POST['fontsize_sel'] <> '') {
		$fontsize = $_POST['fontsize_sel'];
	} else {
		$fontsize = '18pt';
	}
	if ($_GET['lh'] <> '') {
		$lineheight = $_GET['lh'];
	} elseif ($_POST['lineheight_sel'] <> '') {
		$lineheight = $_POST['lineheight_sel'];
	} else {
		$lineheight = '1.2';
	}
	if ($_GET['h'] <> '') {
		$height = $_GET['h'];
	} elseif ($_POST['height_sel'] <> '') {
		$height = $_POST['height_sel'];
	} else {
		$height = '100%';
	}
	if ($_GET['ft'] <> '') {
		$fonttype = $_GET['ft'];
	} elseif ($_POST['fonttype_sel'] <> '') {
		$fonttype = $_POST['fonttype_sel'];
	} else {
		$fonttype = 'm';
	}
	if ($fonttype == 'g') {
		$fontfamily = 'ＭＳ ゴシック';
	} else {
		$fontfamily = 'ＭＳ 明朝';
	}
	if ($_GET['fw'] <> '') {
		$fontweight = $_GET['fw'];
	} elseif ($_POST['fontweight_sel'] <> '') {
		$fontweight = $_POST['fontweight_sel'];
	} else {
		$fontweight = 'normal';
	}
	setcookie('tategaki_fontsize', $fontsize, time() + LOGIN_COOKIE_EXPIRE, '/');
	setcookie('tategaki_lineheight', $lineheight, time() + LOGIN_COOKIE_EXPIRE, '/');
	setcookie('tategaki_height', $height, time() + LOGIN_COOKIE_EXPIRE, '/');
	setcookie('tategaki_fonttype', $fonttype, time() + LOGIN_COOKIE_EXPIRE, '/');
	setcookie('tategaki_fontweight', $fontweight, time() + LOGIN_COOKIE_EXPIRE, '/');
	if ($_POST) {
		if ($_POST['url'].'' <> '') {
			$contents = mb_convert_encoding(my_file_get_contents($_POST['url']), 'UTF-8', 'SJIS-win, UTF-8, JIS, EUCJP-win, SJIS-win');
		} else {
			$contents = $_POST['text'];
		}
	} else {
		$contents = mb_convert_encoding(my_file_get_contents($_GET['url']), 'UTF-8', 'SJIS-win, UTF-8, JIS, EUCJP-win, SJIS-win');
	}
	$contents = preg_replace('/<div.*?>/i', '', $contents);
	$contents = str_ireplace('</div>', '', $contents);
	$contents = str_replace("\n", '<br>', str_replace("\r", '', $contents));
	preg_match('/<body.*?>(.*?)<\/body>/i', $contents, $body);
	if ($body[1].'' <> '') {
		$contents = $body[1];
	}
	if ($_POST['title'] <> '') {
		$title = $_POST['title'];
	} else {
		if (preg_match('/<h1.*?>(.*?)<\/h1>/i', $contents, $h1)) {
			$title = $h1[1];
		} elseif (preg_match('/<h2.*?>(.*?)<\/h2>/i', $contents, $h2)) {
			$title = $h2[1];
		} else {
			$title = '縦書き';
		}
	}
	$contents = str_ireplace("<br>", "\n", str_ireplace("<br />", "\n", $contents));
	$contents = preg_replace("/\\n+/", "\n", $contents);		// 連続した改行を1個にする
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta http-equiv="Content-Language" content="ja">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<meta name="author" content="ok.2nd">
<title><?= $title ?></title>
<style type="text/css">
h1 { font-size: 140%; font-weight: bold; margin: 0; padding: 0; }
h2 { font-size: 130%; font-weight: bold; margin: 0; padding: 0; }
h3 { font-size: 120%; font-weight: bold; margin: 0; padding: 0; }
h4 { font-size: 110%; font-weight: bold; margin: 0; padding: 0; }
.vertical {
	height: <?= $height ?>px;
	writing-mode: tb-rl;	/* テキストを縦書きにする */
}
body {
	margin: 0px;
	padding: 20px 40px;
	font-size: <?= $fontsize ?>;
	line-height: <?= $lineheight ?>;
	font-weight: <?= $fontweight ?>;
	font-family: "<?= $fontfamily ?>",arial,helvetica,clean,sans-serif;
}
</style>
</head>
<body>
<div class="vertical">
<?= str_replace("\n", '<br>', $contents) ?>
</div>
</body>
</html>
