<?php
function get_userdata($key,$id='',$uname=''){
global $con;
    if($uname!=''){
    $query = "SELECT ".$key." AS rdata FROM (SELECT tbl_chat.id,tbl_chat.joined,tbl_chat.online,tbl_chat.last_acive,tbl_chat.status,if(tbl_user.image IS NULL,'duser.jpg',tbl_user.image) AS picname,tbl_user.name,tbl_chat.id AS username FROM tbl_chat LEFT JOIN tbl_user ON tbl_user.id=tbl_chat.id) res WHERE username='$uname' LIMIT 1";    
    }else{
    $query = "SELECT ".$key." AS rdata FROM (SELECT tbl_chat.id,tbl_chat.joined,tbl_chat.online,tbl_chat.last_acive,tbl_chat.status,if(tbl_user.image IS NULL,'duser.jpg',tbl_user.image) AS picname,tbl_user.name,tbl_chat.id AS username FROM tbl_chat LEFT JOIN tbl_user ON tbl_user.id=tbl_chat.id) res WHERE id='$id' LIMIT 1";    
    }
    $query_result = mysqli_query ($con, $query) OR error(mysqli_error($con));
    $info = mysqli_fetch_array($query_result);

    return $info['rdata'];
}

function update_lastactive($id){
global $con;
$timenow = date('Y-m-d H:i:s');
mysqli_query($con, "UPDATE tbl_chat SET online='1', last_acive = '$timenow' WHERE id='$id'");
}

function getlastActiveTime($id){
global $con; 
    $res = mysqli_query($con, "SELECT * FROM tbl_chat WHERE id='$id' AND TIMESTAMPDIFF(MINUTE, last_acive, NOW()) > 1;");
    if($res === FALSE) {
        die(mysqli_error($con));
    }
    $num = mysqli_num_rows($res);
    if($num == "0")
        $onofst = "Online";
    else
        $onofst = "Offline";

    return $onofst;
}

function timeAgo($timestamp){
    $datetime1=new DateTime("now");
    $datetime2=date_create($timestamp);
    $diff=date_diff($datetime1, $datetime2);
    $timemsg='';
    if($diff->y > 0){
        $timemsg = $diff->y .' year'. ($diff->y > 1?"s":'');

    }
    else if($diff->m > 0){
        $timemsg = $diff->m . ' month'. ($diff->m > 1?"s":'');
    }
    else if($diff->d > 0){
        $timemsg = $diff->d .' day'. ($diff->d > 1?"s":'');
    }
    else if($diff->h > 0){
        $timemsg = $diff->h .' hour'.($diff->h > 1 ? "s":'');
    }
    else if($diff->i > 0){
        $timemsg = $diff->i .' minute'. ($diff->i > 1?"s":'');
    }
    else if($diff->s > 0){
        $timemsg = $diff->s .' second'. ($diff->s > 1?"s":'');
    }
    if($timemsg == "")
        $timemsg = "Just now";
    else
        $timemsg = $timemsg.' ago';

    return $timemsg;
}

function getLocationInfoByIp(){
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = @$_SERVER['REMOTE_ADDR'];
    $result  = array('country'=>'', 'city'=>'');
    if(filter_var($client, FILTER_VALIDATE_IP)){
        $ip = $client;
    }elseif(filter_var($forward, FILTER_VALIDATE_IP)){
        $ip = $forward;
    }else{
        $ip = $remote;
    }
    $ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));
    if($ip_data && $ip_data->geoplugin_countryName != null){
        $result['code'] = $ip_data->geoplugin_countryCode;
        $result['country'] = $ip_data->geoplugin_countryName;
        $result['city'] = $ip_data->geoplugin_city;
    }
    return $result;
}
?>