<?php

/*
 * Plugin: Third-party Plugins UpToDate
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * For a detailed description and documentation, please refer to:
 * http://labs.undef.de/XAseco2/Third-party-Plugins-UpToDate.php
 *
 * ----------------------------------------------------------------------------------
 * Author:		undef.de
 * Version:		0.9.1
 * Date:		2012-12-07
 * Copyright:		2011 - 2012 by undef.de
 * Home:		http://labs.undef.de/
 * System:		XAseco2/1.01+
 * Game:		ManiaPlanet Trackmania2 (TM2)
 * ----------------------------------------------------------------------------------
 *
 * LICENSE: This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * ----------------------------------------------------------------------------------
 *
 * Dependencies: none
 *
 * ManialinkID's
 * ~~~~~~~~~~~~~
 * 91900			id for manialink Window
 * 91901			id for manialink LegendWindow
 *
 * ActionID's
 * ~~~~~~~~~~
 *  91900			id for action open Window
 *  91901			id for action close Window
 *  91902			id for action open LegendWindow
 *  91903			id for action close LegendWindow
 *  91904 - 91910		UNSED BUT RESERVED
 * -91910 to -91960		id for action Pluginslist previous page in Table (max. 50 pages a 20 entries = 1000 total)
 *  91910 to  91960		id for action Pluginslist next page in Table (max. 50 pages a 20 entries = 1000 total)
 *
 */

Aseco::registerEvent('onSync',				'utd_onSync');
Aseco::registerEvent('onPlayerConnect',			'utd_onPlayerConnect');
Aseco::registerEvent('onPlayerManialinkPageAnswer',	'utd_onPlayerManialinkPageAnswer');
Aseco::registerEvent('onEndMap1',			'utd_onEndMap1');

Aseco::addChatCommand('checkplugins',			'Checks for new releases of installed Plugins', true);

global $utd_config;

/*
#///////////////////////////////////////////////////////////////////////#
#									#
#///////////////////////////////////////////////////////////////////////#
*/

function utd_onSync ($aseco) {
	global $utd_config;


	// Check for the right XAseco2-Version
	$xaseco2_min_version = '1.01';
	if ( defined('XASECO2_VERSION') ) {
		$version = str_replace(
			array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'),
			array('.1','.2','.3','.4','.5','.6','.7','.8','.9'),
			XASECO2_VERSION
		);
		if ( version_compare($version, $xaseco2_min_version, '<') ) {
			trigger_error('[plugin.third_party_plugins_uptodate.php] Not supported XAseco version ('. XASECO2_VERSION .')! Please update to min. version '. $xaseco2_min_version .'!', E_USER_ERROR);
		}
	}
	else {
		trigger_error('[plugin.third_party_plugins_uptodate.php] Can not identify the System, "XASECO2_VERSION" is unset! This plugin runs only with XAseco2/'. $xaseco2_min_version .' and up.', E_USER_ERROR);
	}

	if ($aseco->server->getGame() != 'MP') {
		trigger_error('[plugin.third_party_plugins_uptodate.php] This plugin supports only TM2 (ManiaPlanet), can not start with a "'. $aseco->server->getGame() .'" Dedicated-Server!', E_USER_ERROR);
	}

	$utd_config['version'] = '0.9.1';
	$utd_config['url'] = 'http://labs.undef.de/.uptodate/xaseco2-plugin-releases.xml';

	$utd_config['user_agent'] = 'XAseco2 Third-party Plugins UpToDate/'. $utd_config['version'];
	$utd_config['connect_timeout'] = 30;
	$utd_config['wait_timeout'] = 60;
	$utd_config['manialink_id'] = '919';

	$utd_config['display_state'] = false;
	$utd_config['plugins'] = array();

	// Register this to the global version pool (for up-to-date checks)
	$aseco->plugin_versions[] = array(
		'plugin'	=> 'plugin.third_party_plugins_uptodate.php',
		'author'	=> 'undef.de',
		'version'	=> $utd_config['version']
	);


	// Generate the checksum for compare
	foreach ($aseco->plugins as $filename) {
		foreach ($aseco->plugin_versions as &$installed_plugin) {
			if ($filename == $installed_plugin['plugin']) {
				$plugin = file_get_contents('plugins/'.$filename);
				$plugin = preg_replace('/\s*/', '', $plugin);
				$installed_plugin['checksum'] = md5($plugin);
			}
		}
		unset($installed_plugin);
	}
}

