<?php
if (!defined('MOD')) die("access die");
$channelid = intval( $_GET['channelid']);
$action    = $_GET['action'];
if( $channelid==-1){
	purviewCheck('explorer', 'plugin');
	$rootdir = R_P.'/templates';             // for Windows
	$rooturl = '../templates/';		 // 可以浏览图片, swf 等文件
	$dirname = "模板目录";
	$pthotourl = "";
}elseif( $channelid==0){
	purviewCheck('files', 'plugin');
	$rootdir = R_P.'/attachment';             // for Windows
	$rooturl = '../attachment/'; 	 // 可以浏览图片, swf 等文件
	$dirname = "附件目录/";
	$pthotourl = "../../../";
}elseif( $channelid > 0){
	purviewCheck( $channelid, 'acc_channel');
	$rootdir = R_P.'/attachment/'.$channeldir;             // for Windows
	$rooturl = '../attachment/'.$channeldir.'/'; 	 // 可以浏览图片, swf 等文件
	$dirname = "{$channelname}目录";
	$pthotourl = "attachment/{$channeldir}/";
}else{
	qbase::purviewCheckDie();
}
clearstatcache();
require_once R_P.'/include/class.files.php';
function deletedir($file){
	if(is_dir($file)){
		$handle =opendir($file);
		while(false!==($filename=readdir($handle))){
			if($filename!="."&&$filename!="..")
				deletedir($file."/".$filename);
		}
		closedir($handle);
		@rmdir($file);
		return true;
	}else{
		@unlink($file);
	}
}
$dirpath = substr($rooturl, 3) . substr($urlpath, 2);
$show_debug_info = false;
$filename_pattern = "#[\\x00-\\x19\\\/\?\'\|\*:<>]#i";
switch($action){
	case 'delete':
		checkAdminReferer();
		if($_GET[file]){
			$real_file_path = "{$currdir}/{$_GET[file]}{$_GET[ext]}";
			if(is_dir($real_file_path)){
				$message = (deletedir($real_file_path)) ? "删除文件夹成功。`{$_GET[file]}{$_GET[ext]}` 已经被删除。" : "删除文件夹失败！`{$_GET[file]}{$_GET[ext]}` 没有被删除。" ;
			}elseif(is_file($real_file_path)){
				if(@unlink($real_file_path)){
					$message = "删除文件成功。`{$_GET[file]}{$_GET[ext]}` 已经被删除。";
				}else{
					$message = "删除文件失败！`{$_GET[file]}{$_GET[ext]}` 没有被删除。";
				}
			}else{
				$message = " `{$_GET[file]}{$_GET[ext]}` 不存在！";
			}
		}
		if(AJAX) die(qbase::xmlPrint($message));
	break;

	case 'remove':
		checkAdminReferer();
		if($_GET[file]&& $_GET[aimdir]){
			$newdir = getAbsolutePath($_GET[aimdir]) ;
			if( is_dir($newdir) && !strstr($_GET[aimdir], '..') ){
				$oldfile_name = "{$currdir}/{$_GET[file]}{$_GET[ext]}";
				$newfile_name = "{$newdir}/{$_GET[file]}{$_GET[ext]}";
				if(@rename($oldfile_name, $newfile_name)){
					$message = "`{$_GET[file]}{$_GET[ext]}` 移动到文件夹 `$_GET[aimdir]` 成功。";
					header("Location: system.php?sys=explorer&path=$_GET[aimdir]&message=$message&channelid=$channelid");
		 			echo $newdir;
					exit();
				}else{
					$message = "`{$_GET[file]}{$_GET[ext]}` 移动到文件夹 `$_GET[aimdir]` 失败！";	
				}
			}else{
				$message = "$_GET[aimdir] 文件夹无效或不存在! ";
			}
		}
	break;

	case 'rename':
		checkAdminReferer();
		if($_GET[file] && $_GET[newname]){
			if( $newname = getAvailFilename($_GET['newname']) ){
				$oldfile_name = "{$currdir}/{$_GET[file]}{$_GET[ext]}";
				$newfile_name = "{$currdir}/{$newname}";
				$message = (@rename($oldfile_name, $newfile_name)) ? "`$_GET[file]` 改名为 `$newname` 成功。" : "`{$_GET[file]}{$_GET[ext]}` 改名为 `$newname` 失败！";
			}else{
				$message = "文件名不规范！`{$_GET[file]}{$_GET[ext]}` 没有重命名！";
			}
		}
	break;

	case 'mkdir':
		checkAdminReferer();
		$dir = trim($_POST['dir']);
		if($dir && $dir = getAvailFilename($dir)){
			$newdir = $currdir .'/'. $dir;
			if(!is_dir($newdir)) qbase::createdir($newdir);
			$status = array('status'=>200, 'msg'=>"目录{$dir}创建成功");
		}else{
			$status = array('status'=>404, 'msg'=>"提交的文件名非法");
		}
		die(qbase::xmlPrint(qbase::jsonencode($status)));
	break;

	case 'mkfile':
		checkAdminReferer();
		$name = trim($_POST['name']);
		$content = stripslashes(trim($_POST['content']));
		if($name && $name = getAvailFilename($name)){
			$newname = $currdir .'/'. $name;
			if(!is_file($newname)){
				qbase::writetofile($newname, $content);
				$status = array('status'=>200, 'msg'=>"文件{$name}创建成功");
			}else{
				$status = array('status'=>404, 'msg'=>"{$name}已近存在，不能创建");
			}
		}else{
			$status = array('status'=>404, 'msg'=>"提交的文件名非法");
		}
		die(qbase::xmlPrint(qbase::jsonencode($status)));
	break;

	case 'down':
		checkAdminReferer();
		if($_GET['file']){
			$real_file_path = "{$currdir}/{$_GET[file]}{$_GET[ext]}";
			if(is_file($real_file_path)){
				@set_time_limit(600);
				$filename = basename($real_file_path);
				$filesize = filesize($real_file_path);
				header("Content-Type: application/force-download; name=\"$filename\"");
				header("Content-Transfer-Encoding: binary");
				header("Content-Length: $filesize");
				header("Content-Disposition: attachment; filename=\"$filename\"");
				header("Expires: 0");
				header("Cache-Control: no-cache, must-revalidate");
				header("Pragma: no-cache");
				readfile("$real_file_path");
				exit();
			}
		}
	break;
  
	case 'editor';
		checkAdminReferer();
		require( 'editor.php');
	break;

	case 'remove_start';
		//checkAdminReferer();
		require( 'remove.php');
	break;
}

