<?php
$IN=array();
$ADMIN=array();
$explorer	= new explorer();

class explorer{
	var $base_url='';
	var $img_url='';
	var $upload_dir='';
	var $upload_url='';
	var $upload_folder_name='·我的文档';
	var $errors='';
	var $exists_save_log_func=0;

	function explorer(){
		global $IN,$AS,$ADMIN;
		
		$this->base_url='explorer.php?';
		$this->upload_dir='uploads';
		$this->upload_url='uploads';
		$this->img_url='exp_images';
		
		$ADMIN['allow_operate']=1;
		$ADMIN['max_upload_size']=100;
		$ADMIN['upload_type']='jpg|jpeg|gif|swf|avi|mp3|rm|wmv';
		
		$IN=$this->parse_incoming();
		
		$IN['path']=preg_replace("/^\/+/",'/',$IN['path']);
		$IN['path']=preg_replace("/\/+$/",'',$IN['path']);
		//---------------------------------------
		// Kill globals - globals bad, Homer good.
		//---------------------------------------
		$tmp_in = array_merge( $_GET, $_POST, $_COOKIE );
		
		foreach ( $tmp_in as $k => $v ){
			unset($$k);
		}
		unset($tmp_in);
		
		if($ADMIN['allow_operate']){
			switch($IN['code']){
				case 'new':
					$this->new_folder();
					break;
				case 'display':
					$this->show_list();
					break;
				case 'up':
					if($ADMIN['max_upload_size'])$this->upload();
					break;
				case 'doup':
					if($ADMIN['max_upload_size'])$this->do_upload();
					break;
				case 'rename':
					$this->do_rename();
					break;
				case 'delete':
					$this->do_delete();
					break;
				case 'paste':
					$this->do_paste();
					break;
				case 'interface' :
					$this->make_interface();
					break;
				default :
					$this->main_frame();
			}				
		}else if($IN['code']=='display'){
			$this->show_list();
		}else if($IN['code']=='interface'){
			$this->make_interface();
		}else{
			$this->main_frame();
		}
	}
	
	function parse_incoming(){
    	global $HTTP_GET_VARS, $HTTP_POST_VARS, $HTTP_CLIENT_IP, $REQUEST_METHOD, $REMOTE_ADDR, $HTTP_PROXY_USER, $HTTP_X_FORWARDED_FOR;
    	
		$return = array();
    	
		if( is_array($HTTP_GET_VARS) ){
			while( list($k, $v) = each($HTTP_GET_VARS) ){
				if( is_array($HTTP_GET_VARS[$k]) ){
					while( list($k2, $v2) = each($HTTP_GET_VARS[$k]) ){
						$return[$k][$k2] = $this->safe_stripslashes($v2);
					}
				}else{
					$return[$k] = $this->safe_stripslashes($v);
				}
			}
		}
		
		if( is_array($HTTP_POST_VARS) ){
			while( list($k, $v) = each($HTTP_POST_VARS) ){
				if ( is_array($HTTP_POST_VARS[$k]) ){
					while( list($k2, $v2) = each($HTTP_POST_VARS[$k]) ){
						$return[$k][$k2] = $this->safe_stripslashes($v2);
					}
				}else{
					$return[$k] = $this->safe_stripslashes($v);
				}
			}
		}
		
		//??????IP
		$addrs = array();
		
		foreach( array_reverse( explode( ',', $HTTP_X_FORWARDED_FOR ) ) as $x_f ){
			$x_f = trim($x_f);
			
			if ( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $x_f ) ){
				$addrs[] = $x_f;
			}
		}
		
		$addrs[] = $_SERVER['REMOTE_ADDR'];
		$addrs[] = $HTTP_PROXY_USER;
		$addrs[] = $REMOTE_ADDR;
		
		$return['IP_ADDRESS'] = $this->select_var( $addrs );
		$return['IP_ADDRESS'] = preg_replace( "/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/", "\\1.\\2.\\3.\\4", $return['IP_ADDRESS'] );
		$return['request_method'] = ( $_SERVER['REQUEST_METHOD'] != "" ) ? strtolower($_SERVER['REQUEST_METHOD']) : strtolower($REQUEST_METHOD);
		