/*
#///////////////////////////////////////////////////////////////////////#
#									#
#///////////////////////////////////////////////////////////////////////#
*/

function chat_checkplugins ($aseco, $command) {
	global $utd_config;


	// Check for a MasterAdmin
	if ( $aseco->isMasterAdmin($command['author']) ) {
		if (count($utd_config['plugins']) == 0) {
			// Check for outdated Plugins
			utd_checkForOutdatedPlugins();
		}

		// Always display the PluginlistWindow
		$xml = utd_buildPluginListWindow(0, true);

		// Send to given Player
		$aseco->client->query('SendDisplayManialinkPageToLogin', $command['author']->login, $xml, 0, false);
	}
}

/*
#///////////////////////////////////////////////////////////////////////#
#									#
#///////////////////////////////////////////////////////////////////////#
*/

function utd_onPlayerConnect ($aseco, $player) {
	global $utd_config;


	// Check for a MasterAdmin
	if ( $aseco->isMasterAdmin($player) ) {
		$result = false;
		if (count($utd_config['plugins']) == 0) {
			// Check for outdated Plugins
			$result = utd_checkForOutdatedPlugins();
		}

		if ($result == true) {
			$xml = utd_buildPluginListWindow(0, true);

			// Send to given Player
			$aseco->client->query('SendDisplayManialinkPageToLogin', $player->login, $xml, 0, false);
		}
	}
}

/*
#///////////////////////////////////////////////////////////////////////#
#									#
#///////////////////////////////////////////////////////////////////////#
*/

// $answer = [0]=PlayerUid, [1]=Login, [2]=Answer
function utd_onPlayerManialinkPageAnswer ($aseco, $answer) {
	global $utd_config;


	// If id = 0, bail out immediately
	if ($answer[2] == 0) {
		return;
	}

	// Get the Player object
	$player = $aseco->server->players->player_list[$answer[1]];

	// Check for a MasterAdmin
	if ( !$aseco->isMasterAdmin($player) ) {
		return;
	}


	$xml = '';

	if ($answer[2] == (int)$utd_config['manialink_id'] .'00') {

		if (count($utd_config['plugins']) == 0) {
			// Check for outdated Plugins
			utd_checkForOutdatedPlugins();
		}

		// Open Window
		$xml .= utd_buildPluginListWindow(0, true);

	}
	else if ($answer[2] == (int)$utd_config['manialink_id'] .'01') {

		// Close all Windows
		$xml .= utd_closeAllWindows();

	}
	else if ($answer[2] == (int)$utd_config['manialink_id'] .'02') {

		// Open LegendWindow
		$xml .= utd_buildLegendWindow(true);

	}
	else if ($answer[2] == (int)$utd_config['manialink_id'] .'03') {

		// Close LegendWindow
		$xml .= utd_buildLegendWindow(false);

	}
	else if ( ($answer[2] <= -(int)$utd_config['manialink_id'] .'10') && ($answer[2] >= -(int)$utd_config['manialink_id'] .'60') ) {

		if (count($utd_config['plugins']) == 0) {
			// Wrapper (Only display Window, when outdated Plugins was found)
			utd_checkForOutdatedPlugins();
		}

		// Get the wished Page
		$page = intval( str_replace($utd_config['manialink_id'], '', abs($answer[2])) - 10 );
		$xml .= utd_buildPluginListWindow($page, true);

	}
	else if ( ($answer[2] >= (int)$utd_config['manialink_id'] .'10') && ($answer[2] <= (int)$utd_config['manialink_id'] .'60') ) {

		if (count($utd_config['plugins']) == 0) {
			// Wrapper (Only display Window, when outdated Plugins was found)
			utd_checkForOutdatedPlugins();
		}

		// Get the wished Page
		$page = intval( str_replace($utd_config['manialink_id'], '', $answer[2]) - 10 );
		$xml .= utd_buildPluginListWindow($page, true);

	}

	if ($xml != '') {
		// Send to given Player
		$aseco->client->query('SendDisplayManialinkPageToLogin', $player->login, $xml, 0, false);
	}
}

