Просмотр файла src/functionsChat.php

Размер файла: 13.88Kb
<?php
/*
* Harris WapChat
*
* http://www.successkid.com
*
* Harris WapChat is a wap chat application for personal and corporate used.
* Visit {@link http://www.successkid.com} for more info.
* Copyright (C) 2005 Harris Yusuf Arifin
* 
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* @author		Harris Yusuf Arifin <[email protected]>
* @copyright	Copyright 2005, Harris Yusuf Arifin
*
* Please support this software by send DONATION to E-GOLD account :
* E-gold Number : 827550
* E-gold Name 	 : earthweb egold
*/



	function cekChar($text) {
$txt=array("'","\"","$","#","!","@","%","^","&","*","(",")","-","+","=","~","`");
$text=str_replace(" ","",$text);
$retVal = "1";;
foreach($txt as $val) {
	if (strpos($text,$val) > 0) {
		$retVal = "0";
		break;
	} else {
	}
}
return $retVal;
}

	function countOnline($sesuser, $roomid, $sesCobID) {

		$sql="select distinct(USER_ID) from wc_message where ROOM_ID='$roomid' AND COB_ID='$sesCobID' AND USER_ID !='$sesuser'";
		$query=mysql_query($sql) or die(mysql_error());
		$num=mysql_num_rows($query);
		return $num;
	}
	
	function delMsgUserAll($sesuser,$datetime,$sesCobID) {
		$sql = "delete from wc_message where COB_ID='$sesCobID' AND USER_ID='$sesuser' AND DATETIME='$datetime'";
		$query = mysql_query($sql) or die(mysql_error());	
	}	

	function delMsgUser($sesuser,$sesCobID) {
		$sql = "delete from wc_message where USER_ID='$sesuser' AND COB_ID='$sesCobID'";
		$query = mysql_query($sql) or die(mysql_error());	
	}	
	
	
	function cekExpMsgUser($sesuser,$sesExpTime, $sesCobID) {
		GLOBAL $timebefore, $timenow,$temp3,$txtError;
		$timenow 	=	date("Y-m-d H:i:s"); 
		$sqlget		=	"select DATETIME from wc_message where COB_ID='$sesCobID' AND USER_ID='$sesuser' AND (DEST_ID = '$sesuser|PUBLIC' OR DEST_ID like '%0|%') order by DATETIME desc limit 1";
		$qget		=	mysql_query($sqlget) or die(mysql_error());
		$rowget		=	mysql_fetch_row($qget);
		$timebefore =	$rowget[0];
	
		$sql1 		= "select TO_DAYS('$timenow')- TO_DAYS('$timebefore')"; 
		$result1 	= mysql_query($sql1)  or die(mysql_error());           
		$temp1 		= mysql_result($result1,0);
		if (($temp1 < 0) ||( $temp1 > 0)) {
			delMsgUser($sesuser,$sesCobID);
		} else {
			$sql 	= "select DATE_FORMAT('$timenow', '%H:%i:%s')- DATE_FORMAT('$timebefore','%H:%i:%s')"; 
			$result = mysql_query($sql)  or die(mysql_error());     
			$temp 	= mysql_result($result,0); 
			if (($temp > 0) || ($temp < 0)) {
				delMsgUser($sesuser,$sesCobID);
			} else {
				$sql3 		= "select DATE_FORMAT('$timenow', '%i:%s')- DATE_FORMAT('$timebefore','%i:%s')"; 
				$result3 	= mysql_query($sql3)  or die(mysql_error());      
				$temp3 		= mysql_result($result3,0); 
				if ($temp3 > (int)$sesExpTime) {
					delMsgUser($sesuser,$sesCobID);
				} else {
				 	//do nothing
				}
	 	  }
       }
	}
	
	
	function cekExpMsg($expTime,$sesCobID) {
		$timenow 	= date("Y-m-d H:i:s");	
		$sqlget="select USER_ID, DATETIME from wc_message where COB_ID='$sesCobID'";
		$qget=mysql_query($sqlget) or die(mysql_error());
		while ($rowget=mysql_fetch_row($qget)) {
			$userid		 =	$rowget[0];
			$timebefore  =	$rowget[1];
	
		$sql1 = "select TO_DAYS('$timenow')- TO_DAYS('$timebefore')"; 
		$result1 = mysql_query($sql1)  or die(mysql_error());           
		$temp1 = mysql_result($result1,0);
		if (($temp1 < 0) ||( $temp1 > 0)) {
			delMsgUserAll($userid,$timebefore,$sesCobID);
		} else {
			$sql = "select DATE_FORMAT('$timenow', '%H:%i:%s')- DATE_FORMAT('$timebefore','%H:%i:%s')"; 
			$result = mysql_query($sql)  or die(mysql_error());     
			$temp = mysql_result($result,0); 
			if (($temp > 0) || ($temp < 0)) {
				delMsgUserAll($userid,$timebefore,$sesCobID);
			} else {
				$sql3 = "select DATE_FORMAT('$timenow', '%i:%s')- DATE_FORMAT('$timebefore','%i:%s')"; 
				$result3 = mysql_query($sql3)  or die(mysql_error());      
				$temp3 = mysql_result($result3,0); 
				if ($temp3 > $expTime) {
					delMsgUserAll($userid,$timebefore,$sesCobID);
				} else {
					//do nothing
				}
	 		}
    	 }
   }
}


	function cekUser($sesuser,$sesCobID) {
		$sql="select USER_ID from wc_message where USER_ID='$sesuser' AND COB_ID='$sesCobID'";
		$query = mysql_query($sql) or die(mysql_error());
		if (mysql_num_rows($query) > 0) {
			$retVal=TRUE;
		} else {
			$retVal=FALSE;
		}
	return $retVal;
	}
	
	function adEditSave($adid,$adname,$adtext,$adwrap) {
		$sql = "update wc_advert set ADV_NAME='$adname', ADV_TEXT='$adtext', ADV_WRAP='$adwrap', DATETIME=now()  where ID='$adid'";
		$query = mysql_query($sql) or die(mysql_error());
	}
	
	function adList($adid) {
		$sql="select ADV_NAME, ADV_TYPE, ADV_ACTIVATE, ADV_WRAP, ADV_TEXT from wc_advert where ID='$adid'";
		$query=mysql_query($sql) or die(mysql_error());			
		$num=mysql_num_rows($query);
		if ($num > 0 ) {
		$row=mysql_fetch_row($query);
			$retVal[0]=$row[0]; //ad name
			$retVal[1]=$row[1]; //ad type 
			$retVal[2]=$row[2]; //ad active
			$retVal[3]=$row[3]; //ad wrap
			$retVal[4]=$row[4]; //ad text
		}
		return $retVal;
	}
	
	function updateAd($adid,$adname, $adtext, $adtype, $adactiv) {
		$sql = "update wc_advert set ADV_ACTIVATE='0' where ID='$adid'";
		$query = mysql_query($sql) or die(mysql_error());
	}
	
	function adDisplay($adid, $sesCobID,$roomid,&$num) {
		$sql="select ADV_TEXT, ADV_WRAP from wc_advert where ADV_TYPE='$adid' AND COB_ID='$sesCobID' AND ROOM_ID='$roomid' AND ADV_ACTIVATE='1'";
		$query=mysql_query($sql) or die(mysql_error());			
		$num=mysql_num_rows($query);
		if ($num > 0 ) {
		$row=mysql_fetch_array($query);
			$retVal[0]=$row['ADV_TEXT'];
			$retVal[1]=$row['ADV_WRAP'];
		}
		return $retVal;
	}

	function adDeactivated($adid) {
		$sql = "update wc_advert set ADV_ACTIVATE='0' where ID='$adid'";
		$query = mysql_query($sql) or die(mysql_error());
	}
	
	function adActivate($adid,$advtype, $sesCobID,$roomid,$sesuser) {
		$sql = "update wc_advert set ADV_ACTIVATE='0' where ADV_TYPE='$advtype' AND COB_ID='$sesCobID' AND ROOM_ID='$roomid' AND CREATED_BY='$sesuser'";
		$query = mysql_query($sql) or die(mysql_error());
		
		$sql = "update wc_advert set ADV_ACTIVATE='1' where ID='$adid'";
		$query = mysql_query($sql) or die(mysql_error());	
	}

		
	function adDelete($adid) {
		$sql = "delete from wc_advert where ID='$adid'";
		$query = mysql_query($sql) or die(mysql_error());	
	}

	
	function dispAdvByType($idad, $sesCobID,$roomid,$sesuser) {
		$retVal=array();
		$sql="select * from wc_advert where ADV_TYPE='$idad' AND COB_ID='$sesCobID' AND ROOM_ID='$roomid' AND CREATED_BY='$sesuser' ORDER BY DATETIME desc";
		$query=mysql_query($sql) or die(mysql_error());	
		while ($row=mysql_fetch_array($query)) 
		{
			$retVal[]=$row;
		}
		return $retVal;
	}
	
	
	function dispAdvType($sesCobID,$sesuser,$idroom) {
		$retVal=array();
		$sql="select distinct(ADV_TYPE) from wc_advert where ROOM_ID='$idroom' AND COB_ID='$sesCobID' AND CREATED_BY='$sesuser'";
		$query=mysql_query($sql) or die(mysql_error());	
		while ($row=mysql_fetch_array($query)) 
		{
				$retVal[]=$row;
		}
		return $retVal;
	}
	
	
	function addAdv($adname, $adtext, $adtype, $adactiv, $sesUsername, $adroom,$sesCobID,$adwrap) {
		if ($adactiv == '1') {
			$sql = "update wc_advert set ADV_ACTIVATE='0' where ADV_TYPE='$adtype' AND ROOM_ID='$adroom'";
			$query = mysql_query($sql) or die(mysql_error());
		}
		
		$sql="insert into wc_advert VALUES (null,'$adname','$adtype','$adactiv','$adwrap','$adtext','$sesCobID','$adroom','$sesUsername',now())";
		$query=mysql_query($sql) or die(mysql_error());	
	}

	
	function updateLogTime($sesuser,$sesadmin, $sesCobID) {
		if ($sesuser == $sesadmin) {
			//do nothing
		} else {
			$sql="update wc_user set LAST_LOGIN=now() where USER_ID='$sesuser' AND COB_ID='$sesCobID'";
			$query=mysql_query($sql) or die(mysql_error());	
		}	
	}

	
	function viewMember($sesuser, $roomid,$sesCobID) {
		$retVal=array();
		$sql="select distinct(USER_ID) from wc_message where ROOM_ID='$roomid' AND COB_ID='$sesCobID' AND USER_ID !='$sesuser'";
		$query=mysql_query($sql) or die(mysql_error());	
		while ($row=mysql_fetch_array($query)) 
		{
				$retVal[]=$row;
		}
		return $retVal;
	}
	
	function viewMemberAll($roomid,$sesCobID) {
		$retVal=array();
		$sql="select distinct(USER_ID) from wc_message where ROOM_ID='$roomid' AND COB_ID='$sesCobID'";
		$query=mysql_query($sql) or die(mysql_error());	
		while ($row=mysql_fetch_array($query)) 
		{
				$retVal[]=$row;
		}
		return $retVal;
	}
	
	function searchMember($mbr, $roomid, $sesuser, $sesCobID) {
		$retVal=array();
		$sql="select distinct(USER_ID) from wc_message where ROOM_ID='$roomid' AND USER_ID like '%$mbr%' AND USER_ID !='$sesuser' AND COB_ID='$sesCobID'";
		$query=mysql_query($sql) or die(mysql_error());	
		while ($row=mysql_fetch_array($query)) 
		{
				$retVal[]=$row;
		}
		return $retVal;
	}
	
	function parseMsg($sesuser, $userid,$msg,$dest) {
		$expMsg=explode("019|",$msg);
		if (!empty($expMsg[1])) {
			$txtMsg= "<i>".$expMsg[1]."</i>";
		} else {
			if (eregi("PUBLIC",$dest)) {
				$expPubMsg=explode("|",$dest);
				$txtMsg= $expPubMsg[0].": ".$msg;
			} else {
				
				$expDest=explode("|",$dest);
				if ($expDest[0] == 0) 
				{ 
					$txtMsg= "> ".$expDest[1].": ".$msg;
				} else {
					$txtMsg= "* ".$expDest[1].": ".$msg;
				}
				
			}
		}
		return $txtMsg;
	}
		
	function selectRoom($roomid,$sesCobID) {
		$sql="select ROOM_NAME from wc_room where ROOM_ID='$roomid' AND COB_ID='$sesCobID'";
		$query=mysql_query($sql) or die(mysql_error());	
		$row=mysql_fetch_row($query);
		$roomname=$row[0];
		return $roomname;
	}
	
	function insertMsgSystem($sesuser,$sesdest,$sesroom,$msg,$sesCobID) {	
		$sesdest=$sesuser."|".$sesdest;
		$sql="insert into wc_message VALUES ('$sesCobID','$sesuser','$sesdest','$sesroom','$msg',now())";
		$query=mysql_query($sql) or die(mysql_error());					
	}
	
	
	function insertMsgPublic($sesuser,$sesdest,$sesroom,$msg,$sesCobID) {	
			$msg=strip_tags($msg);
			$members=viewMemberAll($sesroom,$sesCobID);
			$sesdest=$sesuser."|".$sesdest;
			for ($i=0; $i<count($members); $i++) {
					$listusers = $members[$i]['USER_ID'];
					$sql="insert into wc_message VALUES ('$sesCobID','$listusers','$sesdest','$sesroom','$msg',now())";
					$query=mysql_query($sql) or die(mysql_error());	
			}				
	}
	
	
	function insertMsgPrivate($sesuser,$sesdest,$sesroom,$msg,$sesCobID) {	
			
			$msg=strip_tags($msg);
			// private message
			// msg to sender	
			$sesdest1="0|".$sesdest;
			$sql="insert into wc_message VALUES ('$sesCobID','$sesuser','$sesdest1','$sesroom','$msg',now())";
			$query=mysql_query($sql) or die(mysql_error());	
		
			//msg to dest
			$sesuser2="1|".$sesuser;
			$sql="insert into wc_message VALUES ('$sesCobID','$sesdest','$sesuser2','$sesroom','$msg',now())";
			$query=mysql_query($sql) or die(mysql_error());		

	}
	
	
	function dispMsgChat($sesuser,$sesroom,$sesCobID) {
	GLOBAL $sesPhoneCtkDisplayed;
	$retVal=array();
		$sql="select * from wc_message where USER_ID='$sesuser' AND ROOM_ID='$sesroom' AND COB_ID='$sesCobID' ORDER BY DATETIME desc limit $sesPhoneCtkDisplayed";
		$query=mysql_query($sql) or die(mysql_error());	
		while ($row=mysql_fetch_array($query)) {
		$retVal[]=$row;
		}
		return $retVal;
	}
	
	
	function delMsg($sesuser,$sesCobID) {
	$sql="delete from wc_message where USER_ID='$sesuser' AND COB_ID='$sesCobID'";
	$query=mysql_query($sql) or die(mysql_error());
	}
	
	
	function deleteRoom($id,$sesuser,$sesCobID) {
		$sql="delete from wc_room where ROOM_ID='$id' AND CREATED_BY='$sesuser' AND COB_ID='$sesCobID'";
		$query=mysql_query($sql) or die(mysql_error());
	
		$sql="delete from wc_message where ROOM_ID='$id' AND COB_ID='$sesCobID'";
		$query=mysql_query($sql) or die(mysql_error());
	
		$sql="delete from wc_advert where ROOM_ID='$id' AND COB_ID='$sesCobID'";
		$query=mysql_query($sql) or die(mysql_error());
	}
	
	
	function addRoom($rn, $sesuser, $sesCobID) {	
		$roomid=$sesuser.date('YmdHis');
		
		$sql="select ROOM_ID from wc_room where ROOM_ID='$roomid' AND COB_ID='$sesCobID'";
		$query=mysql_query($sql) or die(mysql_error());	
		if (mysql_num_rows($query) > 0) {
		$retVal = FALSE;
		} else {
		$sql="insert into wc_room VALUES ('$sesCobID','$roomid','$rn','$sesuser',now())";
		$query=mysql_query($sql) or die(mysql_error());	
		$retVal = TRUE;
		}
	}
	
	function displayRoom($sesCobID) {
		$retVal=array();
		$sql="select * from wc_room where COB_ID='$sesCobID' ORDER BY DATETIME desc";
		$query=mysql_query($sql) or die(mysql_error());	
		while ($row=mysql_fetch_array($query)) {
		$retVal[]=$row;
		}
		return $retVal;
	}
	
	
	

	
 	function addUser($user,$pass,$email,$adminuser,$sesCobID) {
	$user = trim(strip_tags($user));
	$user = strtolower($user);
	if (($user == "public") OR ($user == $adminuser)) {
	$ret=FALSE;
	} else {
		$sql  = "select USER_ID from wc_user where USER_ID='$user' AND COB_ID='$sesCobID'";
		$query=mysql_query($sql) or die(mysql_error());	
		if (mysql_num_rows($query) > 0) {
			$ret=FALSE;
		} else {	
			$sql="insert into wc_user VALUES ('$sesCobID','$user','$pass','$email',now(),now())";
			$query=mysql_query($sql) or die(mysql_error());	
			$ret=TRUE;
		}
	}
	return $ret;
	}
	
	function cekLogin($user,$pass,$sesCobID) {
	GLOBAL $sesUserAdminCob, $sesPassAdminCob;
	
	if (($user ==  $sesUserAdminCob) && ($pass == $sesPassAdminCob)) {
		$ret=TRUE;
	} else {	
		$sql="select USER_ID from wc_user where USER_ID='$user' AND PASSWORD='$pass' AND COB_ID='$sesCobID'";
		$query=mysql_query($sql) or die(mysql_error());	
		if (mysql_num_rows($query) > 0) {
			$ret=TRUE;
		} else {
			$ret=FALSE;
		}
	} 
	  return $ret;
  }
?>