<?php
if (!defined('GV32_COM')) exit('GV32.COM No direct script access allowed');
/**
 *----------------------------------------------------------------------------
 * 版权所有 2005-2010 GV32.COM，并保留所有权利。
 * 网站地址: http://www.gv32.com；
 * 作者: GV32.COM
**/
class Filelist
{
	var $Upload_dir = '';
	function Filelist()
	{
		//var_dump($_REQUEST);
		/* 
		var_dump($_REQUEST);
array(7) { ["load"]=> string(8) "filelist" ["CKEditor"]=> string(11) "pro_content" ["CKEditorFuncNum"]=> string(1) "2" ["langCode"]=> string(5) "zh-cn" ["bdshare_firstime"]=> string(13) "1358850287750" ["PHPSESSID"]=> string(32) "307a4776d7872372e8f05d7f13998723" ["ck_num"]=> string(32) "019fa4fdf1c04cf73ba25aa2223769cd" } 	
		*/
		$act = $_REQUEST["act"];
		if($act==''){
		$this -> infolist();
		}
		
	}
	function infolist( $frontstr ='' , $pageNum = 1)
	{
		
		$frontstr = $_REQUEST["dir"];
		$pageNum = $_REQUEST["pageNum"];
		
		$this -> Upload_dir = UPLOADS_DIR; //文件的根目录 UPLOADS_DIR
		
		//$pageSize = 30; //没能分类显示
		
		$fileList = $this -> read_dir( $this -> Upload_dir , $frontstr );
		
		$GLOBALS['Templ']->assign('fileList',$fileList);

		//分页显示
		$GLOBALS['Templ']->display('filelist_tpl.html');
		
	}
	
	function read_dir( $dir ='' ,  $frontstr = '' )
	{
	   $fileList = array();

	   if($frontstr !=''){ $rootdir = $dir."/".$frontstr; }else{ $rootdir = $dir; }
	   $rootdir;
	   @$d = dir($rootdir);

	   while(false !== ($entry = @$d -> read()))
	   {
		   if($entry!='.' && $entry!='..')
		   {
 
			   if(is_dir($rootdir.'/'.$entry))
			   {
			   		
					if($frontstr!='')
					{
						$fileList['0000' . $entry] = array('fileName'  => $entry,
														   'fileName_url' => $frontstr.'/'.urlencode($entry),
														   ///'dir_url' => $frontstr.urlencode($entry),
														   'is_dir'    => '1');
					}else{
						$fileList['0000' . $entry] = array('fileName'  => $entry,
														   'fileName_url' => urlencode($entry),
														   ///'dir_url' => $frontstr.urlencode($entry),
														   'is_dir'    => '1');
					}
					
				//这是目录处理
			   }else{
						@$fileAttribute = getimagesize($rootdir.'/'.$entry);
						$imgFile = '';
						$fileExt = explode('.', $entry);
						$fileExt = $fileExt[count($fileExt) - 1];
						$imgFileval = strtolower($fileExt);

						if(in_array($imgFileval, $GLOBALS["cfg"]["img"]))
						{
							$imgFile = 'img';
						}elseif(in_array($imgFileval, $GLOBALS["cfg"]["zip"]))
						{
							$imgFile = 'zip';
						}elseif(in_array($imgFileval, $GLOBALS["cfg"]["flh"]))
						{
							$imgFile = 'flh';
						}elseif(in_array($imgFileval, $GLOBALS["cfg"]["doc"]))
						{
							$imgFile = 'doc';
						}elseif(in_array($imgFileval, $GLOBALS["cfg"]["xls"]))
						{
							$imgFile = 'xls';
						}elseif(in_array($imgFileval, $GLOBALS["cfg"]["ppt"]))
						{
							$imgFile = 'ppt';
						}elseif($imgFile ==''){
							$imgFile = 'no';
						}

					//echo $imgFile;
					//echo $dir .'/'. $entry;
					
					$fileSize = filesize($rootdir.'/'. $entry);
					
					$fileSize = ($fileSize >= 1000000) ? round($fileSize / 1000000, 2) . ' Mb' : (($fileSize >= 1000) ? round($fileSize / 1000, 2) . ' Kb' : $fileSize . ' b');
					
					if($frontstr!=''){
					$fileList[$entry] = array('fileName'  => $entry,
										'is_dir'    => '0',
										'fileSize'  => $fileSize,
										'fileName_url' => urlencode($entry),
										'dir_url' => $frontstr.'/',
										'fileType'  => $fileAttribute['mime'],
										'imgFile'   => $imgFile,
										'fileXYWH'  => $fileAttribute[0] ? $fileAttribute[0] . ' X ' . $fileAttribute[1] : '',
										'filemTime' => date('Y-m-d H:i', filemtime($rootdir.'/'.$entry)));
					}else{
					$fileList[$entry] = array('fileName'  => $entry,
										'is_dir'    => '0',
										'fileSize'  => $fileSize,
										'fileName_url' => urlencode($entry),
										'dir_url' => $frontstr,
										'fileType'  => $fileAttribute['mime'],
										'imgFile'   => $imgFile,
										'fileXYWH'  => $fileAttribute[0] ? $fileAttribute[0] . ' X ' . $fileAttribute[1] : '',
										'filemTime' => date('Y-m-d H:i', filemtime($rootdir.'/'.$entry)));
					
					}
										
					

			   }
		   }
	   }
	   
	   $d->close();
	   return $fileList;
	}
	
