<?php
	require("../__common__/__define_common.php");
	require("../__common__/include-common-all.php");
	require("../__common__/include-common-html.php");
	require("../account/__logincheck.php");
	if (isset($_POST['reset'])) {
		$url = '';
	} else {
		$url = $_POST['url'];
	}
//	if (isset($_POST['url'])) {
//		$url = $_POST['url'];
//		setcookie("html_text_mail_url", $url, time() + LOGIN_COOKIE_EXPIRE, "/");
//	} else {
//		$url = $_COOKIE['html_text_mail_url'];
//	}
?>
<!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">
<link rel="stylesheet" href="<?= _STYLE_SHEET_FOLDER ?>/tools_common.css?20120406" type="text/css">
<title>HTML テキスト抽出 & メール送信</title>
<style type="text/css">
body {
	background-color: #ffffff;
	margin: 10px;
	padding: 0px;
}
#title{
	font-size: 120%;
	font-weight: bold;
}
</style>
</head>
<body>
<p><?php
	page_header_return_index();
?>
<span id="tool_title">HTML テキスト抽出 & メール送信</span>
</p>
<form method="POST" action="<?= $_SERVER['SCRIPT_NAME'] ?>">
	URL：<input class="text" type="text" name="url" value="<?= $url ?>" size=80>
	<input type="submit" name="gethtml" value="テキスト抽出">
	<input type="submit" name="reset" value="リセット">
</form>
<p><a href="<?= $url ?>" target="_blank"><?= $url ?></a></p>
<?php
	if (isset($_POST['gethtml']) and $_POST['url'] <> '') {
		if (left($_POST['url'],7) <> 'http://') {
			error_exit('http://以外は取得できません。');
		}
		if (HTTP_PROXY_HOST.'' <> '') {
			$proxy = array(
				'http' => array(
					'proxy' => 'tcp://'.HTTP_PROXY_HOST.':'.HTTP_PROXY_PORT,
					'request_fulluri' => true,
				),
			);
			$proxy_context = stream_context_create($proxy);
			$html = @file_get_contents($_POST['url'], false, $proxy_context);
		} else 	{
			$html = @file_get_contents($_POST['url']);
		}
		if ($html) {
			$html = mb_convert_encoding($html, 'UTF-8', MB_CONVERT_ENCODING_AUTO);
			view_textarea(get_intag($html,'<title>','</title>',$pos=0), $_POST['url']."\n\n".my_strip_tags($html), $url);
		} else {
			error_exit('取得できませんでした。');
		}
	} elseif (isset($_POST['sendmail']) and $_POST['text'] <> '') {
		$title = form_str_filepath(mb_convert_kana($_POST['title'], 'KV', 'UTF-8'));
		$text = form_str_filepath(mb_convert_kana($_POST['text'], 'KV', 'UTF-8'));
		view_textarea($title, $text, $url);
		post_sendmail($title, $text);
	}
