<?php
	require("__include-common.php");
	require("../account/__logincheck.php");
	if ($_POST) {
		check_post_account($_POST['login_id'], $_POST['current_id']);
		post_done_proc();
	} else {
		html_header(HTML_TITLE, '', '', ' onload="document.form0.c_name.focus()"');
		page_header();
		input_form();
		page_footer();
		html_footer();
	}
	exit();
?>
<?php
function post_done_proc() {
	if ($_POST['user_id']."" <> "") {
		$user_id = $_POST['user_id'];
	} else {
		error_exit("不正アクセス：ユーザーIDなし", True);
	}
	if (check_permit_id($_SESSION['login_id'], $user_id) != "w") {
		error_exit("不正アクセス：書き込み権限がありません。", True);
	}
	if ($_POST['update_id']) {
		if (!is_numeric($_POST['update_id'])) {
			error_exit("不正アクセス", True);
		}
		$id = intval($_POST['update_id']);
	} else {
		$id = 0;
	}
	if (isset($_POST['削除'])) {
		//	if ($_POST['削除する'] <> "YES") {
		//		error_exit("削除するにチェックしてください。", True);
		//	}
	} elseif ($_POST['c_name'] == "") {
		error_exit("登録名なし", True);
	}
	$con = my_mysql_connect(_DB_SCHEMA);
	if ($id == 0) {
		$sql = "insert into m_email";
		$sql .= " (id_account";
		$sql .= ", id_category";
		$sql .= ", c_name";
		$sql .= ", c_displayColor";
		$sql .= ", c_username";
		$sql .= ", c_password";
		$sql .= ", c_host_pop3";
		$sql .= ", c_port_pop3";
		if ($_SESSION['current_id'] == $_SESSION['login_id']) {
			$sql .= ", c_privacy";
		}
		$sql .= ", c_registtime";
		$sql .= ", c_updatetime";
		$sql .= ") values ";
		$sql .= "( '".$user_id."'";
		$sql .= ", '".$_POST['id_category']."'";
		$sql .= ", '".post_to_mysql("c_name")."'";
		$sql .= ", '".post_to_mysql("c_displayColor")."'";
		$sql .= ", '".post_to_mysql("c_username")."'";
		$sql .= ", '".post_to_mysql("c_password")."'";
		$sql .= ", '".post_to_mysql("c_host_pop3")."'";
		$sql .= ", '".post_to_mysql("c_port_pop3")."'";
		if ($_SESSION['current_id'] == $_SESSION['login_id']) {
			if ($_POST['c_privacy'] == '444') {
				$sql .= ", 444";
			} else {
				$sql .= ", 0";
			}
		}
		$sql .= ", '". date("Y/m/d H:i:s") . "'";
		$sql .= ", '". date("Y/m/d H:i:s") . "'";
		$sql .= ")";
		$ret = my_mysql_query($sql, "登録できませんでした。");
	} elseif ($_POST['削除'] <> "") {
		$sql = "update m_email set";
		$sql .= " c_delete = 999";
		$sql .= ", c_updatetime = '". date("Y/m/d H:i:s") . "'";
		$sql .= " where id_email = ".$id;
		$sql .= " and id_account = ".$user_id;
		$ret = my_mysql_query($sql, "削除できませんでした。");
	} else {
		$sql = "update m_email set";
		$sql .= " id_category = '".$_POST['id_category']."'";
		$sql .= ", c_name = '".post_to_mysql("c_name")."'";
		$sql .= ", c_displayColor = '".post_to_mysql("c_displayColor")."'";
		$sql .= ", c_username = '".post_to_mysql("c_username")."'";
		$sql .= ", c_password = '".post_to_mysql("c_password")."'";
		$sql .= ", c_host_pop3 = '".post_to_mysql("c_host_pop3")."'";
		$sql .= ", c_port_pop3 = '".post_to_mysql("c_port_pop3")."'";
		if ($_SESSION['current_id'] == $_SESSION['login_id']) {
			if ($_POST['c_privacy'] == '444') {
				$sql .= ", c_privacy = 444";
			} else {
				$sql .= ", c_privacy = 0";
			}
		}
		$sql .= ", c_updatetime = '". date("Y/m/d H:i:s") . "'";
		$sql .= " where id_email = ".$id;
		$sql .= " and id_account = ".$user_id;
		$ret = my_mysql_query($sql, "更新できませんでした。");
	}
	$con = mysql_close($con);
	redirect("list.php?".$_SERVER['QUERY_STRING']);
}
function input_form() {
	if (check_permit_id($_SESSION['login_id'], $_SESSION['current_id']) != "w") {
		error_exit("書き込み権限がありません。", True);
	}
	if ($_GET['id'] <> "") {
		$id = $_GET['id'];
		if ($_GET['page'] <> "") {
			$page = $_GET['page'];
		}
	} else {
		$id = 0;
	}
	$con = my_mysql_connect(_DB_SCHEMA);
	$sql = "select * from m_email where id_email = ".$id." and c_delete = 0";
	$sql .= " and id_account = ".$_SESSION['current_id'];
	$rs = my_mysql_query($sql);
	$rec = mysql_fetch_array($rs);
?>
<div class="input_form">
<h3><?= $_SESSION['current_handle'] ?><a class="a_cancel_back" href='list.php?<?=$_SERVER['QUERY_STRING'] ?>'>戻る</a></h3>
<script>
function formCheck(form) {
	if (form.c_name.value == '') {
		window.alert('「登録名」を入れてください。');
		return false;		// 送信を中止
	}
	return true;	// 送信を実行
}
</script>
<script src="../scripts/valueconvertor.js"></script>
<script src="../scripts/jqKey.js"></script>
<script>
$(function() {
	$('table#form_table td').jqKey({
		Enter:true
	})
});
</script>
<table style="border-style:none;"><tr><td style="border-style:none;">
<form name="form0" method="POST" action="<?= $_SERVER['SCRIPT_NAME'] ?>?<?= $_SERVER['QUERY_STRING'] ?>" onSubmit="return formCheck(this)">
	<input type="hidden" name="user_id" value="<?= $_SESSION['current_id'] ?>">
	<input type="hidden" name="update_id" value="<?= $id ?>">
	<input type="hidden" name="login_id" value="<?= $_SESSION['login_id'] ?>">
	<input type="hidden" name="current_id" value="<?= $_SESSION['current_id'] ?>">
<table id="form_table">
<tr>
	<td nowrap>カテゴリ</td>
	<td nowrap>
<?php
	$sqlsel = "select * from m_category where id_account = '".$_SESSION['current_id']."'";
	$sqlsel = $sqlsel . " and c_delete = 0";
	$sqlsel = $sqlsel . " order by c_categoryDisplayOrder";
	$rs_sel = my_mysql_query($sqlsel);
?>
	<select name="id_category">
<?php
		if ($id == 0) {
			$category = (int)$_GET['cat'];
		} else {
			$category = $rec['id_category'];
		}
		while ($rec_sel=mysql_fetch_array($rs_sel)) {
?>
		<option value="<?= $rec_sel['id_category'] ?>"<?= $rec_sel['id_category'] == $category ? " selected" : "" ?>><?= my_htmlspecialchars($rec_sel['c_categoryName']) ?>
<?php
		}
?>
	</select>
	<?php if ($_SESSION['current_id'] == $_SESSION['login_id']) { ?>
		&nbsp;&nbsp;<label><input type="checkbox" name="c_privacy" value="444" <?= $rec['c_privacy'] == 444 ? ' checked' : '' ?>>非公開</label>
	<?php } ?>
	</td>
</tr>
<tr>
	<td>メールBOX名</td>
	<td>
		<input class="text" type="text" name="c_name" size=30 value="<?= my_htmlspecialchars($rec['c_name']) ?>" style="ime-mode: active;">
	</td>
</tr>
<tr>
	<td>枠カラー</td>
	<td>
		<input class="text" type="text" name="c_displayColor" size=20 value="<?= my_htmlspecialchars($rec['c_displayColor']) ?>" style="ime-mode: active;">
	</td>
</tr>
<tr>
	<td>POP3ホスト名</td>
	<td>
		<input class="text ascii" type="text" name="c_host_pop3" size=40 value="<?= my_htmlspecialchars($rec['c_host_pop3']) ?>" style="ime-mode: disabled;"><br>
		SSLを使う場合は、頭に「ssl://」を付けてください。(例：ssl://pop.gmail.com)
	</td>
</tr>
<tr>
	<td>POP3ポート番号</td>
	<td>
		<input class="text ascii" type="text" name="c_port_pop3" size=20 value="<?= my_htmlspecialchars($rec['c_port_pop3']) ?>" style="ime-mode: disabled;">
		例)  110, 995(SSLを使う場合)
	</td>
