$timeout = 50; // Sekunden
$timeout_msg = 2; // Stunden
if (isset($_POST["action"])) {
include("connect.php");
db_connect();
db_select();
checkTimeout();
switch($_POST["action"]) {
case "login": doLogin($_POST["nickname"], $_POST["uid"]); break;
case "logout": doLogout($_POST["uid"], true); break;
case "posting": doPosting($_POST["message"], $_POST["uid"]); break;
case "update": doUpdate($_POST["uid"], false); break;
}
}
function checkTimeout() {
global $timeout, $timeout_msg;
$now = date("Y-m-d H:i:s");
$query = 'DELETE FROM chat_user WHERE last_action < DATE_SUB("'.$now.'", INTERVAL '.$timeout.' SECOND)';
$result = mysql_query($query) or doError(mysql_error());
$ar = mysql_affected_rows();
$query = 'DELETE FROM chat_history WHERE msg_time < DATE_SUB("'.$now.'", INTERVAL '.$timeout_msg.' HOUR)';
$result = mysql_query($query) or doError(mysql_error());
if ($ar > 0 || mysql_affected_rows() > 0) {
initTables();
}
}
function initTables() {
$query = 'SELECT * from chat_history WHERE 1';
$result = mysql_query($query) or doError(mysql_error());
$lines = mysql_num_rows($result);
if ($lines == 0) {
$query = 'ALTER TABLE chat_history AUTO_INCREMENT=0';
$result = mysql_query($query) or doError(mysql_error());
}
$query = 'SELECT * from chat_user WHERE 1';
$result = mysql_query($query) or doError(mysql_error());
$lines = mysql_num_rows($result);
if ($lines == 0) {
$query = 'ALTER TABLE chat_user AUTO_INCREMENT=0';
$result = mysql_query($query) or doError(mysql_error());
}
}
function doLogin($nickname, $uid) {
if (isset($uid)) {
doLogout($uid, false);
}
if (!nickExists($nickname)) {
$uid = uniqid(rand());
$query = 'SELECT id FROM chat_history WHERE 1 ORDER BY id DESC LIMIT 1';
$result = mysql_query($query) or doError(mysql_error());
if ($line = mysql_fetch_assoc($result)) {
$lid = $line["id"];
} else {
$lid = 0;
}
$now = date("Y-m-d H:i:s");
$query = 'INSERT into chat_user (nickname, user_id, last_id, last_action) VALUES("'.$nickname.'", "'.$uid.'", "'.$lid.'", "'.$now.'")';
$result = mysql_query($query) or doError(mysql_error());
doUpdate($uid, false);
} else {
doError("Nick exists");
}
}
function nickExists($nick) {
$query = 'SELECT id from chat_user WHERE nickname="'.$nick.'"';
$result = mysql_query($query) or doError(mysql_error());
if (mysql_affected_rows() > 0) {
return true;
} else {
return false;
}
}
function doLogout($uid, $update) {
$query = 'DELETE FROM chat_user WHERE user_id = "'.$uid.'"';
$result = mysql_query($query) or doError(mysql_error());
if ($update) doUpdate($uid, true);
}
function doPosting($msg, $uid) {
if ($uid && $uid != "") {
$now = date("Y-m-d H:i:s");
$query = 'INSERT INTO chat_history (msg_time, user_id, message) VALUES("'.$now.'", "'.$uid.'", "'.$msg.'")';
$result = mysql_query($query) or doError(mysql_error());
doUpdate($uid, false);
} else {
doError("posting without ID");
}
}
function doUpdate($uid, $logout) {
if ($uid) {
// Userliste ausgeben:
$query = 'SELECT * FROM chat_user WHERE 1';
$result = mysql_query($query) or die(mysql_error());
$last_id = 0;
if ($logout) {
$myid = "";
} else {
$myid = $uid;
}
$ustr = '<users myid="'.$myid.'">';
while ($line = mysql_fetch_assoc($result)) {
if ($line["user_id"] == $uid) {
$last_id = $line["last_id"];
}
$ustr .= '<user id="'.$line["user_id"].'" nick="'.$line["nickname"].'" />';
}
$ustr .= '</users>';
// Letzte Postings ausgeben:
if (!$logout) {
$query = 'SELECT * FROM chat_history WHERE id > "'.$last_id.'" ORDER BY msg_time ASC';
$result = mysql_query($query) or doError(mysql_error());
$hstr = '<messages>';
while ($line = mysql_fetch_assoc($result)) {
$hstr .= '<msg uid="'.$line["user_id"].'" time="'.$line["msg_time"].'" ><![CDATA['.$line["message"].']]></msg>';
$last_id = $line["id"];
}
$hstr .= '</messages>';
// Letzte ID speichern:
$now = date("Y-m-d H:i:s");
$query = 'UPDATE chat_user SET last_id="'.$last_id.'", last_action="'.$now.'" WHERE user_id = "'.$uid.'"';
$result = mysql_query($query) or doError(mysql_error());
} else {
$hstr = "<messages />";
}
doOutput($ustr, $hstr);
} else {
doError("not logged in");
}
}
function doOutput($user, $msg) {
echo ($user.$msg);
}
function doError($msg) {
echo '<error msg="'.$msg.'" />';
exit();
}