Location: PHPKode > projects > pmchat > pmchat/js_getnupdate.php
<meta http-equiv="refresh" content="5">
<?
include("db.inc");
$userid=0;
$messagetype="0";
$privatelist="";
$result="";
$result1="";


	$chatroom="";
	if(isset($_GET['chatroom']) && !empty($_GET['chatroom']))
	{
	$chatroom=$_GET['chatroom'];
	}
	if(isset($_POST['chatroom']) && ! empty($_POST['chatroom']) )
	{
	$chatroom=$_POST['chatroom'];
	}


if(isset($_POST['userid']) && !empty($_POST['userid']) )
{

	$userid=$_POST['userid'];

}
if(isset($_POST['messagetype']) && !empty($_POST['messagetype']) )
{

        $messagetype=$_POST['messagetype'];

}
//if(isset($_POST['privatelist']) && !empty($_POST['privatelist']) )
if(isset($_POST['privatelist']) )
{

        $privatelist=$_POST['privatelist'];

}

//select the public messages (messagetype=0) allocated for this user
	

$query="select * from PublicMessage pm  inner join Message m on m.messageid=pm.messageid inner join UserStatus us on us.USERID=pm.USERID where pm.DEST_USERID_LIST like '%".$userid."%' and pm.MESSAGETYPE='".$messagetype."' and pm.ROOMID=".$chatroom;
$rs=mysql_query($query);

	while($rs && $row=mysql_fetch_array($rs))
	{

	$message=$row['MESSAGE'];
	$ulist=$row['DEST_USERID_LIST'];
	$messageid=$row['MESSAGEID'];
	$uname=$row['USERNAME'];

	$result.=js($message,$uname).",";
	
	$ulist1=split(",",$ulist);
	
	$res="";

	$res=filter($userid,$ulist1);
	
	if($res!="")
	{
	$query="update PublicMessage set DEST_USERID_LIST = '".$res."' where MESSAGEID = ".$messageid." and ROOMID=".$chatroom;
	$upd=mysql_query($query);
	}
	else
	{
	$query="delete from Message where MESSAGEID=".$messageid." and ROOMID=".$chatroom;
	$upd=mysql_query($query);
	$query="delete from PublicMessage where MESSAGEID=".$messageid." and ROOMID=".$chatroom;
	$upd1=mysql_query($query);
	}
	
	$upd=mysql_query($query);
	
	}//while

	$result1=getUserList1($userid,$chatroom);


if($messagetype=="0")
{ 

$query="select * from PublicMessage pm  inner join Message m on m.messageid=pm.messageid inner join UserStatus us on us.USERID=pm.USERID where pm.DEST_USERID_LIST like '%".$userid."%' and pm.MESSAGETYPE='1' and pm.ROOMID=".$chatroom;

$rs=mysql_query($query);
	if($privatelist!="")
	{
	}

        while($rs && $row=mysql_fetch_array($rs))
        {

        $message=$row['MESSAGE'];
        $ulist=$row['DEST_USERID_LIST'];
        $messageid=$row['MESSAGEID'];
        $uname=$row['USERNAME'];
        $uid=$row['USERID'];

	if(isAllowed($uid,$privatelist))
	{

        $result.=js1($message,$uname).",";

        $ulist1=split(",",$ulist);

        $res="";
        $res=filter($userid,$ulist1);

        if($res!="")
        {
        $query="update PublicMessage set DEST_USERID_LIST = '".$res."' where MESSAGEID = ".$messageid." and ROOMID=".$chatroom;
        $upd=mysql_query($query);
        }
        else
        {
        $query="delete from Message where MESSAGEID=".$messageid." and ROOMID=".$chatroom;;
        $upd=mysql_query($query);
        $query="delete from PublicMessage where MESSAGEID=".$messageid." and ROOMID=".$chatroom;;
        $upd1=mysql_query($query);
        }
	}

        }//while
}

if($result!=""){

	$result=substr($result,0,strlen($result)-1);
	}

$output="[{MESSAGE:[".$result."],USERLIST:".$result1."}]";

echo "<pmresponse>".$output."</pmresponse>";

function js($message,$uname)
{

$message=str_replace("'","\'",$message);
$uname=str_replace("'","\'",$uname);
$str="{";
$str=$str."USER:'".$uname."',MSG:'".$message."'";
$str=$str."}";
return $str;
}

function js1($message,$uname)
{
$message=str_replace("'","\'",$message);
$uname=str_replace("'","\'",$uname);
$str="{";
$str=$str."USER:'".$uname."[Private]',MSG:'".$message."'";
$str=$str."}";
return $str;
}
function getUserList1($userid,$chatroom)
{
$query="select * from UserStatus where ACTIVE=1 and USERID!=".$userid." and ROOMID=".$chatroom;
$res=mysql_query($query);

$usernames="";
$userids="";

while($row=mysql_fetch_array($res))
{
        if($usernames=="" && $userids=="")
        {
                $usernames=$row['USERNAME'];
                $userids=$row['USERID'];
        }
        else
        {
                $usernames=$usernames.",".$row['USERNAME'];
                $userids=$userids.",".$row['USERID'];
        }
}//while

$scrpt="{USERIDS:'".$userids."',USERNAMES:'".$usernames."'}";
//$scrpt="<script language='javascript'>parent.updateUserList('".$userids."','".$usernames."')</script>";

return $scrpt;
}

function checkGroupChat($userid)
{

}//end of function


function isAllowed($uid,$plist)
{
$found=false;
if($plist=="")
{
return true;
}

$tok = strtok($plist, ",");

while ($tok !== false) {
	if($uid==$tok)
	{
		return false;
	}
   $tok = strtok(",");
}//while

return true;

}

function filter($userid,$ulist1)
{

$res="";

for($i=0;$i<count($ulist1);$i++)
{
        $temp=$ulist1[$i];
        if($temp==$userid)
        {
        }
        else
        {
                if($res=="")
                {
                $res=$temp;
                }
                else
                {
                $res=",".$res;
                }
        }
}
return $res;
}
?>
Return current item: pmchat