/*
#///////////////////////////////////////////////////////////////////////#
#									#
#///////////////////////////////////////////////////////////////////////#
*/

function utd_onEndMap1 ($aseco, $race) {
	global $utd_config;


	if ($utd_config['display_state'] == true) {

		// Close all Windows
		$xml = utd_closeAllWindows();

		// Send to all Players (not only MasterAdmins
		$aseco->client->query('SendDisplayManialinkPage', $xml, 0, false);
	}

	// Empty current list, to refresh them next time
	$utd_config['plugins'] = array();
}

/*
#///////////////////////////////////////////////////////////////////////#
#									#
#///////////////////////////////////////////////////////////////////////#
*/

function utd_checkForOutdatedPlugins () {
	global $aseco, $utd_config;


	if ( !isset($aseco->plugin_versions) ) {
		$aseco->console('[plugin.third_party_plugins_uptodate.php] No supported Plugins found, skip checks!');
		return null;
	}

	$response = utd_httpConnect($utd_config['url'], 'GET', false, $utd_config['user_agent']);
	if ($response['Code'] == 200) {

		if (!$releases = $aseco->xml_parser->parseXML($response['Message'], false)) {
			$aseco->console('[plugin.third_party_plugins_uptodate.php] Could not parse response from server!');
		}
		else {
			$releases = $releases['RELEASES']['PLUGIN'];

			$utd_config['plugins'] = array();
			$outdated = false;
			foreach ($releases as &$plugin) {

				foreach ($aseco->plugin_versions as &$installed_plugin) {
					if ( ($plugin['NAME'][0] == $installed_plugin['plugin']) && ($plugin['AUTHOR'][0] == $installed_plugin['author']) ) {
						if ( version_compare($plugin['RELEASE'][0]['VERSION'][0], $installed_plugin['version'], '>') ) {
							$plugin['UPDATED'] = true;
							$outdated = true;
						}
						else if ( (version_compare($plugin['RELEASE'][0]['VERSION'][0], $installed_plugin['version'], '==')) && ($plugin['RELEASE'][0]['CHECKSUM'][0] != $installed_plugin['checksum']) ) {
							$plugin['UPDATED'] = true;
							$outdated = true;
						}
						else {
							$plugin['UPDATED'] = false;
						}
						$plugin['CURRENT'] = $installed_plugin['version'];

						$utd_config['plugins'][] = $plugin;
					}
				}
				unset($installed_plugin);
			}
			unset($plugin);

			// Now resort the Plugins by require update
			$update = array();
			$plugin = array();
			foreach ($utd_config['plugins'] as $key => &$row) {
				$update[$key] = $row['UPDATED'];
				$plugin[$key] = strtolower($row['TITLE'][0]);
			}
			array_multisort($update, SORT_DESC, $plugin, SORT_ASC, $utd_config['plugins']);
			unset($update, $plugin);

			return $outdated;
		}
	}
	else {
		$aseco->console('[plugin.third_party_plugins_uptodate.php] utd_uptodateCheck() connection failed with '. $response['Code'] .' ('. $response['Reason'] .') for url ['. $utd_config['url'] .']');
	}
}

/*
#///////////////////////////////////////////////////////////////////////#
#									#
#///////////////////////////////////////////////////////////////////////#
*/