		return $return;
	}
	
	function select_var($array) {
    	if ( !is_array($array) ) return -1;
		
    	ksort($array);
		
    	$chosen = -1;  // Ensure that we return zero if nothing else is available
    	foreach ($array as $k => $v){
    		if (isset($v)){
    			$chosen = $v;
    			break;
    		}
    	}
    	
    	return $chosen;
    }
	
	function new_folder(){
		global $IN,$AD_FUNC;
		
		$folder=$temp=$this->upload_dir.$IN['path']."/".$IN["new_name"];
		
		$count=2;
		
		while(@is_dir($folder)){
			$folder=$temp.' ('.$count++.')';
		}
		
		if(!@mkdir($folder,0777)){
			echo "<script>alert(\"创建文件夹失败!\")</script>";
		}else if($this->exists_save_log_func){
		
			$AD_FUNC->save_log("创建文件夹 ".$folder);
			@chmod($folder, 0777);
		}
		
		$this->make_interface();
	}
	function do_upload(){
		global $ADMIN,$IN,$HTTP_POST_FILES,$AD_FUNC;
		@set_time_limit(10000);
		if($ADMIN['max_upload_size']>=1){ 
			if (($HTTP_POST_FILES['file']['name'] != "") && ($HTTP_POST_FILES['file']['name'] != "none") ){
				$FILE_NAME = $this->safe_stripslashes($HTTP_POST_FILES['file']['name']);
				$FILE_SIZE = $HTTP_POST_FILES['file']['size'];
				if ($FILE_SIZE <= ($ADMIN['max_upload_size']*1024)){
					$ext=strtolower(substr($FILE_NAME,strrpos($FILE_NAME,".")+1));
					if(preg_match("/^({$ADMIN['upload_type']})$/i",$ext)){
						if(!@move_uploaded_file( $HTTP_POST_FILES['file']['tmp_name'], $this->upload_dir.$IN['_target']."/".$FILE_NAME)){
							echo "<script>alert(\"文件上传失败!请检查 文件夹{$this->upload_dir}{$IN['_target']} 的权限!\")</script>";
						}else if($this->exists_save_log_func){
		
							$AD_FUNC->save_log("上传文件 ".$this->upload_dir.$IN['_target']."/".$FILE_NAME);
						}
					}else{
						echo "<script>alert(\"你只能上传格式为 ".implode(' ',explode('|',$ADMIN['upload_type']))." 的文件!\")</script>";
					}
				}else{
					echo "<script>alert('你上传的文件大小超过 ".$this->funFormatSize($ADMIN['max_upload_size']*1024)."!')</script>";
				}
			}else{
				echo "<script>alert('你上传的文件不存在!')</script>";
			}
			@set_time_limit(30);
		}else{
			echo "<script>alert('你没有足够的权限上传文件!')</script>";
		}
		?>
		<script language="JavaScript" type="text/JavaScript">
		parent.document.getElementsByName('btUpload')[0].disabled=parent.document.getElementsByName('btSelect')[0].disabled=false;
		parent.changeDir("<?php echo $IN['_target'] ?>",1)
		</script>
		<?php
		$this->upload();
	}
	function do_rename(){
		global $IN,$AD_FUNC,$ADMIN;
		$ext=strtolower(substr($IN["new_name"],strrpos($IN["new_name"],".")+1));
		if(strrpos($IN["new_name"],".")>0 && !preg_match("/^({$ADMIN['upload_type']})$/i",$ext)){
			echo "<script>alert(\"不能重命名为新文件名的扩展名为 $ext 的名称!\")</script>";
		}else{
			$file=preg_replace( "#/$#", "",$this->upload_dir.$IN['path']."/".$IN["old_name"]);
			@chmod($file, 0777);
			if(!@rename($file,preg_replace( "#/$#", "",$this->upload_dir.$IN['path']."/".$IN["new_name"]))){
				echo "<script>alert(\"文件重命名失败!\")</script>";
			}else if($this->exists_save_log_func){
			
				$AD_FUNC->save_log("将文件(夹) ".$file." 重命名为 ".$this->upload_dir.$IN['path']."/".$IN["new_name"]);
			}
		}
		$this->make_interface();
	}
	function do_delete(){
		global $IN,$AD_FUNC;
		$file=$this->upload_dir.$IN['path']."/".$IN["target_name"];
		if($file==$this->upload_dir) return;
		@set_time_limit(10000);
		if(!$this->rm_dir($file)){
			echo "<script>alert(\"删除文件(夹) {$IN['target_name']} 失败!\")</script>";
		}else if($this->exists_save_log_func){
		
			$AD_FUNC->save_log("删除文件(夹) ".$file);
		}
		@set_time_limit(30);
		$this->make_interface();
	}
	function do_paste(){
		global $IN,$AD_FUNC;
		
		$from_path=preg_replace("#/$#",'',$this->upload_dir.$IN['from_path']);
		$to_path=preg_replace("#/$#",'',$this->upload_dir.$IN['path']."/".$IN["target_name"]);

		@set_time_limit(10000);
		
		if(strtolower($from_path)==strtolower($to_path)){
		
			if($IN['operation']=='copy'){
				$to_path=$this->upload_dir.$IN['path']."/复制 ".$IN["target_name"];
				
				$count=2;
			
				while(@file_exists($to_path)){
					$to_path=$this->upload_dir.$IN['path']."/复制".' ('.$count++.') '.$IN["target_name"];
				}
				if(is_file($from_path)){
					if(!copy($from_path,$to_path)){
						echo "<script>alert(\"复制文件 {$IN['target_name']} 失败!\")</script>";
					}else if($this->exists_save_log_func){
		
						$AD_FUNC->save_log("将文件文件(夹) {$from_path} 复制到 {$to_path}");
					}
				}
				if(is_dir($from_path)){
					if(!$this->copy_dir($from_path,$to_path)){
						echo "<script>alert(\"".addslashes($this->errors)."\")</script>";
					}else if($this->exists_save_log_func){
			
						$AD_FUNC->save_log("将文件文件(夹) {$from_path} 复制到 {$to_path}");
					}
				}
			}
		}else{
			$temp_oper=$IN['operation']=='cut' ? '移动' : '复制';
			if(is_file($from_path)){
				if(!($t=copy($from_path,$to_path))){
					echo "<script>alert(\"{$temp_oper}文件 {$IN['target_name']} 失败!\")</script>";
				}else if($this->exists_save_log_func){
			
						$AD_FUNC->save_log("将文件文件(夹) {$from_path} {$temp_oper}到 {$to_path}");
				}
			}
			if(is_dir($from_path)){
				if(!($t=$this->copy_dir($from_path,$to_path))){
					echo "<script>alert(\"".addslashes($this->errors)."\")</script>";
				}else if($this->exists_save_log_func){
			
						$AD_FUNC->save_log("将文件文件(夹) {$from_path} {$temp_oper}到 {$to_path}");
				}
			}
			if($IN['operation']=='cut'){
				$IN['operation']='';
				$IN['from_path']='';
				if($t)$this->rm_dir($from_path);
			}
		}
		@set_time_limit(30);
		$this->make_interface();
	}
	
	function folder_tree($dir,$t="&nbsp;&nbsp;"){
		global $IN;
		if (is_dir($dir)){
			$handle=opendir($dir);
			$final_folder=$this->get_final_folder($dir);
			while (false!==($folder = readdir($handle)) ){
				if (!preg_match("/^\.\.?$/i", $folder)&&is_dir($dir."/".$folder))
				{	

					echo '<option value="'.substr($dir."/".$folder,strlen($this->upload_dir)).'">'.$t.($final_folder==$folder?'└':'├').$folder.'</option>';
					$this->folder_tree($dir."/".$folder,$t.($final_folder==$folder?'&nbsp;&nbsp;':'│'));


				}
			}
			closedir($handle); 
		}		
	}
	function get_final_folder($dir){
		$handle=@opendir($dir);
		$temp='';
		while (false!==($folder = readdir($handle)) ){
			if (!preg_match("/^\.\.?$/i", $folder)&&is_dir($dir."/".$folder)){
				$temp=$folder;
			}
		}
		return $temp;
	}
	function funFormatSize($n){
		if ($n>=0 and  $n<1000){
			return $n . " Bytes";
		}else if ($n>=1000 and  $n<1000000){
				return intval($n/1024).".".substr(($n %1024)."00",0,2) . " KB";
		}else if ($n>=1000000 and  $n<1000000000 ){
			return intval($n/1048576).".".substr(($n %1048576)."00",0,2). " MB";
		}else{
			return intval($n/1073741824) .".". substr((intval($n/1048576) % 1024)."00",2) . " GB";
		}
	}
	
	function safe_addslashes($v){
		if(!get_magic_quotes_gpc()) {
			$v = addslashes($v );
      	}
		return $v;
	}
	function safe_stripslashes($v){
		if(get_magic_quotes_gpc()) {
			$v = stripslashes($v );
      	}
		return $v;
	}
	function copy_dir($from_path, $to_path, $mode = 0777)
	{
	
		
		// Strip off trailing slashes...
		
		$from_path = preg_replace( "#/$#", "", $from_path);
		$to_path   = preg_replace( "#/$#", "", $to_path);
	
		if ( ! is_dir($from_path) )
		{
			$this->errors = "无法找到目录：'$from_path'";
			return FALSE;
		}
	
		if ( ! is_dir($to_path) )
		{
			if ( ! @mkdir($to_path, $mode) )
			{
				$this->errors = "无法建立目录：'$to_path'，请检查目录属性（CHMOD 777）后再试！";
				return FALSE;
			}
			else
			{
				@chmod($to_path, $mode);
			}
		}
		
		$this_path = getcwd();
		
		if (is_dir($from_path))
		{
			$handle=opendir($from_path);
			while (($file = readdir($handle)) !== false)
			{
				if (($file != ".") && ($file != ".."))
				{
					
					if (is_dir($from_path."/".$file))
					{
						
						$this->copy_dir($from_path."/".$file, $to_path."/".$file);
						
					}
					
					if ( is_file($from_path."/".$file) )
					{
						copy($from_path."/".$file, $to_path."/".$file);
						@chmod($to_path."/".$file, 0777);
					} 
				}
			}
			closedir($handle); 
		}
		
		if ($this->errors == "")
		{
			return TRUE;
		}
	}
	
	//**********************************************/
	// rm_dir
	//
	// Removes directories, if non empty, removes
	// content and directories
	// (Code based on annotations from the php.net
	// manual by pal@degerstrom.com)
	//**********************************************/
	
	function rm_dir($file)
	{
		
		$errors = 0;
		
		// Remove trailing slashes..
		
		$file = preg_replace( "#/$#", "", $file );
		
		if(strtolower(substr($file,0,strlen($this->upload_dir)))!=strtolower($this->upload_dir))return;
		
		if ( file_exists($file) )
		{
			// Attempt CHMOD
			
			@chmod($file, 0777);
			
			if ( is_dir($file) )
			{
				$handle = opendir($file);
				
				while (($filename = readdir($handle)) !== false)
				{
					if (($filename != ".") && ($filename != ".."))
					{
						$this->rm_dir($file."/".$filename);
					}
				}
				
				closedir($handle);
				
				if ( ! @rmdir($file) )
				{
					$errors++;
				}
			}
			else
			{
				if ( ! @unlink($file) )
				{
					$errors++;
				}
			}
		}
		
		if ($errors == 0)
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
	function main_frame(){
		//$this->out_header();
		global $IN;
		echo "<html><head>
					 <meta http-equiv=\"Content-Language\" content=\"zh-cn\">
					 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">
					 <meta name=\"author\" content=\"Powered By Unlimited Workstation\">
					 <meta NAME=\"keywords\" CONTENT=\"download,asp,php,cgi,mysql,sql,mssql,unlimited,workstation,network\">
					 <meta name=\"copyright\" content=\"Copyright 2005 www.nlimi.com, Unlimited Workstation\">
					  <title>资源管理器 - 官方网站(http://www.nlimi.com)</title></head>".
			'<script language="javascript">defaultPath=window.dialogArguments;'.
			($IN['code']=='noscript'?'':'if (top.location != self.location) { top.location = self.location }')
			.'</script><body style="margin:0px" onload=""><iframe name="ifrmMain" marginheight="0" marginwidth="0" frameborder="0" style="width:100%;height:100%;border-width:0px" scrolling="no" src="'. $this->base_url .'&act=explorer&code=interface"></iframe>';
		$this->out_footer();
	}
	function make_interface(){
		global $AS,$ADMIN,$IN;
		$this->out_header();
		?>
		<script language="JavaScript" type="text/JavaScript">
		var whichSelect,selectedName='',tempIsFile,currentPath='',oper='<?php echo $IN['operation'] ?>',fromPath="<?php echo $IN['from_path'] ?>"
		var arrButton=new Array('imgParent','imgNew','imgCopy','imgCut','imgPaste','imgRename','imgDelete')
		function main(){
			init()
			for(var i=0;i<arrButton.length;i++){
				var obj=document.getElementById(arrButton[i])
				if(obj==null) continue;
				with(obj){
					onmouseover=function(){
						if(this.enabled){
							with(this.parentNode){
								className='overBorder'
								style.backgroundColor="#FFFFFF"
							}
						}
					}
					onmouseout=function(){
						if(this.enabled){
							with(this.parentNode){
								className='upBorder'
								style.backgroundColor=""
							}
						}
					}
					onmousedown=function(){
						if(this.enabled){
							with(this.parentNode){
								className='downBorder'
								style.backgroundColor="#EEEEEE"
							}
						}
					}
					onmouseup=function(){
						if(this.enabled){
							this.onmouseover()
						}
					}
					ondrag=function(){
							return false;
					}
					onselectstart=function(){
						return false;
					}
					oncontextmenu=function(){
						return false;
					}
				}
			}
			if(parent.defaultPath!=null&&parent.defaultPath!=""){
				with(parent){
					defaultPath=defaultPath.substring("<?php echo $this->upload_url ?>".length)
					defaultPath=defaultPath.substring(0,defaultPath.lastIndexOf("/"))
				}
				if(parent.defaultPath!=""&&setSelectedIndex(parent.defaultPath)){
					changeDir(parent.defaultPath,1)
				}else{
					changeDir("<?php echo $IN['path'] ?>",1)
				}
			}else{
				changeDir("<?php echo $IN['path'] ?>",1)
			}
			parent.defaultPath=null
		}
		function init(){
			for(var i=0;i<arrButton.length;i++){
				var obj=document.getElementById(arrButton[i])
				if(obj==null) continue;
				obj.enabled=arrButton[i]=='imgNew'?1:0
				if((oper=='copy'||oper=='cut')&&arrButton[i]=='imgPaste'){
					obj.enabled=1
				}
				obj.style.filter=obj.enabled ? '' : 'gray'
			}
			selectedName=''
			tdInfo.innerText=' '
			document.getElementsByName('txtFilename')[0].value=''
			document.getElementById('tdWin').innerHTML='&nbsp;'
			whichSelect=null
		}
		function selectIt(){
			var filename=document.getElementsByName('txtFilename')[0].value
			if(filename==""){
				alert("没有选中文件!")
				return
			}
			window.returnValue=("<?php echo $this->upload_url ?>"+"/"+currentPath+"/"+filename).replace(/\/+/g,"/")
			window.close()
		}
		function changeDir(tempPath,flag){
			initForm()
			with(document.theForm){
				path.value=flag || tempPath=='' ? tempPath : currentPath+'/'+tempPath
				from_path.value=fromPath
				operation.value=oper
				submit()
			}

		}
		function initForm(code,target){
			var els=document.theForm.elements
			for(var i=0;i<els.length;i++){
				els[i].value=''
			}
			document.theForm.target=target!=null ? target : 'ifrmDisplay'
			els('code').value=code!=null ? code : 'display'
			els('type').value=document.getElementsByName('sltType')[0].value
		}
		function setSelectedIndex(value){
			var obj=document.getElementsByName('sltPath')[0]
			var objs=obj.options
			var selected=false
			for(var i=0;i<objs.length;i++){
				if(objs[i].value==(value==null ? currentPath : value)){
					selected=objs[i].selected=true
					break
				}
			}
			ifrmPath.t.value=objs[0].text+obj.value
			return selected
		}
		function funSelectIt(obj,name,is_f){
			if(whichSelect!=null){
				whichSelect.style.backgroundColor='#FFFFFF'
			}
			whichSelect=obj
			selectedName=name
			tempIsFile=is_f
			var e=selectedName!=''
			var arr=new Array('imgCopy','imgCut','imgRename','imgDelete')
			for(var i=0;i<arr.length;i++){
				document.getElementById(arr[i]).style.filter=e ? '' : 'gray'
				document.getElementById(arr[i]).enabled=e
			}
			if(obj){
				obj.style.backgroundColor='#CCCCCC'
			}
		}
		function preview(name,typ){
			document.getElementsByName('txtFilename')[0].value=name
			var obj=document.getElementById('tdWin')
			var path="<?php echo $this->upload_url ?>".replace(/\/$/,"")+currentPath+"/"+name
			tdInfo.innerText=name
			switch(typ){
				case 'img':
					obj.innerHTML='<div id=divWin style="width:100%;height:100%;overflow: hidden;position: absolute;" align="center" valign="middle"><img src="'+path+'" onload="if(!this.loaded){tdInfo.innerText+=\'\\n(\'+this.width+\'X\'+this.height+\')\';if(this.width>150){this.width=150};if(this.height>198){this.height=198};this.loaded=true}"></div>'
					break
				case 'swf':
					obj.innerHTML="<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' width='100%'><param name='movie' value=\""+path+"\"><embed src=\""+path+"\" quality='high' type='application/x-shockwave-flash' width='100%'></embed></object>"
					break
				case 'media':
					obj.innerHTML='<embed src="'+path+'" width=100%></embed>'
					break
				case 'web':
					obj.innerHTML='<iframe marginWidth=0 marginHeight=0 src="'+path+'" frameBorder=0 style="position:static;width:100%;height:100%"></IFRAME>'
					break
				default:
					obj.innerHTML='&nbsp;'
					tdInfo.innerText=' '
			}
		}
		function createNewFolder(){
			folderName=createName("")
			if(folderName==null)return;
			initForm('new','')
			with(document.theForm){
				path.value=currentPath
				new_name.value=folderName
				//paste use it
				from_path.value=fromPath
				operation.value=oper
				submit()
			}
			
		}
		function createName(oldName){
			var errorChar=/(\\|\/|:|\*|\?|"|\<|\>|\|)/
			var name=oldName;
			var s=''
			do{
				var name=prompt(s+"请输入名称，名称不能含下列字符之一:\n\\ / ? : * < > \" |",name);
				var recreate=false
				if(name==null) return null;
				name=name.replace(/(^\s+)|(\s$)/g,'')
				if(errorChar.test(name)||name==''||/^\./.test(name)){
					s=name==""||/^\./.test(name)? '必须输入名称,' :''
					recreate=true
				}else if(exists(name)&&name.toLowerCase()!=oldName.toLowerCase()){
					s=" \""+name+"\" 已存在！"
					recreate=true
				}else if(name.toLowerCase()==oldName.toLowerCase()){
					s='新名称和旧名称相同!'
					recreate=true
				}else if( name.lastIndexOf('.')>-1 && !/\.(<?php echo $ADMIN['upload_type']?>)$/i.test(name)){
					s="你只能命名扩展名为 <?php echo implode(' ',explode('|',$ADMIN['upload_type'])) ?> 的文件!"
					recreate=true
				}
			}while(recreate)
			return name;
		}
		function rename(){	
			if(!document.getElementById('imgRename').enabled)return;
			if(selectedName==null||selectedName=="")return;
			tempName=createName(selectedName)
			if(tempName!=null&&tempName.toLowerCase()!=selectedName.toLowerCase()){
				initForm('rename','')
				
				with(document.theForm){
					path.value=currentPath
					old_name.value=selectedName
					new_name.value=tempName
					//paste use it
					// 
					if((currentPath+"/"+selectedName).toLowerCase().indexOf(fromPath.toLowerCase())!=0){
						from_path.value=fromPath
						operation.value=oper
					}
					submit()
					
				}
			}
		}
		function dele(){
			if(!document.getElementById('imgDelete').enabled)return;
			if(selectedName==null||selectedName=='')return;
			if(!confirm("确实要删除“"+selectedName+"”吗？"))return;
			initForm('delete','')
			
			with(document.theForm){
				path.value=currentPath
				target_name.value=selectedName
				//paste use it
				if((currentPath+"/"+selectedName).toLowerCase().indexOf(fromPath.toLowerCase())!=0){
					from_path.value=fromPath
					operation.value=oper
				}
				submit()
				
			}
		}
		function copy(){
			if(!document.getElementById('imgCopy').enabled)return;
			if(selectedName==null||selectedName=='')return;
			fromPath=currentPath+"/"+selectedName
			oper='copy'
			document.getElementById('imgPaste').enabled=1
			document.getElementById('imgPaste').style.filter=''
		}
		function cut(){
			if(!document.getElementById('imgCut').enabled)return;
			if(selectedName==null||selectedName=='')return;
			fromPath=currentPath+"/"+selectedName
			oper='cut'
			document.getElementById('imgPaste').enabled=1
			document.getElementById('imgPaste').style.filter=''
		}
		function paste(){
			if(!document.getElementById('imgPaste').enabled)return;
			if(fromPath==null||fromPath=='')return;
			if(oper!='copy'&&oper!='cut')return;
			if(currentPath.toLowerCase().indexOf(fromPath.toLowerCase())==0){
				alert("无法"+(oper=="copy"?"复制":"剪切")+" "+fromPath+" "+(currentPath.toLowerCase()==fromPath.toLowerCase() ? "目标文件和源文件相同!" : "目标文件是源文件的子文件!"))
				return
			}
			tempName=fromPath.substring(fromPath.lastIndexOf("/")+1)
			if(exists(tempName)){
				if((currentPath+"/"+tempName).toLowerCase()==fromPath.toLowerCase()){
					if(oper=='cut'){
						return;
					}
				}else{
					if(!confirm("文件 "+tempName+" 已存在!要替换吗？")){
						var tempName0=createName(tempName)
						if(tempName0==null||tempName0.toLowerCase()==tempName.toLowerCase()){
							return
						}
						tempName=tempName0
					}
				}
			}
			initForm('paste','')
			with(document.theForm){
				path.value=currentPath
				from_path.value=fromPath
				target_name.value=tempName
				operation.value=oper
				submit()
			}
		}
		function exists(name){
			var arr=ifrmDisplay.arrFolder
			for(var i=0;i<arr.length;i++){
				if(arr[i].toLowerCase()==name.toLowerCase()){
					return true
					break;
				}
			}
			var arr=ifrmDisplay.arrFile
			for(var i=0;i<arr.length;i++){
				if(arr[i].toLowerCase()==name.toLowerCase()){
					return true
					break;
				}
			}
			return false
		}
		function ifrmHTML(w,f,v){
			return '<BODY style="margin: 0px;"><input readonly'+(v!=null?' value="'+v+'"':'')+' type="text" id="t" style="border: none;font-family: 宋体,Verdana, Arial, Helvetica, sans-serif;height 18px;width:'+w+';font-size: 12px;"'+(f?' onkeyup="parent.unitIsEnabled(\''+f+'\',this.value)"  onchange="parent.unitIsEnabled(\''+f+'\',this.value)"':'')+'></body>'
		}
		function checkUploadForm(obj){
			if("<?php echo $ADMIN['upload_type'] ?>"==""){
				alert("你没有足够的权限上传文件!")
				return false
			}
			if(obj.file.value==""){
				alert("你没有选择需要上传的文件！")
				return false	
			}
			if(!/\.(<?php echo $ADMIN['upload_type']?>)$/i.test(obj.file.value)){
				alert("你只能上传格式为 <?php echo implode(' ',explode('|',$ADMIN['upload_type'])) ?> 的文件!")
				return false	
			}
			var canUp=true
			var t=obj.file.value.substring(obj.file.value.lastIndexOf("\\")+1)
			if(exists(t)){
				if(!confirm("文件 "+t+" 已存在！是否要覆盖？")){
					canUp=false
				}
			}
			if(canUp){
				document.getElementsByName('btSelect')[0].disabled=true;
				document.getElementsByName('btUpload')[0].disabled=true;
				obj._target.value=currentPath
				obj.submit()
				return true
			}		
		 }
		 document.onkeydown=function(){
			if(event.shiftKey && event.keyCode==78)createNewFolder();
			if(event.keyCode==113)rename();
			if(event.shiftKey && event.keyCode==46)dele();
			if(event.shiftKey && event.keyCode==67)copy();
			if(event.shiftKey && event.keyCode==88)cut();
			if(event.shiftKey && event.keyCode==86)paste();
		}
		</script>
		
		<body style="margin:5px;" onLoad="main()" style="background:#E0DFE3 url(<?php echo $this->img_url ?>/resize.gif) no-repeat right bottom">
		<form name=theForm style="display:inline" action="<?php echo $this->base_url ?>&act=explorer" method="post" target="ifrmDisplay">
		<input name="code" type="hidden" value="display">
		<input name="path" type="hidden" value="">
		<input name="type" type="hidden" value="">
		<input name="old_name" type="hidden" value="">
		<input name="new_name" type="hidden" value="">
		<input name="target_name" type="hidden" value="">
		<input name="from_path" type="hidden" value="">
		<input name="operation" type="hidden" value="">
		</form>
		<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
		  <tr>
			<td height="100%"  valign="top">
				<table width="100%" height="100%"  border="0" cellpadding="0" cellspacing="0">
				  <tr>
				  
					<td height="24" width='100%'>
					<table height="100%" width='100%' border="0" cellpadding="2" cellspacing="2">
						  <tr>
<td width="1"><nobr>查找范围:</nobr></td>
							<td width="100%" width="16"><div style="position: absolute;width:100%;height:100%;"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="100%"><iframe id="ifrmPath" width="100%" scrolling="no" frameborder="0" src="javascript:parent.ifrmHTML('100%');"></iframe></td>
    <td width=19 valign="top"><img width=19 height=1></td>
  </tr>
</table>
</div><select name="sltPath" style="width:100%" onChange="changeDir(this.value,1)">
							<option value=""><?php echo $this->upload_folder_name ?></option>
							<?php $this->folder_tree($this->upload_dir);?>
							</select></td>
<td width="16" class=upBorder><img id="imgParent" alt="单击向上一级 双击向上两级" src="<?php echo $this->img_url ?>/parent_folder.gif" width="16" height="16" style="filter:gray" onClick="if(this.parentFolder!=null){changeDir(this.parentFolder,1)}" onDblClick="if(this.parentFolder!=null){changeDir(this.parentFolder.substring(0,this.parentFolder.lastIndexOf('/')),1)};"></td>
						 <?php if($ADMIN['allow_operate']){?>
						  <td width="16" class=upBorder><img id="imgNew" onClick="createNewFolder()" alt="新建文件夹 Shift+N" src="<?php echo $this->img_url ?>/new_folder.gif" width="16" height="16"></td>
						  <td width="16" class=upBorder><img id="imgCopy" onClick="copy()" alt="复制 Shift+C" src="<?php echo $this->img_url ?>/copy.gif" width="16" height="16" style="filter:gray"></td>
						  <td width="16" class=upBorder><img id="imgCut" onClick="cut()" alt="剪切 Shift+X" src="<?php echo $this->img_url ?>/cut.gif" width="16" height="16" style="filter:gray"></td>
						  <td width="16" class=upBorder><img id="imgPaste" onClick="paste()" alt="粘帖 Shift+V" src="<?php echo $this->img_url ?>/paste.gif" width="16" height="16" style="filter:gray"></td>
						  <td width="16" class=upBorder><img id="imgRename" onClick="rename() " alt="重命名 F2" src="<?php echo $this->img_url ?>/rename.gif" width="16" height="16" style="filter:gray"></td>
						  <td width="16" class=upBorder><img id="imgDelete" onClick="dele() " alt="删除 Shift+Delete" src="<?php echo $this->img_url ?>/delete.gif" width="16" height="16" style="filter:gray"></td>
						 <?php }?>
						  </tr>
						</table></td>
				  </tr>
				  <tr>
					<td>
				<iframe src="about:blank" name="ifrmDisplay" frameborder="0" style="width:100%;height:100%;style-width:0px;" scrolling="yes"></iframe>
				
				</td>
				  </tr>
				  <tr>
					<td height="1">		    <table width="100%" hieght="100%" border="0" cellspacing="3" cellpadding="0">
							  <tr>
								<td width="80"  height="30">文件名:</td>
								<td><input type="text" readonly name="txtFilename" style="width:100%;"></td>
								<td width="100" align="right"><input name="btSelect" type="button" class="width74" value="确 定" onClick="selectIt()"></td>
							  </tr>
							  <tr>
								<td  height="24">文件类型:</td>
								<td><select name="sltType" style="width:100%;" onChange="changeDir(currentPath,1)">
									<option value="htm|html|hta|htc|asp|php|aspx|jsp">网页文件(*.htm;*.html;*.hta;*.htc;*.asp;*.aspx;*.php;*.jsp)</option>
									<option value="jpg|jpeg|jpe|bmp|gif|png|tif|tga">图像文件(*.jpg;*.jpeg;*.jpe;*.bmp;*.gif;*.png;*.tif;*.tga)</option>
									<option value="swf|ra|rpm|m|ram|rmm|rm|mp3|asf|wmv|mov|wav|avi">媒体文件(swf;*.ra;*.rpm;*.m;*.ram;*.rmm;*.rm;*.mp3;*.asf;*.wmv;*.mov;*.wav;*.avi)</option>
									<option value="" selected>所有文件(*.*)</option>
								</select></td>
								<td align="right"><input name="btCancel" type="button" class="width74" value="取 消" onClick="window.close()"></td>
							  </tr>
							   <?php if($ADMIN['allow_operate']&&$ADMIN['max_upload_size']){?>
							  <tr>
								<td height="24" >上传文件:</td>
								<td><iframe name="ifrmUpload" marginheight="0" marginwidth="0" frameborder="0" style="width:100%;height:21px;border-width:0px" scrolling="no" src="<?php echo $this->base_url ?>&act=explorer&code=up"></iframe>
							&nbsp;</td>
								<td valign="bottom"  align="right"><input name="btUpload" type="button" class="width74" value="上 传" onClick="checkUploadForm(ifrmUpload.document.form)"></td>
							  </tr>
							  <?php }?>
						  </table></td>
				  </tr>
				</table>
				
				</td>
			<td width="155" height="100%" style="padding-left:5px">
			<table width="100%"  height="100%" border="0" cellpadding="0" cellspacing="0">
		  <tr>
			<td height="21" align="center">预览</td>
		  </tr>
		  <tr>
			<td height="21" align="center" id=tdInfo>&nbsp;</td>
		  </tr>
		  <tr>
			<td id=tdWin valign="top">&nbsp;</td>
		  </tr>
		</table>
		</td>
		  </tr>
		</table>
		<?php
		$this->out_footer();
	}
	
	function show_list(){
		global $AS,$IN;
		
		$parentPath=@dirname($IN['path']);
		if($parentPath=="\\"||$parentPath=="/")$parentPath="";
		
		$this->out_header();
		?>
		<script language=JavaScript>
		parent.currentPath="<?php echo $IN['path']?>"
		parent.init()
		parent.setSelectedIndex()
		var e=<?php echo ($parentPath!=$IN['path']?1:0)."\r\n" ?>
		parent.document.getElementById('imgParent').enabled=e ? 1 : 0
		parent.document.getElementById('imgParent').parentFolder=e ? "<?php echo $parentPath?>" : null
		parent.document.getElementById('imgParent').style.filter=e ? '' : 'gray'
		
		if(!e){
			parent.document.getElementById('imgParent').parentNode.className='upBorder'
			parent.document.getElementById('imgParent').parentNode.style.backgroundColor=""
		}

		var arrFolder=new Array()
		var arrFile=new Array()
		
		document.onkeydown=function(){
			if(event.shiftKey && event.keyCode==78)parent.createNewFolder();
			if(event.keyCode==113)parent.rename();
			if(event.shiftKey && event.keyCode==46)parent.dele();
			if(event.shiftKey && event.keyCode==67)parent.copy();
			if(event.shiftKey && event.keyCode==88)parent.cut();
			if(event.shiftKey && event.keyCode==86)parent.paste();
		}
		</script>

		<body style="background-color:#ffffff;cursor:default" onClick="parent.funSelectIt(null,'','')">
		<table width="100%" height="17" border="0" cellpadding="0" cellspacing="0">
		  <tr bgcolor="#DDDDDD"> 
			<td bordercolor="#DDDDDD"><table width="100%" border="0" cellspacing="0" cellpadding="3">
				<tr> 
				  <td width="70%" bordercolor="#DDDDDD">&nbsp;<strong>名称</strong></td>
				  <td align="right" nowrap><strong>大小</strong></td>
				  <td width="120" nowrap><strong>修改时间</strong>&nbsp;</td>
				</tr>
			  </table>
		</td>
		  </tr>
		<?php
		//chdir($this->upload_dir.$IN['path']);
		if ($dir = @opendir($this->upload_dir.$IN['path'])){
			while (false !== ($bin = readdir($dir))){
				if (!preg_match("/^\.\.?$/i", $bin) && @is_dir($this->upload_dir.$IN['path']."/".$bin)){
					$tmpFolder[$bin] = $bin;
				}else if(!preg_match("/^\.\.?$/i", $bin) && !@is_dir($this->upload_dir.$IN['path']."/".$bin)){
					$tmpFile[$bin] = $bin;
				}
			}
		}
		@closedir($dir);
		if ($tmpFolder){
			//asort($tmpFolder);
			foreach ($tmpFolder as $k => $v){
				?>
				  <tr> 
					<td height="20" onDblClick="parent.changeDir('<?php echo addslashes($v)?>')" onClick="parent.funSelectIt(this,'<?php echo addslashes($v)?>',0);event.cancelBubble=true" onMouseOver="if(parent.whichSelect!=this){this.style.backgroundColor='#EEEEEE';}" onMouseOut="if(parent.whichSelect!=this){this.style.backgroundColor='#FFFFFF';}"> 
					  <table width="100%" height="16" border="0" cellpadding="0" cellspacing="0">
						<tr> 
						  <td width="70%" bordercolor="#FFFFFF"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
							  <tr> 
								<td width="1">&nbsp;</td>
								<td width="20"><img src="<?php echo $this->img_url ?>/folder.gif" width="16" height="16"></td>
								<td nowrap><?php echo htmlspecialchars($v) ?><script language="JavaScript">arrFolder[arrFolder.length]="<?php echo $v ?>"</script></td>
							  </tr>
							</table></td>
						  <td align="right" nowrap bordercolor="#FFFFFF">&nbsp;</td>
						  <td width=120 nowrap bordercolor="#FFFFFF">&nbsp;</td>
						</tr>
					  </table></td>
				  </tr>
				 <?php
			}
		}
		if ($tmpFile){
			//asort($tmpFile);
			$typ=strtolower($IN['type']);
			foreach($tmpFile as $k=>$v){
				$ext=strtolower(substr($v,strrpos($v,".")+1));
				if($typ=="" || preg_match("/^($typ)$/",$ext)){
					$icon=$this->get_file_icon($ext);
					$moditime = date("Y-m-d H:i:s", filemtime($this->upload_dir.$IN['path']."/".$v));

					?>
					  <tr> 
						<td height="20" onClick="parent.funSelectIt(this,'<?php echo addslashes($v)?>',1);parent.preview('<?php echo addslashes($v)?>','<?php echo $icon?>');event.cancelBubble=true" onMouseOver="if(parent.whichSelect!=this){this.style.backgroundColor='#EEEEEE';}" onMouseOut="if(parent.whichSelect!=this){this.style.backgroundColor='#FFFFFF';}"> 
						  <table width="100%" height="16" border="0" cellpadding="0" cellspacing="0">
							<tr> 
							  <td width="70%" bordercolor="#FFFFFF"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
								  <tr> 
									<td width="1">&nbsp;</td>
									<td width="20"><img src="<?php echo $this->img_url ?>/file_icon/<?php echo $icon ?>.gif" width="16" height="16"></td>
									<td nowrap><?php echo htmlspecialchars($v) ?><script language="JavaScript">arrFile[arrFile.length]="<?php echo $v ?>"</script></td>
								  </tr>
								</table></td>
							  <td align="right" nowrap bordercolor="#FFFFFF">
							  <?php
							   
							  echo $this->funFormatSize(filesize($this->upload_dir.$IN['path']."/".$v));
							  ?>&nbsp;</td>
							  <td width=120 nowrap bordercolor="#FFFFFF"><?php echo $moditime ?></td>
							</tr>
						  </table></td>
					  </tr>
					<?php 
				}
			}
		}
		 ?>
		</table>
		<?php
		$this->out_footer();
	}
	
	function upload(){
		global $AS,$admin,$IN;
		$this->out_header();
		?>
		<body>
		<form name="form" method="post" action="<?php echo $this->base_url ?>&act=explorer&code=doup" enctype="multipart/form-data" >
		  <input name="file" type="file"  style="width:100%;">
		  <input type='hidden' name='MAX_FILE_SIZE' value='1000000'>
		  <input type="hidden" name="_target">
		</form>
		<?php
		$this->out_footer();
	}


	//
	function out_header($title='资源管理器'){
		global $AS;
		?>
		<html>
		<head>
		<title><?php echo $title ?></title>
		<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
		<link href="dreampower/includes/style_0.css" rel="stylesheet" type="text/css">
		</head>
		<?php
	}
	function out_footer(){
		echo "</body>
		</html>";
	}
	function get_file_icon($ext){
		
		$icons=array(
			"htm|html|hta|htc|asp|php|aspx|jsp"				=>	'web',
			"jpg|jpeg|jpe|bmp|gif|png|tif|tga"				=>	'img',
			"ra|rpm|m|ram|rmm|rm|mp3|asf|wmv|mov|wav|avi"	=>	'media',
			"swf"											=>	'swf',
			"rar|zip"										=>	'rar',
			"txt" 											=>	'txt',
			"rtf|doc" 										=>	'doc',
			"ppt" 											=>	'ppt',
			"xls" 											=>	'xls',
			"xml" 											=>	'xml',
			"pdf" 											=>	'pdf',
			"mdb" 											=>	'mdb',
			"exe|com" 										=>	'exe',
			"js|jse|vbs|vbe" 								=>	'js',
			".*"											=>	'file',
		);
		
		foreach($icons as $k=>$v){
		
			if(preg_match("/^({$k})$/i",$ext)){
			
				return $v;
				break;
			}
		}
	}
}
?>