$thedir = new Folder($currdir,$channelid);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv='Content-Type' content='text/html; charset=<?php echo $_SET['def_charset']; ?>'>
<link href="<?php echo AW_P; ?>images/main.css" rel="stylesheet" type="text/css">
<link href="<?php echo AW_P; ?>images/skin.css" rel="stylesheet" type="text/css" />
<script language="javaScript" src="<?php echo WW_P; ?>images/js/qmecms.js"></script>
<script language="javaScript" src="<?php echo AW_P; ?>images/js/admin.js"></script>
<script language="javaScript" src="<?php echo AW_P; ?>images/js/explorer.js"></script>
<style>
#divDirectoryTable td{padding:1px 5px;}
</style>
<script type="text/javascript">
var charset = '<?php echo $_SET['def_charset']; ?>';
var titlejs="<?=W_P?>";
var web_path="<?=$_SET[webpath]?>";
var cookie_pre = "<?=$_SET[cookie_pre]?>";
var config = {charset:charset,channelid : '<?php echo $channelid;?>',channeldir : '<?php echo $channeldir;?>',url : "<?php echo W_P;?>",web_path : web_path, awp:'<?php echo AW_P;?>', wwp:'<?php echo WW_P;?>'};
</script>
<script language=javaScript>
<!--
var channelid = "<?=$channelid?>";
var rootdir   = "<?=$pthotourl?>";
var showType  = <?=intval($_GET['showtype']);?>;
var d = new Directory('d');
d.parentPath = "<?=$parentpath?>";
d.relatePath = "<?=$urlpath?>";
d.urlRoot = "<?=$rooturl?>";

