query($sql); } if ( $verb == 'unregister' ) { $u = $_SESSION['userid']; $sql = "UPDATE chatusers set logout=NOW() WHERE uid='".$u."'"; $kdb->query($sql); } if ( $verb == "listusers" && isset($_POST['roomid']) && is_numeric($_POST['roomid']) ) { $sql = "SELECT uid FROM chatusers WHERE logout='0000-00-00 00:00:00' AND roomid=".$_POST['roomid']; $ret = $kdb->query($sql); $ar = array(); while ( $row = mysql_fetch_array($ret) ) { $ar[] = $row[0]; } mysql_free_result($ret); print json_encode($ar); } if ( $verb == 'message' && isset($_POST['roomid']) && is_numeric($_POST['roomid']) ) { $u = $_SESSION['userid']; $sql = "INSERT INTO chatmessages (uid, msg, room) VALUES ('".$u."', '".$_POST['msg']."', ".$_POST['roomid'].")"; $kdb->query($sql); print "\"0\""; } if ( $verb == 'getmessages' && isset($_POST['roomid']) && is_numeric($_POST['roomid'])) { $u = $_SESSION['userid']; $sql = ""; if ( isset($_POST['last']) ) { $last = "'".$_POST['last']."'"; } else { $last = "(SELECT login FROM chatusers WHERE uid='".$u."')"; } $sql = "SELECT * FROM chatmessages WHERE room=".$_POST['roomid']." AND sent>".$last." ORDER BY sent"; $ret = $kdb->query($sql); $ar = array('messages' => array(), 'joined_users' => array(), 'left_users' => array()); while ( $row = mysql_fetch_array($ret) ) { $ar['messages'][] = array($row['uid'], $row['msg'], $row['sent'], $row['room']); } $usql = "SELECT * FROM chatusers where roomid=".$_POST['roomid']." AND login>".$last." AND logout='0000-00-00 00:00:00' ORDER BY login"; $ret2 = $kdb->query($usql); while ( $row = mysql_fetch_array($ret2) ) { $ar['joined_users'][] = array($row['uid'], $row['login']); } $lsql = "SELECT * FROM chatusers where roomid=".$_POST['roomid']." AND logout>".$last; $ret3 = $kdb->query($lsql); while ( $row = mysql_fetch_array($ret3) ) { $ar['left_users'][] = array($row['uid'], $row['logout']); } mysql_free_result($ret); mysql_free_result($ret2); mysql_free_result($ret3); print json_encode($ar); } } ?>