<?php

class SableFileBrowser
{
	var $path, $dirs = array(), $files = array(), $dirCount = 0, $fileCount = 0;
	function __construct($path){$this->path = $path;}
	function __destruct(){}
	
	private function getFileIcon($dir, $ext = '')
	{
		if ($dir == true)
			$icon = SABLE_IMAGES_PATH.'filetypes/closed.gif';
		else
		{
			$icon = SABLE_IMAGES_PATH.'filetypes/'.$ext;
			$iconFile = SableSystem::getSableImgPath('filetypes/'.$ext); //get the physical file name as on the server
			if (!file_exists($iconFile)) $icon = SableSystem::getSableImgPath('filetypes/_blank.png'); //default
		}
		return $icon;
	}
	
	public static function humanFileSize($size, $unit="") 
	{
	  if( (!$unit && $size >= 1<<30) || $unit == "GB") return number_format($size/(1<<30),2)."GB";
	  if( (!$unit && $size >= 1<<20) || $unit == "MB") return number_format($size/(1<<20),2)."MB";
	  if( (!$unit && $size >= 1<<10) || $unit == "KB") return number_format($size/(1<<10),2)."KB";
	  return number_format($size)." bytes";
	}
	
	private function getPathFiles(DirectoryIterator $dir)
	{
		/*This function reads the contents of the path given and returns an array representing the contents of the directory*/
		foreach ($dir as $node)
		{
			$fileName = $node->getFilename();
			$fileExt  = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
			
			//check for files to include/exclude:
			$include = true;
			if ($fileExt  == 'php')	$include = false;
			if ($fileExt  == 'js')	$include = false;
			if ($fileName == '..')  $include = false;
			if ($fileName == '.')   $include = false;
			if (strtolower($fileName) == 'thumbs.db')   $include = false;
			if (strtolower($fileName) == 'desktop.ini')	$include = false;
			if (strtolower($fileName) == '_notes')		$include = false;
			
			if ($include)
			{
				if ($node->isDir())
				{
					$this->dirs[$this->dirCount]['file_name'] = $fileName;
					$this->dirs[$this->dirCount]['file_path'] = $node->getPath();
					$this->dirs[$this->dirCount]['file_url']  = $node->getPath().'/'.$fileName;
					SableSystem::convertWorkPathToURLPath($this->dirs[$this->dirCount]['file_url']);
					$this->dirs[$this->dirCount]['file_size'] = self::humanFileSize($node->getSize());
					$this->dirs[$this->dirCount]['file_date'] = date('Y-m-d H:i:s', $node->getMTime());
					$this->dirs[$this->dirCount]['file_icon'] = SableGeneralFunctions::getFileIcon(true);					
					$this->dirCount++;	
				}
				else 
				{
					if ($node->isFile()) 
					{
						$this->files[$this->fileCount]['file_name'] = $fileName;
						$this->files[$this->fileCount]['file_ext']  = $fileExt;
						$this->files[$this->fileCount]['file_path'] = $node->getPath();
						$this->files[$this->fileCount]['file_url']  = $node->getPath().'/'.$fileName;
						SableSystem::convertWorkPathToURLPath($this->files[$this->fileCount]['file_url']);
						$this->files[$this->fileCount]['file_size'] = self::humanFileSize($node->getSize());
						$this->files[$this->fileCount]['file_date'] = date('Y-m-d H:i:s', $node->getMTime());
						$this->files[$this->fileCount]['file_icon'] = SableGeneralFunctions::getFileIcon(false, $fileExt);					
						$this->fileCount++;
					}
				}
			}
		} //for each
	}
	
	public function getDirContents($path = '', $canCreate = false)
	{
		$paramPath = empty($path) ? pathinfo(SABLE_UNKNOWN_IMAGE, PATHINFO_DIRNAME) : $path;
		$this->path = empty($this->path) ? $paramPath : $this->path;
		if (!file_exists($this->path) && $canCreate) mkdir($this->path, 0755, true);
		$var = new DirectoryIterator($this->path);
		$arr = self::getPathFiles(new DirectoryIterator($this->path));
	}
	
}