<?php
$thedir->getFolderInfo();
?>

-->
</script>
<script type="text/javascript">
var removeItemData = [];
var removeItemDataIndex = 0;
function removeAllFileItem(){
	if(removeItemDataIndex < removeItemData.length){
		d.act(removeItemData[removeItemDataIndex], 'delete', 1);
		removeItemDataIndex++;
	}else{
		alert("删除完成");
		window.location.reload();
	}
}
function removeAllFile(){
	removeItemData = [];
	removeItemDataIndex = 0;
	var o = $('mytable').getElementsByTagName('input');
	for(var i=0;i<o.length;i++){
		if(o[i].id.substr(0,4) == 'ids_' && o[i].checked == true){
			removeItemData.push(o[i].value);
		}
	}
	if(removeItemData.length == 0) return alert('请选择需要删除的文件');
	if( window.confirm('确实要删除这'+removeItemData.length+'个文件吗？') ){
		removeAllFileItem();
	}
}
function newDirAction(){
	Form.send('ajaxForm', function(resp){
		var json = evaljson(resp);
		if(typeof json == 'undefined' || typeof json.status == 'undefined') return alert('系统出现问题，请稍后再试');
		if(json.status == 200){
			qmeCms.dialogView.setMessage(json.msg);
			window.location.reload();
		}else{
			alert(json.msg);
		}
	});
}
function newDir(){
	qmeCms.dialogView.alert("建立文件夹","<form onsubmit=\"newDirAction();return false;\" name='ajaxForm' id='ajaxForm' method='post' action='system.php?sys=explorer&channelid="+channelid+"&action=mkdir&path="+d.relatePath+"'><p>目录名字：<input type='text' class='f-text' name='dir' /></p><p style='padding-top:12px;'>在非WIN的服务器下，尽量不要使用中文字符</p></form>");
	qmeCms.dialogView.setSubmit(true, "确定", newDirAction);
}
function newFile(){
	var template = [
		"<form onsubmit=\"newDirAction();return false;\" name='ajaxForm' id='ajaxForm' method='post' action='system.php?sys=explorer&channelid="+channelid+"&action=mkfile&path="+d.relatePath+"'>",
			"<p>文件名称：<input type='text' class='f-text' name='name' /> <font color='blue'>在非WIN的服务器下，尽量不要使用中文字符</font></p>",
			"<p style='padding-top:12px;'>内容：</p>",
			"<div style=\"position:relative;\">",
				"<textarea id=\"newfile-data\" class=\"text-data\"  style='width:650px;height:300px;word-break:keep-all' name='content' wrap=\"off\" oncontextmenu=\"return false\"></textarea>",
				"<textarea id=\"newfile-index\" style=\"width:40px;height:300px;\" readonly=\"true\" class=\"text-index\"></textarea>",
				"<div style=\"width:42px;height:302px;\"class=\"text-index-parent\"></div>",
			"</div>",
		"</form>"
	].join('');
	qmeCms.dialogView.form("新建文件", template, {width:740,type:'abs'});
	qmeCms.dialogView.setSubmit(true, "确定", newDirAction);
	new qmeCms.areaIndex('newfile');
}
function uploadComplete(json){
	$('exploreTips').innerHTML = '<font color="red"><b>'+json[0].substr(json[0].lastIndexOf('/')+1)+'上传完成</b></font>';
}
</script>
</head>
<body  leftmargin='0' topmargin='0' marginheight='0' marginwidth='0'>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
	<tr>
		<td width="17" valign="top"><img src="<?php echo AW_P; ?>images/skin/left-top-right.gif" width="17" height="29" style="display:block;" /></td>
		<td valign="top" background="<?php echo AW_P; ?>images/skin/content-bg.gif">
			<table width="100%" height="29" border="0" cellpadding="0" cellspacing="0" class="left_topbg" id="table2">
				<tr>
				<td height="29"><div class="titlebt"><span class="title_right" id="doc_subject">附件管理</span></div></td>
				</tr>
			</table>
		</td>
		<td width="16" valign="top"><img src="<?php echo AW_P; ?>images/skin/nav-right-bg.gif" width="16" height="29"  style="display:block;" /></td>
	</tr>
