Topic: Ajax Chat 1.2 - SQL statement changes to modchatyxmlrpc.php
Here's my modchatxmlrpc.php - remember to change the forum id for your site:
<?php
define('PUN_ROOT', './');
require_once(PUN_ROOT.'include/common.php');
require_once(PUN_ROOT.'lang/'.$pun_user['language'].'/modchat.php');
/*****************************
* EDIT THE VALUE BELOW! *
*****************************
* Chat Log Forum
* Edit to match the ID of the forum you created where messages will be logged
*/
$db_logForum_id = 1;
/****************************************
* DO NOT EDIT ANYTHING BELOW THIS LINE *
****************************************/
if($db_logForum_id == 0)
die($lang_modchat['not configured'] . "<br />\n");
/* Headers
* to match those of PunBB
*/
header('Expires: Thu, 21 Jul 1977 07:30:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache'); // For HTTP/1.0 compability
header('Content-type: text/html; charset='.$lang_common['lang_encoding']);
switch($_POST['action']) {
case 'send':
auth();
echo send($_POST['message']);
break;
case 'receive':
auth();
echo receive($_POST['returnall']);
break;
case 'toggleChat':
echo toggleChat();
break;
case 'activeUsers':
auth();
echo activeUsers();
break;
default:
echo null;
}
function auth() {
global $pun_user;
if($pun_user['is_guest']) {
exit;
}
}
function safelyReadFile($file) {
if(function_exists('file_get_contents')) {
return file_get_contents($file);
} else {
$h = fopen($file, 'r');
$c = fread($h, filesize($file));
fclose($h);
return $c;
}
}
function toggleChat() {
global $db, $pun_user, $lang_modchat;
if(!$pun_user['is_guest']) {
$res = $db->query("SELECT showchat
FROM {$db->prefix}users
WHERE id = {$pun_user['id']}");
$format = "UPDATE {$db->prefix}users
SET showchat = %d
WHERE id = {$pun_user['id']}";
if ((int)$db->result($res) === 1) {
$qry = $db->query(sprintf($format, 0));
return null;
} else {
$db->query(sprintf($format, 1));
return safelyReadFile(PUN_ROOT.'/include/user/modchat.htm');
}
// user is guest
} else {
return "<script>alert('{$lang_modchat['guest message']}');</script>";
}
}
function send($message = null) {
global $db_logForum_id, $db, $pun_user;
$username = $db->escape($pun_user['username']);
if (!is_null($message)) {
$topic_id = activeChat();
$time = time();
if (!$topic_id) {
$subject = date('D, M jS o g:i a');
$res = $db->query("INSERT INTO {$db->prefix}topics (poster, subject, posted, last_post, last_poster, forum_id)
VALUES ('$username', '$subject', $time, $time, '$username', $db_logForum_id)")
or die("SQL Error #01 <br />\n");
$norplyinc = true;
$topic_id = $db->insert_id();
}
$res = $db->query("INSERT INTO {$db->prefix}posts (poster, poster_id, poster_ip, message, hide_smilies, posted, topic_id)
VALUES ('$username', {$pun_user['id']}, '{$_SERVER['REMOTE_ADDR']}', '".$db->escape(pun_trim(utf8_tohtml($message)))."', 0, $time, $topic_id)")
or die("SQL Error #02 <br />\n");
$post_id = $db->insert_id();
// update forums stats
$rplyinc = ($norplyinc) ? 0 : 1;
updateTopic($topic_id, $post_id, $rplyinc, $username);
update_forum($db_logForum_id);
}
return stripslashes(messageOutput($username, $message, $time, 'self'));
}
function updateTopic($topic_id, $post_id, $replyIncriment, $username) {
global $db;
$time = time();
$db->query("UPDATE {$db->prefix}topics
SET last_poster = '$username', last_post = $time, last_post_id = $post_id, num_replies = num_replies + $replyIncriment
WHERE id = $topic_id");
}
function activeChat() {
global $db_logForum_id, $db;
$time = (time()-(60*10));
$res = $db->query("SELECT id FROM {$db->prefix}topics
WHERE forum_id = $db_logForum_id
AND last_post > $time
LIMIT 0,1");
$topic_id = $db->result($res);
return ($topic_id) ? $topic_id : false;
}
function messageOutput($username, $message, $time, $self = null) {
require_once(PUN_ROOT.'/include/parser.php');
$time = userTime((int)$time);
$message = str_replace(array('<p>', '</p>'), '', parse_message($message, '0'));
return "<span class=\"modchattimestamp\">$time</span>" .
"<span class=\"modchatinput{$self}username\"><$username></span> " .
"<span class=\"modchatinput{$self}message\">". utf8_tohtml($message) ."</span><br />\n";
}
function userTime($time = false) {
global $db, $pun_user, $pun_config;
$time = ($time) ? $time : time();
$diff = ($pun_user['timezone'] - $pun_config['o_server_timezone']) * 3600;
$time += $diff;
return date('g:i', $time);
}
function receive($returnall) {
global $db, $pun_user;
$return = null;
$chat_id = activeChat();
$username = $db->escape($pun_user['username']);
if ($chat_id) {
if ($returnall == 'true') {
$res = $db->query("SELECT id,poster,message,posted
FROM {$db->prefix}posts
WHERE topic_id = $chat_id
ORDER BY id ASC");
} else {
$res = $db->query("SELECT {$db->prefix}posts.id,{$db->prefix}posts.poster,{$db->prefix}posts.message,{$db->prefix}posts.posted
FROM {$db->prefix}posts,{$db->prefix}users
WHERE {$db->prefix}posts.topic_id = $chat_id
AND {$db->prefix}posts.poster != '$username'
AND {$db->prefix}posts.id > {$pun_user['modchatlastmsg']}
ORDER BY {$db->prefix}posts.id ASC");
}
if ($db->num_rows($res) > 0) {
while ($row = $db->fetch_assoc($res)) {
if($lstMsg != $row['message']) {
$self = ($row['poster']==$pun_user['username']) ? 'self' : null;
$return .= messageOutput($row['poster'], $row['message'], $row['posted'], $self);
$id = $row['id'];
$lstMsg = $row['message'];
}
}
if($id) updateUserLastMessage($id);
}
}
return $return;
}
function updateUserLastMessage($lastMsg_id) {
global $db, $pun_user;
$db->query("UPDATE {$db->prefix}users SET modchatlastmsg = $lastMsg_id WHERE id = {$pun_user['id']}");
}
function activeUsers() {
global $db, $lang_modchat;
$ret = null;
$res = $db->query("SELECT {$db->prefix}users.username
FROM {$db->prefix}users,{$db->prefix}online
WHERE {$db->prefix}users.showchat = 1
AND {$db->prefix}online.ident = {$db->prefix}users.username
ORDER by {$db->prefix}users.username ASC");
while ($row = $db->fetch_assoc($res)) {
$ret .= '<li class="modchatuser">' .$row['username'] . "</li>\n";
}
$count = count(explode("\n", $ret)) - 1;
$unit = ($count < 2) ? $lang_modchat['user'] : $lang_modchat['users'];
return "<span id=\"modchatTotalUsers\">$count $unit {$lang_modchat['chatting']}</span>" .
"<br />\n<ul>\n$ret</ul>";
}
/* UTF8 helper function
*
* @license LGPL (http://www.gnu.org/copyleft/lesser.html)
* @author Andreas Gohr <andi@splitbrain.org>
*/
function utf8_tohtml ($str) {
$ret = '';
$max = strlen($str);
$last = 0; // keeps the index of the last regular character
for ($i=0; $i<$max; $i++) {
$c = $str{$i};
$c1 = ord($c);
if ($c1>>5 == 6) { // 110x xxxx, 110 prefix for 2 bytes unicode
$ret .= substr($str, $last, $i-$last); // append all the regular characters we've passed
$c1 &= 31; // remove the 3 bit two bytes prefix
$c2 = ord($str{++$i}); // the next byte
$c2 &= 63; // remove the 2 bit trailing byte prefix
$c2 |= (($c1 & 3) << 6); // last 2 bits of c1 become first 2 of c2
$c1 >>= 2; // c1 shifts 2 to the right
$ret .= '&#' . ($c1 * 100 + $c2) . ';'; // this is the fastest string concatenation
$last = $i+1;
}
}
return $ret . substr($str, $last, $i); // append the last batch of regular characters
}
Nullig