	function del()
	{
		//var_dump($_REQUEST);
		/*
array(8) { ["load"]=> string(8) "filelist" ["act"]=> string(3) "del" ["dir"]=> string(0) "" ["name"]=> string(9) "Thumbs.db" ["deltype"]=> string(7) "delFile" ["bdshare_firstime"]=> string(13) "1358850287750" ["PHPSESSID"]=> string(32) "307a4776d7872372e8f05d7f13998723" ["ck_num"]=> string(32) "019fa4fdf1c04cf73ba25aa2223769cd" } 
		*/
		
		if($_REQUEST["deltype"]!=''){
			$deltype = $_REQUEST["deltype"];
		}else{
			$deltype = 'delFile';
		}
		$dirName  = $_REQUEST["dir"];
		$filename = $_REQUEST["name"];
		
		$this -> Upload_dir = UPLOADS_DIR."/"; //文件的根目录
		
		switch($deltype)
		{
			case 'delFile':
				//删除文件../../image/
				if( $filename != '' )
				{
					if(file_exists($this -> Upload_dir.$dirName . $filename))
					{
						unlink($this -> Upload_dir.$dirName . $filename);
						
						$GLOBALS['Templ'] -> assign('infomessage','文件“' . $filename . '”删除成功！');
						$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
						$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=filelist&act=infolist&dir=".$dirName);
						$GLOBALS['Templ'] -> display('suggestion_tpl.html');

					}
				}
				break;

			case 'delDir':
			
				//删除文件目录

				if($filename != '')
				{
					//echo  $this -> Upload_dir. $dirName . $filename;
					if(file_exists( $this -> Upload_dir. $dirName . $filename) )  //if(file_exists($delete_filetype) && substr($delete_filetype, 0, 7) == UPLOADDIR)
					{
						$this -> deleteDir($dirName . $filename);
						
						$GLOBALS['Templ'] -> assign('infomessage','目录“' . $dirName .'/'. $filename . '”删除成功！');
						$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
						$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=filelist&act=infolist&dir=".$dirName);
						$GLOBALS['Templ'] -> display('suggestion_tpl.html');
					}
				}
				break;
		}
	}
	
	
	function deleteDir($dir_name)
	{
		$this -> Upload_dir = UPLOADS_DIR."/";

		$Dir = dir($this -> Upload_dir.$dir_name);
		while($entry = $Dir->read())
		{
			if($entry != '' && $entry != '.' && $entry != '..' && $entry != './' && $entry != '../')
			{
				//如果是目录
				$this -> Upload_dir . $dir_name.'/'.$entry;

				if(is_dir($this -> Upload_dir . $dir_name.'/'.$entry))
				{
					$this -> deleteDir( $dir_name .'/'. $entry );
				}else
				{
					unlink( $this -> Upload_dir .$dir_name.'/'. $entry );
				}
			}
		}
		
		$Dir->close();
		
		rmdir($this -> Upload_dir.$dir_name);
	}
	
	
	function buildPageStr( $url = '/adminsys/?load=filelist&f=infolist', $pageNum  , $pageSize  , $resultCount = '' )
	{
		
	
	}
	
/*
$str='<script type="text/javascript">window.parent.CKEDITOR.tools.callFunction('.$fn.', \''.$fileurl.'\', \''.$message.'\');</script>';
exit($str);
*/

}
$GLOBALS["cfg"]["zip"]=array("zip","rar","7z"); //压缩包允许后缀

$GLOBALS["cfg"]["img"]=array("jpg","jpeg","bmp","gif","png"); //img允许后缀

$GLOBALS["cfg"]["flh"]=array("flv","swf"); //flash允许后缀

$GLOBALS["cfg"]["doc"]=array("doc","docx","docm","dotx","dotm");
$GLOBALS["cfg"]["xls"]=array("xls","xlsx","xlsm","xltm","xlsb","xlam","csv","xlw");
							//,"wk4","wk3","wk1","wks","dbf",
$GLOBALS["cfg"]["ppt"]=array("ppt","pptx","pptm","ppsx","potx","potm","ppam"); 					//office允许后缀

$Filelist = new Filelist();

 
?>

