src/Neuron/GameServer.php
public function dispatch ()
	{
		if (!isset ($this->objGame))
		{
			throw new Neuron_Core_Error ('Neuron_GameServer did not receive a Neuron_GameServer_Game object.');
		}

		if ($this->objGame instanceof Neuron_GameServer_Interfaces_Dispatch)
		{
			if (!$this->getRidOfSessionID ())
			{
				return;
			}

			$this->objGame->dispatch ();
			return;
		}

		Neuron_URLBuilder::getInstance ()->setOpenCallback (array ($this, 'getOpenUrl'));
		Neuron_URLBuilder::getInstance ()->setUpdateCallback (array ($this, 'getUpdateUrl'));

		$pgen = Neuron_Core_PGen::__getInstance ();
		$pgen->start ();

		// Start counter
		$sInputs = explode ('/', isset ($_GET['module']) ? $_GET['module'] : null);
		$sModule = $sInputs[0];

		switch ($sModule)
		{
			case 'openid':
				$this->openid ();
				break;

			case 'gameserver':

				array_shift ($sInputs);
				array_shift ($sInputs);

				$assetPath = realpath (__DIR__ . '/../../assets/');
				$filename = $assetPath . '/' . implode ('/', $sInputs);

				if (file_exists($filename)) {

					$ext = explode ('.', $filename);
					$ext = array_pop ($ext);

					switch ($ext)
					{
						case 'css':
							header ('Content-Type: text/css');
							break;

						default;
							$finfo = finfo_open(FILEINFO_MIME_TYPE);
							$mimetype = finfo_file($finfo, $filename);
							finfo_close($finfo);

							header ('Content-Type: ' . $mimetype);
						break;
					}
					echo file_get_contents($filename);
				}
				else {
					http_response_code(404);
					echo 'File not found: ' . implode ('/', $sInputs);
				}

				break;

			case 'dialog':

				// A little overwrite 
				$output = $this->objGame->getCustomOutput ();
				if ($output)
				{
					header("Content-Type: text/xml; charset=utf-8;");

					echo '<?xml version="1.0" encoding="utf-8"?>';
					echo '<root><command command="refresh"></command></root>';
					return;
				}

				require_once (self::SCRIPT_PATH.'dialog/dialog.php');
				break;

			case 'api':
				require_once (self::SCRIPT_PATH.'api/api.php');
				break;

			case 'map':
				// Close the session (lock)
				//session_write_close ();

				require_once (self::SCRIPT_PATH.'map/map.php');
				break;

			case 'image':
				// Close the session (lock)
				session_write_close ();

				require_once (self::SCRIPT_PATH.'image/image.php');
				break;

			case 'test':
				// Close the session (lock)
				session_write_close ();

				// Login
				$player = Neuron_GameServer::getPlayer ();

				$overwritelogin = true;

				if ((!$player || !$player->isAdmin ()) && !$overwritelogin)
				{
					echo 'You must login.';
				}
				else
				{
					$file = isset ($sInputs[1]) ? $sInputs[1] : null;

					if (@include ('scripts/tests/' . $sInputs[1] . '.php'))
					{
						//include_once ('scripts/tests/' . $sInputs[1] . '.php');
					}
					else if (include (self::SCRIPT_PATH.'tests/'.$file.'.php'))
					{
						// ok	
					}
					else
					{
						echo "File not found: " . self::SCRIPT_PATH.'tests/'.$file.'.php';
					}

					/*if (file_exists (self::SCRIPT_PATH.'tests/'.$file.'.php'))
					{
					*/

					/*}
					else
					{
						echo "File not found: " . self::SCRIPT_PATH.'tests/'.$file.'.php';
					}*/
				}
				break;

			case 'admin':
				// Check for page
				setcookie ('session_id', session_id (), 0, COOKIE_BASE_PATH.'admin/');

				$login = Neuron_Core_Login::getInstance (1);
				if (!$login->isLogin ())
				{
					$objPage = new Neuron_GameServer_Pages_Admin_Login ($login);
				}
				else
				{
					$sPage = isset ($sInputs[1]) ? $sInputs[1] : 'Index';
					$sClassname = 'Neuron_GameServer_Pages_Admin_'.ucfirst (strtolower ($sPage));

					$myself = Neuron_GameServer::getPlayer ();

					if ($myself && $myself->isChatModerator ())
					{
						if ($objPage = $this->objGame->getAdminPage ($sPage))
						{
						}

						elseif (class_exists ($sClassname))
						{
							$objPage = new $sClassname ();
						}
						else
						{
							$objPage = new Neuron_GameServer_Pages_Admin_Index ();
						}
					}
					else
					{
						$objPage = new Neuron_GameServer_Pages_Admin_Invalid ();
					}
				}

				echo $objPage->getHTML ();
				break;

			case 'page':
				// Check for page
				$sPage = isset ($sInputs[1]) ? $sInputs[1] : 'Index';
				$sClassname = 'Neuron_GameServer_Pages_'.ucfirst (strtolower ($sPage));

				$myself = Neuron_GameServer::getPlayer ();

				if ($objPage = $this->objGame->getPage ($sPage))
				{
				}

				else if (class_exists ($sClassname))
				{
					$objPage = new $sClassname ();
				}
				else
				{
					$objPage = new Neuron_GameServer_Pages_Index ();
				}

				echo $objPage->getOutput ();
				break;

			case 'time':
				echo 'time=' . round (microtime (true) * 1000);
				break;

			case '':
				$_SESSION['tmp'] = null;

				// Now, if we have a NOLOGIN_REDIRECT set, redirect here
				if (defined ('NOLOGIN_REDIRECT') && !isset ($_GET['DEBUG']))
				{
					$player = Neuron_GameServer::getPlayer ();

					if (!$player)
					{
						header ("Location: " . NOLOGIN_REDIRECT);
						echo "Redirecting to " . NOLOGIN_REDIRECT;
					}

					else
					{
						$this->showIndexPage ();
					}
				}

				else
				{
					$this->showIndexPage ();
				}
				break;

			case 'favicon.ico':
			case 'favicon.icon':
				header ('Content-type: image/x-icon');
				echo file_get_contents ('./favicon.ico');
				break;

			default:
				//throw new Exception ('Invalid API call: module "'.$sModule.'" not found.');
				echo '<p>Invalid module: '.$sModule.'</p>';
				break;
		}

		if (isset ($profiler) && defined (USE_PROFILE) && USE_PROFILE)
		{
			// Dump the profiler
			if (intval($profiler->getTotalDuration ()) > 2)
			{
				$cache = Neuron_Core_Cache::__getInstance ('profiler/'.$_SERVER['REMOTE_ADDR'].'/');
				$cache->setCache (date ('dmYHis'), (string)$profiler);
			}
		}
	}