function utd_buildPluginListWindow ($page = 0, $display = true) {
	global $utd_config;


	$xml  = '<?xml version="1.0" encoding="UTF-8"?>';
	$xml .= '<manialinks>';
	$xml .= '<manialink id="'. $utd_config['manialink_id'] .'00">';

	// Set default to false
	$utd_config['display_state'] = false;
	if ($display == true) {

		// Now set to display
		$utd_config['display_state'] = true;


		// Window
		$xml .= '<frame posn="-40.8 30.55 -4">';	// BEGIN: Window Frame
		$xml .= '<quad posn="-0.2 0.2 0.01" sizen="81.8 59" style="Bgs1InRace" substyle="BgTitle2"/>';
		$xml .= '<quad posn="1.8 -4.1 0.02" sizen="77.7 49.9" bgcolor="0018"/>';

		// Header Line
		$xml .= '<quad posn="-0.6 0.6 0.02" sizen="82.6 6" style="Bgs1InRace" substyle="BgTitle3_3"/>';
		$xml .= '<quad posn="-0.6 0.6 0.03" sizen="82.6 6" style="Bgs1InRace" substyle="BgTitle3_3"/>';

		// Header Icon
		$xml .= '<quad posn="1.8 -0.7 0.20" sizen="3.2 3.2" style="Icons128x128_1" substyle="Editor"/>';
		$xml .= '<label posn="5.5 -1.7 0.04" sizen="75.4 0" textsize="2" scale="0.9" textcolor="000F" text="List of supported and installed Plugins"/>';

		// About
		$xml .= '<label posn="9.6 -55.8 0.04" sizen="23 2" halign="center" valign="center" textsize="1" scale="0.7" url="http://labs.undef.de/" focusareacolor1="0000" focusareacolor2="FFF5" textcolor="000F" text="THIRD-PARTY PLUGINS UP-TO-DATE/'. $utd_config['version'] .'"/>';

		// Close Button
		$xml .= '<frame posn="76.7 -0.15 0">';
		$xml .= '<quad posn="0 0 0.07" sizen="4.5 4.5" action="'. $utd_config['manialink_id'] .'01" style="Icons64x64_1" substyle="ArrowUp"/>';
		$xml .= '<quad posn="1.2 -1.2 0.08" sizen="2 2" bgcolor="EEEF"/>';
		$xml .= '<quad posn="0.7 -0.7 0.09" sizen="3.1 3.1" style="Icons64x64_1" substyle="Close"/>';
		$xml .= '</frame>';


		// BEGIN: Frame for Legend/Previous/Next Buttons
		$xml .= '<frame posn="55.35 -53.3 0.04">';

		// Legend button
		$xml .= '<quad posn="13.35 -1 0.12" sizen="3 3" action="'. $utd_config['manialink_id'] .'02" style="Icons64x64_1" substyle="Maximize"/>';
		$xml .= '<quad posn="13.75 -1.4 0.13" sizen="2.1 2.1" bgcolor="000F"/>';
		$xml .= '<quad posn="13.95 -1.6 0.14" sizen="1.8 1.8" style="Icons64x64_1" substyle="TrackInfo"/>';

		// Previous button
		if ($page > 0) {
			$xml .= '<quad posn="16.65 -1 0.12" sizen="3 3" action="-'. $utd_config['manialink_id'] . sprintf("%02d", ($page + 9)) .'" style="Icons64x64_1" substyle="Maximize"/>';
			$xml .= '<quad posn="17.05 -1.4 0.13" sizen="2.1 2.1" bgcolor="000F"/>';
			$xml .= '<quad posn="16.85 -1.2 0.14" sizen="2.5 2.5" style="Icons128x128_1" substyle="Back"/>';
		}
		else {
			$xml .= '<quad posn="16.65 -1.15 0.12" sizen="2.8 2.7" style="UIConstructionSimple_Buttons" substyle="Item"/>';
		}

		// Next button (display only if more pages to display)
		if ( ($page < 50) && (count($utd_config['plugins']) > 18) && (($page+1) < (ceil(count($utd_config['plugins'])/18))) ) {
			$xml .= '<quad posn="19.95 -1 0.12" sizen="3 3" action="'. $utd_config['manialink_id'] . sprintf("%02d", ($page + 11)) .'" style="Icons64x64_1" substyle="Maximize"/>';
			$xml .= '<quad posn="20.35 -1.4 0.13" sizen="2.1 2.1" bgcolor="000F"/>';
			$xml .= '<quad posn="20.15 -1.2 0.14" sizen="2.5 2.5" style="Icons64x64_1" substyle="MediaPlay"/>';
		}
		else {
			$xml .= '<quad posn="19.95 -1.15 0.12" sizen="2.8 2.7" style="UIConstructionSimple_Buttons" substyle="Item"/>';
		}
		$xml .= '</frame>';
		// END: Frame for Previous/Next Buttons



		// BEGIN: Titles
		$xml .= '<frame posn="2.6 -5.4 0">';
		$xml .= '<label posn="0.5 0 0.14" sizen="25 0" textsize="1" textcolor="FF0F" text="Plugin Name"/>';
		$xml .= '<label posn="26 0 0.14" sizen="13.5 0" textsize="1" textcolor="FF0F" text="Author"/>';
		$xml .= '<label posn="45 0 0.14" sizen="5 0" textsize="1" halign="right" textcolor="FF0F" text="Installed"/>';
		$xml .= '<label posn="51 0 0.14" sizen="5 0" textsize="1" halign="right" textcolor="FF0F" text="Released"/>';
		$xml .= '<label posn="52 0 0.14" sizen="8 0" textsize="1" textcolor="FF0F" text="Release Date"/>';
		$xml .= '</frame>';
		// END: Titles

		// BEGIN: Entries
		$xml .= '<frame posn="2.6 -7.6 0">';
		$line_height = 2.43;
		$position = 0;
		for ($i = ($page * 18); $i < (($page * 18) + 18); $i ++) {

			// Is there a Plugin?
			if ( !isset($utd_config['plugins'][$i]) ) {
				break;
			}

			// Get Plugin
			$plugin = $utd_config['plugins'][$i];

			// Set the right Background-Color for a line
			$background = '888E';
			if ($plugin['UPDATED'] == true) {
				if ($plugin['RELEASE'][0]['STATE'][0] == 'update') {
					$background = '3E3E';
				}
				else if ($plugin['RELEASE'][0]['STATE'][0] == 'beta') {
					$background = '09FE';
				}
				else if ($plugin['RELEASE'][0]['STATE'][0] == 'bugfix') {
					$background = 'EE3E';
				}
				else if ($plugin['RELEASE'][0]['STATE'][0] == 'hotfix') {
					$background = 'E33E';
				}
			}

			$xml .= '<label posn="0 '. $position .' 0.12" sizen="76 '. $line_height .'" action="'. $utd_config['manialink_id'] .'02" focusareacolor1="'. $background .'" focusareacolor2="BBBE" text=" "/>';
			$xml .= '<quad posn="-2.5 '. $position .' 0.13" sizen="81 '. $line_height .'" style="BgsPlayerCard" substyle="BgRacePlayerLine"/>';

			$xml .= '<format textsize="1" textcolor="'. (($plugin['UPDATED'] == true) ? '000F' : 'FFFF') .'"/>';
			$xml .= '<label posn="0.5 '. ($position - 0.5) .' 0.14" sizen="25 0" text="'. $plugin['TITLE'][0] .'"/>';
			$xml .= '<label posn="26 '. ($position - 0.5) .' 0.14" sizen="13.5 0" text="'. $plugin['AUTHOR'][0] .'"/>';
			$xml .= '<label posn="45 '. ($position - 0.5) .' 0.14" sizen="5 0" halign="right" text="'. $plugin['CURRENT'] .'"/>';
			$xml .= '<label posn="51 '. ($position - 0.5) .' 0.14" sizen="5 0" halign="right" text="'. $plugin['RELEASE'][0]['VERSION'][0] .'"/>';
			$xml .= '<label posn="52 '. ($position - 0.5) .' 0.14" sizen="8 0" text="'. $plugin['RELEASE'][0]['DATE'][0] .'"/>';

			$xml .= '<quad posn="59.5 '. ($position - 0.12) .' 0.14" sizen="8 2.2" style="Bgs1InRace" substyle="BgButtonBig" url="'. $plugin['URL'][0]['INFORMATION'][0] .'"/>';
			$xml .= '<label posn="63.5 '. ($position - 0.6) .' 0.15" sizen="8 0" halign="center" textsize="1" scale="0.85" text="$0BF$OInformation"/>';

			$xml .= '<quad posn="67.5 '. ($position - 0.12) .' 0.14" sizen="8 2.2" style="Bgs1InRace" substyle="BgButtonBig" url="'. $plugin['URL'][0]['DOWNLOAD'][0] .'"/>';
			$xml .= '<label posn="71.5 '. ($position - 0.6) .' 0.15" sizen="8 0" halign="center" textsize="1" scale="0.85" text="$0BF$ODownload"/>';


			$position -= ($line_height + 0.1);
		}
		$xml .= '</frame>';
		// END: Entries


		$xml .= '</frame>';	// Window
	}

	$xml .= '</manialink>';
	$xml .= '</manialinks>';

	return $xml;
}

