<?php
	require("../__common__/__define_common.php");
	require("../__common__/include-common-all.php");
	require("../__common__/include-common-html.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("FILE_MANAGER_DEFAULT_FOLDER")) {
		define("FILE_MANAGER_DEFAULT_FOLDER", $_SERVER['DOCUMENT_ROOT']);
	}
	if ($_POST['path'].'' <> '') {
		$open_path = form_str_filepath($_POST['path']);
	} elseif ($_GET['path'].'' <> '') {
		$open_path = $_GET['path'];
	}
	$open_path = str_replace("\\", "/", right_slash_strip($open_path));
	if ($open_path == "") {
		$open_path = FILE_MANAGER_DEFAULT_FOLDER;
	}
	if (isset($_POST['search_key']) && $_POST['search_key'].'' <> '') {
		$_SESSION['file_manager_search_key'] = $_POST['search_key'];
	} elseif ($_GET['key'].'' <> '') {
		$_SESSION['file_manager_search_key'] = $_GET['key'];
	}
	if (isset($_POST['search_reset'])) {
		$_SESSION['file_manager_search_key'] = '';
	}

	if ($_GET['sort'].'' <> '') {
		$_SESSION['file_manager_sort'] = $_GET['sort'];
	}
	if ($_GET['order'].'' <> '') {
		$_SESSION['file_manager_sort_order'] = $_GET['order'];
	}
	$file_mode = folder_check($open_path, $path_SJIS, $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="ファイルマネージャー,ファイル暗号化">
<link rel="stylesheet" href="<?= _STYLE_SHEET_FOLDER ?>/tools_common.css?20090925" 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;
}
.file_zip {
	background-image: url(icon/zip.png);
	background-repeat: no-repeat;
	background-position: top left;
	padding-left: 24px;
}
.file_other {
	background-image: url(icon/other.png);
	background-repeat: no-repeat;
	background-position: top left;
	padding-left: 24px;
}
.file_encode {
	background-image: url(icon/encode.png);
	background-repeat: no-repeat;
	background-position: top left;
	padding-left: 24px;
}
.encode_file {
	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>
<script language="javascript" src="../scripts/URLencode.js" type="text/javascript"></script>
<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);
}
function delete_file(path){
	fname = path.substring(path.lastIndexOf('/')+1);
	if (window.confirm('ファイル「'+fname+'」を削除しますか？')) {
		location.href='file-delete.php?path='+URLencode(path);
	} else {
		//window.alert('キャンセルされました');
	}
}
function unzip_file(path, dest){
	dest = window.prompt("解凍先フォルダ名を入れてください。\nフォルダ名を省略するとカレントフォルダに(上書き)解凍します。", dest);
	location.href='file-unzip.php?path='+URLencode(path)+'&dest='+URLencode(dest);
}
function cipher_file(path){
	if (document.getElementById('encode_mode').checked) {
		mode = 2;
	} else {
		mode = 3;
	}
	location.href='file-cipher.php?path='+URLencode(path)+'&mode='+mode;
}
function rename_folder(path, fname){
	fname = window.prompt("新しいフォルダ名を入れてください。", fname);
//	window.alert(fname);
	location.href='folder-rename.php?path='+URLencode(path)+'&new='+URLencode(fname);
}
function delete_folder(path){
	fname = path.substring(path.lastIndexOf('/')+1);
	if (window.confirm('フォルダ「'+fname+'」を削除しますか？')) {
		location.href='folder-delete.php?path='+URLencode(path);
	} else {
		//window.alert('キャンセルされました');
	}
}
//-->
</script>
<title>（簡易）ファイルマネージャー（暗号化/復号化付き）</title>
</head>
<body>
<a href="../tools/" class="return_index">■</a>
<span id="tool_title">（簡易）ファイルマネージャー（暗号化/復号化付き）</span>
<div id="header">
	<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="変更">
		ファイル名：<input class="text" type="text" name="search_key" value="<?= my_htmlspecialchars($_SESSION['file_manager_search_key']) ?>" size=10>
		<input type="submit" name="search" value="検索">
		<input type="submit" name="search_reset" value="リセット">
		<br>
		<p id="file_name_box">フォルダ：<span id="file_name"><?= $open_path ?></span>
	</form>
</div>
<div id="body">
<?php
	if ($file_mode == 'Folder') {
		view_folder($open_path, $path_SJIS, $_SESSION['file_manager_search_key']);
	} else {
		echo '<p class="error_msg">'.$error_msg.'</p>';
	}
?>
</div>
<div id="footer">
<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);
		foreach ($files as $file) {
			if ($file != "." && $file != "..") {
				$filepath_SJIS = $path_SJIS.'/'.$file;
				if (is_dir($filepath_SJIS)) {
					$prt_dir[] = $file;
				} else {
					$type = chk_source_filename($file);
					if ($type == False) {
						if (($ext = strtolower(strrchr($file, '.'))) == '.encode3' or $ext == '.encode2' or $ext == '.encode') {
							$type = 'encode';
						} else if ($ext == '.zip') {
							$type = 'zip';
						} else {
							$type = 'other';
						}
					}
					if (($stat = stat($filepath_SJIS)) <> False) {
						$prt_file[] = array($file, $type, $stat['size'], $stat['mtime']);
						$prt_file_name[] = $file;
						$prt_file_size[] = $stat['mtime'];
					}
				}
			}
		}
		closedir($dir);
	}
	print_dir($open_path, $prt_dir, $search_key);
	print_file($open_path, $prt_file, $search_key, $prt_file_name, $prt_file_size);
}
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 = mb_convert_encoding($dir, 'UTF-8', 'SJIS-win');
		$path_UTF = $open_path.'/'.mb_convert_encoding($dir, 'UTF-8', 'SJIS-win');
