<?php
	require("../__common__/__define_common.php");
	require("../__common__/include-common-all.php");
	require("__define_code-file.php");
	require("__include-common-code-file.php");
	my_session_start();
	require("../account/__logincheck.php");
	if ($_SESSION['システム管理者'] <> "YES") {
		exit;
	}
	if (FILE_MANAGER_ID_PASSWORD_USE == 'YES') {
		require("../id-manager/__include-im-login.php");
		require("../id-manager/im-logincheck.php");
	}
	if ($_POST['保存して戻る']) {
		post_done_proc();
		exit();
	}
	if ($_POST['保存のみ']) {
		post_save_proc();
	}
	if ($_GET['path'].'' == '') {
		exit;
	}
	$open_path = my_GET('path');
	$file_mode = file_check_get_text($open_path, $path_SJIS, $source_text, $source_encode, $file_type, $error_msg);
	if ($file_mode == 'Error') {
		error_simple_html('ソースコード編集エラー', $error_msg);
	}
	if ($file_mode <> 'File') {
		exit;
	}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="author" content="ok.2nd">
<meta name="description" content="PHP/JavaScriptソースコードエディタ">
<meta name="keywords" content="ソースコード編集,ソースコード整形表示,syntax,highlight,css">
<style>
body, div, img, ul, ol, li, p, th, td, form, pre, dl, dt, dd, fieldset, blockquote, caption, h1, h2, h3, h4, h5, h6 {
	margin: 0;
	padding: 0;
}
body {margin: 10px; }
input, select, textarea { margin: 0; }
img { border: 0; }
body {
	background-color: #ffffff;
	text-align: left;
	font-family: "MS PGothic","Osaka",Arial,sans-serif;
}
body, input, button, textarea {
	font-size: 12px;
	line-height: 1.2;
}
button, input[type="button"], input[type="submit"], input[type="reset"] {
	overflow: visible;
	margin: 1px 0;
	padding: 1px 3px;
	padding: 0 2px\9;	/* IE8以下 */
}
body:not(:target) {
	padding: 0 2px\9;	/* IE9 */
}
body {
	margin: 5px;
}
h2 { font-size: 120%; }
a { text-decoration: underline; font-weight: normal; }
a:link { color: #0000ff; }
a:visited { color: #0000ff; }
a:hover { color: #ff0000; }
a:active { color: #ff0000; }
.error_msg { color: red; margin: 20px; padding: 0; }
div#header {
	margin: 10px;
}
div#body {
	margin: 0px;
}
div#footer {
	margin: 10px;
}
p#file_name_box {
	margin: 2px 0 0 0;
}
span#file_name {
	text-decoration: underline;
	background-color: #e0f0ff;
	border: 0px;
	padding: 1px 4px 1px 4px;
}
span#file_encode {
	background-color: #fff0e0;
	border: 0px;
	padding: 1px 4px 1px 4px;
}
</style>
<!--
Powered by http://www.cdolivet.net/editarea/
-->
<script src="../scripts/jquery.js"></script>
<script src="../scripts/jquery.cookie.js"></script>
<script src="../scripts/ok2nd.js"></script>
<script src="scripts/editarea/edit_area/edit_area_full.js"></script>
<script>
editAreaLoader.init({
	id: "src_code"
	,start_highlight: true
	,allow_resize: "both"
	,allow_toggle: true
	,language: "ja"
	,font_size: 9
	,syntax: "<?= file_type_ea_syntax($file_type) ?>"
	,toolbar: "fullscreen, |, search, go_to_line, |, undo, redo, |, select_font, |, syntax_selection, |, change_smooth_selection, highlight, reset_highlight, |, help"
	,syntax_selection_allow: "html,php,python,js,css,sql,xml"
});
</script>
<script>
function submitCheck() {
	if (window.confirm('保存しますか？')) {
		return true;
	} else {
		//window.alert('キャンセルされました。');
		return false;
	}
}
</script>
<title>PHP/JavaScript ソースコード編集</title>
</head>
<body>
<div id="body">
	<form name="edit" method="POST" action="<?= $_SERVER['SCRIPT_NAME'] ?>?<?= $_SERVER['QUERY_STRING'] ?>" onSubmit="return submitCheck(this)">
	<p id="file_name_box">ファイル：<span id="file_name"><?= filename_bold_in_path($open_path) ?></span>
	&nbsp;文字コード：<span id="file_encode"><?= $source_encode ?></span></p>
	<input class="input_form_button" type="submit" name="保存して戻る" value="　保存して戻る　">&nbsp;&nbsp;
	<input class="input_form_button" type="submit" name="保存のみ" value="　保存のみ　">&nbsp;&nbsp;
	<?php
		if ($_GET['ret'] == 'up') {
			$qry = 'path='.urlencode(up_folder_path(my_GET('path')));
		} else {
			$qry = $_SERVER['QUERY_STRING'];
		}
	?>
	<input type="button" name="" value="保存しないで戻る" onClick="location.href='code-file-view.php?<?= $qry ?>'">
	<input type="hidden" name="file_name" value="<?= $open_path ?>">
	<input type="hidden" name="source_encode" value="<?= $source_encode ?>">
	<fieldset>
	<textarea id="src_code" style="height: <?= CODE_FILE_VIEW_EDIT_TEXTAREA_HEIGHT ?>; width: 100%;" name="src_text"><?= my_htmlspecialchars_notrim($source_text) ?></textarea>
	</fieldset>
	<input class="input_form_button" type="submit" name="保存して戻る" value="　保存して戻る　">&nbsp;&nbsp;
	<input class="input_form_button" type="submit" name="保存のみ" value="　保存のみ　">&nbsp;&nbsp;
</form>
</div>
<div id="footer">
<a href="http://www.cdolivet.net/editarea/" target="_blank" style="color:#b0b0ff;">Powered by EditArea</a><br />
<a href="http://ok2nd.web.fc2.com/" target="_blank" style="color:#b0b0ff;">Powered by ok.2nd</a>
</div>
</body>
</html>
<?php
function post_done_proc() {
	if ($_POST['source_encode'] == 'UTF-8') {
		file_put_contents(myfile_ENCODE($_POST['file_name']),
				stripcslashes($_POST['src_text']));
	} elseif ($_POST['source_encode'] == 'SJIS-win') {
		file_put_contents(myfile_ENCODE($_POST['file_name']),
				myfile_ENCODE(stripcslashes($_POST['src_text'])));
	}
	if ($_GET['ret'] == 'up') {
		$qry = 'path='.urlencode(up_folder_path(my_GET('path')));
	} else {
		$qry = $_SERVER['QUERY_STRING'];
	}
	header("Location: code-file-view.php?".$qry);
}
function post_save_proc() {
	if ($_POST['source_encode'] == 'UTF-8') {
		file_put_contents(myfile_ENCODE($_POST['file_name']),
				stripcslashes($_POST['src_text']));
	} elseif ($_POST['source_encode'] == 'SJIS-win') {
		file_put_contents(myfile_ENCODE($_POST['file_name']),
				myfile_ENCODE(stripcslashes($_POST['src_text'])));
	}
}
?>
