Created
March 7, 2019 23:25
-
-
Save josefnpat/b81f83ee4189e3873773b7133f38029c to your computer and use it in GitHub Desktop.
Simple Server List API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$mckey = "rcs"; | |
$memcache = new Memcache; | |
$memcache->addServer("localhost"); | |
$server = $memcache->get($mckey); | |
if($server == false){ | |
$server = new stdClass(); | |
$server->list = array(); | |
} | |
$server->time = time(); | |
$timeout = 10; | |
$max_connections = 100; | |
$server_change = false; | |
$attempt_validate = true; | |
$result=null; | |
$error=null; | |
foreach($server->list as $connection_key => $connection){ | |
if(time()-$connection->time > $timeout){ | |
unset($server->list[$connection_key]); | |
$server_change = true; | |
} | |
} | |
if(count($server->list) >= $max_connections){ | |
$attempt_validate = false; | |
$error = "Server has reached max connections"; | |
} | |
$connection = new stdClass(); | |
$input = $_GET; | |
if(isset($_GET['i'])){ | |
// decode json with assoc tables | |
$data = json_decode($_GET['i'],true); | |
if($data !== null){ | |
$input = $data; | |
} | |
} | |
if(isset($input['name'])){ | |
$connection->name = $input['name']; | |
} else { | |
$attempt_validate = false; | |
} | |
$connection->ip = $_SERVER['REMOTE_ADDR']; | |
if(isset($input['port'])){ | |
$connection->port = intval($input['port']); | |
} else { | |
$attempt_validate = false; | |
} | |
if(isset($input['started'])){ | |
$connection->started = filter_var($input['started'], FILTER_VALIDATE_BOOLEAN); | |
} else { | |
$attempt_validate = false; | |
} | |
if(isset($input['players'])){ | |
$connection->players = intval($input['players']); | |
} else { | |
$attempt_validate = false; | |
} | |
if(isset($input['owner'])){ | |
$connection->owner = $input['owner']; | |
} else { | |
$attempt_validate = false; | |
} | |
$connection->time = time(); | |
function valid_connection($connection){ | |
if(strlen($connection->name)==0){ | |
return "Connection name must be greater than zero characters"; | |
} | |
if(!filter_var($connection->ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)){ | |
return "Connection ip must be valid IPv4 address"; | |
} | |
if($connection->port < 1 or $connection->port > 65535){ | |
return "Connection port must be valid port"; | |
} | |
if(strlen($connection->owner)==0){ | |
return "Connection owner must be greater than zero characters"; | |
} | |
return true; | |
} | |
if($attempt_validate){ | |
$result = valid_connection($connection); | |
if($result === true){ | |
foreach($server->list as $scon_key => $scon){ | |
if($connection->owner == $scon->owner){ | |
unset($server->list[$scon_key]); | |
} | |
} | |
$server->list[] = $connection; | |
$server_change = true; | |
} | |
} | |
if($server_change){ | |
$server->list = array_values($server->list); | |
$memcache->set($mckey,$server,MEMCACHE_COMPRESSED,0); | |
} | |
if($result !== null and $result !== true){ | |
$server->error = $result; | |
} elseif($error) { | |
$server->error = $error; | |
} | |
header('Content-Type: application/json'); | |
echo json_encode($server,true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment