<?php
/* ******************************************************************
 * Main: API
 * ****************************************************************** */

// get a new instance
$page = new THF_Admin_Tool_ToolBox(); // See at the bottom.

// Read all the data from the tools
$page->readAllIniFiles();

// Echo the heading of the webpage.
echo $page->htmlHeading();

// Dit we select a valid tool or nothing at all?
$tool = $page->readUri();

// Switch for the content depending on the $tool variable
if( NULL === $tool )
{
	// startpage
	echo $page->htmlToolTable();
}
elseif( FALSE === $tool )
{
	// Impossible or bad people?
	echo '<br /><h3 style="color:red">Invalid tool request.</h3><br />';
	echo $page->htmlToolTable();
}
else
{
	// Show the form connected to the tool.
	$page->invokeTool($tool);
}

echo $page->htmlFooter();



/* ******************************************************************
 *
 * Class for easy handling this tool page.
 *
 * ****************************************************************** */

class THF_Admin_Tool_ToolBox
{
	// Holds all the info about every tool
	private $_about;

	public function __construct()
	{
		$this->_about = array();
	}

	public function readAllIniFiles()
	{
		$this->_about = array();
		$dir = new DirectoryIterator('./');
		foreach( $dir as $subdir )
		{
			if( $subdir->isDir() AND ! $subdir->isDot() )
			{
				$inipath = $subdir->getPathname() . DIRECTORY_SEPARATOR . 'about.ini';
				$content = parse_ini_file($inipath);
				$content = array($subdir->getBasename() => $content);
				$this->_about = array_merge($this->_about, $content);
			}
		}
	}

	public function htmlToolTable()
	{
		$html  = '<table border="1" cellpadding="5">';
		$html .= '<tr><th align="left">tool</th><th align="left">description</th></tr>';
		foreach( $this->_about as $tool => $data )
		{
			$html .= '<tr><td align="left"><a href="?tool='.$tool.'">'.$data['name'].'</a><br />'.$data['version'].'</td><td align="left" style="vertical-align:text-top">'.$data['description'].'</td></tr>';
		}
		$html .= '</table>';
		return $html;
	}

	public function invokeTool($tool)
	{
		$path = './'.$tool.'/form.php';
		include $path;
	}

	public function readUri()
	{
		if( ! isset($_REQUEST['tool']) )
				return NULL;

		$tool = $_REQUEST['tool'];
		if( ! array_key_exists($tool, $this->_about) )
				return FALSE;

		return $tool;
	}

	public function htmlHeading()
	{
		$out  = '<h1>Toolbox</h1>';
		$out .= '<a href="?">hoofdmenu toolbox</a>';
		$out .= '<hr /><br />';
		return $out;
	}

	public function htmlFooter()
	{
		$out  = '<br /><hr />';
		$out .= '<a href="?">hoofdmenu toolbox</a>';
		$out .= '&nbsp;&nbsp;&nbsp;&nbsp;<span>'.date('d M Y').'</span>';
		return $out;
	}

}
?>