чат на Ajax/jQuery.ajax
юзер:
сообщения общий, приват
модератор:
удаление сообщений
админ:
очистка чата (команда
/prune)
просмотр привата
файл в папку блоки
block-chatik.phpCODE
<?php
if (!defined('BLOCK_FILE')) {
header("Location: ../index.php");
exit;
}
$content=<<<BLOCKHTML
<style type="text/css">
.shoot_private{
width: 100%;
background: #ccffff;
}
.shoot_public {
width: 100%;
}
</style>
<form action="shoutbox1.php" method="post" name="shoutform" onsubmit="return sendShout(this);">
<table cellspacing="0" cellpadding="5" width="100%">
<tr>
<td colspan="2">
<div id="shoutbox" style="overflow: auto; height: 270px; width: 100%; padding-top: 0cm">
Загрузка...
</div>
</td>
</tr>
<tr>
<td align="center" id="loading-layer"> </td>
<td style="white-space: nowrap" width="90%">
<center>
<a href="java script: SmileIT(';-)','shoutform','shout')"><img src=pic/smilies/wink.gif border=0></a>
<a href="java script: SmileIT(':-P','shoutform','shout')"><img src=pic/smilies/tongue.gif border=0></a>
<a href="java script: SmileIT(':-)','shoutform','shout')"><img border=0 src=pic/smilies/smile1.gif border=0></a>
<a href="java script: SmileIT(':w00t:','shoutform','shout')"><img border=0 src=pic/smilies/w00t.gif border=0></a>
<a href="java script: SmileIT(':-D','shoutform','shout')"><img border=0 src=pic/smilies/grin.gif border=0></a>
<a href="java script: SmileIT(':lol:','shoutform','shout')"><img border=0 src=pic/smilies/laugh.gif border=0></a>
<a href="java script: SmileIT(':-/','shoutform','shout')"><img border=0 src=pic/smilies/confused.gif border=0></a>
<a href="java script: SmileIT(':-(','shoutform','shout')"><img border=0 src=pic/smilies/sad.gif border=0></a>
<a href="java script: SmileIT(':-O','shoutform','shout')"><img src=pic/smilies/ohmy.gif border=0></a>
<a href="java script: SmileIT('8-)','shoutform','shout')"><img src=pic/smilies/cool1.gif border=0></a>
<a href="java script: SmileIT(':sly:','shoutform','shout')"><img src=pic/smilies/sly.gif border=0></a>
<a href="java script: SmileIT(':greedy:','shoutform','shout')"><img src=pic/smilies/greedy.gif border=0></a>
<a href="java script: SmileIT(':weirdo:','shoutform','shout')"><img src=pic/smilies/weirdo.gif border=0></a>
<a href="java script: SmileIT(':sneaky:','shoutform','shout')"><img src=pic/smilies/sneaky.gif border=0></a>
<a href="java script: SmileIT(':?:','shoutform','shout')"><img src=pic/smilies/question.gif border=0></a>
<a href="java script: SmileIT(':!:','shoutform','shout')"><img src=pic/smilies/excl.gif border=0></a> <input type="button" value="Смайлы" onClick="java script:winop()"></center>
<div style="vertical-align: middle;">
<input type="text" name="shout" style="width: 80%" MAXLENGTH="200"> <input type="submit" name="shout_sub" value="Отправить"></div>
</td>
</table>
</form>
<script type="text/javascript">
<!--
function SmileIT(smile,form,text){
document.forms[form].elements[text].value = document.forms[form].elements[text].value+" "+smile+" ";
document.forms[form].elements[text].focus();
}
function shoot_to(username){
parent.document.shoutform.shout.focus();
parent.document.shoutform.shout.value='['+username+'] '+parent.document.shoutform.shout.value;
return false
}
function shoot_private(username){
parent.document.shoutform.shout.focus();
parent.document.shoutform.shout.value='privat('+username+') '+parent.document.shoutform.shout.value;
return false
}
function winop() {
windop = window.open("moresmiles.php?form=shoutform&text=shout","mywin","height=500,width=450,resizable=no,scrollbars=yes");
}
function sendShout(formObj) {
Shout = formObj.shout.value
if (Shout.replace(/ /g, '') == '') {
alert('Вы должны ввести сообщение!')
return false
}
sb_Clear();
jQuery("input[name=shout]").attr("disabled","disabled");
jQuery("input[name=shout_sub]").attr("disabled","disabled");
jQuery("input[name=shout]").attr("value","sending.....");
jQuery.post("shoutbox1.php", { 'do_c': "shout", 'shout': escape(Shout)}, function(data) {
if(typeof(data.error) != 'undefined')
{
if(data.error != '')
{
alert(data.error);
}else
{
setTimeout("jQuery('input[name=shout]').attr('value','susses!!');",1000);
}
}
}, "json");
setTimeout("sb_enable();",2000);
return false
}
function getShouts() {
jQuery("#loading-layer").append("<img src=\"pic/loading.gif\" border=\"0\">");
jQuery.get("shoutbox1.php", function(data){
jQuery("#shoutbox").empty();
jQuery("#shoutbox").append(data);});
setTimeout("jQuery('#loading-layer').empty();",1000);
setTimeout("getShouts();", 10000);
return false
}
function sb_Clear() {
jQuery("input[name=shout]").attr("value","");
return true;
}
function sb_enable() {
jQuery("input[name=shout]").removeAttr("disabled");
jQuery("input[name=shout_sub]").removeAttr("disabled");
jQuery("input[name=shout]").attr("value","");
return true;
}
function deleteShout(id) {
jQuery.post("shoutbox1.php", { 'do_c': "delete", 'id': id}, function(data) {
if(typeof(data.error) != 'undefined')
{
if(data.error != '')
{
alert(data.error);
}else
{
jQuery("#s"+id).empty();
}
}
}, "json");
return false
}
getShouts();
-->
</script>
BLOCKHTML;
?>
файл в корень
shoutbox1.phpCODE
<?
require_once("include/bittorrent.php");
dbconn();
loggedinorreturn();
header("Content-Type: text/html; charset=Windows-1251");
function decode_unicode_url($str) {
$res = '';
$i = 0;
$max = strlen($str) - 6;
while ($i <= $max) {
$character = $str[$i];
if ($character == '%' && $str[$i + 1] == 'u') {
$value = hexdec(substr($str, $i + 2, 4));
$i += 6;
if ($value < 0x0080) // 1 byte: 0xxxxxxx
$character = chr($value);
else if ($value < 0x0800) // 2 bytes: 110xxxxx 10xxxxxx
$character =
chr((($value & 0x07c0) >> 6) | 0xc0)
. chr(($value & 0x3f) | 0x80);
else // 3 bytes: 1110xxxx 10xxxxxx 10xxxxxx
$character =
chr((($value & 0xf000) >> 12) | 0xe0)
. chr((($value & 0x0fc0) >> 6) | 0x80)
. chr(($value & 0x3f) | 0x80);
} else
$i++;
$res .= $character;
}
return $res . substr($str, $i);
}
function convert_text($s)
{
$out = "";
for ($i=0; $i<strlen($s); $i++)
{
$c1 = substr ($s, $i, 1);
$byte1 = ord ($c1);
if ($byte1>>5 == 6) // 110x xxxx, 110 prefix for 2 bytes unicode
{
$i++;
$c2 = substr ($s, $i, 1);
$byte2 = ord ($c2);
$byte1 &= 31; // remove the 3 bit two bytes prefix
$byte2 &= 63; // remove the 2 bit trailing byte prefix
$byte2 |= (($byte1 & 3) << 6); // last 2 bits of c1 become first 2 of c2
$byte1 >>= 2; // c1 shifts 2 to the right
$word = ($byte1<<8) + $byte2;
if ($word==1025) $out .= chr(168); // ?
elseif ($word==1105) $out .= chr(184); // ?
elseif ($word>=0x0410 && $word<=0x044F) $out .= chr($word-848); // ?-? ?-?
else
{
$a = dechex($byte1);
$a = str_pad($a, 2, "0", STR_PAD_LEFT);
$b = dechex($byte2);
$b = str_pad($b, 2, "0", STR_PAD_LEFT);
$out .= "&#x".$a.$b.";";
}
}
else
{
$out .= $c1;
}
}
return $out;
}
if ( isset($_POST["do_c"]) ){
if ($_POST["do_c"] == "shout") {
$shout = convert_text(urldecode(decode_unicode_url($_POST["shout"])));
if ($shout == "/prune" && get_user_class() >= UC_ADMINISTRATOR) {
mysql_query("TRUNCATE TABLE shoutbox1");
$shout = "очистил чат.";
}
$sender = $CURUSER["id"];
if (!empty($shout)) {
$shout = preg_replace("/\/me /", $CURUSER["username"]." ", $shout);
mysql_query("INSERT INTO shoutbox1 (public, userid, text) VALUES (" . time() . ", " . $sender . ", '" . $shout . "');") or $error = 'err';
} else
$error = 'Введи сообщение!';
} elseif ($_POST["do_c"] == "delete" && get_user_class() >= UC_MODERATOR && is_valid_id($_POST["id"])) {
$id = $_POST["id"];
mysql_query("DELETE FROM shoutbox1 WHERE id = $id") or $error = 'err';
}
echo "{";
echo "error: '" . $error . "',\n";
echo "msg: 'ok'\n";
echo "}";
die();
}
$res = mysql_query("SELECT shoutbox1.*, users.username, users.warned, users.id as uid, users.class FROM shoutbox1 INNER JOIN users ON shoutbox1.userid = users.id ORDER BY id DESC LIMIT 90") or sqlerr(__FILE__,__LINE__);
if (mysql_num_rows($res) == 0)
die("Сообшений нет");
print("\n");
while ($arr = mysql_fetch_array($res)) {
$warn = "";
if ( $arr["warned"] == "yes" )
$warn = "<img src=\"pic/warned.gif\" alt=\"Warned\"/>";
$public = date("d m : H:i:s", $arr["public"]);
$user_id = $arr["uid"];
$username = $arr["username"];
$arr["text"] = format_comment($arr["text"]);
$arr["text"] = str_replace("[$CURUSER[username]]","<font color=#000000>{</font><b style='color: red; background: #FFFFFF;'>$CURUSER[username]</b><font color=#000000>}</font>",$arr["text"]);
$arr["text"] = preg_replace("/\[((\s|.)+?)\]/", "<font color=#000000>[</font><b style='color: black;'>\\1</b><font color=#000000>]</font>", $arr["text"]);
$moder = (get_user_class() >= UC_MODERATOR ? "<span id='s$arr[id]' onclick=\"deleteShout($arr[id]);\" style=\"cursor: pointer; color: red; font-weight: bold;\">X</span>" : "");
$public_head = $moder . " [$public] <a href=\"userdetails.php?id=$user_id\"><img src=\"pic/info/guest.gif\" title=\"Посмотреть профиль\" border=\"0\"></a> <a target=\"_blank\" href=\"message.php?action=sendmessage&receiver=$user_id\" title=\"Отправить ЛС\"><img src=\"pic/pn_inbox.gif\" border=\"0\"></a> <span onClick=\"shoot_to('$username');\" style=\"cursor: pointer;font-weight: bold;\">" . get_user_class_color($arr["class"], $arr["username"]) . "</span> <span onclick=\"shoot_private('$username');\" style=\"cursor: pointer;\"><img src=\"pic/info//group.gif\" title=\"Приват\" border=\"0\"></span>$warn: ";
if (strpos($arr["text"], "privat(") !== false) {
$variabila = "/privat($CURUSER[username])/";
if ( (preg_match($variabila, $arr["text"])) OR (get_user_class() >= UC_ADMINISTRATOR)) {
$arr["text"] = preg_replace("/privat\(([^()<>\s]+?)\)/i","<b style='color: #000000;'>>> \\1</b>", $arr["text"]);
print("<div class=\"shoot_private\">" . $public_head . ($arr["text"]). "</div>\n");
}
} else {
print("<div class=\"shoot_public\">" . $public_head . ($arr["text"])."</div>\n");
}
}
?>
база
SQL
CREATE TABLE `shoutbox1` (
`id` smallint(6) NOT NULL auto_increment,
`userid` smallint(6) NOT NULL default '0',
`public` int(11) default '0',
`text` text NOT NULL,
PRIMARY KEY (`id`)
)