<?php
	require("__include-common.php");
	require("../account/__logincheck.php");
//	require("../account/__logincheck_write_permit.php");
	require("__include-im-login.php");
	require("im-logincheck.php");

	if ($_POST) {
		check_post_account($_POST['login_id']);
		post_done_proc();
	} else {
		html_header(HTML_TITLE, '', '#ffffff', ' onload="document.form0.c_subject.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);
		}
	}
	if ($_POST['c_subject'] == "") {
		error_exit("件名なし", True);
	}
	$con = my_mysql_connect(_DB_SCHEMA);
	$updateD = date("Y/m/d H:i:s");
	for ($ix=1; $ix<=6; $ix++) {
		if ($_POST['c_val'.$ix] <> "") {
			//$c_val[$ix] = str2ang($_POST['c_val'.$ix], $updateD);
			$c_val[$ix] = str2ang(form_str_adjust($_POST['c_val'.$ix]), $updateD);
		} else {
			$c_val[$ix] = "";
		}
	}
	if ($id == 0) {
		$sql = "insert into m_pass (c_subject, c_body, id_category, c_homepageUrl, id_account";
		$sql .= ", c_item1, c_item2, c_item3, c_item4, c_item5, c_item6";
		if ($_SESSION['current_id'] == $_SESSION['login_id']) {
			$sql .= ", c_privacy";
		}
		$sql .= ", c_updatetime, c_registtime)";
		$sql .= " values (";
		$sql .= "'" . post_to_mysql("c_subject") . "'";
		$sql .= ", '" . post_to_mysql("c_body") . "'";
		$sql .= ", '" . post_to_mysql("id_category") . "'";
		$sql .= ", '" . post_to_mysql_strip_tags("c_homepageUrl") . "'";
		$sql .= ", '" . $user_id . "'";
		$sql .= ", '" . post_to_mysql("c_item1") . "'";
		$sql .= ", '" . post_to_mysql("c_item2") . "'";
		$sql .= ", '" . post_to_mysql("c_item3") . "'";
		$sql .= ", '" . post_to_mysql("c_item4") . "'";
		$sql .= ", '" . post_to_mysql("c_item5") . "'";
		$sql .= ", '" . post_to_mysql("c_item6") . "'";
		if ($_SESSION['current_id'] == $_SESSION['login_id']) {
			if ($_POST['c_privacy'] == '444') {
				$sql .= ", 444";
			} else {
				$sql .= ", 0";
			}
		}
		$sql .= ", '" . $updateD . "'";
		$sql .= ", '" . $updateD . "'";
		$sql .= ")";
		$ret = my_mysql_query($sql, "追加 失敗。(1)");
		$id = mysql_insert_id($con);	//自動採番ID
		$con = mysql_close($con);

		$con2 = my_mysql_connect(_DB_SCHEMA_2);
		$sql = "insert into m_pass2 (id_pass, c_val1, c_val2, c_val3, c_val4, c_val5, c_val6)";
		$sql .= " values (";
		$sql .= $id;
		$sql .= ", '" . str_for_mysql($c_val[1]) . "'";
		$sql .= ", '" . str_for_mysql($c_val[2]) . "'";
		$sql .= ", '" . str_for_mysql($c_val[3]) . "'";
		$sql .= ", '" . str_for_mysql($c_val[4]) . "'";
		$sql .= ", '" . str_for_mysql($c_val[5]) . "'";
		$sql .= ", '" . str_for_mysql($c_val[6]) . "'";
		$sql .= ")";
		$ret = my_mysql_query($sql, "追加 失敗。(2)");
		$con2 = mysql_close($con2);
	} elseif ($_POST['削除'] <> "") {
		$sql = "update m_pass set c_delete = 999";
		$sql .= ", c_updatetime = '". $updateD . "'";
		$sql .= " where id_pass = " . $id;
		$sql .= " and id_account = '" . $user_id . "'";
		$ret = my_mysql_query($sql, "削除 失敗。");
		$con = mysql_close($con);
	} else {
		$sql = "update m_pass set";
		$sql .= " c_subject = '" . post_to_mysql("c_subject") . "'";
		$sql .= ", c_body = '" . post_to_mysql("c_body") . "'";
		$sql .= ", id_category = '" . post_to_mysql("id_category") . "'";
		$sql .= ", c_homepageUrl = '" . post_to_mysql_strip_tags("c_homepageUrl") . "'";
		$sql .= ", c_item1 = '" . post_to_mysql("c_item1") . "'";
		$sql .= ", c_item2 = '" . post_to_mysql("c_item2") . "'";
		$sql .= ", c_item3 = '" . post_to_mysql("c_item3") . "'";
		$sql .= ", c_item4 = '" . post_to_mysql("c_item4") . "'";
		$sql .= ", c_item5 = '" . post_to_mysql("c_item5") . "'";
		$sql .= ", c_item6 = '" . post_to_mysql("c_item6") . "'";
		if ($_SESSION['current_id'] == $_SESSION['login_id']) {
			if ($_POST['c_privacy'] == '444') {
				$sql .= ", c_privacy = 444";
			} else {
				$sql .= ", c_privacy = 0";
			}
		}
		$sql .= ", c_updatetime = '". $updateD . "'";
		$sql .= " where id_pass = " . $id;
		$sql .= " and id_account = '" . $user_id . "'";
		$ret = my_mysql_query($sql, "更新 失敗。(1)");
		$con = mysql_close($con);

		$con2 = my_mysql_connect(_DB_SCHEMA_2);
		$sql = "update m_pass2 set";
		$sql .= " c_val1 = '" . str_for_mysql($c_val[1]) . "'";
		$sql .= ", c_val2 = '" . str_for_mysql($c_val[2]) . "'";
		$sql .= ", c_val3 = '" . str_for_mysql($c_val[3]) . "'";
		$sql .= ", c_val4 = '" . str_for_mysql($c_val[4]) . "'";
		$sql .= ", c_val5 = '" . str_for_mysql($c_val[5]) . "'";
		$sql .= ", c_val6 = '" . str_for_mysql($c_val[6]) . "'";
		$sql .= " where id_pass = " . $id;
		$ret = my_mysql_query($sql, "更新 失敗。(2)");
		$con2 = mysql_close($con2);
	}

	if ($_POST['削除'] <> "") {
		redirect("list.php");
	} elseif ($id == 0) {
		redirect("list.php?" . $_SERVER['QUERY_STRING']);
	} else {
		redirect("list.php?" . $_SERVER['QUERY_STRING']);
	}
	exit;
}
function input_form() {
	if ($_GET['id'] <> "") {
		$id = $_GET['id'];
		if ($_GET['page'] <> "") {
			$page = $_GET['page'];
		}
	} else {
		$id = 0;
	}
	$con = my_mysql_connect(_DB_SCHEMA);
	$sql = "select * from v_pass where id_pass = ".$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='javascript:history.back();'>戻る</a></h3>
<script language="JavaScript">
<!--
function formCheck(form) {
	if (form.c_subject.value == '') {
		window.alert('件名を入れてください。');
		return false;		// 送信を中止
	}
	return true;	// 送信を実行
}
-->
</script>
<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="page" value="<?= $page ?>">
	<input type="hidden" name="login_id" value="<?= $_SESSION['login_id'] ?>">
	<input type="hidden" name="current_id" value="<?= $_SESSION['current_id'] ?>">
<script language="JavaScript">
<!--
function textareaBigSmall(obj, bigSmall)
{
	if (bigSmall == "大") {
		document.form0.elements[obj].rows = document.form0.elements[obj].rows + 5;
	} else {
		if (document.form0.elements[obj].rows > 5) {
			document.form0.elements[obj].rows = document.form0.elements[obj].rows - 5;
		}
	}
}
-->
</script>
<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>
	</td>
</tr>
<tr>
	<td nowrap>件名</td>
	<td nowrap>
		<input class="text" type="text" name="c_subject" value="<?= my_htmlspecialchars($rec['c_subject']) ?>" size=80 style="ime-mode: active;">
		<?php if ($_SESSION['current_id'] == $_SESSION['login_id']) { ?>
			&nbsp;&nbsp;<input type="checkbox" name="c_privacy" value="444" <?= $rec['c_privacy'] == 444 ? ' checked' : '' ?>>非公開
		<?php } ?>
		<br>
	</td>
</tr>
<tr>
	<td nowrap>URL</td>
	<td nowrap>
		<input class="text" type="text" name="c_homepageUrl" value="<?= my_htmlspecialchars($rec['c_homepageUrl']) ?>" size=100 style="ime-mode: inactive;"><br>
	</td>
</tr>
<tr>
	<td nowrap>項目</td>
	<td nowrap>
<?php
	if ($id == 0) {
		$rec['c_item1'] = "ID";
		$rec['c_item2'] = "PW";
	}
	for ($ix=1; $ix<=6; $ix++) {
?>
		(<?= $ix ?>)&nbsp;<input class="text" type="text" name="c_item<?= $ix ?>" value="<?= my_htmlspecialchars($rec['c_item'.$ix]) ?>" size=30 style="ime-mode: active;">
			<?php $val = ang2str($rec['c_val'.$ix], $rec['c_updatetime']); ?>
		<input class="text" type="text" name="c_val<?= $ix ?>" value="<?= $val ?>" size=50 style="ime-mode: inactive;"><br>
<?php
	}
?>
	</td>
</tr>
<tr>
	<td nowrap>備考</td>
	<td nowrap>
		<div class="block">
		<div class="block_left">
		<textarea name="c_body" cols=80 rows=10 wrap="soft"><?= my_htmlspecialchars($rec['c_body']) ?></textarea>
		</div>
		<div class="block_left">
		<input type="button" value="小" OnClick="textareaBigSmall('c_body','小')"><br>
		<input type="button" value="大" OnClick="textareaBigSmall('c_body','大')">
		</div>
		</div>
	</td>
	</tr>
</table>
	<input class="input_form_button" type="submit" name="登録" value="登録"></font>&nbsp;&nbsp;
<?php
	if ($id <> 0) {
?>
	<input type="checkbox" name="削除する" value="YES">削除する
	<input class="input_form_button" type="submit" name="削除" value="削除">
<?php
	}
?>
	</form>
</div>
<?
}
?>
