md-master-server /library/Server.php

Language PHP Lines 485
MD5 Hash 763b9b39c7420dfb4c048b8d5d514e52
Repository https://github.com/liquidhot/md-master-server.git View Raw File
  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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?php
require_once "Utility.php";

class Server {

	//Static Functions
	public static function updateServer(Server $server) {
		if(Server::getServerByPrivateKey($server->getPrivateKey()) === null) {
			return Server::createServer($server);
		}
	
		//Grab connection object
		$mysqli = Utility::getSQLConnection();
		
		$sql =  "UPDATE servers SET ";
		$sql .= "address = ?, port = ?, version = ?, name = ?, key_hash = ?, max_clients = ?, ";
		$sql .= "public = ?, password_protected = ?, allow_guests = ?, user_count = ?, ";
		$sql .= "user_list = ?, motd = ?, game_mode = ?, last_heartbeat_date = NOW() ";
		$sql .= "WHERE private_key = ?";
		
		
		$stmt = $mysqli->prepare($sql);
		if ($mysqli->errno) {
			trigger_error($mysqli->error,E_USER_ERROR);
		}
		
		//Bind parameters
		$stmt->bind_param("sissssississss", 
			$server->getAddress(), 
			$server->getPort(), 
			$server->getVersion(),
			$server->getName(), 
			$server->getPublicKey(),
			$server->getMaxClients(),
			$server->getPublic(),
			$server->getPasswordProtected(),
			$server->getAllowGuests(),
			$server->getUserCount(),
			$server->getUserList(),
			$server->getMotd(),
			$server->getGameMode(),
			$server->getPrivateKey());
		
		//Execute statement
		$stmt->execute();
		
		if ($mysqli->errno) {
			trigger_error($mysqli->error,E_USER_ERROR);
		}
		
		$stmt->close();
		unset($stmt);
		
		return "Server updated succesfully.\nserver=" . $server->getPublicKey();
	}
	
	//Note: this is private.
	private static function createServer(Server $server) {
		//Grab connection object
		$mysqli = Utility::getSQLConnection();
		
		$sql =  "INSERT INTO servers(";
		$sql .= "address, port, version, name, private_key, key_hash, max_clients, public, password_protected, ";
		$sql .= "allow_guests, user_count, user_list, motd, game_mode, last_heartbeat_date";
		$sql .= ") VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,NOW())";
		
		$stmt = $mysqli->prepare($sql);
		if ($mysqli->errno) {
			trigger_error($mysqli->error,E_USER_ERROR);
		}
		
		//Bind parameters
		$stmt->bind_param("sissssisssisss", 
			$server->getAddress(), 
			$server->getPort(),
			$server->getVersion(),
			$server->getName(), 
			$server->getPrivateKey(),
			$server->getPublicKey(),
			$server->getMaxClients(),
			$server->getPublic(),
			$server->getPasswordProtected(),
			$server->getAllowGuests(),
			$server->getUserCount(),
			$server->getUserList(),
			$server->getMotd(),
			$server->getGameMode());
			
		//Execute statement
		$stmt->execute();
		
		if ($mysqli->errno) {
			trigger_error($mysqli->error,E_USER_ERROR);
		}
		
		$stmt->close();
		unset($stmt);
		
		return "Server added successfully.\nserver=" . $server->getPublicKey();
	}
	
	public static function getServerByPublicKey($keyHash) {
		$server = new Server();
		
		//Grab connection object
		$mysqli = Utility::getSQLConnection();
		
		$sql =  "SELECT server_id, address, port, version, name, private_key, key_hash, ";
		$sql .= "max_clients, public, password_protected, allow_guests, user_count, ";
		$sql .= "user_list, motd, game_mode, last_heartbeat_date, created_date ";
		$sql .= "FROM servers WHERE key_hash = ?";
		
		//Prepare query to find server
		$stmt = $mysqli->prepare($sql);
		if ($mysqli->errno) {
			trigger_error($mysqli->error,E_USER_ERROR);
		}
		
		$stmt->bind_param("s", $keyHash);
		
		//Run query
		$stmt->execute();
		if ($mysqli->errno) {
			$stmt->close();
		    unset($stmt);
			trigger_error($mysqli->error,E_USER_ERROR);
		}
		
		$stmt->bind_result($serverId, $address, $port, $version, $name, $privateKey, $keyHash, 
						   $maxClients, $public, $passwordProtected, $allowGuests, $userCount, 
						   $userList, $motd, $gameMode, $lastHeartbeatDate, $createdDate);
		
		$result = $stmt->fetch();
		
		$stmt->close();
		unset($stmt);
		
		if(!$result) { //Not found or error.
			return null; 
		}		
		
		//Fill server object
		$server->setServerId($serverId);
		$server->setAddress($address);
		$server->setPort($port);
		$server->setVersion($version);
		$server->setName($name);
		$server->setPrivateKey($privateKey);
		$server->setPublicKey($keyHash);
		$server->setMaxClients($maxClients);
		$server->setPublic($public);
		$server->setPasswordProtected($passwordProtected);
		$server->setAllowGuests($allowGuests);
		$server->setUserCount($userCount);
		$server->setUserList($userList);
		$server->setMotd($motd);
		$server->setGameMode($gameMode);
		$server->setLastHeartbeatDate($lastHeartbeatDate);
		$server->setCreatedDate($createdDate);
				
		return $server;
	}
	
