<?PHP
	session_start();
	require('db.php');

	define("BASEDIR", "/home/tajgoren/Bilder");

	$_SESSION[PLUGIN_DIR] = "plugins";

	// make sure we start off with setting the base directory as current dir.
	if(!isset($_SESSION[current_local_dir]))
	{
//		These are the old settings that start browsing the current working dir
//		$_SESSION[current_local_dir] = getcwd();
//		$_SESSION[local_base_dir] = $_SESSION[current_local_dir];
		$_SESSION[local_base_dir] = BASEDIR;
		$_SESSION[current_local_dir] = $_SESSION[local_base_dir];
		$_SESSION[current_web_dir] = preg_replace('/\/[\s\d\w.\-_]+$/',"",$_SERVER[PHP_SELF]);
		$_SESSION[web_base_dir] = $_SESSION[current_web_dir];
	}

	// scan for and include all the plugins in the plugin directory
	// also add the supported file types from the plugins to the _SESSION[SUPPORTED_FILES] array

	$_SESSION[SUPPORTED_FILES] = array();
	$plugins = scandir($_SESSION[PLUGIN_DIR]);
	foreach($plugins as $plugin)
	{
		if($plugin != "." && $plugin != "..")
		{
			$supported_files = file($_SESSION[PLUGIN_DIR]."/".$plugin."/supported_files");
			foreach($supported_files as $supported_file)
			{
				$supported_file = rtrim($supported_file);
				if(isset($_SESSION[SUPPORTED_FILES][$supported_file]))
				{
					// We've found a plugin that can do the same as another plugin!
					if(is_array($_SESSION[SUPPORTED_FILES][$supported_file]))
					{
						// We allready have multiple support for this file type, add to the list of plugins for this file.
						$_SESSION[SUPPORTED_FILES][$supported_file][count($_SESSION[SUPPORTED_FILES][$supported_file])] = $plugin;
					}
					else
					{
						$existing_plugin = $_SESSION[SUPPORTED_FILES][$supported_file];
						$_SESSION[SUPPORTED_FILES][$supported_file] = array();
						$_SESSION[SUPPORTED_FILES][$supported_file][0] = $existing_plugin;
						$_SESSION[SUPPORTED_FILES][$supported_file][1] = $plugin;
					}
				}
				else
					$_SESSION[SUPPORTED_FILES][$supported_file] = $plugin;
			}
		}
	}

	// read configuration file
	require_once('browser.cfg');

	function show_dir($path)
	{
		$contents = scandir($path);
		if($contents)
		{
			foreach($contents as $item)
			{
				// Hide everything the webserver process dont have permission to read.
				if(is_readable($path."/".$item))
				{
					if($_SESSION[show_files] && is_file($path."/".$item))
						echo "<a target=\"monitor\" href=\"file_handler.php?item=".$item."\">$item</a><br>\n";
					else if(is_dir($path."/".$item))
						echo "[ <a href=\"$_SERVER[PHP_SELF]?action=cd&dir=$item\">$item</a> ]<br>\n";
				}
			}
		}
		else
			$messages = $messages."Access denied for this file or directory!<br>\n";
	}

	if(isset($_GET[action]))
	{
		if($_GET[action] == "cd" && isset($_GET[dir]))
		{
			if(is_dir($_SESSION[current_local_dir]."/".$_GET[dir]))
			{
				// Make sure we're not about to leave the local_base_dir
				$path = preg_replace('/\//','\\\/',$_SESSION[local_base_dir]);
				$pattern = "/^".$path."/";
				$target = realpath($_SESSION[current_local_dir]."/".$_GET[dir]);
				if(preg_match($pattern,$target))
				{
					if($_GET[dir] == "..")
					{
						$_SESSION[current_web_dir] = preg_replace('/\/[\s\d\w.\-_]+$/',"",$_SESSION[current_web_dir]);
						$_SESSION[current_local_dir] = preg_replace('/\/[\s\d\w.\-_]+$/',"",$_SESSION[current_local_dir]);
					}
					elseif($_GET[dir] != ".")
					{
						$_SESSION[current_web_dir] = $_SESSION[current_web_dir]."/".$_GET[dir];
						$_SESSION[current_local_dir] = $_SESSION[current_local_dir]."/".$_GET[dir];
					}
				}
				else
					$messages = $messages."You're trying to leave the base dir, this is not allowed!<br>\n";
			}
		}
		elseif($_GET[action] == "change_plugin" && isset($_GET[preferred_plugin]) && isset($_GET[file_type]))
		{
			$_SESSION[preferred_plugin][$_GET[file_type]] = $_GET[preferred_plugin];
		}
		elseif($_GET[action] == "reset")
		{
			$_SESSION = array();
			session_destroy();
			header("Location: $_SERVER[PHP_SELF]");
			exit;
		}
		elseif($_GET[action] == "change_show_files")
			$_SESSION[show_files] = !$_SESSION[show_files];
	}
PHP?>

<html>
<head>
<title>Tajgoren's server browser 0.3b</title>
</head>

<body>

<table border=0 cellpadding=0 cellspacing=0 width=100% height=100%>
<tr>
<td valign=top width=25% height=150><b>Current status:</b><br>
<?PHP
		echo "Current dir is: ".$_SESSION[current_local_dir]."<br>\n";
		if($_SESSION[show_files])
			echo "Show files in the left view (<a href=\"$_SERVER[PHP_SELF]?action=change_show_files\">change</a>)<br>\n";
		else
			echo "Don't show files in the left view (<a href=\"$_SERVER[PHP_SELF]?action=change_show_files\">change</a>)<br>\n";
		if(is_array($_SESSION[preferred_plugin]))
		{
			foreach($_SESSION[preferred_plugin] as $preference)
			{
				echo "Using ".$preference." to show items of type ".key($_SESSION[preferred_plugin])." (change to: ";
				foreach($_SESSION[SUPPORTED_FILES][key($_SESSION[preferred_plugin])] as $alternative)
				{
					if($alternative != $preference)
					{
						echo "<a href=\"".$_SERVER[PHP_SELF]."?action=change_plugin&preferred_plugin=".$alternative."&file_type=".key($_SESSION[preferred_plugin])."\">".$alternative."</a>";
					}
				}
				echo " )<br>\n";
			}
		}
PHP?>
</td>
<td valign=top width=75% height=150><b>Messages:</b><br><?PHP	echo $messages;	PHP?></td>
</tr>

<tr>
<td width=250 valign=top><?PHP	show_dir($_SESSION[current_local_dir]);	PHP?></td>
<td><iframe name="monitor" border=0 frameborder=0 height=100% width=100% src="file_handler.php?item=<?PHP	echo $_SESSION[current_local_dir];	PHP?>"></iframe></td>
</tr>

<tr>
<td colspan=2 height=20><br><br><a href="browser.php?action=reset">RESET</a><br></td>
</tr>
</table>

</body>
</html>
