File size: 7.18Kb
<?php
/* ############################################################ *\
----------------------------------------------------------------
GUN GPL
----------------------------------------------------------------
\* ############################################################ */
class chatbar{
function index() {
exit;
}
function chat_msg($uid) {
global $client;
if (!is_numeric($uid)) die('bad uid');
if (!$client) die('pls login');
$res = sql_query("select count(*) as num from ".tb()."friends where uid='{$client['id']}' and fid='{$uid}'");
$row = sql_fetch_array($res);
if (!$row['num']) {
die('you can only chat with friends');
}
$timeline = time() - 3600;
$res = sql_query("select * from ".tb()."chatrooms where
((uid='{$client['id']}' and fid='{$uid}') or (fid='{$client['id']}' and uid='{$uid}') )
and updated > $timeline order by updated desc limit 1");
$chat = sql_fetch_array($res);
$timeline = time();
if (!$chat['id']) {
$chat = array('created'=>$timeline);
sql_query("insert into ".tb()."chatrooms (uid,fid,updated,created,request_id) values({$client['id']},{$uid},$timeline,$timeline,$uid)");
$chat['id'] = insert_id();
}
else {
if ($chat['request_id'] == $client['id']) {
$newcontent = '<div class="citem"><i><strong>'.$client['username'].' joined!</strong></i></div>'.addslashes($chat['content']);
$update_request = ",request_id=0,content='$newcontent' ";
}
elseif ($chat['request_id']) {
$user = valid_user($uid);
$status = '<strong>'.t('waiting for the joining of {1}...',$user['username']).'</strong>';
}
sql_query("update ".tb()."chatrooms set updated={$timeline} $update_request where id={$chat['id']}");
}
echo $status.$chat['content'].'<br />Started from '.get_date($chat['created']);
exit;
}
function send_msg() {
global $client;
$uid = $_POST['uid'];
if (!is_numeric($uid)) die('bad uid');
if (!$client) die('pls login');
$res = sql_query("select count(*) as num from ".tb()."friends where uid='{$client['id']}' and fid='{$uid}'");
$row = sql_fetch_array($res);
if (!$row['num']) {
die('you can only chat with friends');
}
$timeline = time() - 3600;
$res = sql_query("select * from ".tb()."chatrooms where
((uid='{$client['id']}' and fid='{$uid}') or (fid='{$client['id']}' and uid='{$uid}') )
and updated > $timeline order by updated desc limit 1");
$chat = sql_fetch_array($res);
$timeline = time();
if (!$chat['id']) {
die('please wait a few seconds');
}
else {
$newcontent =
'<div class="citem"><span class="cuser">'.$client['username'].':</span>'.nl2br(h($_POST['message'])).'</div>'
.addslashes($chat['content']);
sql_query("update ".tb()."chatrooms set content='$newcontent' where id={$chat['id']}");
}
exit;
}
function chatroom($uid) {
global $client;
if (!$client) die('pls login');
$user = valid_user($uid);
if (!$user['id']) die('wrong uid');
$res = sql_query("select count(*) as num from ".tb()."friends where uid='{$client['id']}' and fid='{$user['id']}'");
$row = sql_fetch_array($res);
if (!$row['num']) {
die('you can only chat with friends');
}
echo '
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="'.uhome().'/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="'.uhome().'/js/jquery.form.js"></script>
<title>Chatting with '.$user['username'].'</title>
</head>
<body>
<style>
body {
background: #eee;
}
.citem {
margin-bottom:5px;
padding: 5px;
border-bottom: #ccc 1px solid;
}
.cuser {
font-weight: bold;
}
</style>
<script>
function update_msg() {
$("#chat_msg").load("'.uhome().'/index.php?p=chatbar/chat_msg/'.$user['id'].'");
}
jQuery(document).ready(function($) {
update_msg();
setInterval(function() {
update_msg();
}, 5000);
});
</script>
<table>
<tr><td>
Chatting with '.$user['username'].'
<div id="chat_msg" style="background:white;height:270px;width:350px;overflow:auto;font-size:11px;">
</div>
</td></tr>
<tr><td>
<script>
$(document).ready( function(){
var coptions = {beforeSubmit:showcRequest,success:showComment};
$("#pc_form").ajaxForm(coptions);
function showcRequest() {
if ($("#pc_message")[0].value == "") {
alert("please input something..");
return false;
}
$("span#pc_status").html("<img src=\"'.uhome().'/files/loading.gif\" /> Submitting");
$("#pc_form_box").toggle("slow");
}
function showComment(responseText, statusText) {
$("span#pc_status").html("");
$("#pc_message").attr("value","");
$("#pc_submit").attr("disabled",true);
$("#pc_form_box").toggle("slow");
}
$("#pc_message").bind("change keyup",function() {
$("#pc_submit").removeAttr("disabled");
$("#pc_submit").addClass("att_submit_active");
});
});
</script>
<span id="pc_status"></span>
<div id="pc_form_box">
<form id="pc_form" action="'.url('chatbar/send_msg').'" method="post">
<p>
<textarea name="message" rows="2" id="pc_message" class="fpost" style="width:300px" ></textarea>
<input type="hidden" name="uid" value="'.$uid.'" />
<input type="submit" value=" '.t('Send').' " class="fbutton" id="pc_submit" disabled />
</p>
</form>
</div>
<div id="pc_head"></div>
</td></tr>
</table>
</body>
</html>';
exit;
}
function update_chat_bar() {
global $client;
if (!$client) die('pls login');
$timeline = time() - 30;
$res = sql_query("select count(*) as num from ".tb()."friends as f left join ".tb()."accounts as u on u.id=f.fid where f.uid='{$client['id']}' and u.lastlogin>$timeline");
$row = sql_fetch_array($res);
echo 'chat ('.$row['num'].')';
exit;
}
function chat_friends_list() {
global $client;
if (!$client) die('pls login');
$res = sql_query("select count(*) as num from ".tb()."friends where uid='{$client['id']}'");
$row = sql_fetch_array($res);
$friends_all = $row['num'];
$friends_online = 0;
$timeline = time() - 15;
$reqs = array();
$res = sql_query("select uid from ".tb()."chatrooms where request_id='{$client['id']}' and updated>$timeline");
while($row = sql_fetch_array($res)) {
$reqs[] = $row['uid'];
}
$timeline = time() - 30;
$res = sql_query("select u.id as userid,u.username from ".tb()."friends as f left join ".tb()."accounts as u on u.id=f.fid where f.uid='{$client['id']}' and u.lastlogin>$timeline");
while ($user = sql_fetch_array($res)) {
if (in_array($user['userid'],$reqs)) {
$user['req'] = ' - Request!';
}
$friends_online++;
$f .= '<li class="chat_boxes"><a href="#" onclick="javascript:chatWith(\''.$user['username'].'\');hide_chat_list();return false;" class="chat_boxes" >'.$user['username'].'</a>'.$user['req'].'</li>';
}
if (!strlen($f)) {
$f = t('No one is available to chat');
}
echo '<div class="sub chat_boxes">'.$friends_online.' '.t('Online').' / '.$friends_all.' '.t('Friends').'</div>';
echo '<ul class="chat_boxes">'.$f.'</ul>';
exit;
}
}