username = $_SESSION['phpgw_info']['instant_messenger']['user'];
$this->password = $_SESSION['phpgw_info']['instant_messenger']['passwd'];
$this->server = $_SESSION['phpgw_info']['instant_messenger']['name_jabber'];
$this->port = $_SESSION['phpgw_info']['instant_messenger']['port_jabber'];
$this->resource = $_SESSION['phpgw_info']['instant_messenger']['resource_jabber'];
$this->jid = $this->username."@".$this->server . "/" . $this->resource;
parent :: __construct($this->jid, $this->password, $this->port);
}
function __destruct()
{
parent :: __destruct();
}
/*
* List Users
*/
function list_user()
{
$contacts = $this->UpdateContacts();
$group_users = array();
$group_nusers = array();
for($i=0; $i < count($contacts); $i++){
$group_users[] = strtolower(urldecode($contacts[$i]['group'])).";".urldecode($contacts[$i]['name']).";".$contacts[$i]['jid'].";".$contacts[$i]['subscription'].";".$contacts[$i]['online'];
}
// VCARD - PESSOAL
$VCARD_P = $this->getVcard($this->username."@".$this->server);
$group_nusers[0]['VCARD_FN'] = $VCARD_P[0]['FN'];
$group_nusers[0]['VCARD_NICKNAME'] = $VCARD_P[0]['NICKNAME'];
$group_nusers[0]['VCARD_ORGNAME'] = $VCARD_P[0]['ORGNAME'];
$group_nusers[0]['VCARD_ORGUNIT'] = $VCARD_P[0]['ORGUNIT'];
$group_nusers[0]['VCARD_ROLE'] = $VCARD_P[0]['ROLE'];
$group_nusers[0]['VCARD_BDAY'] = $VCARD_P[0]['BDAY'];
$group_nusers[0]['VCARD_DESC'] = $VCARD_P[0]['DESC'];
$group_nusers[0]['VCARD_JID'] = $VCARD_P[0]['JID'];
unset($VCARD_P);
natcasesort($group_users);
$i=1;
// VCARD / CONTATOS - USUARIOS
foreach($group_users as $tmp){
$aux = explode(";",$tmp);
$group_nusers[$i]['group'] = $aux[0];
$group_nusers[$i]['name'] = $aux[1];
$group_nusers[$i]['jid'] = $aux[2];
$group_nusers[$i]['subscription'] = trim($aux[3]);
$group_nusers[$i]['online'] = $aux[4];
$aux_jid = explode("@",$aux[2]);
$VCARD_U = $this->getVcard($aux_jid[0]."@".$this->server);
$group_nusers[$i]['VCARD_FN'] = $VCARD_U[0]['FN'];
$group_nusers[$i]['VCARD_NICKNAME'] = $VCARD_U[0]['NICKNAME'];
$group_nusers[$i]['VCARD_ORGNAME'] = $VCARD_U[0]['ORGNAME'];
$group_nusers[$i]['VCARD_ORGUNIT'] = $VCARD_U[0]['ORGUNIT'];
$group_nusers[$i]['VCARD_ROLE'] = $VCARD_U[0]['ROLE'];
$group_nusers[$i]['VCARD_BDAY'] = $VCARD_U[0]['BDAY'];
$group_nusers[$i]['VCARD_DESC'] = $VCARD_U[0]['DESC'];
$group_nusers[$i]['VCARD_JID'] = $VCARD_U[0]['JID'];
unset($VCARD_U);
$i++;
}
return $group_nusers;
}
/*
* Contacts - Update - Add - Remove
*/
function UpdateContacts()
{
$array_contacts = array();
$array_db = array();
$array_return = array();
$this->sendIq('get', 'update1', NULL, NULL, 'jabber:iq:roster');
usleep(50000);
$result = $this->readSocketFromServer();
if(count($result['iq'][1]['#']['query'][0]['#']['item']) > 0){
$array_contacts = $result['iq'][1]['#']['query'][0]['#']['item'];
}
$j = 0;
// Consulta no Servidor Jabber;
if(count($array_contacts) > 0 ){
for($i=0 ; $i < count($array_contacts); $i++)
{
$array_return[$j]['subscription'] = $array_contacts[$i]['@']['subscription'];
$array_return[$j]['name'] = $array_contacts[$i]['@']['name'];
$array_return[$j]['jid'] = $array_contacts[$i]['@']['jid'];
$array_return[$j]['group'] = $array_contacts[$i]['#']['group']['0']['#'];
$aux_jid = explode("@",$array_return[$j]['jid']);
$array_return[$j]['online'] = ($this->Last_access_User(trim($aux_jid[0]."@".$this->server))) ? 1:0;
$j++;
}
}
// Consulta no Bd Postgres;
$db_im = new db_im();
$array_db = $db_im->get_contacts_im(NULL,$this->jid);
if(count($array_db) > 0 )
{
for($i=0 ; $i < count($array_db); $i++)
{
$array_return[$j]['subscription'] = 'to';
$array_return[$j]['name'] = $array_db[$i]['im_from'];
$array_return[$j]['jid'] = $array_db[$i]['im_from'];
$array_return[$j]['group'] = 'sem_grupo';
$array_return[$j]['online'] = 0;
$j++;
}
}
return $array_return;
}
function AddNewContacts($pUser)
{
$jid = explode("@",$pUser['email']);
$pJid = $jid[0]."@".$this->server;
$pName = $pUser['name'];
$pGroup = $pUser['group'];
if(trim($jid[0]) != trim($this->username)){
$addid = ( $addid ) ? $addid : "adduser_" . time();
if ( $pJid )
{
$newcontact = "- " . urlencode($pGroup) . "\n
";
if($this->sendIq('set', $addid, NULL, NULL, "jabber:iq:roster", $newcontact)){
// Bd;
$db_im = new db_im();
if($db_im->delete_contacts($pJid,$this->jid)){
$this->Subscribe($pJid);
}
$this->AcceptRequest($request = array("jid" => $pJid,"subscription" => $pUser['subscription']));
return true;
}else{
return false;
}
}
else
{
$this->writeLog("ERROR: RosterAddUser() #1");
return false;
}
}else{
return false;
}
}
function UpdateUser($pUser)
{
$pJid = $pUser['jid'];
$pName = $pUser['name'];
$pGroup = $pUser['group'];
$upid = ( $upid ) ? $upid : "updateuser_" . time();
if ( $pJid )
{
$updatecontact = "- " . urlencode($pGroup) . "\n
";
if($this->sendIq('set', $upid, NULL, NULL, "jabber:iq:roster", $updatecontact)){
return true;
}else{
return false;
}
}
else
{
$this->writeLog("ERROR: RosterUpdateUser() #1");
return false;
}
}
function RosterRemoveUser($pUser)
{
$pJid = $pUser['jid'];
$pId = 'deluser_' . time();
if ($pJid && $pId)
{
$db_im = new db_im();
$db_im->delete_contacts($pJid,$this->jid);
if(!$this->sendIq('set',$pId,NULL,NULL,"jabber:iq:roster"," ")){
return false;
}
return true;
}else{
$this->writeLog("ERROR: RosterRemoveUser() #1");
return false;
}
}
/*
* Vcard
*/
function getVcard($pJid)
{
$array_vcard = array();
$id_n = explode("@",$pJid);
$id = $id_n[0];
$this->sendIq('get',$id,$pJid,NULL,"vcard-temp","");
while(($result = $this->readSocketFromServer()) == NULL ){
usleep(40);
}
if( trim($result['iq'][0]['@']['type']) === trim("result") ){
$arr = $result['iq'][0]['#']['vCard'][0]['#'];
// vCard
$array_vcard[0]['FN'] = urldecode($arr['FN']['0']['#']);
$array_vcard[0]['NICKNAME'] = urldecode($arr['NICKNAME']['0']['#']);
$array_vcard[0]['ORGNAME'] = urldecode($arr['ORG']['0']['#']['ORGNAME']['0']['#']);
$array_vcard[0]['ORGUNIT'] = urldecode($arr['ORG']['0']['#']['ORGUNIT']['0']['#']);
$array_vcard[0]['ROLE'] = urldecode($arr['ROLE']['0']['#']);
$array_vcard[0]['BDAY'] = urldecode($arr['BDAY']['0']['#']);
$array_vcard[0]['DESC'] = urldecode($arr['DESC']['0']['#']);
$array_vcard[0]['JID'] = $pJid;
}else{
$array_vcard[0]['FN'] = $pJid;
$array_vcard[0]['NICKNAME'] = $id;
$array_vcard[0]['ORGNAME'] = "";
$array_vcard[0]['ORGUNIT'] = "";
$array_vcard[0]['ROLE'] = "";
$array_vcard[0]['BDAY'] = "";
$array_vcard[0]['DESC'] = "";
$array_vcard[0]['JID'] = $pJid;
}
return $array_vcard;
}
function newVcard($pVcard_User)
{
$pVcard = explode(";",$pVcard_User['vcard']);
$nid = explode("@",$this->jid);
$id = $nid[0];
$xmlVcard = "";
// Vcard
$xmlVcard = "";
$xmlVcard .= "";
$xmlVcard .= "".urlencode($pVcard[0])."";
$xmlVcard .= "".urlencode($pVcard[1])."";
$xmlVcard .= "";
$xmlVcard .= "".urlencode($pVcard[2])."";
$xmlVcard .= "".urlencode($pVcard[3])."";
$xmlVcard .= "";
$xmlVcard .= "".urlencode($pVcard[4])."";
$xmlVcard .= "".urlencode($pVcard[5])."";
$xmlVcard .= "".urlencode($pVcard[6])."";
$xmlVcard .= "";
if(!$this->SendPacket($xmlVcard))
return false;
return true;
}
/*
* getMessage
*/
function getMessages()
{
$this->presence();
$this->sendIq('get','roster',NULL,NULL,"jabber:x:offline");
$array_return1 = $this->readSocketFromServer();
$array_message = array();
$j = 0;
for($i=0; $i < count($array_return1['message']); $i++)
{
$array_message[$j]['from'] = $array_return1['message'][$i]['@']['from'];
$array_message[$j]['to'] = $array_return1['message'][$i]['@']['to'];
$array_message[$j]['body'] = urldecode(($array_return1['message'][$i]['#']['body'][0]['#']));
$time = explode("T",$array_return1['message'][$i]['#']['x'][0]['@']['stamp']);
$h = substr($time[1],0,2);
$m = substr($time[1],3,2);
$s = substr($time[1],6,2);
// Carregando a hora;
$array_message[$j]['timestamp'] = ($h - 4 ).":".$m.":".$s;
$j++;
}
return $array_message;
}
function SendMessage($pSendMessage)
{
$to = $pSendMessage['to'];
$type = "normal";
$id = "";
$content = array(
"subject" => $pSendMessage['subject'] ? $pSendMessage['subject'] : "",
"thread" => $pSendMessage['thread'] ? $pSendMessage['thread'] : "",
"body" => $pSendMessage['body'] ? urlencode((trim(preg_replace('/^
|
$/', '', $pSendMessage['body'])))) : ""
);
$payload = "";
if ($to && is_array($content))
{
if (!$id)
{
$id = $type . "_" . time();
}
$xml = "\n";
if ($content['subject'])
{
$xml .= "" . $content['subject'] . "\n";
}
if ($content['thread'])
{
$xml .= "" . $content['thread'] . "\n";
}
$xml .= "" . $content['body'] . "\n";
$xml .= $payload;
$xml .= "\n";
unset($id);
if ($this->SendPacket($xml))
{
return TRUE;
}
else
{
$this->writeLog("ERROR: SendMessage() #1");
return FALSE;
}
}
else
{
$this->writeLog("ERROR: SendMessage() #2");
return FALSE;
}
}
/*
* Log -
*/
/*
* Verifica os acessos de cada contato no servidor;
*/
function Last_access_User($pJid)
{
$id = "last_" . time();
$result = array();
$this->sendIq('get',$id,$pJid,NULL,'jabber:iq:last');
usleep(50000);
$result = $this->readSocketFromServer();
$time_offline = $result['iq'][0]['#']['query'][0]['@']['seconds'];
return $this->time_off_line($time_offline);
}
function time_off_line($pTime)
{
$time_access = $pTime / 15;
if($time_access < 2)
return TRUE;
else
return FALSE;
}
/*
* Permissão - Autorizar / Negar ;
*/
function AcceptRequest($pRequest)
{
$jid = explode("@",$pRequest['jid']);
$pToJid = $jid[0]."@".$this->server;
if(trim($pRequest['subscription']) == 'none' || trim($pRequest['subscription']) == 'from'){
$db_im = new db_im();
if(count($db_im->get_contacts_im($this->jid, $pToJid)) == 0){
if(!$db_im->insert_contacts($this->jid , $pToJid))
return false;
else
return $this->SubscriptionAcceptRequest($pToJid);
}else{
return false;
}
}else{
if(trim($pRequest['subscription']) == 'to'){
return $this->SubscriptionAcceptRequest($pToJid);
}else{
return false;
}
}
}
function SubscriptionAcceptRequest($to = NULL)
{
return ($to) ? $this->presence("subscribed", $to) : FALSE;
}
function SubscriptionDenyRequest($to = NULL)
{
return ($to) ? $this->presence("unsubscribed", $to) : FALSE;
}
function Subscribe($to = NULL)
{
return ($to) ? $this->presence("subscribe", $to) : FALSE;
}
function Unsubscribe($to = NULL)
{
return ($to) ? $this->presence("unsubscribe", $to) : FALSE;
}
// TESTE
function encode(&$item, $val = 'encode')
{
switch( gettype($item) )
{
case 'object' :
$item = get_object_vars($item);
encode($item);
break;
case 'array' :
array_walk_recursive($item, 'encode');
break;
default : $item = utf8_encode($item);
}
}
}
?>