2010-07-27 17:14:58 +02:00
< ? php
2013-11-08 13:41:11 +01:00
// Copyright (C) 2012 Mark Vejvoda, Titus Tscharntke and Tom Reynolds
// The Megaglest Team, under GNU GPL v3.0
// ==============================================================
2010-07-27 17:14:58 +02:00
if ( ! defined ( 'INCLUSION_PERMITTED' ) || ( defined ( 'INCLUSION_PERMITTED' ) && INCLUSION_PERMITTED !== true ) ) { die ( 'This file must not be invoked directly.' ); }
# This function cleans out special characters
function clean_str ( $text )
{ // tomreyn says: I'm afraid this function is more likely to cause to trouble than to fix stuff (you have mysql escaping and html escaping elsewhere where it makes more sense, but strip off < and > here already, but then you don't filter non-visible bytes here)
//$text=strtolower($text);
2010-10-23 23:47:34 +02:00
//$code_entities_match = array('!','@','#','$','%','^','&','*','(',')','_','+','{','}','|','"','<','>','?','[',']','\\',';',"'",',','/','*','+','~','`','=');
//$code_entities_replace = array('','','','','','','','','','','','','','','','','','','','','');
$code_entities_match = array ( '$' , '%' , '^' , '&' , '_' , '+' , '{' , '}' , '|' , '"' , '<' , '>' , '?' , '[' , ']' , '\\' , ';' , " ' " , '/' , '+' , '~' , '`' , '=' );
$code_entities_replace = array ( '' , '' , '' , '' , '' , '' , '' , '' , '' , '' , '' , '' , '' );
2010-07-27 17:14:58 +02:00
$text = str_replace ( $code_entities_match , $code_entities_replace , $text );
return $text ;
}
function db_connect ()
{
// If we may use persistent MYSQL database server links...
if ( defined ( 'MYSQL_LINK_PERSIST' ) && MYSQL_LINK_PERSIST === true )
{
// ...then reuse an existing link or create a new one, ...
$linkid = mysql_pconnect ( MYSQL_HOST , MYSQL_USER , MYSQL_PASSWORD );
}
else
{
// ...otherwise create a standard link
$linkid = mysql_connect ( MYSQL_HOST , MYSQL_USER , MYSQL_PASSWORD );
}
2011-01-26 20:13:10 +01:00
mysql_select_db ( MYSQL_DATABASE );
2010-07-27 17:14:58 +02:00
return $linkid ;
}
function db_disconnect ( $linkid )
{
// note that mysql_close() only closes non-persistent connections
return mysql_close ( $linkid );
}
function get_localsubnet_ip_prefix ()
{
// If this is supposed to match any RFC1918 or even any unroutable IP address space then this is far from complete.
// Consider using something like the 'IANA Private list' provided at http://sites.google.com/site/blocklist/ instead.
// The data in this list is a subset of what IANA makes available at http://www.iana.org/assignments/ipv4-address-space/ipv4-address-space.xhtml
// Note that you may want/need to add in the IPv6 equivalent, too: http://www.iana.org/assignments/ipv6-address-space/ipv6-address-space.xhtml
return " 192. " ;
}
function get_external_ip ()
{
return $_SERVER [ 'SERVER_ADDR' ];
//return "209.52.70.192";
}
2011-01-26 20:13:10 +01:00
function cleanupServerList ()
2010-07-27 17:14:58 +02:00
{
// on a busy server, this function should be invoked by cron in regular intervals instead (one SQL query less for the script)
2013-11-01 18:23:17 +01:00
mysql_query ( 'DELETE FROM glestserver WHERE status <> 3 AND lasttime < DATE_add(NOW(), INTERVAL -1 minute);' );
2011-01-26 20:13:10 +01:00
//return mysql_query( 'UPDATE glestserver SET status=\'???\' WHERE lasttime<DATE_add(NOW(), INTERVAL -2 minute);' );
2013-11-01 18:23:17 +01:00
}
function cleanupGameStats ()
{
// Purge completed games that are less than x minutes in duration
2013-11-01 18:25:37 +01:00
mysql_query ( 'DELETE FROM glestserver WHERE status = 3 AND gameUUID in (SELECT gameUUID from glestgamestats where framesToCalculatePlaytime / 40 / 60 < ' . MAX_MINS_OLD_COMPLETED_GAMES . ');' );
2013-11-01 18:23:17 +01:00
// Cleanup game stats for games that are purged
mysql_query ( 'DELETE FROM glestgamestats WHERE gameUUID NOT IN (SELECT gameUUID from glestserver);' );
mysql_query ( 'DELETE FROM glestgameplayerstats WHERE gameUUID NOT IN (SELECT gameUUID from glestgamestats);' );
}
2011-01-10 21:09:19 +01:00
function addLatestServer ( $remote_ip , $service_port , $serverTitle , $connectedClients , $networkSlots )
{
// insert the new server
$server = " $remote_ip : $service_port " ;
$players = " $connectedClients / $networkSlots " ;
2011-01-26 20:13:10 +01:00
mysql_query ( " INSERT INTO recent_servers (name, server, players) VALUES(' $serverTitle ', ' $server ', ' $players ') " );
2011-01-10 21:09:19 +01:00
// make sure there are not too much servers
2011-01-26 20:13:10 +01:00
$count_query = mysql_fetch_assoc ( mysql_query ( " SELECT COUNT(*) as count FROM recent_servers " ));
2011-01-10 21:09:19 +01:00
$count = ( int ) $count_query [ 'count' ];
$over = $count - MAX_RECENT_SERVERS ;
2011-01-26 20:13:10 +01:00
mysql_query ( " DELETE FROM recent_servers ORDER BY id LIMIT $over " );
2011-01-10 21:09:19 +01:00
}
function updateServer ( $remote_ip , $service_port , $serverTitle , $connectedClients , $networkSlots ) {
// find the id of the server to update
$server = " $remote_ip : $service_port " ;
$players = " $connectedClients / $networkSlots " ;
2011-01-26 20:13:10 +01:00
$find_query = mysql_fetch_assoc ( mysql_query ( " SELECT id FROM recent_servers WHERE server =' $server ' ORDER BY id desc LIMIT 1 " ));
2011-01-10 21:09:19 +01:00
$id = ( int ) $find_query [ 'id' ];
// update it.
2011-01-26 20:13:10 +01:00
mysql_query ( " UPDATE recent_servers SET name=' $serverTitle ', players=' $players ' WHERE id= $id LIMIT 1 " );
2011-01-10 21:09:19 +01:00
}
2013-10-31 04:34:28 +01:00
function getTimeString ( $frames ) {
2013-10-31 05:00:26 +01:00
$framesleft = ( int ) $frames ;
2013-10-31 04:34:28 +01:00
$updateFps = 40.0 ;
2013-10-31 05:00:26 +01:00
$hours = ( int )( $frames / $updateFps / 3600.0 );
2013-10-31 04:34:28 +01:00
$framesleft = $framesleft - $hours * 3600 * $updateFps ;
2013-10-31 05:00:26 +01:00
$minutes = ( int )( $framesleft / $updateFps / 60.0 );
2013-10-31 04:34:28 +01:00
$framesleft = $framesleft - $minutes * 60 * $updateFps ;
2013-10-31 05:00:26 +01:00
$seconds = ( int )( $framesleft / $updateFps );
2013-10-31 04:34:28 +01:00
2013-10-31 05:00:26 +01:00
$hourstr = $hours ;
2013-10-31 04:34:28 +01:00
if ( $hours < 10 ) $hourstr = " 0 " . $hourstr ;
2013-10-31 05:00:26 +01:00
$minutestr = $minutes ;
2013-10-31 04:34:28 +01:00
if ( $minutes < 10 ) $minutestr = " 0 " . $minutestr ;
2013-10-31 05:00:26 +01:00
$secondstr = $seconds ;
2013-10-31 04:34:28 +01:00
if ( $seconds < 10 ) $secondstr = " 0 " . $secondstr ;
return $hourstr . " : " . $minutestr . " : " . $secondstr ;
}
2010-07-27 17:14:58 +02:00
?>