File: index.php

package info (click to toggle)
openclonk 8.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 169,656 kB
  • sloc: cpp: 180,484; ansic: 108,988; xml: 31,371; python: 1,223; php: 767; makefile: 148; sh: 101; javascript: 34
file content (226 lines) | stat: -rw-r--r-- 9,315 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php

/**
 * C4Masterserver engine backend
 *
 * @package C4Masterserver
 * @version 1.2.0-en
 * @author  Benedict Etzel <b.etzel@live.de>
 * @license http://creativecommons.org/licenses/by/3.0/ CC-BY 3.0
 */
//error_reporting(E_ALL); //suppress errors

require_once('include/C4Masterserver.php');
require_once('include/C4Network.php');
require_once('include/FloodProtection.php');
require_once('include/ParseINI.php');
$C4HostTestIncludeMode = true;
require_once('include/C4HostTest.php');

$config = file_get_contents('include/config.ini');
$link = mysql_connect(
	ParseINI::parseValue('mysql_host', $config),
	ParseINI::parseValue('mysql_user', $config),
	ParseINI::parseValue('mysql_password', $config)); //connect to MySQL
$db = mysql_selectdb(ParseINI::parseValue('mysql_db', $config), $link); //select the database

header('Content-Type: text/plain'); //output as plain text

if ($link && $db) {
	$prefix = ParseINI::parseValue('mysql_prefix', $config);
	$server = new C4Masterserver($link, $config);
	$server->setTimeoutgames(intval(ParseINI::parseValue('c4ms_timeoutgames', $config)));
	$server->setDeletegames(intval(ParseINI::parseValue('c4ms_deletegames', $config)));
	$server->setMaxgames(intval(ParseINI::parseValue('c4ms_maxgames', $config)));
	$protect = new FloodProtection($link, $prefix);
	$protect->setMaxflood(intval(ParseINI::parseValue('flood_maxrequests', $config)));
	if ($protect->checkRequest($_SERVER['REMOTE_ADDR'])) { //flood protection
		C4Network::sendAnswer(C4Network::createError('Flood protection.'));
		die();
	}
	$server->cleanUp(true); //Cleanup old stuff
	if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'release-file') {
		try {
			registerRelease();
		} catch(Exception $e) {
			C4Network::sendAnswer(C4Network::createError($e->getMessage()));
		}
	} else if (isset($GLOBALS['HTTP_RAW_POST_DATA'])) { //data sent from engine?
		$input = $GLOBALS['HTTP_RAW_POST_DATA'];
		$action = ParseINI::parseValue('Action', $input);
		$csid = ParseINI::parseValue('CSID', $input);
		$csid = mysql_real_escape_string($csid, $link);
		$reference = mysql_real_escape_string(strstr($input, '[Reference]'), $link);
		$engine_string = ParseINI::parseValue('c4ms_engine', $config);
		if (empty($engine_string) || ParseINI::parseValue('Game', $input) == $engine_string) {
			switch ($action) {
			case 'Start': //start a new round
				if (ParseINI::parseValue('LeagueAddress', $reference)) {
					C4Network::sendAnswer(C4Network::createError('League not supported!'));
				} else {
					$csid = $server->addReference($reference);
					if ($csid) {
						$answer = array('Status' => 'Success', 'CSID' => $csid);
						if(!testHostConn($input))
							$answer['Message'] = 'Your network failed to pass certain tests. It is unlikely that are you able to host for the public.|To fix that, you need port forwarding in your router.';
						C4Network::sendAnswer(C4Network::createAnswer($answer));
						unset($answer);
					} else {
						C4Network::sendAnswer(C4Network::createError('Round signup failed. (To many tries?)'));
					}
				}
				break;
			case 'Update': //update an existing round
				if ($server->updateReference($csid, $reference)) {
					C4Network::sendAnswer(C4Network::createAnswer(array('Status' => 'Success')));
				} else {
					C4Network::sendAnswer(C4Network::createError('Round update failed.'));
				}
				break;
			case 'End': //remove a round
				if ($server->removeReference($csid)) {
					C4Network::sendAnswer(C4Network::createAnswer(array('Status' => 'Success')));
				} else {
					C4Network::sendAnswer(C4Network::createError('Round end failed.'));
				}
				break;
			default:
				if (!empty($action)) {
					C4Network::sendAnswer(C4Network::createError('Unknown action.'));
				} else {
					C4Network::sendAnswer(C4Network::createError('No action defined.'));
				}
				break;
			}
		} else {
			C4Network::sendAnswer(C4Network::createError('Wrong engine, "' . ParseINI::parseValue('Game', $input) . '" expected.'));
		}
	} else { //list availabe games
		$list = array();
		if (!isset($_GET['action']) || (isset($_GET['action']) && $_GET['action'] != 'version')) {
			$list = $server->getReferenceArray(false);
		}
		$message = '';
		$engine = ParseINI::parseValue('c4ms_title_engine', $config);
		$platform = isset($_REQUEST['platform']) ? mysql_real_escape_string($_REQUEST['platform'], $link) : 0;
		$client_version = isset($_REQUEST['version']) ? mysql_real_escape_string($_REQUEST['version'], $link) : 0;
		if (!empty($engine)) {
			$message .= '[' . $engine . ']' . PHP_EOL;
			if (ParseINI::parseValue('oc_enable_update', $config) == 1) {
				if ($platform && $client_version) {
					$result = mysql_query('SELECT `new_version` FROM `' . ParseINI::parseValue('mysql_prefix', $config) . 'update` WHERE `old_version` = \'\' AND `platform` = \'' . $platform . '\'');
					$row = mysql_fetch_assoc($result);
					$version = $row['new_version'];
					if ($version) {
						$message .= 'Version=' . $version . PHP_EOL;
						// strip build version from client request
						$n = 0;
						for($i=0;$i<strlen($client_version);$i++){
							if($client_version[$i]=='.'){
								$n++;
								if($n>=3){
									break;
								}
							}
						}
						$client_version = substr($client_version,0,$i);
						if (version_compare($client_version, $version) < 0) { //We need to update
							$result = mysql_query('SELECT `file` FROM `' . $prefix . 'update` WHERE `old_version` = \'' . $client_version . '\' AND `platform` = \'' . $platform . '\'');
							$row = mysql_fetch_assoc($result);
							if ($row['file'])
								$message .= 'UpdateURL=' . ParseINI::parseValue('oc_update_url', $config) . $row['file'] . PHP_EOL;
						}
					}
				}
			}
			$motd = ParseINI::parseValue('c4ms_motd', $config);
			if (!empty($motd))
				$message .= 'MOTD=' . $motd . PHP_EOL;
		}
		foreach ($list as $reference) {
			if (!empty($message))
				$message .= PHP_EOL;
			$message .= $reference['data'];
			$message .= 'GameId=' . $reference['id'] . PHP_EOL;
			$message .= 'OfficialServer=false' . PHP_EOL;
		}
		C4Network::sendAnswer($message);
	}
	mysql_close($link);
}
else {
	C4Network::sendAnswer(C4Network::createError('Database error.'));
}


