<?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 User_file extends HC_Controller
{
	//注意无须构造函数,使用了构造函数得重构父类 parent::__construct();
	private $user_dir = '/users';
	private $url_dir =  '/users';
	function __construct()
	{
		parent::__construct();
		$this -> load_lang('user/user_file');
		$this -> load('user');
		$this -> user -> online_user();
		$this -> set_user_dir(); //设置用户目录
		$this -> build_dir();
		$this -> load('helper');
		
		$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');
	}
	private function set_user_dir()
	{
		if($_SESSION['user']['use_id']){
			$this -> user_dir = UPLOADS_DIR.'/users/'.$_SESSION['user']['use_id'];
			$this -> url_dir = IMG_URL.'/users/'.$_SESSION['user']['use_id'];
		}else{
			exit('Access Violation');
		}
	}
 

	public function info_list( $frontstr ='' , $page_num = 1)
	{
		//$page_size = 30; //没能分类显示
		
		$file_list = $this -> read_dir( $this -> user_dir , $frontstr );
		
		$this -> view -> assign('get_id',trim($_REQUEST['get_id']));
		$this -> view -> assign('file_list',$file_list);
		$this -> view -> assign('image_url',$this -> url_dir);
		//分页显示
		$this -> view -> display('user_file.tpl');
		
	}
	private function build_dir($mode = 0755 )
	{
		if (is_dir($this -> user_dir) || @mkdir($this -> user_dir,$mode)) return true;
		if (!mk_dir(dirname($this -> user_dir),$mode)) return false;
		return @mkdir($this -> user_dir,$mode);
	}
	
	private function read_dir( $dir ='' ,  $front_str = '' )
	{
	  
	   $file_list = array();

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

	   @$d = dir($rootdir);
		
	   while(false !== ($entry = @$d -> read()))
	   {
		   if($entry!='.' && $entry!='..')
		   {
 
			   if(is_dir($rootdir.'/'.$entry))
			   {

					if(strchr($entry,IMG_SMALLWIDTH.'X'.IMG_SMALLHEIGHT))
					{
						//echo 'X';
					}else{
						if($front_str!='')
						{
							$file_list['0000' . $entry] = array('file_name'  => $entry,
															   'file_name_url' => $front_str.'/'.urlencode($entry),
															   ///'dir_url' => $front_str.urlencode($entry),
															   'is_dir'    => '1');
						}else{
							$file_list['0000' . $entry] = array('file_name'  => $entry,
															   'file_name_url' => urlencode($entry),
															   ///'dir_url' => $front_str.urlencode($entry),
															   'is_dir'    => '1');
						}
					}
				
				//这是目录处理
			   }else{
						@$fileAttribute = getimagesize($rootdir.'/'.$entry);
						$img_file = '';
						$fileExt = explode('.', $entry);
						$fileExt = $fileExt[count($fileExt) - 1];
						$img_file_val = strtolower($fileExt);

						if(in_array($img_file_val, $this -> cfg_arr["img"]))
						{
							$img_file = 'img';
							
							//是图片现示
							
							$file_size = filesize($rootdir.'/'. $entry);
							
							$file_size = ($file_size >= 1000000) ? round($file_size / 1000000, 2) . ' Mb' : (($file_size >= 1000) ? round($file_size / 1000, 2) . ' Kb' : $file_size . ' b');
							
							if(strchr($entry,IMG_SMALLWIDTH.'X'.IMG_SMALLHEIGHT))
							{
								//echo 'X';
								//echo $entry;
								
							}else{
								
		 
								if(file_exists($this -> helper ->  build_img($this -> upload_dir.'/'.$front_str.'/'.$entry)))
								{
									$entrysmall = $this -> helper ->  build_img($front_str.'/'.$entry);	
								}else{
									$entrysmall = false;						
								}
								if($front_str!=''){
									$this -> view -> assign('image_url',$this -> image_url.$front_str.'/');
								}else{
									$this -> view -> assign('image_url',$this -> image_url);
								}
								if($front_str!='')
								{
									$file_list[$entry] = array('file_name'  => $entry,
														'is_dir'    => '0',
														'file_size'  => $file_size,
														'file_name_url' => urlencode($entry),
														'dir_url' => $front_str.'/',
														'file_type'  => $fileAttribute['mime'],
														'img_file'   => $img_file,
														'file_XYWH'  => $fileAttribute[0] ? $fileAttribute[0] . ' X ' . $fileAttribute[1] : '',
														//'filem_time' => date('Y-m-d H:i', filemtime($rootdir.'/'.$entry)),
														'filesmall' => $this -> image_url.$entrysmall );
														
														
								}else{
									$file_list[$entry] = array('file_name'  => $entry,
														'is_dir'    => '0',
														'file_size'  => $file_size,
														'file_name_url' => urlencode($entry),
														'dir_url' => $front_str,
														'file_type'  => $fileAttribute['mime'],
														'img_file'   => $img_file,
														'file_XYWH'  => $fileAttribute[0] ? $fileAttribute[0] . ' X ' . $fileAttribute[1] : '',
														//'filem_time' => date('Y-m-d H:i', filemtime($rootdir.'/'.$entry)),
														'filesmall' => $this -> image_url.$entrysmall );
								
								}
								
							}	
							
							
						}elseif($img_file =='')
						{
							$img_file = 'no';
							//非图片不显示
						}	

			   }
		   }
	   }
	   
	   //var_dump($file_list);
	   
	   $d->close();
	   return $file_list;
	}
	
		
	private function del()
	{

		
		if($_REQUEST["deltype"]!=''){
			$deltype = $_REQUEST["deltype"];
		}else{
			$deltype = 'delFile';
		}
		$dirName  = $_REQUEST["dir"];
		$filename = $_REQUEST["name"];
		
		$this -> user_dir = UPLOADS_DIR."/"; //文件的根目录
		
		switch($deltype)
		{
			case 'delFile':
				//删除文件../../image/
				if( $filename != '' )
				{
					if(file_exists($this -> user_dir.$dirName . $filename))
					{
						unlink($this -> user_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 -> user_dir. $dirName . $filename;
					if(file_exists( $this -> user_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 up_load_file(){
		$add_dir = $this -> input ->  req["add_dir"];
		$get_id = $this -> input ->  req["get_id"];
	 
        //上传文件
        $up_file = $_FILES['up_file'];
		//var_dump($up_file);
        if($up_file['type'] == 'image/pjpeg' || $up_file['type'] == 'image/gif'
        || $up_file['type'] == 'image/x-png' || $up_file['type'] == 'image/png'
        || $up_file['type'] == 'image/jpeg' || $up_file['type'] == 'application/x-shockwave-flash')
        {
            $fileExt = explode('.', $up_file['name']);
			$ext_arr = array("jpeg", "jpg","png","JPEG","JPG", "GIF", "PNG");
			if(in_array($fileExt, $ext_arr))
			{
				$fileExt = $fileExt[count($fileExt) - 1];
				$filename = $dirName .  time() . '.' . $fileExt;
				echo $this -> user_dir.$filename;
				copy($up_file['tmp_name'], $this -> user_dir.'/'.$filename);
				@chmod($filename, 0600);
				echo  '<script language="javascript">alert(\'文件上传成功！\');</script>';
				echo  '<script language="javascript">window.opener.document.getElementById(\''.$get_id.'\').value="'.$this -> image_url.'/'.$add_dir.'/'.$filename.'";</script>';
				echo  '<script language="javascript">window.opener = null;</script>';
				echo  '<script language="javascript">window.open(\'\',\'_top\');</script>';
				echo  '<script language="javascript">window.top.close();</script>';
				exit();
			}else{
				echo    '<script language="javascript">alert(\'非法文件上传，只能上传“word文档”、“xls文档”、“.gif”、“.jpg”、“.png”、“.swf”文件！\')</script>'; 
				echo '<script language="javascript">history.back(-1);</script>'; 
				exit();
			}	
        }else
        {
          echo    '<script language="javascript">alert(\'非法文件上传，只能上传“word文档”、“xls文档”、“.gif”、“.jpg”、“.png”、“.swf”文件！\')</script>'; 
		  echo '<script language="javascript">history.back(-1);</script>';
		  exit();
        }
	
	}



}
 