/*
#///////////////////////////////////////////////////////////////////////#
#									#
#///////////////////////////////////////////////////////////////////////#
*/

function utd_buildLegendWindow ($display = true) {
	global $utd_config;


	$xml =  '<?xml version="1.0" encoding="UTF-8"?>';
	$xml .= '<manialinks>';
	$xml .= '<manialink id="'. $utd_config['manialink_id'] .'01">';

	if ($display == true) {
		$xml .= '<frame posn="-20.9 15.9 -2">';	// BEGIN: Window Frame
		$xml .= '<quad posn="-0.2 0.2 0.01" sizen="41.7 32.05" style="Bgs1InRace" substyle="BgTitle2"/>';
		$xml .= '<quad posn="1.8 -4.1 0.02" sizen="37.6 25.1" bgcolor="0018"/>';

		// Header Line
		$xml .= '<quad posn="-0.6 0.6 0.02" sizen="42.5 6" style="Bgs1InRace" substyle="BgTitle3_3"/>';
		$xml .= '<quad posn="-0.6 0.6 0.03" sizen="42.5 6" style="Bgs1InRace" substyle="BgTitle3_3"/>';

		// Title
		$xml .= '<quad posn="1.4 -1 0.04" sizen="3 3" style="UIConstructionSimple_Buttons" substyle="Tools"/>';
		$xml .= '<label posn="4.9 -1.6 0.04" sizen="37 0" textsize="2" scale="0.9" textcolor="000F" text="Legend"/>';

		// Close Button
		$xml .= '<frame posn="36.6 -0.15 0.05">';
		$xml .= '<quad posn="0 0 0.01" sizen="4.5 4.5" action="'. $utd_config['manialink_id'] .'03" style="Icons64x64_1" substyle="ArrowUp"/>';
		$xml .= '<quad posn="1.2 -1.2 0.02" sizen="2 2" bgcolor="EEEF"/>';
		$xml .= '<quad posn="0.7 -0.7 0.03" sizen="3.1 3.1" style="Icons64x64_1" substyle="Close"/>';
		$xml .= '</frame>';


		// Frame for the Legend
		$xml .= '<frame posn="-4 -5 0">';

		$xml .= '<quad posn="8.8 -2.2 0.12" sizen="11 3" bgcolor="E33E"/>';
		$xml .= '<label posn="14.2 -3.05 0.12" sizen="11 3" halign="center" textsize="1" scale="0.9" text="$000Critical Update"/>';
		$xml .= '<label posn="22 -3.05 0.12" sizen="35 3" textsize="1" scale="0.9" text="$FFFUpdate as soon as possible!"/>';

		$xml .= '<quad posn="8.8 -6.2 0.12" sizen="11 3" bgcolor="EE3E"/>';
		$xml .= '<label posn="14.2 -7.05 0.12" sizen="11 3" halign="center" textsize="1" scale="0.9" text="$000Bugfix Update"/>';
		$xml .= '<label posn="22 -7.05 0.12" sizen="35 3" textsize="1" scale="0.9" text="$FFFUpdate is required!"/>';

		$xml .= '<quad posn="8.8 -10.2 0.12" sizen="11 3" bgcolor="3E3E"/>';
		$xml .= '<label posn="14.2 -11.05 0.12" sizen="11 3" halign="center" textsize="1" scale="0.9" text="$000Feature Update"/>';
		$xml .= '<label posn="22 -11.05 0.12" sizen="35 3" textsize="1" scale="0.9" text="$FFFUpdate if you want the new features."/>';

		$xml .= '<quad posn="8.8 -14.2 0.12" sizen="11 3" bgcolor="09FE"/>';
		$xml .= '<label posn="14.2 -15.05 0.12" sizen="11 3" halign="center" textsize="1" scale="0.9" text="$000Beta Release"/>';
		$xml .= '<label posn="22 -15.05 0.12" sizen="35 3" textsize="1" scale="0.9" text="$FFFUpdate, that can run unstable."/>';

		$xml .= '<quad posn="8.8 -18.2 0.12" sizen="11 3" bgcolor="888E"/>';
		$xml .= '<label posn="14.2 -19.05 0.12" sizen="11 3" halign="center" textsize="1" scale="0.9" text="$FFFUp-To-Date"/>';
		$xml .= '<label posn="22 -19.05 0.12" sizen="35 3" textsize="1" scale="0.9" text="$FFFInstalled version is up-to-date."/>';

		$xml .= '</frame>';


		$xml .= '</frame>';	// END: Window Frame
	}

	$xml .= '</manialink>';
	$xml .= '</manialinks>';

	return $xml;
}