?>
		<li class="file_folder"><a href="<?= $_SERVER['SCRIPT_NAME'] ?>?path=<?= urlencode($path_UTF) ?>&key=<?= urlencode($search_key) ?>"><?= $file_UTF ?></a>
		<input type="button" class="copy_button" value="Ｃ" onClick="location.href='folder-copy.php?path=<?= urlencode($path_UTF) ?>'">
		<input type="button" class="copy_button" value="Ｒ" onClick="rename_folder('<?= $path_UTF ?>', '<?= $file_UTF ?>')">
		<input type="button" class="copy_button" value="Ｄ" onClick="delete_folder('<?= $path_UTF ?>')">
		</li>
<?php
	}
?>
	</ul>
	</div>
<?php
}
function print_file($open_path, $prt_file, $search_key, $prt_file_name, $prt_file_size) {
	if (!$prt_file_name) return;
?>
	<div id="print_file">
	<?php if (defined("FILE_MANAGER_CIPHER_SEED") and FILE_MANAGER_CIPHER_SEED <> '') { ?>
		<p style="margin: 5px 0 0 20px; color:green">
		※ 暗号化は、すでに暗号化ファイルがあった場合、上書き処理します。<br>
		※ 復号化は、元ファイルがある場合、エラーとします。<br>
		※ unZipは、元のタイムスタンプは保存されません。<br>
		※ 暗号化/復号化/unZipは、ファイルサイズが大きいとかなり処理時間がかかります。
		</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="Ｄ">：削除
		<input type="checkbox" id="encode_mode" value="2"<? if ($_SESSION['file_manager_encode_mode'] == '2') echo ' checked'; ?>>XORで暗号化(暗号化キーコードが推測されるリスクがあります)
		</p>
	<?php } else {?>
		<p style="margin: 10px 0 10px 20px; color:red">
		※ 実運用定義ファイルで、FILE_MANAGER_CIPHER_SEEDが定義されていないと暗号化/復号化はできません。<br>
		</p>
	<?php } ?>
	<table>
<?php
	$href = $_SERVER['SCRIPT_NAME'] . '?path=' .urlencode($open_path);
	if ($_SESSION['file_manager_sort'] == 'date') {
		if ($_SESSION['file_manager_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['file_manager_sort_order'] == 'desc') {
			array_multisort($prt_file_name, 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 = mb_convert_encoding($file[0], 'UTF-8', 'SJIS-win');
		$path_UTF = $open_path.'/'.mb_convert_encoding($file[0], 'UTF-8', 'SJIS-win');
		$path_SJIS = mb_convert_encoding($path_UTF, 'SJIS-win', 'UTF-8');
		if ($search_key == '' or ($search_key <> '' and stristr($file_UTF, $search_key)) ) {
?>
		<tr><td class="file_list" nowrap>
		<?php if ($file[1] == 'encode') { ?>
			<?php $cip_val = '←復号化' ?>
			<span class="file_<?= $file[1] ?>">
				<a href="file-download.php?path=<?= urlencode($path_UTF) ?>"><span class="encode_file"><?= $file_UTF ?></sapn></a>
			</sapn>
		<?php } else { ?>
			<?php $cip_val = '暗号化' ?>
			<span class="file_<?= $file[1] ?>">
				<a href="file-download.php?path=<?= urlencode($path_UTF) ?>"><?= $file_UTF ?></a>
			</sapn>
		<?php } ?>
		<?php if ($file[1] == 'zip') { ?>
			<input type="button" class="copy_button" value="unZip" onClick="unzip_file('<?= $path_UTF ?>', '<?= substr($file_UTF, 0, -4) ?>')">
		<?php } else if ($file[1] != 'other' && $file[1] != 'encode') { ?>
			<input type="button" class="copy_button" value="Ｓ" onClick="window.open().location.href='code-file-view.php?path=<?= urlencode($path_UTF) ?>'">
		<?php } ?>
		<?php if (defined("FILE_MANAGER_CIPHER_SEED") and FILE_MANAGER_CIPHER_SEED <> '') { ?>
	<input type="button" class="copy_button" value="<?= $cip_val ?>" onClick="cipher_file('<?= $path_UTF ?>')">
		<?php } ?>
	<input type="button" class="copy_button" value="Ｃ" onClick="location.href='file-copy.php?path=<?= urlencode($path_UTF) ?>'">
	<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 folder_check($open_path, &$path_SJIS, &$error_msg) {
	$path_SJIS = mb_convert_encoding($open_path, 'SJIS-win', 'UTF-8');
	if (file_exists($path_SJIS)) {
		if (is_dir($path_SJIS)) {
			return 'Folder';
		} else {
			$error_msg =  'ファイル『 '.$open_path.' 』は表示できません。';
			return 'File';
		}
	} else {
		$error_msg =  'フォルダまたはファイル『 '.$open_path.' 』が、みつかりません。';
		return 'Error';
	}
}
?>
