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

	 /**
	  * Functions for this script.
	  */
		/**
		 * Converts black slashes into forward slashes.
		 */
		function fwdSlashes($string)
		{
			$string = str_replace( "\\", "/", $string );
			$string = str_replace( "//", "/", $string );
			
			return $string;
		}
	
		/**
		 * Returns a HTML image code with the image source
		 * as the given url.
		 */
		function fileIcon($icon)
		{
			global $cfgVars;
			
			return "<img src=\"". $cfgVars['FILE_ICONS'][$icon] ."\" alt=\"$icon\" title=\"$icon\" border=\"0\" hspace=\"7\">";
		}
	
		/**
		 * Returns the extension of a file
		 * ex: "test.html" returns "html"
		 */
		function getFileExt($filename)
		{
			return substr(strrchr($filename, "."), 1);
		}
	
	
	/**
	 * Start of browser script.
	 */
	
	require_once("assets/includes/config.inc.php");
	
	// check if path was set
	if(isset($path) && $path != "")
	{
		$path = realpath($path); // convert user input to a valid path
	}
	else
	{
		$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" type="text/javascript">
<!--
	function init() {}
//-->
</script>

<tt class="filepath"><?= $path ?></tt>

<hr>

<!-- file list -->
<table class="browserTable" border="0" cellspacing="0" cellpadding="2" width="100%">
<tr class="rowHead">
	<td>Name</td>
	<td>Size</td>
</tr>
<tr onmouseover="hoverOn_row(this)" onmouseout="hoverOff_row(this)">
	<td colspan="2"><a href="?path=<?= realpath($path ."/..") ?>"><?= fileIcon('back') ?>Parent Directory</a></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);
			
			// prepare output
			
			$output = "";
			
			for($i=0; $i<count($fileArray); $i++)
			{
				// get the physical file path
				
					$filePath = fwdSlashes($path ."/". $fileArray[$i]);
					
					// frequently used boolean, so I stored it in a var
					$is_dir = is_dir($filePath);
					
					// http link
					$virtual_file = str_replace($cfgVars['BROWSER_ROOT_PATH'], $cfgVars['BROWSER_ROOT_URL'], $filePath);
				
				// prepare output for this row
				
					$output .= "<tr class=\"".($i%2 == 0 ? "row" : "rowAlt")."\" onmouseover=\"hoverOn_row(this)\" onmouseout=\"hoverOff_row(this)\">\n";
					
					// display file/dir name and link
					
						if($is_dir)
						{
							$output .= "<td><a href=\"?path=". $filePath ."\">". fileIcon('dir') . $fileArray[$i] ."</a></td>";
						}
						else
						{
							if(in_array(getFileExt($filePath), $cfgVars['FILE_TYPES_IGNORE']))
							{
								$output .= "<td>". fileIcon('binary') . $fileArray[$i] ."</td>";
							}
							else
							{
								$output .= "<td><a href=\"editor.php?path=". $filePath ."\" target=\"editor\">". fileIcon('file') . $fileArray[$i] ."</a></td>"; 
							}
						}
						
					// display file size
					
						if($is_dir)
						{
							$output .= "<td>-</td>";
						}
						else
						{
							$output .= "<td>". fileSizeLabel($filePath) ."</td>";
						}
				
				/*
				$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(getFileExt($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";
			}
			
			// display all rows
			echo $output;
	}
?>
</table>
<!--// file list -->
<hr>
<form method="get" action="">
	Path:<br>
	<input class="pathInput" type="text" name="path" value="<?= $path ?>">&nbsp;<input type="submit" value="Go">
</form>
<form enctype="multipart/form-data" method="post" action="upload.php" target="_blank">
	Upload File:<br>
	<input class="pathInput" type="file" name="upfile">&nbsp;<input type="submit" value="Upload">
	<input type="hidden" name="path" value="<?= $path ?>">
	<input type="hidden" name="browser_submit" value="1">
</form>
<br>
<?
	include("assets/includes/page-footer.inc.php");
?>