public function dispatch ()
	{
		if (!isset ($this->objGame))
		{
			throw new Neuron_Core_Error ('Neuron_GameServer did not receive a Neuron_GameServer_Game object.');
		}

		if ($this->objGame instanceof Neuron_GameServer_Interfaces_Dispatch)
		{
			if (!$this->getRidOfSessionID ())
			{
				return;
			}

			$this->objGame->dispatch ();
			return;
		}

		Neuron_URLBuilder::getInstance ()->setOpenCallback (array ($this, 'getOpenUrl'));
		Neuron_URLBuilder::getInstance ()->setUpdateCallback (array ($this, 'getUpdateUrl'));

		$pgen = Neuron_Core_PGen::__getInstance ();
		$pgen->start ();

		// Start counter
		$sInputs = explode ('/', isset ($_GET['module']) ? $_GET['module'] : null);
		$sModule = $sInputs[0];

		switch ($sModule)
		{
			case 'openid':
				$this->openid ();
				break;

			case 'gameserver':

				array_shift ($sInputs);
				array_shift ($sInputs);

				$assetPath = realpath (__DIR__ . '/../../assets/');
				$filename = $assetPath . '/' . implode ('/', $sInputs);

				if (file_exists($filename)) {

					$ext = explode ('.', $filename);
					$ext = array_pop ($ext);

					switch ($ext)
					{
						case 'css':
							header ('Content-Type: text/css');
							break;

						default;
							$finfo = finfo_open(FILEINFO_MIME_TYPE);
							$mimetype = finfo_file($finfo, $filename);
							finfo_close($finfo);

							header ('Content-Type: ' . $mimetype);
						break;
					}
					echo file_get_contents($filename);
				}
				else {
					http_response_code(404);
					echo 'File not found: ' . implode ('/', $sInputs);
				}

				break;

			case 'dialog':

				// A little overwrite 
				$output = $this->objGame->getCustomOutput ();
				if ($output)
				{
					header("Content-Type: text/xml; charset=utf-8;");

					echo '<?xml version="1.0" encoding="utf-8"?>';
					echo '<root><command command="refresh"></command></root>';
					return;
				}

				require_once (self::SCRIPT_PATH.'dialog/dialog.php');
				break;

			case 'api':
				require_once (self::SCRIPT_PATH.'api/api.php');
				break;

			case 'map':
				// Close the session (lock)
				//session_write_close ();

				require_once (self::SCRIPT_PATH.'map/map.php');
				break;

			case 'image':
				// Close the session (lock)
				session_write_close ();

				require_once (self::SCRIPT_PATH.'image/image.php');
				break;

			case 'test':
				// Close the session (lock)
				session_write_close ();

				// Login
				$player = Neuron_GameServer::getPlayer ();

				$overwritelogin = true;

				if ((!$player || !$player->isAdmin ()) && !$overwritelogin)
				{
					echo 'You must login.';
				}
				else
				{
					$file = isset ($sInputs[1]) ? $sInputs[1] : null;

					if (@include ('scripts/tests/' . $sInputs[1] . '.php'))
					{
						//include_once ('scripts/tests/' . $sInputs[1] . '.php');
					}
					else if (include (self::SCRIPT_PATH.'tests/'.$file.'.php'))
					{
						// ok	
					}
					else
					{
						echo "File not found: " . self::SCRIPT_PATH.'tests/'.$file.'.php';
					}

					/*if (file_exists (self::SCRIPT_PATH.'tests/'.$file.'.php'))
					{
					*/

					/*}
					else
					{
						echo "File not found: " . self::SCRIPT_PATH.'tests/'.$file.'.php';
					}*/
				}
				break;

			case 'admin':
				// Check for page
				setcookie ('session_id', session_id (), 0, COOKIE_BASE_PATH.'admin/');

				$login = Neuron_Core_Login::getInstance (1);
				if (!$login->isLogin ())
				{
					$objPage = new Neuron_GameServer_Pages_Admin_Login ($login);
				}
				else
				{
					$sPage = isset ($sInputs[1]) ? $sInputs[1] : 'Index';
					$sClassname = 'Neuron_GameServer_Pages_Admin_'.ucfirst (strtolower ($sPage));

					$myself = Neuron_GameServer::getPlayer ();

					if ($myself && $myself->isChatModerator ())
					{
						if ($objPage = $this->objGame->getAdminPage ($sPage))
						{
						}

						elseif (class_exists ($sClassname))
						{
							$objPage = new $sClassname ();
						}
						else
						{
							$objPage = new Neuron_GameServer_Pages_Admin_Index ();
						}
					}
					else
					{
						$objPage = new Neuron_GameServer_Pages_Admin_Invalid ();
					}
				}

				echo $objPage->getHTML ();
				break;

			case 'page':
				// Check for page
				$sPage = isset ($sInputs[1]) ? $sInputs[1] : 'Index';
				$sClassname = 'Neuron_GameServer_Pages_'.ucfirst (strtolower ($sPage));

				$myself = Neuron_GameServer::getPlayer ();

				if ($objPage = $this->objGame->getPage ($sPage))
				{
				}

				else if (class_exists ($sClassname))
				{
					$objPage = new $sClassname ();
				}
				else
				{
					$objPage = new Neuron_GameServer_Pages_Index ();
				}

				echo $objPage->getOutput ();
				break;

			case 'time':
				echo 'time=' . round (microtime (true) * 1000);
				break;

			case '':
				$_SESSION['tmp'] = null;

				// Now, if we have a NOLOGIN_REDIRECT set, redirect here
				if (defined ('NOLOGIN_REDIRECT') && !isset ($_GET['DEBUG']))
				{
					$player = Neuron_GameServer::getPlayer ();

					if (!$player)
					{
						header ("Location: " . NOLOGIN_REDIRECT);
						echo "Redirecting to " . NOLOGIN_REDIRECT;
					}

					else
					{
						$this->showIndexPage ();
					}
				}

				else
				{
					$this->showIndexPage ();
				}
				break;

			case 'favicon.ico':
			case 'favicon.icon':
				header ('Content-type: image/x-icon');
				echo file_get_contents ('./favicon.ico');
				break;

			default:
				//throw new Exception ('Invalid API call: module "'.$sModule.'" not found.');
				echo '<p>Invalid module: '.$sModule.'</p>';
				break;
		}

		if (isset ($profiler) && defined (USE_PROFILE) && USE_PROFILE)
		{
			// Dump the profiler
			if (intval($profiler->getTotalDuration ()) > 2)
			{
				$cache = Neuron_Core_Cache::__getInstance ('profiler/'.$_SERVER['REMOTE_ADDR'].'/');
				$cache->setCache (date ('dmYHis'), (string)$profiler);
			}
		}
	}
/*
Get the output that is required.