	public static function getServerByPrivateKey($privateKey) {
		$server = new Server();
		
		//Grab connection object
		$mysqli = Utility::getSQLConnection();
		
		$sql =  "SELECT server_id, address, port, version, name, private_key, key_hash, ";
		$sql .= "max_clients, public, password_protected, allow_guests, user_count, ";
		$sql .= "user_list, motd, game_mode, last_heartbeat_date, created_date ";
		$sql .= "FROM servers WHERE private_key = ?";
		
		//Prepare query to find server
		$stmt = $mysqli->prepare($sql);
		if ($mysqli->errno) {
			trigger_error($mysqli->error,E_USER_ERROR);
		}
		
		$stmt->bind_param("s", $privateKey);
		
		//Run query
		$stmt->execute();
		if ($mysqli->errno) {
			$stmt->close();
		    unset($stmt);
			trigger_error($mysqli->error,E_USER_ERROR);
		}
		
		$stmt->bind_result($serverId, $address, $port, $version, $name, $privateKey, $keyHash, 
						   $maxClients, $public, $passwordProtected, $allowGuests, $userCount, 
						   $userList, $motd, $gameMode, $lastHeartbeatDate, $createdDate);
		
		$result = $stmt->fetch();
		
		$stmt->close();
		unset($stmt);
		
		if(!$result) { //Not found or error.
			return null; 
		}		
		
		//Fill server object
		$server->setServerId($serverId);
		$server->setAddress($address);
		$server->setPort($port);
		$server->setVersion($version);
		$server->setName($name);
		$server->setPrivateKey($privateKey);
		$server->setPublicKey($keyHash);
		$server->setMaxClients($maxClients);
		$server->setPublic($public);
		$server->setPasswordProtected($passwordProtected);
		$server->setAllowGuests($allowGuests);
		$server->setUserCount($userCount);
		$server->setUserList($userList);
		$server->setMotd($motd);
		$server->setGameMode($gameMode);
		$server->setLastHeartbeatDate($lastHeartbeatDate);
		$server->setCreatedDate($createdDate);
				
		return $server;
	}
	
	public static function getServerList() {
		//Grab connection object
		$mysqli = Utility::getSQLConnection();
		
		$sql =  "SELECT server_id, address, port, version, name, private_key, key_hash, ";
		$sql .= "max_clients, public, password_protected, allow_guests, user_count, ";
		$sql .= "user_list, motd, game_mode, last_heartbeat_date, created_date ";
		$sql .= "FROM servers ";
		$sql .= "WHERE last_heartbeat_date > (NOW() - INTERVAL 8 HOUR) ";
		$sql .= "ORDER BY last_heartbeat_date DESC";
		
		//Prepare query to find server
		$stmt = $mysqli->prepare($sql);
		if ($mysqli->errno) {
			trigger_error($mysqli->error,E_USER_ERROR);
		}
		
		//Run query
		$stmt->execute();
		if ($mysqli->errno) {
			$stmt->close();
		    unset($stmt);
			trigger_error($mysqli->error,E_USER_ERROR);
		}
		
		$stmt->bind_result($serverId, $address, $port, $version, $name, $privateKey, $keyHash, 
						   $maxClients, $public, $passwordProtected, $allowGuests, $userCount, 
						   $userList, $motd, $gameMode, $lastHeartbeatDate, $createdDate);
		
		$serverList = array();
		while($stmt->fetch()) {
			$server = new Server();
		
			//Fill server object
			$server->setServerId($serverId);
			$server->setAddress($address);
			$server->setPort($port);
			$server->setVersion($version);
			$server->setName($name);
			$server->setPrivateKey($privateKey);
			$server->setPublicKey($keyHash);
			$server->setMaxClients($maxClients);
			$server->setPublic($public);
			$server->setPasswordProtected($passwordProtected);
			$server->setAllowGuests($allowGuests);
			$server->setUserCount($userCount);
			$server->setUserList($userList);
			$server->setMotd($motd);
			$server->setGameMode($gameMode);
			$server->setLastHeartbeatDate($lastHeartbeatDate);
			$server->setCreatedDate($createdDate);
			
			$serverList[] = $server;
		}
		
		$stmt->close();
		unset($stmt);	

		return $serverList;
	}
	