/*
#///////////////////////////////////////////////////////////////////////#
#									#
#///////////////////////////////////////////////////////////////////////#
*/

function utd_closeAllWindows () {
	global $utd_config;


	$xml =  '<?xml version="1.0" encoding="UTF-8"?>';
	$xml .= '<manialinks>';
	$xml .= '<manialink id="'. $utd_config['manialink_id'] .'00"></manialink>';	// Window
	$xml .= '<manialink id="'. $utd_config['manialink_id'] .'01"></manialink>';	// LegendWindow
	$xml .= '</manialinks>';

	return $xml;
}

/*
#///////////////////////////////////////////////////////////////////////#
#									#
#///////////////////////////////////////////////////////////////////////#
*/

// Simple HTTP-Connect function with timeout, taken from XAseco basic.inc.php/web_access.inc.php
// and modified to our needs.
// $method = 'GET' or 'POST'
function utd_httpConnect ($geturl, $method, $content = false, $useragent = 'InGameClient') {
	global $aseco, $utd_config;


	$url = parse_url($geturl);
	$url['port'] = isset($url['port']) ? $url['port'] : 80;

	$fp = @fsockopen($url['host'], $url['port'], $errno, $errstr, $utd_config['connect_timeout']);
	if ($fp == false) {
		return array(
			'Code'		=> 503,
			'Reason'	=> $errstr,
			'Message'	=> ''
		);
	}

	if (!(get_resource_type($fp) == 'stream')) {
		return array(
			'Code'		=> 503,
			'Reason'	=> $errstr,
			'Message'	=> ''
		);
	}

	if ($method == 'GET') {
		$query = isset($url['query']) ? '?'.$url['query'] : '';

		$request = '';
		$request .= 'GET '. $url['path'] . $query ." HTTP/1.0\r\n";
		$request .= 'Host: '. $url['host'] ."\r\n";
		$request .= 'User-Agent: '. $useragent ."\r\n";
		$request .= "Accept-Charset: UTF-8\r\n";
		$request .= "Accept-Encoding: identity;\r\n";
		$request .= "Connection: Close\r\n";
		$request .= "\r\n";

		fwrite($fp, $request);
	}
	else if ($method == 'POST') {
		$request = '';
		$request .= 'POST '. $url['path'] ." HTTP/1.0\r\n";
		$request .= 'Host: '. $url['host'] ."\r\n";
		$request .= 'User-Agent: '. $useragent ."\r\n";
		$request .= "Accept-Charset: UTF-8\r\n";
		$request .= "Accept-Encoding: identity;\r\n";
		$request .= "Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n";
		$request .= "Content-Range: bytes\r\n";
		$request .= 'Content-Length: '. strlen($content) ."\r\n";
		$request .= "Connection: Close\r\n";
		$request .= "\r\n";
		$request .= $content;

		fwrite($fp, $request);
	}
	stream_set_timeout($fp, $utd_config['wait_timeout']);

	$response = '';
	$meta['timed_out'] = false;
	while (!feof($fp) && !$meta['timed_out']) {
		$response .= fread($fp, 128);
		$meta = stream_get_meta_data($fp);
	}
	fclose($fp);

	if ( $meta['timed_out'] ) {
		return array(
			'Code'		=> 408,
			'Reason'	=> 'Request Timeout',
			'Message'	=> ''
		);
	}
	else {
		if (substr($response, 9, 3) != '200') {
			return array(
				'Code'		=> substr($response, 9, 3),
				'Reason'	=> substr($response, 13),
				'Message'	=> ''
			);
		}
		else {
			$content = explode("\r\n\r\n", $response, 2);
			return array(
				'Code'		=> 200,
				'Reason'	=> 'OK',
				'Message'	=> $content[1]
			);
		}
	}
}

?>
