<?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 (!defined("CODE_FILE_VIEW_EDIT_DEFAULT_FOLDER")) {
		define("CODE_FILE_VIEW_EDIT_DEFAULT_FOLDER", $_SERVER['DOCUMENT_ROOT']);
	}
	if ($_POST['path'].'' <> '') {
		$open_path = form_str_filepath($_POST['path']);
	} elseif ($_GET['path'].'' <> '') {
		$open_path = my_GET('path');
	}
	$open_path = str_replace(DIRECTORY_SEPARATOR, "/", right_slash_strip($open_path));
	if ($open_path == "") {
		$open_path = CODE_FILE_VIEW_EDIT_DEFAULT_FOLDER;
	}
	if (isset($_POST['search_key']) && $_POST['search_key'].'' <> '') {
		$_SESSION['code_view_search_key'] = $_POST['search_key'];
	} elseif ($_GET['key'].'' <> '') {
		$_SESSION['code_view_search_key'] = $_GET['key'];
	}
	if (isset($_POST['search_reset'])) {
		$_SESSION['code_view_search_key'] = '';
	}
	if ($_GET['uplow'].'' <> '') {
		$_SESSION['code_view_search_uplow'] = $_GET['uplow'];
	}
	if ($_GET['sort'].'' <> '') {
		$_SESSION['code_view_sort'] = $_GET['sort'];
	}
	if ($_GET['order'].'' <> '') {
		$_SESSION['code_view_sort_order'] = $_GET['order'];
	}
	$file_mode = file_check_get_text($open_path, $path_SJIS, $source_text, $source_encode, $file_type, $error_msg);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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">
