<?php

function getFileIcon__($dir, $ext = '')
{
	if ($dir == true)
		$icon = SABLE_IMAGES_PATH.'filetypes/closed.gif';
	else
	{
		$icon = SABLE_IMAGES_PATH.'filetypes/'.$ext.'.png';
		$iconFile = SABLE_WORKING_PATH.'media/images/filetypes/'.$ext.'.png'; //get the physical file name as on the server
		if (!file_exists($iconFile)) $icon = SABLE_IMAGES_PATH.'filetypes/_blank.png'; //default
	}
	return $icon;
}

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";
}

function getPathFiles(DirectoryIterator $dir, &$dirs, &$files, &$countDir, &$countFile)
{
	/*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())
			{
				$dirs[$countDir]['file_name'] = $fileName;
				$dirs[$countDir]['file_path'] = $node->getPath();
				$dirs[$countDir]['file_size'] = humanFileSize($node->getSize());
				$dirs[$countDir]['file_date'] = date('Y-m-d H:i:s', $node->getMTime());
				$dirs[$countDir]['file_icon'] = SableGeneralFunctions::getFileIcon(true);					
				$countDir++;	
			}
			else 
			{
				if ($node->isFile()) 
				{
					$files[$countFile]['file_name'] = $fileName;
					$files[$countFile]['file_ext']  = $fileExt;
					$files[$countFile]['file_path'] = $node->getPath();
					$files[$countFile]['file_size'] = humanFileSize($node->getSize());
					$files[$countFile]['file_date'] = date('Y-m-d H:i:s', $node->getMTime());
					$files[$countFile]['file_icon'] = SableGeneralFunctions::getFileIcon(false, $fileExt);					
					$countFile++;
				}
			}
		}
	} //for each
}

function getDirContents($path = '', &$dirs, &$files, &$countDir, &$countFile)
{
	$rootPath = empty($path)?pathinfo(SABLE_THEME_WORKING_PATH, PATHINFO_DIRNAME):$path;
	$var = new DirectoryIterator($rootPath);
	$arr = getPathFiles(new DirectoryIterator($rootPath), $dirs, $files, $countDir, $countFile);
}
?>