<?php
/**
 *----------------------------------------------------------------------------*
 * 版权所有 2010-2016 HuCart.COM，并保留所有权利。
 * 此程序是HuCart的一部分。  
 * 根据MIT许可证授权(在程序和软件的所有副本中都必须包含版权声明和许可声明。)
 * 官方网址:http://Www.HuCart.Com
 * 技术支持:http://Www.cheermi.com
 * 对于完整的版权和许可证信息，请参阅MIT-LICENSE.TXT
 * 程序的再发布必须保留以上版权声明。
 *
 *@author pgmk<pgmk@hucart.com>
 *@copyright pgmk<pgmk@hucart.com>
 *@link http://www.hucart.com
 *@license The MIT License.
 *----------------------------------------------------------------------------*
**/
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
class Files_list extends HC_Controller
{
	//注意无须构造函数,使用了构造函数得重构父类 parent::__construct();
	function __construct()
	{
		parent::__construct();
		$this -> load_lang('adminsys/file_list');
		$this -> load('admin'); 
		$this -> admin -> online_admin();
		$this -> cfg_arr["zip"] = array("zip","rar","7z"); //压缩包允许后缀
		
		$this -> cfg_arr["img"] = array("jpg","jpeg","bmp","gif","png"); //img允许后缀
		
		$this -> cfg_arr["flh"] = array("flv","swf"); //flash允许后缀
		
		$this -> cfg_arr["doc"] = array("doc","docx","docm","dotx","dotm");
		
		$this -> cfg_arr["xls"] = array("xls","xlsx","xlsm","xltm","xlsb","xlam","csv","xlw");
									//,"wk4","wk3","wk1","wks","dbf",
		$this -> cfg_arr["ppt"] = array("ppt","pptx","pptm","ppsx","potx","potm","ppam"); 					//office允许后缀
		
		
		//$this -> load('helper');
	}

	function info_list( $frontstr ='' , $page_num = 1)
	{
		$dirval 	= $this -> input -> get["dir"];
		$page_num 	= $this -> input -> get["page_num"];
		$ret_input  = $this -> input -> get["ret_input"];
		
		//var_dump($this);
		//exit();
		//var_dump($this);
		if($dirval!=''){
			$Upload_dir = UPLOADS_DIR.'/'.$dirval; //文件的根目录 UPLOADS_DIR
		}else{
			$Upload_dir = UPLOADS_DIR; //文件的根目录 UPLOADS_DIR
		}
		//$page_size = 30; //没能分类显示
		//echo  $this -> Upload_dir;
		$fileList = $this -> read_dir( $Upload_dir , $frontstr );
		
		$this -> view -> assign('fileList',$fileList);
		$this -> view -> assign('dir',$dirval);
		//分页显示
		$this -> view -> display('files_list.tpl');
		
	}
	
	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);
						//var_dump($this -> cfg_arr);
						if(in_array($imgFileval, $this -> cfg_arr["img"]))
						{
							$imgFile = 'img';
						}elseif(in_array($imgFileval, $this -> cfg_arr["zip"]))
						{
							$imgFile = 'zip';
						}elseif(in_array($imgFileval, $this -> cfg_arr["flh"]))
						{
							$imgFile = 'flh';
						}elseif(in_array($imgFileval, $this -> cfg_arr["doc"]))
						{
							$imgFile = 'doc';
						}elseif(in_array($imgFileval, $this -> cfg_arr["xls"]))
						{
							$imgFile = 'xls';
						}elseif(in_array($imgFileval, $this -> cfg_arr["ppt"]))
						{
							$imgFile = 'ppt';
						}elseif($imgFile ==''){
							$imgFile = 'no';
						}
					
					$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()
	{

		
		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);
						
						$this -> view ->  assign('info_title','文件“' . $filename . '”删除成功！');
						$this -> view ->  assign('URL_TIME',URL_TIME);
						$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=filelist&act=info_list&dir=".$dirName);
						$this -> view ->  display('suggestion.tpl');

					}
				}
				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);
						
						$this -> view ->  assign('info_title','目录“' . $dirName .'/'. $filename . '”删除成功！');
						$this -> view ->  assign('URL_TIME',URL_TIME);
						$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=filelist&act=info_list&dir=".$dirName);
						$this -> view ->  display('suggestion.tpl');
					}
				}
				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 get_admin_page( $url = '/adminsys/?load=filelist&f=info_list', $page_num  , $page_size  , $result_count = '' )
	{
		
	
	}
	
/*
$str='<script type="text/javascript">window.parent.CKEDITOR.tools.callFunction('.$fn.', \''.$fileurl.'\', \''.$message.'\');</script>';
exit($str);
*/

}
/*
$this -> cfg_arr["zip"]=array("zip","rar","7z"); //压缩包允许后缀

$this -> cfg_arr["img"]=array("jpg","jpeg","bmp","gif","png"); //img允许后缀

$this -> cfg_arr["flh"]=array("flv","swf"); //flash允许后缀

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

$Filelist = new Filelist();

 */