<meta name="description" content="PHP/JavaScriptソースコード表示＆編集">
<meta name="keywords" content="ソースコード整形表示,ソースコード編集,syntax,highlight,css">
<link rel="stylesheet" href="<?= _STYLE_SHEET_FOLDER ?>/tools_common.css?20100318" type="text/css">
<style type="text/css">
<!--
div#header {
	margin: 5px;
}
div#body {
	margin: 0px;
}
div#footer {
	margin: 5px;
}
div#print_dir {
	margin: 5px;
}
div#print_file {
	margin: 5px;
}
div#change_file_type {
	margin: 5px;
}
#selected_type {
	background-color: #ffc080;
}
.file_list {
	font-family: arial,sans-serif;
}
.file_list a:link { color: #101010; }
.file_list a:visited { color: #101010; }
.file_list a:hover { color: #ff0000; }
.file_list a:active { color: #ff0000; }
ul.file_list {
	margin: 10px 0 5px 10px;
}
td.file_list {
	padding-left: 10px;
}
td.file_size {
	padding: 1px 0 1px 10px;
	text-align: right;
}
td.file_time {
	padding: 1px 0 1px 10px;
}
th.file_list {
	padding-left: 34px;
	background-color: #e0e0ff;
}
th.file_size {
	padding: 1px 0 1px 10px;
	text-align: right;
	color: #101010;
	background-color: #e0e0ff;
}
th.file_time {
	padding: 1px 0 1px 10px;
	background-color: #e0e0ff;
}
th a:link { color: #101010; font-weight: bold;}
th a:visited { color: #101010; font-weight: bold;}
th a:hover { color: #ff0000; font-weight: bold;}
th a:active { color: #ff0000; font-weight: bold;}
a.edit_btn {
	margin: 0 2px 0 2px;
}
a.edit_btn:link  { color: #ff8000; font-weight: normal;}
a.edit_btn:visited { color: #ff8000; font-weight: normal;}
a.edit_btn:hover { color: #ff0000; font-weight: normal; background-color: #f0f0f0}
a.edit_btn:active { color: #ff0000; font-weight: normal; background-color: #e0e0e0}
.order_mark {
	font-size: 80%;
	color: #808080;
	padding-left: 2px;
}
.file_folder {
	background-image: url(icon/folder.gif);
	background-repeat: no-repeat;
	background-position: top left;
	list-style: none;
	list-style-type: none;
	margin: 2px 0 2px 0;
	padding-left: 24px;
}
.file_php {
	background-image: url(icon/php.png);
	background-repeat: no-repeat;
	background-position: top left;
	padding-left: 24px;
}
.file_js {
	background-image: url(icon/js.gif);
	background-repeat: no-repeat;
	background-position: top left;
	padding-left: 24px;
}
.file_html {
	background-image: url(icon/html.png);
	background-repeat: no-repeat;
	background-position: top left;
	padding-left: 24px;
}
.file_css {
	background-image: url(icon/css.gif);
	background-repeat: no-repeat;
	background-position: top left;
	padding-left: 24px;
}
.file_txt {
	background-image: url(icon/txt.gif);
	background-repeat: no-repeat;
	background-position: top left;
	padding-left: 24px;
}
.file_xml{
	background-image: url(icon/txt.gif);
	background-repeat: no-repeat;
	background-position: top left;
	padding-left: 24px;
}
.file_sql {
	background-image: url(icon/sql.gif);
	background-repeat: no-repeat;
	background-position: top left;
	padding-left: 24px;
}
.key_found {
	color: red;
	font-weight: bold;
}
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>
	<?php if ($file_mode == 'File') {	// **************** ?>
<style type="text/css">
<!--
.dp-highlighter .my_search_key_class {
	color: red;
	font-weight: bold;
	background-color: #ffff00;
}
-->
</style>
<!--
Powered by http://code.google.com/p/syntaxhighlighter/
Powered by http://bizcaz.com/archives/2007/04/11-220828.php
Powered by http://winofsql.jp/sh/html/syntax_highlighter.htm
-->
<link rel="stylesheet" href="scripts/dp.SyntaxHighlighter.winofsql/SyntaxHighlighter.css" type="text/css" />
<script type="text/javascript" src="scripts/dp.SyntaxHighlighter.winofsql/shCore.js"></script>
<script type="text/javascript" src="scripts/dp.SyntaxHighlighter/Scripts/shBrushXml.js"></script>
<script type="text/javascript" src="scripts/dp.SyntaxHighlighter/Scripts/shBrushCss.js"></script>
<script type="text/javascript" src="scripts/dp.SyntaxHighlighter/Scripts/shBrushPhp.js"></script>
<script type="text/javascript" src="scripts/dp.SyntaxHighlighter/Scripts/shBrushJScript.js"></script>
<script type="text/javascript" src="scripts/dp.SyntaxHighlighter/Scripts/shBrushSql.js"></script>
<script type="text/javascript" src="scripts/dp.SyntaxHighlighter/Scripts/webscript.js"></script>
<script type="text/javascript">
<!--
//	↓ Google Chromeでハングする。
//	window.onload = function() {
//	mykeyword = '<?= $_SESSION['code_view_search_key'] ?>!my_search_key_class!'
//	dp.SyntaxHighlighter.HighlightAll('src_code',true,false,null,null,null,mykeyword);

	window.onload = function() {
	dp.SyntaxHighlighter.HighlightAll('src_code', true, false);
}
-->
</script>
<script type="text/javascript">
<!--
function changeFileType_XXX(fileType) {
	// この方法ではハイライトコードがどんどん追記されてしまう。
	document.getElementById('code_view').className = fileType;
	dp.SyntaxHighlighter.HighlightAll('src_code', true, false);
}
-->
</script>
<script language="javascript" src="../scripts/encodeURL.js" type="text/javascript"></script>
<script type="text/javascript">
<!--
function changeFileType(fileType) {
	//url = "<?= $_SERVER['SCRIPT_NAME'] ?>?path=" + encodeURL(document.form0.path.value) + "&type=" + fileType;
	url = "<?= $_SERVER['SCRIPT_NAME'] ?>?path=" + encodeURL(document.getElementById('path').value) + "&type=" + fileType + "&key=<?= urlencode($_SESSION['code_view_search_key']) ?>";
	top.location.href = url;
}
-->
</script>
	<?php }	else {			// **************** ?>
<script language="javascript" src="../scripts/URLencode.js" type="text/javascript"></script>
	<?php }				// **************** ?>
<title>PHP/JavaScript ソースコード表示</title>
</head>
<body>
<a href="../tools/" class="return_index">■</a>
<span id="tool_title">PHP/JavaScript ソースコード表示</span>
<span style="margin-left:10px; color:Hotpink">ファイルは、php関連のテキストファイルのみを表示します。</span>
<div id="header">
	<script language="JavaScript">
	<!--
	function CheckboxUpLow(onOff)
	{
		if (onOff == 'on') {
			window.location.href = '<?= $_SERVER['SCRIPT_NAME'] ?>?uplow=off&path=<?= urlencode($open_path) ?>';
		} else {
			window.location.href = '<?= $_SERVER['SCRIPT_NAME'] ?>?uplow=on&path=<?= urlencode($open_path) ?>';
		}
	}
	-->
	</script>
	<form name="form0" action="<?= $_SERVER['SCRIPT_NAME'] ?>?type=<?= $_GET['type'] ?>" method="post">
		<input type="button" name="" value="↑" onClick="location.href='<?= $_SERVER['SCRIPT_NAME'] ?>?path=<?= urlencode(up_folder_path($open_path)) ?>'">
		<input class="text" type="text" id="path" name="path" value="<?= my_htmlspecialchars($open_path) ?>" size=46>
		<input type="submit" name="path_change" value="変更">
	<?php if ($file_mode == 'Folder') { ?>
		ファイル内文字：
		<input class="text<?= $_SESSION['code_view_search_key'] <> '' ? ' list_filter_true' : '' ?>" type="text" name="search_key" value="<?= my_htmlspecialchars($_SESSION['code_view_search_key']) ?>" size=10>
		<input type="submit" name="search" value="検索">
		<input type="submit" name="search_reset" value="リセット">
		<input type="checkbox" value="on" onClick="CheckboxUpLow('<?= $_SESSION['code_view_search_uplow'] ?>')"<?= $_SESSION['code_view_search_uplow'] == 'on' ? ' checked' : '' ?>>大/小文字区別
		<br>
		<p id="file_name_box">フォルダ：<span><?= up_folder_direct('?path=', $open_path) ?></span>
	<?php } else { ?>
		<br>
		<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>
	<?php } ?>
	</form>
</div>
<div id="body">
<?php
	if ($file_mode == 'Folder') {
		view_folder($open_path, $path_SJIS, $_SESSION['code_view_search_key']);
	} elseif ($file_mode == 'File') {
		view_code($open_path, $source_text, $file_type);
	} else {
		echo '<p class="error_msg">'.$error_msg.'</p>';
	}
?>
</div>
<div id="footer">
	<?php if ($file_mode == 'File') {	// **************** ?>
<a href="http://code.google.com/p/syntaxhighlighter/" target="_blank" style="color:#b0b0ff;">Powered by SyntaxHighlighter</a><br/>
	<?php }				// **************** ?>
<a href="http://ok2nd.web.fc2.com/" target="_blank" style="color:#b0b0ff;">Powered by ok.2nd</a>
</div>
</body>
</html>
<?php
function view_folder($open_path, $path_SJIS, $search_key) {
	$prt_dir = array();
	$prt_file = array();
	if ($dir = opendir($path_SJIS)) {
		$files = scandir($path_SJIS);
		natcasesort($files);
		$order = 0;
		foreach ($files as $file) {
			if ($file != "." && $file != "..") {
				$filepath_SJIS = $path_SJIS.'/'.$file;
				if (is_dir($filepath_SJIS)) {
					$prt_dir[] = $file;
				} elseif ($type = chk_source_filename($file)) {
					if (($stat = stat($filepath_SJIS)) <> False) {
						$prt_file[] = array($file, $type, $stat['size'], $stat['mtime']);
						$prt_file_name[] = $file;
						$prt_file_size[] = $stat['mtime'];
						// array_multisort()で日本語の順番が乱れる対策としてnatcasesort()の順番を使う
						$prt_order[] = ++$order;
					}
				}
			}
		}
		closedir($dir);
	}
	print_dir($open_path, $prt_dir, $search_key);
	print_file($open_path, $prt_file, $search_key, $prt_file_name, $prt_file_size, $prt_order);
}
function print_dir($open_path, $prt_dir, $search_key) {
?>
	<div id="print_dir">
	<ul class="file_list">
<?php
	foreach ($prt_dir as $dir) {
		$file_UTF = myfile_DECODE($dir);
		$path_UTF = $open_path.'/'.myfile_DECODE($dir);
?>
		<li class="file_folder"><a href="<?= $_SERVER['SCRIPT_NAME'] ?>?path=<?= urlencode($path_UTF) ?>&key=<?= urlencode($search_key) ?>"><?= $file_UTF.'/' ?></a></li>
<?php
	}
?>
	</ul>
	</div>
<?php
}
function print_file($open_path, $prt_file, $search_key, $prt_file_name, $prt_file_size, $prt_order) {
	if (!$prt_file_name) return;
?>
	<div id="print_file">
		<p style="margin: 5px 0 0 20px; color:red">
		※ IE6ではサイズが大きいファイルのソース表示で実行速度の警告が出ます。
		</p>
		<p style="margin: 0 0 5px 20px;">
		<input type="button" class="copy_button" value="Ｃ">：コピー
		<input type="button" class="copy_button" value="Ｒ">：リネーム
		<input type="button" class="copy_button" value="Ｄ">：削除
		</p>
<script language="JavaScript">
<!--
function rename_file(path, fname){
	fname = window.prompt("新しいファイル名を入れてください。", fname);
//	window.alert(fname);
	location.href='file-rename.php?path='+URLencode(path)+'&new='+URLencode(fname)+'&ret=<?= $_SERVER['SCRIPT_NAME'] ?>';
}
function delete_file(path){
	fname = path.substring(path.lastIndexOf('/')+1);
	if (window.confirm('ファイル「'+fname+'」を削除しますか？')) {
		location.href='file-delete.php?path='+URLencode(path)+'&ret=<?= $_SERVER['SCRIPT_NAME'] ?>';
	} else {
		//window.alert('キャンセルされました');
	}
}
//-->
</script>
	<span style="color: #ff0000"></span>
	<table>
<?php
	$href = $_SERVER['SCRIPT_NAME'] . '?path=' .urlencode($open_path);
	if ($_SESSION['code_view_sort'] == 'date') {
		if ($_SESSION['code_view_sort_order'] == 'desc') {
			array_multisort($prt_file_size, SORT_DESC, $prt_file);
			$date_ch_order = '&order=asc';
			$date_om = '<span class="order_mark">▼</span>';
		} else {
			array_multisort($prt_file_size, SORT_ASC, $prt_file);
			$date_ch_order = '&order=desc';
			$date_om = '<span class="order_mark">▲</span>';
		}
	} else {
		if ($_SESSION['code_view_sort_order'] == 'desc') {
			// array_multisort()で日本語の順番が乱れる対策としてnatcasesort()の順番を使う
			array_multisort($prt_order, SORT_DESC, $prt_file);
			$name_ch_order = '&order=asc';
			$name_om = '<span class="order_mark">▼</span>';
		} else {
			$name_ch_order = '&order=desc';
			$name_om = '<span class="order_mark">▲</span>';
		}
	}
?>
	<tr>
	<th class="file_list"><a href="<?= $href ?>&sort=name<?= $name_ch_order ?>">ファイル名</a><?= $name_om ?></th>
	<th class="file_size">サイズ</th>
	<th class="file_time"><a href="<?= $href ?>&sort=date<?= $date_ch_order ?>">更新日時</a><?= $date_om ?></th>
	</tr>
<?php
	foreach ($prt_file as $file) {
		$file_UTF = myfile_DECODE($file[0]);
		$path_UTF = $open_path.'/'.myfile_DECODE($file[0]);
		$path_SJIS = myfile_ENCODE($path_UTF);
		if ($search_key <> '') {
			$key_found = '';
			if ($_SESSION['code_view_search_uplow'] == 'on') {
				if (mb_strpos(file_get_contents($path_SJIS), $search_key) !== False) {
					$key_found = 'key_found';
				}
			} else {
				if (mb_stripos(file_get_contents($path_SJIS), $search_key) !== False) {
					$key_found = 'key_found';
				}
			}
		}
?>
		<tr><td class="file_list" nowrap>
	<a class="file_<?= $file[1] ?>" href="<?= $_SERVER['SCRIPT_NAME'] ?>?path=<?= urlencode($path_UTF) ?>&key=<?= urlencode($search_key) ?>"><span class="<?= $key_found ?>"><?= $file_UTF ?></span></a>
	<a class="edit_btn" href="code-file-edit.php?path=<?= urlencode($path_UTF) ?>&ret=up">編集</a>
	<input type="button" class="copy_button" value="Ｃ" onClick="location.href='file-copy.php?path=<?= urlencode($path_UTF) ?>&ret=<?= $_SERVER['SCRIPT_NAME'] ?>'">
	<input type="button" class="copy_button" value="Ｒ" onClick="rename_file('<?= $path_UTF ?>', '<?= $file_UTF ?>')">
	<input type="button" class="copy_button" value="Ｄ" onClick="delete_file('<?= $path_UTF ?>')">
	</td>
	<td class="file_size"><?= ceil($file[2]/1024) ?> KB</td><td class="file_time"><?= date("Y/m/d H:i", $file[3]) ?></td></tr>
<?php
	}
?>
	</table>
	</div>
<?php
}
function view_code($open_path, &$source_text, $file_type) {
	if ($_GET['type'].'' <> '') {
		$class = $_GET['type'];
	} else {
		$class = file_type_sh_class($file_type);
	}
?>
<div id="change_file_type">
	<input type="button" value="html" onClick="changeFileType('html')"<?= $class=='html' ? ' id="selected_type"' : '' ?>>
	<input type="button" value="php" onClick="changeFileType('php')"<?= $class=='php' ? ' id="selected_type"' : '' ?>>
	<input type="button" value="javascript" onClick="changeFileType('javascript')"<?= $class=='javascript' ? ' id="selected_type"' : '' ?>>
	<input type="button" value="css" onClick="changeFileType('css')"<?= $class=='css' ? ' id="selected_type"' : '' ?>>
	<input type="button" value="xml" onClick="changeFileType('xml')"<?= $class=='xml' ? ' id="selected_type"' : '' ?>>
	<input type="button" value="sql" onClick="changeFileType('sql')"<?= $class=='sql' ? ' id="selected_type"' : '' ?>>
	<span style="padding-left: 80px;"><input type="button" value="コード編集" onClick="location.href='code-file-edit.php?<?= $_SERVER['QUERY_STRING'] ?>'"></span>
	<input type="button" name="" value="戻る" onClick="location.href='<?= $_SERVER['SCRIPT_NAME'] ?>?path=<?= urlencode(up_folder_path($open_path)) ?>'">
</div>
<pre id="code_view" name="src_code" class="<?= $class ?>"><?= my_htmlspecialchars_notrim($source_text) ?></pre>
<?php
}
?>