function registerRelease()
{
	global $config, $link, $prefix;

	// check request validity

	if (ParseINI::parseValue('oc_enable_update', $config) != 1)
		throw new Exception('Update disabled on this server.');
		
	// mandatory parameters
	if (!isset($_REQUEST['file']))
		throw new Exception('Missing mandatory parameter "file"');
		
	if (!isset($_REQUEST['hash']))
		throw new Exception('Missing mandatory parameter "hash"');

	if (!isset($_REQUEST['new_version']))
		throw new Exception('Missing mandatory parameter "new_version"');

	if (!isset($_REQUEST['platform']))
		throw new Exception('Missing mandatory parameter "platform"');

	if (!isset($_REQUEST['hash']))
		throw new Exception('Missing mandatory parameter "hash"');
		
	// authorization
	$absolutefile = ParseINI::parseValue('oc_update_path', $config) . $_REQUEST['file'];
		
	if (!file_exists($absolutefile))
		throw new Exception('Specified file "'.$absolutefile.'" not found.');
		
	$filehash = hash_hmac_file('sha256', $absolutefile, ParseINI::parseValue('oc_update_secret', $config));

	if ($filehash != $_REQUEST['hash'])
		throw new Exception('Authorization failure: Hash incorrect.');
		
	// checks done, now update DB
	$old_version = array();
	if (isset($_REQUEST['old_version']) && !empty($_REQUEST['old_version']))
		$old_version = explode(',', mysql_real_escape_string($_REQUEST['old_version'], $link));
		
	$delete_old_files = false;
	if (isset($_REQUEST['delete_old_files']) && $_REQUEST['delete_old_files'] == 'yes')
		$delete_old_files = true;

	$new_version = mysql_real_escape_string($_REQUEST['new_version'], $link);
	$platform = mysql_real_escape_string($_REQUEST['platform'], $link);
	$file = mysql_real_escape_string($_REQUEST['file'], $link);
	
	if (!empty($old_version)) {
		if ($delete_old_files) {
			$result = mysql_query('SELECT `file` FROM `' . $prefix . 'update` WHERE `new_version` != \'' . $new_version . '\' AND `old_version` != \'\' AND `platform` = \'' . $platform . '\'');
			while (($row = mysql_fetch_assoc($result)) != false) {
				unlink(ParseINI::parseValue('oc_update_path', $config) . $row['file']);
			}
		}
		mysql_query('DELETE FROM `' . $prefix . 'update` WHERE `new_version` != \'' . $new_version . '\' AND `old_version` != \'\' AND `platform` = \'' . $platform . '\'');
		foreach ($old_version as $version) {
			mysql_query('INSERT INTO `' . $prefix . 'update` (`old_version`, `new_version`, `platform`, `file`) VALUES (\'' . $version . '\', \'' . $new_version . '\', \'' . $platform . '\', \'' . $file . '\')');
		}
	} else {
		if ($delete_old_files) {
			$row = mysql_fetch_assoc(mysql_query('SELECT `file` FROM `' . $prefix . 'update` WHERE `old_version` = \'\' AND `platform` = \'' . $platform . '\''));
			unlink(ParseINI::parseValue('oc_update_path', $config) . $row['file']);
		}
		mysql_query('DELETE FROM `' . $prefix . 'update` WHERE `old_version` = \'\' AND `platform` = \'' . $platform . '\'');
		mysql_query('INSERT INTO `' . $prefix . 'update` (`old_version`, `new_version`, `platform`, `file`) VALUES (\'\', \'' . $new_version . '\', \'' . $platform . '\', \'' . $file . '\')');
	}
}

?>