<?php

/*
*	Copyright VeryIDE,2007-2010
*	http://www.veryide.com/
*
*	$Id: veryide_files.php,v2 19:47 2008-2-21 Leilei $
*/

require("../../include/header.php");

html_start($c_name."文件管理 - VeryIDE");
?>

<!--body start-->
	<?php
	require(VI_ROOT."/class/ftp.php");
   
    //loader
    loader_setting("naver.stats");
		

	$url = GetQuery("url");
	$action = GetQuery("action");

	//mysql open
	reader_mysql();

	if ($action !=""){
		$list = GetQuery("list");

		switch ($action){

			case "delete":
			
				//检查权限
				$func = "sys-upload-del";
				checker_func($func);				
		
				//查询数据库
				$sql="select id,name,remote from ".VI_DBMANPRE."attach where id in(".$list.")";			
				
				//查询数据库_总记录数
				$result=mysql_query($sql,$mysql);		
				$row_count=mysql_num_rows($result);	
				
				$Project["queries"]++;
				
				while($row=mysql_fetch_assoc($result)){

					//删除数据_记录
					$sql="delete from ".VI_DBMANPRE."attach where id=".$row["id"];
					mysql_query($sql,$mysql);
					
					$Project["queries"]++;
					
					//远程附件
					if( $row["remote"] ){
					
						//连接 FTP
						$ftp = new ClsFTP($Setting["ftp"]["FTP_USER"], $Setting["ftp"]["FTP_PASS"], $Setting["ftp"]["FTP_HOST"], $Setting["ftp"]["FTP_PORT"]);
						
						//删除文件
						$ftp->delete($row["name"]);
						
						//关闭 FTP
						$ftp->close();
			
					}else{
	
						//删除数据_文件
						if(file_exists(VI_ROOT.$row["name"])){
							unlink(VI_ROOT.$row["name"]);
						}
						
					}
				}				
					
				//写入日志
				event_insert($mysql,$func,time(),time(),"删除文件：".$list);				
				
				//reader_redirect($url,"所选文件成功被删除！");
				$Project["message"]=$row_count."个文件成功被删除！";
				
			break;

		}

	}

	$s = GetQuery("s");
	$q = GetQuery("q");
	$f = GetQuery("f");
	$r = GetNumber("r",-1);
	$a = GetNumber("a",0);
	
	//查看方式
	$v = GetQuery("v");
	$v=$v?$v:"list";
	
	//exit($v);


	//日期筛选
	$start = GetQuery("start");
	$end = GetQuery("end");

	?>

	<script type="text/javascript" src="js/mo.calendar.js" charset="utf-8"></script>
    
    <div id="search">

        <!--用于文件操作-->
        <form name="formPost" id="formPost" method="post" action="?">
        <input name="list" id="list" type="hidden" value="">
        <input name="action" id="action" type="hidden" value="" />
        <input name="url" id="url" type="hidden" value="" />
        </form>
        <!--用于文件操作-->
            
        <form name="formSeacrh" id="formSeacrh" method="get" action="?">
			<span class="action">
				<select name="a" id="a" onChange="Mo('#formSeacrh').submit();" ignore="true">
					<optgroup label="所有者" />
					<option value="0">全部人员</option>
					<?php
						echo $VICACHE["sys.admin.option"];
					?>
				</select>
			
				<select name="f" id="f" onChange="Mo('#formSeacrh').submit();">
					<option value="">全部类型</option>
					<option value="image">图片</option>
					<option value="flash">动画</option>
					<option value="media">多媒体</option>
					<option value="files">其它类型</option>	         
				</select>
				<select name="r" id="r" onChange="Mo('#formSeacrh').submit();">
					<option value="-1">全部位置</option>
					<option value="0">本地</option>
					<option value="1">远程</option>
				</select>
            </span>
            
            <select name="s" id="s">
                <option value="name">文件名</option>
                <option value="account">用户</option>
                <option value="input">来源</option>
                <option value="type">类型</option>
            </select>
            <input name="q" type="text" class="text" id="q" value="<?php echo $q; ?>">
            
            <input name="start" type="text" class="date" id="start" value="<?php echo $start;?>" size="12" readonly="true" onclick="return showCalendar(this.id,'y-mm-dd');" title="年月日">
            -
            <input name="end" type="text" class="date" id="end" value="<?php echo $end;?>" size="12" readonly="true" onclick="return showCalendar(this.id,'y-mm-dd');" title="年月日">
            
            <input type="hidden" name="v" value="<?php echo $v;?>" />
            <button type="submit" class="go"></button>

        </form>
        <script type="text/javascript">
			Mo("#f").value("<?php echo $f;?>");
			Mo("#s").value("<?php echo $s;?>");
			Mo("#r").value("<?php echo $r;?>");
			Mo("#a").value("<?php echo $a;?>");
        </script>

    </div>
    
	<?php 
    
    $sql="SELECT * FROM ".VI_DBMANPRE."attach where 1=1";

    if ($q && $s) $sql.=" and  ".$s." like '%".$q."%'";
	
	if ( $r != -1 ) $sql.=" and  remote = ".$r."";
	
	switch( $v ){
		
		case "album":
			$sql.=" and type in ('".implode("','",$Upload['image'])."')";
		break;
		
		case "flash":
			$sql.=" and type = 'swf' ";
		break;
		
		case "list":
			$sql.=" and type in ('".implode("','",$Upload['image'])."')";
		break;
		
	}

	if ($a){
		$sql.=" and  aid ='".$a."'";
	}
	
	//exit($sql);
    
    if ($start && $end){
    
        //开始时间
        $e_array=explode("-",$start);
        $t1=mktime(0, 0, 0, $e_array[1], $e_array[2], $e_array[0]);
        
        //到期时间
        $e_array=explode("-",$end);
        $t2=mktime(23, 59, 59, $e_array[1], $e_array[2], $e_array[0]);
        
        $sql.=" and  dateline >=".$t1." and dateline <= ".$t2;
    }

    $sql.=" order BY id desc";
	
	//echo $sql;
    
    //查询数据库_总记录数
    $row_count = $db -> getValue( str_replace("*","count(id)",$sql) );   

	
	$Project["queries"]++;
    
    $page=GetPage("page");
    $page_start=$Global["pagesize"]*($page-1);
    $sql=$sql." limit $page_start,".$Global["pagesize"];
           
    //$img_types = explode("|",$veryide_image_type);

    $result=mysql_query($sql,$mysql);
	
	$Project["queries"]++;
    
    /********************/
    
    //分页链接
    $url="?q=".$q."&s=".$s."&f=".$f."&a=".$a."&start=$start&end=$end&v=$v&page=";
    
    echo '
    <p id="viewer">
        <strong>查看分类：</strong> 
		<a href="'.preg_replace("/&v=(.*?)&/","&v=list&",$url).'" '.($v=="list"?'class="active"':'').'>全部</a>
		<a href="'.preg_replace("/&v=(.*?)&/","&v=album&",$url).'" '.($v=="album"?'class="active"':'').'>图片</a>
		<a href="'.preg_replace("/&v=(.*?)&/","&v=flash&",$url).'" '.($v=="flash"?'class="active"':'').' onclick="return confirm(\'数据较多时可能会比较慢，确定要继续浏览吗？\');">动画</a>
    </p>
    ';
    
    /********************/
	
	$active = array('list','album','flash');
		
    if( in_array($v,$active) ){
    
    	require("include/attach.".$v.".php");
	
	}
    
    /********************/
    
	
	//mysql close
	reader_mysql(true);	
	
	?>
    
    <div id="saving">
		<?php
            print GetPageLink($page,$row_count,$Global["pagesize"],$url,"page");
        ?>
    </div>

<?php html_close();?>