</tr>
<tr>
	<td>ユーザー名</td>
	<td>
		<input class="text ascii" type="text" name="c_username" size=30 value="<?= my_htmlspecialchars($rec['c_username']) ?>" style="ime-mode: disabled;"><br>
		Gmailの場合は、ユーザー名の頭に「recent:」を付けてください。
	</td>
</tr>
<tr>
	<td>パスワード</td>
	<td>
		<input class="text ascii" type="text" name="c_password" size=30 value="<?= my_htmlspecialchars($rec['c_password']) ?>" style="ime-mode: disabled;"><br>
		登録されたパスワードは、他のユーザーから閲覧される可能性があります。
	</td>
</tr>
</table>
<?php
	if ($id == 0) {
?>
	<input class="input_form_button" type="submit" name="登録" value="登録">
<?php
	} else {
?>
	<input class="input_form_button" type="submit" name="登録" value="修正">
	<input class="input_form_button" type="submit" name="削除" value="削除" onClick="return delete_check();" style="margin-left:20px;">
<?php
	}
?>
</form>
</td><td style="border-style:none;" id="color_chart">
<?
	require("../__common__/in-block-color-chart.php");
?>
</td></tr></table>
<style>
#color_chart table {
	border-style: none;
	background: #fff;
}
#color_chart td {
	border: solid 1px #ffffff;
	padding: 0;
	margin: 1px;
}
</style>
<script>
function delete_check() {
	if (window.confirm('このデータを削除しますか？')) {
		return true;
	} else {
		return false;
	}
}
</script>
<?
}
?>
