<?
	/**
	 * Displays a list of files which user can choose to edit.
	 * User can also traverse directories.
	 *
	 * @author	$Author: suraj $
	 * @version	$Revision: 1.4 $, $Date: 2003/02/11 03:53:38 $
	 */

	 /**
	  * Functions for this script.
	  */
		 
		/**
		 * Returns the folder path only
		 * ex: "c:/test.html" returns "c:/"
		 */
		function getFolderPath($path)
		{
			if(is_dir($path)) return $path;
	
			$path = realpath($path);
			return substr($path, 0, strpos($path, basename($path)));
		}
		
		/**
		 * Converts black slashes into forward slashes.
		 */
		function fixSlash($string)
		{
			$string = str_replace( "\\", "/", $string );
			$string = str_replace( "//", "/", $string );
			
			return $string;
		}
	
		/**
		 * Returns the extension of a file
		 * ex: "test.html" returns "html"
		 */
		function getFileExtension($filename)
		{
			return substr(strrchr($filename, "."), 1);
		}
	
		/**
		 * Returns a HTML image code with the image source
		 * as the given url.
		 */
		function fileIcon($icon_url)
		{
			return "<img src=\"$icon_url\" alt=\"\" border=\"0\" hspace=\"7\">";
		}
	
	
	
	/**
	 * Start of browser script.
	 */
	
	require_once("assets/includes/config.inc.php");
	
	// check if path was set
	if(!isset($path))
	{
		$path = $cfgVars['BROWSER_ROOT_PATH']; // default path
	}

	// check for path beyond top-most allowed directory
	if(!strstr($path, realpath($cfgVars['BROWSER_ROOT_PATH'])))
	{
		$path = $cfgVars['BROWSER_ROOT_PATH'];
	}
	
	// boolean to check if the path is ready to be read from
	$path_ok = file_exists($path) && is_dir($path) && is_readable($path);
	
	// start displaying page
	$page_title = $appVars['name'] . " - File Browser";
	include("assets/includes/page-header.inc.php");
?>
<script language="javascript" type="text/javascript" src="assets/libraries/browser.lib.js"></script>
<script language="javascript">
<!--
	function init()
	{
	}
//-->
</script>

<h1>File Browser <tt class="filepath"><?= $path ?></tt><br>
<div class="info">
	[ <a href="index.php">Return to TextEditor</a> ]
	[ <a href="#" onclick="browseTo()">Browse Path</a> ]
</div></h1>

<table class="box" border="0" cellspacing="0" cellpadding="5" width="100%">
<tr style="font: bold 14pt; background: beige; align: center">
	<td>Name</td>
	<td>Size</td>
	<td>Last Modified</td>
	<td colspan="4">Options</td>
</tr>
<tr style="font-weight: bold" onMouseOver="hoverOn_row(this)" onMouseOut="hoverOff_row(this)">
	<td colspan="3"><a href="?path=<?= $path ?>/.."><?= fileIcon($cfgVars['FILE_ICONS']['back']) ?> Parent Directory</a></td>
	<td>Edit</td>
	<td>View</td>
	<td>Download</td>
	<td>Upload</td>
</tr>
<?
	if($path_ok)
	{
		$fileArray = array();
		
		/**
		 * Prepare a list of files.
		 */
		
			$theDir = opendir($path);
			
			readdir($theDir); // read off '.'
			readdir($theDir); // read off '..'
			
			while(false !== ($fileInDir = readdir($theDir)))
			{
				array_push($fileArray, $fileInDir);
			}
			
			closedir($theDir);
		
		
		
		/**
		 * Process the information
		 */
		
			// sort file array alphabetically
			natcasesort($fileArray);
			
			// temporaray variables
			$physical_file = $virtual_file = $file_link = $is_dir = $output =  "";
			$row_count = 0;
			
			// file icons to be used in display
			$fileIcons = array();
			foreach($cfgVars['FILE_ICONS'] as $icon_type => $icon_path)
			{
				$fileIcons[$icon_type] = fileIcon($icon_path);
			}
			
			foreach($fileArray as $fileInDir)
			{
				$physical_file = fixSlash("$path/$fileInDir"); // get the physical file path
				$is_dir = is_dir($physical_file); // highly used boolean, so I stored it in a var
	
				//echo "<pre>". $cfgVars['BROWSER_ROOT_PATH'] ."<br>". $cfgVars['BROWSER_ROOT_URL'] ."<br>". $physical_file ."<br></pre><hr>";
				$virtual_file = str_replace($cfgVars['BROWSER_ROOT_PATH'], $cfgVars['BROWSER_ROOT_URL'], $physical_file);
				$file_link = ( $is_dir ?  $fileIcons['dir'] : $fileIcons['file'] ) . $fileInDir;
	
				// prepare output for this row
				$output .= "<tr".( ($row_count % 2 == 0) ? " style=\"background: whitesmoke\"" : "")." onMouseOver=\"hoverOn_row(this)\" onMouseOut=\"hoverOff_row(this)\">\n";
				$output .= "<td>". ( $is_dir ? "<a href=\"?path=$physical_file\">$file_link</a>" : "$file_link") ."</td>\n";
				$output .= "<td>". ( $is_dir ? "-" : round(filesize($physical_file)/1000, 2) ." kb") ."</td>\n";
				$output .= "<td>". date("d-M-Y, h:i A", fileatime($physical_file)) ."</td>\n";
				$output .= "<td>". ( $is_dir ? "-" : (in_array(getFileExtension($physical_file), $cfgVars['FILE_TYPES_IGNORE']) ? "-" : "<a href=\"index.php?path=$physical_file\">Edit</a>")) ."</td>\n"; //<img src=\"include/img/edit.gif\" alt=\"Edit\" border=\"0\">
				$output .= "<td>". ( $is_dir ? "-" : "<a href=\"$virtual_file\" target=\"_blank\">View</a>") ."</td>\n";
				$output .= "<td>". ( $is_dir ? "-" : "<a href=\"download.php?path=$physical_file\">Download</a>") ."</td>\n";
				$output .= "<td>". ( $is_dir ? "-" : "<a href=\"upload.php?path=$physical_file\" target=\"_blank\">Upload</a>") ."</td>\n";
				$output .= "</tr>\n";
	
				$row_count++;
			}
			
			// display all rows
			echo $output;
	}
?>
</table><br>
<?
	include("assets/includes/page-footer.inc.php");
?>
