<?php 
session_start(); 
// error_reporting(0);
include('userchat-config.php'); 
$list_mode = $_REQUEST['list_mode'];
$current_user_id = $_REQUEST['curr_user'];

$method = htmlentities($_REQUEST['method'], ENT_QUOTES, "utf-8");

if($method=='user_list'){

if($list_mode=='all'){
$sql = "SELECT `$usertable`.`$unique_field` as uid, `$usertable`.`$name_field` as name, `$usertable`.`userchat` FROM $usertable WHERE `$unique_field` != '$current_user_id'";
}

if($list_mode=='online'){
$time = time()-($user_offline_time*60);
$sql = "SELECT `$usertable`.`$unique_field` as uid, `$usertable`.`$name_field` as name, `$usertable`.`userchat` FROM $usertable WHERE `userchat` >= ".$time." AND `$unique_field`!='$current_user_id'";
}

if($list_mode=='contacts'){
$sql = "SELECT `".$prefix."contacts`.`fid` as uid,`$usertable`.`$name_field` as name,`$usertable`.`userchat`
FROM ".$prefix."contacts INNER JOIN $usertable
ON `$usertable`.`$unique_field`=`".$prefix."contacts`.`fid` WHERE (`".$prefix."contacts`.`uid`='$current_user_id' AND `r`='1' AND `a`='1')";
//echo $sql;
}

$res = mysqli_query($con, $sql);

$i=0;
 
while($row = mysqli_fetch_assoc($res)){
$data['data'][$i]=$row;
if(file_exists('userpics/'.$row['uid'].'.jpg')){
$data['data'][$i]['pic']=$row['uid'];
} else {
$data['data'][$i]['pic']='0';
}
++$i;
}
$data['total'] = $i;
$data['time'] = time();
$data['uid'] = $current_user_id;
if(file_exists('userpics/'.$current_user_id.'.jpg')){
$data['pic'] = $current_user_id;
} else {
$data['pic'] = 0;
}
$data['listmode'] = $list_mode;
echo json_encode($data);
}



