<?PHP
	session_start();
	define("FILE", $_GET[item]);

	if(isset($_GET[preferred_plugin]))
	{
		if(is_dir(FILE))
			$_SESSION[preferred_plugin]['[directory]'] = $_GET[preferred_plugin];
		else
		{
			preg_match('/.([\w\d]+)$/',$_SESSION[current_local_dir]."/".FILE,$matches);
			$_SESSION[preferred_plugin][$matches[1]] = $_GET[preferred_plugin];
		}
	}

	if(is_file($_SESSION[current_local_dir]."/".FILE))
	{
		preg_match('/.([\w\d]+)$/',$_SESSION[current_local_dir]."/".FILE,$matches);
		define('ENDING',$matches[1]);
	}
	elseif(is_dir(FILE))
	{
		define('ENDING',"[directory]");
	}

	if(FILE != null)
	{
		if(!isset($_SESSION[SUPPORTED_FILES][ENDING]))
		{
			echo "There is no plugin available to show files of this type (".ENDING."), would you like to download the file instead? <a href=\"".$_SESSION[current_web_dir]."/".FILE."\">".FILE."</a><br>\n";
		}
		else
		{
			if(is_array($_SESSION[SUPPORTED_FILES][ENDING]))
			{
				if(isset($_SESSION[preferred_plugin][ENDING]))
				{
//					echo "Your preferred plugin for this item is: ".$_SESSION[preferred_plugin][ENDING]."<br>\n";
					header("Location: ".$_SESSION[PLUGIN_DIR]."/".$_SESSION[preferred_plugin][ENDING]."/index.php?file_name=".FILE);
				}
				else
				{
					echo "There are multiple plugins that support this item, please chose one!<br>\n";
					foreach($_SESSION[SUPPORTED_FILES][ENDING] as $plugin)
					{
						echo "<a href=\"".$_SESSION[PLUGIN_DIR]."/".$plugin."/index.php?file_name=".FILE."\">".$plugin."<br>\n";
						echo "<a href=\"".$_SERVER[PHP_SELF]."?item=".FILE."&preferred_plugin=".$plugin."\">".$plugin." and do this for all the files of this type during this session<br>\n";
					}
				}
			}
			else
				header("Location: ".$_SESSION[PLUGIN_DIR]."/".$_SESSION[SUPPORTED_FILES][ENDING]."/index.php?file_name=".FILE);
		}
	}
PHP?>