	//Internal Data
	private $serverId;
	private $address;
	private $port;
	private $version;
	private $name;
	private $privateKey;
	private $keyHash;
	private $maxClients;
	private $public;
	private $passwordProtected;
	private $allowGuests;
	private $userCount;
	private $userList;
	private $motd;
	private $gameMode;
	private $createdDate;
	private $lastHeartbeatDate;
	
	
	//Getters/Setters
	public function getServerId(){
		return $this->serverId;
	}

	public function setServerId($serverId){
		$this->serverId = $serverId;
	}

	public function getAddress(){
		return $this->address;
	}

	public function setAddress($address){
		if(strlen($address) > 1024) {
			throw new Exception("Error: Server address length exceeded. (Max 1024)");
		}
		$this->address = $address;
	}

	public function getPort(){
		return $this->port;
	}

	public function setPort($port){
		$port = intval($port);
		if($port > 65535 || $port < 0) {
			throw new Exception("Error: Port number out of range.");
		}
		$this->port = $port;
	}
	
	public function getVersion(){
		return $this->version;
	}

	public function setVersion($version){
		if(strlen($version) > 32) {
			throw new Exception("Error: Version length exceeded. (Max 32)");
		}
		$this->version = $version;
	}

	public function getName(){
		return $this->name;
	}

	public function setName($name){
		if(strlen($name) > 1024) {
			$name = substr($name, 0, 1024);
		}
		$this->name = $name;
	}

	public function getPrivateKey(){
		return $this->privateKey;
	}

	public function setPrivateKey($privateKey){
		if(strlen($privateKey) > 32) {
			throw new Exception("Error: Private key length exceeded. (Max 32)");
		}
		$this->keyHash = md5($privateKey . Config::webServerHash);
		$this->privateKey = $privateKey;
	}

	public function getPublicKey(){
		return $this->keyHash;
	}

	public function setPublicKey($keyHash){
		if(strlen($keyHash) > 32) {
			throw new Exception("Error: Public key length exceeded. (Max 32)");
		}
		$this->keyHash = $keyHash;
	}

	public function getMaxClients(){
		return $this->maxClients;
	}

	public function setMaxClients($maxClients){
		$this->maxClients = intval($maxClients);
	}

	public function getPublic(){
		return $this->public;
	}

	public function setPublic($public){
		if(strlen($public) > 5) {
			throw new Exception("Error: Public value length exceeded. (Max 5)");
		}
		$this->public = $public;
	}

	public function getPasswordProtected(){
		if(strtoupper($this->passwordProtected) === "TRUE") {
			return "True";
		} else {
			return "False";
		}
	}

	public function setPasswordProtected($passwordProtected){
		if(strlen($passwordProtected) > 5) {
			throw new Exception("Error: Password protected value length exceeded. (Max 5)");
		}
		$this->passwordProtected = $passwordProtected;
	}

	public function getAllowGuests(){
		return $this->allowGuests;
	}

	public function setAllowGuests($allowGuests){
		if(strlen($allowGuests) > 5) {
			throw new Exception("Error: Allow guests value length exceeded. (Max 5)");
		}
		$this->allowGuests = $allowGuests;
	}

	public function getUserCount(){
		return $this->userCount;
	}

	public function setUserCount($userCount){
		$this->userCount = intval($userCount);
	}

	public function getUserList(){
		return $this->userList;
	}

	public function setUserList($userList){
		if(strlen($userList) > 1024) {
			$userList = substr($userList, 0, 1024);
		}
		$this->userList = $userList;
	}

	public function getMotd(){
		return $this->motd;
	}

	public function setMotd($motd){
		if(strlen($motd) > 1024) {
			$motd = substr($motd, 0, 1024);
		}
		$this->motd = $motd;
	}

	public function getGameMode(){
		return $this->gameMode;
	}

	public function setGameMode($gameMode){
		if(strlen($gameMode) > 32) {
			$gameMode = substr($gameMode, 0, 32);
		}
		$this->gameMode = $gameMode;
	}

	public function getCreatedDate(){
		return $this->createdDate;
	}

	public function setCreatedDate($createdDate){
		$this->createdDate = $createdDate;
	}

	public function getLastHeartbeatDate(){
		return $this->lastHeartbeatDate;
	}

	public function setLastHeartbeatDate($lastHeartbeatDate){
		$this->lastHeartbeatDate = $lastHeartbeatDate;
	}
}
Back to Top