<?php
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
//上传图库管理
class Image
{
	var $Upload_dir  = '';
	var $imagedir	 = '';
	function Image()
	{
		//$GLOBALS['WebSe'] -> isOnLine();
		$this -> Upload_dir = UPLOADS_DIR.'/'.$_SESSION["emplyeeUser"]["use_id"];
		$this -> imagedir 	= IMG_URL.'/'.$_SESSION["emplyeeUser"]["use_id"].'/';
	}
	
	function infolist( $frontstr ='' , $page_num = 1)
	{
		
		$frontstr = $_REQUEST["dir"];
		$page_num = $_REQUEST["page_num"];
 
		
		
		$fileList = $this -> read_dir( $this -> Upload_dir , $frontstr );
		
		$this -> view -> assign('fileList',$fileList);
		

		//分页显示
		$this -> view -> display('imagelist.tpl');
		
	}
	
	function read_dir( $dir ='' ,  $frontstr = '' )
	{
	  
	   $fileList = array();

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

	   while(false !== ($entry = @$d -> read()))
	   {
		   if($entry!='.' && $entry!='..')
		   {
 
			   if(is_dir($rootdir.'/'.$entry))
			   {
			   		
					//IMG_SMALLWIDTH IMG_SMALLHEIGHT
					//echo $entry;
					
					if(strchr($entry,IMG_SMALLWIDTH.'X'.IMG_SMALLHEIGHT))
					{
						//echo 'X';
					}else{
						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';
							
							//是图片现示
							
							$fileSize = filesize($rootdir.'/'. $entry);
							
							$fileSize = ($fileSize >= 1000000) ? round($fileSize / 1000000, 2) . ' Mb' : (($fileSize >= 1000) ? round($fileSize / 1000, 2) . ' Kb' : $fileSize . ' b');
							
							if(strchr($entry,IMG_SMALLWIDTH.'X'.IMG_SMALLHEIGHT))
							{
								//echo 'X';
								//echo $entry;
								
							}else{
								
		 
								if(file_exists($GLOBALS["Helpe"] -> build_img($this -> Upload_dir.'/'.$frontstr.'/'.$entry)))
								{
									$entrysmall = $GLOBALS["Helpe"] -> build_img($frontstr.'/'.$entry);	
								}else{
									$entrysmall = false;						
								}
								if($frontstr!=''){
									$this -> view -> assign('imagedir',$this -> imagedir.$frontstr.'/');
								}else{
									$this -> view -> assign('imagedir',$this -> imagedir);
								}
								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)),
														'filesmall' => $this -> imagedir.$entrysmall );
														
														
								}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)),
														'filesmall' => $this -> imagedir.$entrysmall );
								
								}
								
							}	
							
							
						}elseif($imgFile =='')
						{
							$imgFile = 'no';
							//非图片不显示
						}
					

						
					
					

			   }
		   }
	   }
	   
	   $d->close();
	   return $fileList;
	}
	function del()
	{

		
		if($_REQUEST["deltype"]!=''){
			$deltype = $_REQUEST["deltype"];
		}else{
			$deltype = 'delFile';
		}
		$dirName  = $_REQUEST["dir"];
		$filename = $_REQUEST["name"];
		
		
		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=image&act=infolist&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=image&act=infolist&dir=".$dirName);
						$this -> view ->  display('suggestion.tpl');
					}
				}
				break;
		}
	}



	function deleteDir($dir_name)
	{
		$this -> Upload_dir = UPLOADS_DIR."/".$_SESSION["emplyeeUser"]["use_id"].'/';

		$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);
	}



	


		
}

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