if($method=='updater'){

/* update self as online */
$ctime = time();
mysqli_query($con, "UPDATE `$usertable` SET `userchat`='".$ctime."' WHERE `$unique_field`='$current_user_id'");

/* list online people */
if($list_mode=='online' || $list_mode=='all'){
$time = time()-($user_offline_time*60);
$sql = "SELECT `$usertable`.`$unique_field` as uid, `$usertable`.`$name_field` as name, `$usertable`.`userchat` FROM `$usertable` WHERE `userchat` >= ".$time." AND `$unique_field`!='$current_user_id'";
}

if($list_mode=='contacts'){
$time = time()-($user_offline_time*60);
$sql = "SELECT `$usertable`.`$unique_field` as uid, `$usertable`.`$name_field` as name, `$usertable`.`userchat`
FROM `$usertable`
INNER JOIN `".$prefix."contacts`
ON `$usertable`.`$unique_field`=`".$prefix."contacts`.`fid` WHERE `".$prefix."contacts`.`uid`='$current_user_id' AND `userchat` >= ".$time."";
}

$res = mysqli_query($con, $sql);

$i=0;
while($row = mysqli_fetch_assoc($res)){
$data['data'][$i]=$row;
if(file_exists('userpics/'.$row['uid'].'.jpg')){
$data['data'][$i]['pic']=$row['uid'];
} else {
$data['data'][$i]['pic']='0';
}
++$i;
}
$data['total'] = $i;
$data['time'] = time();


/* posting all messages */
if(!isset($_REQUEST['mdata'])){$_REQUEST['mdata']=array();} 
if(count($_REQUEST['mdata'])>0){
foreach($_REQUEST['mdata'] as $msg){
$msgdata = htmlentities($msg['value'], ENT_QUOTES, "UTF-8");
mysqli_query($con, "INSERT INTO `".$prefix."userchat` (`id`, `from`, `to`, `msg`, `sent`, `read`, `fx`, `tx`) 
VALUES (NULL, '$current_user_id', '".$msg['name']."', '$msgdata', '".time()."', '0', '0', '0')");
}
}

/* Getting new messages */
$sql = "SELECT `id`,`from` as `xfrom`,`msg`,`sent` FROM `".$prefix."userchat` WHERE `to`='$current_user_id' AND `read`='0' ORDER BY `sent` ASC";
$ndata = mysqli_query($con, $sql);
$j=0;
while($rowx = mysqli_fetch_assoc($ndata)){
$data['msgs'][$j]=$rowx;
$data['msgs'][$j]['msg']= html_entity_decode($data['msgs'][$j]['msg'], ENT_QUOTES, "UTF-8");
mysqli_query($con, "UPDATE `".$prefix."userchat` SET `read`='1' WHERE `id`='".$rowx['id']."'");
++$j;
}

$data['totalm'] = $j;


/* Getting new contact requests */
$sql = "SELECT `".$prefix."contacts`.`fid` as uid,`$usertable`.`$name_field` as name,`$usertable`.`userchat`
FROM ".$prefix."contacts INNER JOIN $usertable
ON `$usertable`.`$unique_field`=`".$prefix."contacts`.`fid` 
WHERE (`".$prefix."contacts`.`uid`='$current_user_id' AND `".$prefix."contacts`.`r`='0' AND `".$prefix."contacts`.`a`='1')";
$ctdata = mysqli_query($con, $sql);
$k=0;
while($rowx = mysqli_fetch_assoc($ctdata)){
$data['req'][$k]=$rowx;
if(file_exists('userpics/'.$rowx['uid'].'.jpg')){
$data['req'][$k]['pic']=$rowx['uid'];
} else {
$data['req'][$k]['pic']='0';
}
++$k;
}

$data['totreq'] = $k;

echo json_encode($data);
//print_r($_REQUEST['mdata']);
}


if($method=='old_msgs'){
$data = array();

$sql = "SELECT `id`,`from` as `xfrom`,`msg`,`sent` FROM `".$prefix."userchat` WHERE ((`to`='$current_user_id' AND `from`='$_REQUEST[user]' AND `tx`='0') OR (`from`='$current_user_id' AND `to`='$_REQUEST[user]' AND `fx`='0')) AND `sent`<'$_REQUEST[xtime]' ORDER BY `sent` DESC limit 10";
$qdata = mysqli_query($con, $sql);
$j=0;

while($rowx = mysqli_fetch_assoc($qdata)){
$data['msgs'][$j]=$rowx;
$data['msgs'][$j]['msg']= html_entity_decode($data['msgs'][$j]['msg'], ENT_QUOTES, "UTF-8");
++$j;
}

$data['totalm'] = $j;
echo json_encode($data);
}




if($method=='full_user_list'){
$data = array();

$sql = "SELECT `$usertable`.`$unique_field` as id, `$usertable`.`$name_field` as name, `$usertable`.`userchat`
FROM `$usertable`
WHERE `$usertable`.`$unique_field` NOT IN (SELECT `".$prefix."contacts`.`fid`
                          FROM ".$prefix."contacts WHERE `".$prefix."contacts`.`uid`='$current_user_id')";
$qdata = mysqli_query($con, $sql);
$j=0;

while($row = mysqli_fetch_assoc($qdata)){
$data['users'][$j]=$row;
if(file_exists('userpics/'.$row['id'].'.jpg')){
$data['users'][$j]['pic']=$row['id'];
} else {
$data['users'][$j]['pic']='0';
}
++$j;
}
$data['listmode'] = $list_mode;
$data['total'] = $j;
echo json_encode($data);
}



if($method=='add_contact'){
if($current_user_id==$_REQUEST['user_id']){
$rcode = 1;
} else {
$chk = mysqli_query($con, "SELECT * FROM `".$prefix."contacts` WHERE (`uid`='$current_user_id' AND `fid`='".$_REQUEST['user_id']."') OR (`fid`='$current_user_id' AND `uid`='".$_REQUEST['user_id']."')");
$rows = mysqli_num_rows($chk);
if($rows==0){
mysqli_query($con, "INSERT INTO `".$prefix."contacts` (`id`, `uid`, `fid`, `r`, `a`) VALUES (NULL, '$current_user_id', '".$_REQUEST['user_id']."', '1', '0')");
mysqli_query($con, "INSERT INTO `".$prefix."contacts` (`id`, `uid`, `fid`, `r`, `a`) VALUES (NULL, '".$_REQUEST['user_id']."', '$current_user_id', '0', '1')");
$rcode = 2;
} elseif($rows==2) {
$rcode = 'You and this person have already shared contact details.';
} elseif($rows==1){
$data = mysqli_fetch_assoc($chk);
if($data['uid']==$current_user_id){
$rcode = 3;	
} else {
$rcode = 4;	
}
} else {
$rcode = 5;
}
}
$data['rcode'] = $rcode;
echo json_encode($data);
}



if($method=='clear_history'){
$user = $_REQUEST['user'];
mysqli_query($con, "UPDATE ".$prefix."userchat SET `tx`='1' WHERE `to`='$current_user_id' AND `from`='$user'");
mysqli_query($con, "UPDATE ".$prefix."userchat SET `fx`='1' WHERE `from`='$current_user_id' AND `to`='$user'");
}

if($method=='rejectf'){
mysqli_query($con, "DELETE FROM `".$prefix."contacts` WHERE (`fid`='$current_user_id' AND `uid`='".$_REQUEST['user']."') OR (`uid`='$current_user_id' AND `fid`='".$_REQUEST['user']."')");
}

if($method=='acceptf'){
mysqli_query($con, "DELETE FROM `".$prefix."contacts` WHERE (`fid`='$current_user_id' AND `uid`='".$_REQUEST['user']."') OR (`uid`='$current_user_id' AND `fid`='".$_REQUEST['user']."')");
mysqli_query($con, "INSERT INTO `".$prefix."contacts` (`id`, `uid`, `fid`, `r`, `a`) VALUES (NULL, '$current_user_id', '".$_REQUEST['user']."', '1', '1')");
mysqli_query($con, "INSERT INTO `".$prefix."contacts` (`id`, `uid`, `fid`, `r`, `a`) VALUES (NULL, '".$_REQUEST['user']."', '$current_user_id', '1', '1')");
}

mysqli_close($con);
?>