include?('../mysql.php');
if?(get_magic_quotes_gpc())?{
???function?stripslashes_deep($value)
???{
???????$value?=?is_array($value)??
???????????????????array_map('stripslashes_deep',?$value)?:
???????????????????stripslashes($value);
???????return?$value;
???}
???$_POST?=?array_map('stripslashes_deep',?$_POST);
???$_GET?=?array_map('stripslashes_deep',?$_GET);
???$_COOKIE?=?array_map('stripslashes_deep',?$_COOKIE);
}
if?(!isset($_GET['action']))?{
????die('This?chat?server?can?only?be?used?by?the?chat?client.');
}
$action?=?$_GET['action'];
if?($action?!=?'get'?AND?$action?!=?'add')?{?$action?=?'get';?}
//?Do?we?want?to?get?chat?messages?or?add?a?new?message?
if?($action?==?'get')?{
????//?Get?messages
????send_messages();
}?else?{
????//?Add?a?new?message
????add_message();
}
function?send_messages()?{
????global?$db;
????//?Is?there?a?latest?timestamp?
????if?(!isset($_GET['latest']))?{
????????$latest?=?false;
????}?else?{
????????$latest?=?intval($_GET['latest']);
????}
????//?If?there?isn't?a?latest,?get?the?five?newest?messages,?and?return?them
????if?($latest?==?false)?{
????????$messages?=?$db->sql_query?("SELECT?user,?message,?datetimestamp?FROM?message?ORDER?BY?datetimestamp?DESC?LIMIT?0,4");
????}?else?{
????????$messages?=?$db->sql_query?("SELECT?user,?message,?datetimestamp?FROM?message?WHERE?datetimestamp?>?$latest?ORDER?BY?datetimestamp?DESC?LIMIT?0,9");
????}
????//?Any?messages?
????if?($messages?==?false)?{
????????die('no-messages');
????}
????//?Get?newest?timestamp
????$newest?=?$messages['0']['datetimestamp'];
????//?Reverse?array?for?correct?order
????$messages?=?array_reverse($messages);
????//?Return?response
????$response?=?$newest;
????foreach?($messages?as?$message)?{
????????$response?.=?$message['user']?.?'>'?.?$message['message']?.?"\n";
????}
????$response?=?trim($response);
????die($response);
}
function?add_message()?{
????global?$db;
????//?Everything?there?
????if?(!isset($_GET['user']))?{
????????die('error:no-user');
????}
????????
????if?(!isset($_GET['message']))?{
????????die('error:no-message');
????}
????$user?=?ss(htmlentities(strip_tags($_GET['user'])));
????$message?=?ss(htmlentities(strip_tags($_GET['message'])));
????$datetimestamp?=?time();
????//?Insert?message
????$db->query?("INSERT?INTO?message?(user,?message,?datetimestamp)?VALUES?('$user',?'$message',?$datetimestamp)");
????//?Return?any?new?message
????send_messages();
}
?>