2010-07-27 17:14:58 +02:00
< ? php
2012-08-02 02:57:28 +02: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)
2011-01-26 20:13:10 +01:00
return mysql_query ( 'DELETE FROM glestserver WHERE lasttime<DATE_add(NOW(), INTERVAL -1 minute);' );
//return mysql_query( 'UPDATE glestserver SET status=\'???\' WHERE lasttime<DATE_add(NOW(), INTERVAL -2 minute);' );
2010-07-27 17:14:58 +02:00
}
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
}
2010-07-27 17:14:58 +02:00
?>