?>
<div id="footer" style="margin: 10px 0 0 0;">
<a href="http://ok2nd.web.fc2.com/" target="_blank" style="color:#b0b0ff;">Powered by ok.2nd</a>
</div>
</body>
</html>
<?php
function view_textarea($title, $text, $url='') {
//	echo '<p>'.str_replace(chr(10), '<br>', $text).'</p>';
	if (($line = substr_count($text, chr(10))) < 10) {
		$line = 10;
	}
?>
	<form name="form0" method="POST" action="<?= $_SERVER['SCRIPT_NAME'] ?>?#end">
	<input type="hidden" name="url" value="<?= $url ?>">
	件名：<input class="text" type="text" name="title" value="<?= my_htmlspecialchars($title) ?>" size=80><br>
	<script language="JavaScript">
	function textareaBigSmall(obj, bigSmall)
	{
		if (bigSmall == "大") {
			document.form0.elements[obj].rows = document.form0.elements[obj].rows + 10;
		} else {
			if (document.form0.elements[obj].rows > 15) {
				document.form0.elements[obj].rows = document.form0.elements[obj].rows - 10;
			}
		}
	}
	</script>
	<div class="block_left">
		<textarea id="text" name="text" style="width:560px;" rows="<?= $line+2 ?>"><?= $text ?></textarea>
	</div>
	<div class="block_left">
		<input type="button" value="小" OnClick="textareaBigSmall('text','小')"><br>
		<input type="button" value="大" OnClick="textareaBigSmall('text','大')">
	</div>
	<a name="end"></a>
	<div style="clear: both; ">
	<p><input type="submit" name="sendmail" value="メール送信"></p>
	</div>
	</form>
<?php
}
function my_strip_tags($str) {
	$str = str_ireplace('<br>', '--(br)--', $str);
	$str = str_ireplace('</td>', ' ', $str);
	$str = str_ireplace('</th>', ' ', $str);
	$str = str_ireplace('</li>', '--(br)--', $str);
	$str = str_ireplace('</p>', '--(br)--', $str);
	$str = str_ireplace('</div>', '--(br)--', $str);
	$str = str_ireplace('</tr>', '--(br)--', $str);
	$str = preg_replace('@<script[^>]*?>.*?</script>@si', '', $str);	// javascriptを削除
	$str = preg_replace('@<style[^>]*?>.*?</style>@si', '', $str);		// styleを削除
	$str = preg_replace("/\s\s+/", ' ', $str);				// 連続したスペースをスペースに
	$str = str_ireplace("\n", '', $str);					// 改行を除去
	$str = strip_tags($str);
	$search = array ('@&(quot|#34);@i',
			 '@&(amp|#38);@i',
			 '@&(lt|#60);@i',
			 '@&(gt|#62);@i',
			 '@&(nbsp|#160);@i',
			 '@&(iexcl|#161);@i',
			 '@&(cent|#162);@i',
			 '@&(pound|#163);@i',
			 '@&(copy|#169);@i');
	$replace = array ('"',
			  '&',
			  '<',
			  '>',
			  ' ',
			  chr(161),
			  chr(162),
			  chr(163),
			  chr(169));
	$str = preg_replace($search, $replace, $str);
	$str = preg_replace('/\s\s+/', ' ', $str);
	$str = str_ireplace('--(br)--', "\n", $str);
	$str = str_ireplace("\n ", chr(10), $str);
	$str = str_ireplace(" \n", chr(10), $str);
	$str = preg_replace("/\n\n+/", chr(10), $str);				// 連続した改行を1個に
	$str = trim($str);
	return $str;
}
function post_sendmail($subject, $body) {
	$con = my_mysql_connect(_DB_ACCOUNT_SCHEMA);
	$sql = "SELECT * FROM m_account WHERE id_account = '" . $_SESSION['login_id'] . "' and c_delete = 0";
	$rs_account = my_mysql_query($sql);
	$row = mysql_num_rows($rs_account);
	if ($row == 0) {
		error_exit('アカウントがみつかりません。');
	}
	$rec_account = mysql_fetch_array($rs_account);
	if (($to = $rec_account['c_email_calendar']) == '') {
		error_exit('電子メールアドレスが登録されていません。');
	}
	$con = mysql_close($con);
	$header_from = _SENDMAIL_EMAIL_NAME . ' <'._SENDMAIL_EMAIL_ADDR.'>';
	$ret = my_send_mail(_SENDMAIL_EMAIL_ADDR, $header_from, $to, $subject, $body, _SENDMAIL_HOST, _SENDMAIL_PORT, _SENDMAIL_AUTH_USE, _SENDMAIL_EMAIL_USER, _SENDMAIL_EMAIL_PASS);
	if ($ret) {
		echo '<p class="noramal_msg">メール送信しました。</p>';
	} else {
		error_exit('<p>メール送信でエラーが発生しました。</p>');
	}
}
?>