</table>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
	<tr>
		<td width="17" valign="middle" style="background:url(<?php echo AW_P; ?>images/skin/mail_leftbg.gif) repeat-y 0 0">&nbsp;</td>
		<td valign="top" bgcolor="#f7f8f9">
			<table cellSpacing=0 cellPadding=0  class="tableborder" width=100% align=center>
				<tr>
					<td colSpan=6>
						<table cellSpacing=0 width=100% border=0  class="tableborder">
							<tr>
								<td class="tablerow" vAlign=top>
									<div title="<?=$urlpath?>" style="cursor:default; width:100%; height:24px; overflow: hidden;WORD-BREAK: break-all; WORD-WRAP: break-word;">
										<b>当前目录：</b><font color=#CC0000><b><?=$dirname?> >> <?=$urlpath?></b></font>
									</div>
								</td>
								<td class="tablerow" vAlign=top noWrap width=250 align=right rowspan=2>
									<img border=0 style="cursor:pointer;" src="<?php echo AW_P; ?>images/sma/icon_remove.gif"  alt="删除文件" title="删除文件" onclick="removeAllFile();" />
									<img border=0 style="cursor:pointer;" src="<?php echo AW_P; ?>images/sma/file.gif"  alt="新建文件" title="新建文件" onclick="newFile();" />
									<img border=0 style="cursor:pointer;" src="<?php echo AW_P; ?>images/sma/icon_newdir.gif"  alt="新建文件夹" title="新建文件夹" onclick="newDir();" />
									<a href="system.php?sys=explorer&channelid=<?=$channelid?>&path=<?=$urlpath?>"><img border=0 src="<?php echo AW_P; ?>images/sma/icon_reload.gif" alt="刷新浏览" title="刷新浏览"></a>
									<img border=0 style="cursor:pointer;" src="<?php echo AW_P; ?>images/sma/icon_upload.gif" onClick="qmeCms.dialogView.iframe('上传文件','system.php?sys=upload&channelid=-1&path=<?=$dirpath?>&javascript=uploadComplete&file_type=all&type=1&keepName=1',{type:'abs'});" alt="上传文件" title="上传文件" />
								</td>
							</tr>
							<tr>
								<td class="tablerow" vAlign=top noWrap id="exploreTips" style="padding-left:12px;"><font color=red><b><? echo ($message) ? $message : $_GET[message]; ?></b></font></td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
			<div id="divDirectoryTable"></div>
			<div id="divTooltip" style="position: absolute; visibility: hidden; z-index: 0; left:100px;top:200px;"></div>
			<?if($show_debug_info) echo $debug;?>
		</td>
		<td width="16" style="background:url(<?php echo AW_P; ?>images/skin/mail_rightbg.gif) repeat-y 0 0">&nbsp;</td>
	</tr>
</table>
<table width="100%" border="0" cellpadding="0" cellspacing="0" height="17">
	<tr>
		<td width="17" valign="bottom"><img src="<?php echo AW_P; ?>images/skin/buttom_left2.gif" width="17" height="17"  style="display:block;"/></td>
		<td width="100%" style="background:url(<?php echo AW_P; ?>images/skin/buttom_bgs.gif) repeat-x 0 0"></td>
		<td width="16" valign="bottom" ><img src="<?php echo AW_P; ?>images/skin/buttom_right2.gif" width="16" height="17" style="display:block;" /></td>
	</tr>
</table>
<script type="text/javascript">
d.setHTML(2,1,<?=$channelid?>)
</script>
<p style="text-align:center;background-color:#F1F3F5;padding:15px 0;border-top:1px solid #dedede">国内最专业的PHP网站管理系统，轻松建站的首选利器</p>
</body>
</html>
