]+from='(\S+)'[^>]*>(?(?=.*?]*>]*>(.*?)<\/body><\/html>).*?<\/message>/s"; $_messages = preg_match_all($_messages, $xml, $messages); if ( $_messages !== false && $_messages > 0 ) { $xml = preg_replace("//", "", $xml); $return .= $this->getMessages($messages); } ### - Fim messages - ### ### - contacts - ### $_contacts = "/]+id='contacts'[^>]*>.*?<\/iq>/"; $_contacts = preg_match_all($_contacts, $xml, $_xml); $_contacts = "/]+name)[^>]+name='(.*?)')[^>]+subscription='(.*?)'[^>]+jid='(.*?)'"; $_contacts .= "(?(?=[^>]*\/>)[^>]*\/>|[^>]*>(.*?)<\/group><\/item>)/"; $_contacts = preg_match_all($_contacts, $_xml[0][0], $contacts); if ( $_contacts !== false && $_contacts > 0 ) { $xml = preg_replace("/]+id='contacts'[^>]*>.*?<\/iq>/", "", $xml); $return .= rawurldecode(urldecode($this->getContacts($contacts))); } ### - Fim contacts - ### ### - presence - ### $xml = $_SESSION['phpgw_info']['instant_messenger']['presences'] . $xml; $_presence = "/]+\/>)[^>]+\/>|[^>]+>.*?<\/presence>)/"; $_presence = preg_match_all($_presence, $xml, $presence); $patterns[0] = '/[^<]*<\/priority>|<(\S) (?(?=[^>]*\/>)[^>]*\/>|[^>]*>.*?<\/\1>)/'; $patterns[1] = '/ xmlns=\'.*?\'| to=\'.*?\'/'; $patterns[2] = '/> +<'; $_xml = preg_replace($patterns, $replacements, implode('', $presence[0])); $_presence = "/]+from='(\S+)'[^>]*type='(unavailable)'(?(?=[^>]*\/>)[^>]*\/>|[^>]*>.*?<\/presence>)|"; $_presence .= "]+from='(\S+)'(?(?=[^>]+type)[^>]+type='(\S+)')[^>]*"; $_presence .= "(?(?=\/>)\/>|>"; $_presence .= "(?(?=]*?>)]*?>(.*?)<\/s[^>]*?>(?(?=]*?>)]*?>(.*?)<\/s[^>]*?>))"; $_presence .= "<\/presence>)/"; $_presence = preg_match_all($_presence, $_xml, $presence); if ( $_presence !== false && $_presence > 0 ) { foreach ( $presence[5] as $key => $val ) { if ( !empty($presence[1][$key]) && (strpos($presence[1][$key], '/') !== false) ) $presence[1][$key] = substr($presence[1][$key], 0, strpos($presence[1][$key], '/')); if ( !empty($presence[3][$key]) && strpos($presence[3][$key], '/') !== false ) $presence[3][$key] = substr($presence[3][$key], 0, strpos($presence[3][$key], '/')); if ( !empty($val) && !preg_match("/^(away|chat|dnd|xa)$/", $val) ) if ( preg_match("/^(away|chat|dnd|xa)$/", $presence[6][$key]) ) { $aux = $presence[5][$key]; $presence[5][$key] = $presence[6][$key]; $presence[6][$key] = $aux; } else { if ( empty($presence[6][$key]) ) $presence[6][$key] = $presence[5][$key]; $presence[5][$key] = ''; } } $xml = preg_replace("/]\/?>|]>.*?<\/presence>/", "", $xml); $return .= $this->getPresence($presence); } ### - Fim presence - ### ### - vcard - ### ### - Arranjo Tecnico - ### $_xml = $vkrd; $_vcard = "/]+\/>|)<\/iq>/"; $_vcard = preg_match_all($_vcard, $_xml, $vcard); if( $_vcard !== false && $_vcard > 0) { $xml = preg_replace("//", "", $xml); $return .= urldecode($this->getVcard($vcard)); } ### - Fim vcard - ### ## Final do Listen; return $return; } function reconnect() { require_once("IMConfigure.php"); $_transport = TRANSPORT_NON_SECURITY; $_port = PORT_NON_SECURITY; $SERVER = "127.0.0.1"; $fp = ""; @$fp = stream_socket_client($_transport . "://" . $SERVER . ":" . $_port, $errno, $errstr, 10); if ( $fp ) { fwrite($fp, "GET /" . CONNECT . ";" . session_id() . " HTTP/1.1\r\nHost: " . $SERVER . ":" . $_port . "\r\nAccept: */*\r\n\r\n"); return 'true'; } else return 'false'; } /* * Contacts */ function getContacts(array $pContacts) { $_group = array(); $return = NULL; $jid = array_unique($pContacts[3]); foreach ( $jid as $key => $val ) { $_SESSION['phpgw_info']['instant_messenger']['contacts'][] = $val; $item = NULL; $item .= ''; $_group[utf8_decode(strtolower(!empty($pContacts[4][$key]) ? $pContacts[4][$key] : 'default'))][] = $item; } ksort($_group); foreach ( $_group as $key => $val ) $return .= '' . implode('', $val) . ''; return '' . $return . ''; } function requireContacts() { $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['getContacts'] = NULL; } /* * Presence */ function setPresence($pPresence) { $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['setPresence'] = $pPresence; foreach($pPresence as $key=>$presence) if($key == 'show' || $key == 'type') { $_SESSION['phpgw_info']['instant_messenger']['MyPresence'] = $presence; } } function MyPresence() { if(isset($_SESSION['phpgw_info']['instant_messenger']['MyPresence'])) return $_SESSION['phpgw_info']['instant_messenger']['MyPresence']; } function getPresence(array $pPresence) { $offline = array(); foreach ( $pPresence[1] as $key => $val ) { if ( empty($val) ) continue; $_offline = true; for ( $i = $key; $i < sizeof($pPresence[1]); $i++ ) if ( $pPresence[1][$key] == $pPresence[3][$i] || $pPresence[1][$key] == $pPresence[5][$i] ) $_offline = false; if ( $_offline ) $offline[$key] = $val; } $online = array(); foreach ( $pPresence[3] as $key => $val ) { if ( empty($val) ) continue; $_online = true; for ( $i = ($key + 1); $i < sizeof($pPresence[3]); $i++ ) if ( in_array($val, $offline) || $pPresence[3][$key] == $pPresence[3][$i] ) $_online = false; if ( $_online ) $online[$key] = $val; } $return = NULL; foreach ( $offline as $key => $val ) { $val = ( strpos($val, '/') ) ? substr($val, 0, strpos($val, '/')) : $val; $return .= ""; } foreach ( $online as $key => $val ) { $val = ( strpos($val, '/') ) ? substr($val, 0, strpos($val, '/')) : $val; if ( empty($pPresence[4][$key]) ) $pPresence[4][$key] = ( empty($pPresence[5][$key]) ) ? 'available' : $pPresence[5][$key]; $return .= ""; else $return .= "'>" . $pPresence[6][$key] . ""; if ( strpos($pPresence[4][$key], 'subscribe') === false ) { $session .= "" . $pPresence[4][$key] . ""; if ( !empty($pPresence[6][$key]) ) $session .= "" . $pPresence[6][$key] . ""; $session .= ""; } } $_SESSION['phpgw_info']['instant_messenger']['presences'] = $session; return $return; } /* * GetMessage */ function getMessages(array $pMessages) { $_messages = array(); $return = NULL; foreach ( $pMessages[1] as $key => $val ) { trim($pMessages[3][$key]); if ( !empty($pMessages[3][$key]) ) $_messages[$val][] = $pMessages[3][$key]; if ( !empty($pMessages[2][$key]) ) { $return .= '<' . $pMessages[2][$key] . ' from="'; $return .= ( strpos($val, '/') ) ? substr($val, 0, strpos($val, '/')) : $val; $return .= '" />'; } } foreach ( $_messages as $key => $val ) { $return .= ''; foreach ( $val as $msg ) $return .= '' . $msg . ''; $return .= ''; } return $return; } /* * SendMessage */ function SendMessage($pSendMessage) { $pSendMessage['body'] = stripslashes($pSendMessage['body']); $_emotions = '/]+>/'; $_emotions = preg_match_all($_emotions, $pSendMessage['body'], $emotions); foreach ( $emotions[0] as $key => $val ) $emotions[0][$key] = '/' . addcslashes($val, './()[]{}^$*&?') . '/i'; $pSendMessage['body'] = preg_replace($emotions[0], $emotions[1], $pSendMessage['body']); $to = $pSendMessage['to']; $type = "normal"; $id = ""; $content = array( "subject" => $pSendMessage['subject'] ? $pSendMessage['subject'] : "", "thread" => $pSendMessage['thread'] ? $pSendMessage['thread'] : "", "body" => $pSendMessage['body'] ? $pSendMessage['body'] : "" ); $payload = ""; if ($to && is_array($content)) { if (!$id) { $id = $type . "_" . time(); } $xml = ""; if ($content['subject']) { $xml .= "" . $content['subject'] . ""; } if ($content['thread']) { $xml .= "" . $content['thread'] . ""; } $xml .= "" . $content['body'] . ""; $xml .= $payload; $xml .= ""; unset($id); $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['message'] .= $xml; } else { $this->writeLog("ERROR: SendMessage() #2"); return FALSE; } } /* * UpdateContact */ function updateContact($pUser) { $jid = $pUser['jid']; $name = $pUser['name']; $group = $pUser['group']; if ( $jid ) { $updatecontact = ""; $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['updateContact'] = $updatecontact; } $this->requireContacts(); } /* * AddContact */ function addContacts($pUser) { $jid = explode("@",$pUser['email']); $pJid = $jid[0]."@".$_SESSION['phpgw_info']['instant_messenger']['name_jabber']; $pName = $pUser['name']; $pGroup = $pUser['group']; if(trim($jid[0]) != trim($this->username)) { if ( $pJid ) { $newcontact = ""; $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['addContacts'] = $newcontact; $this->subscribe($pJid); $this->requireContacts(); } } } /* * RemoveContact */ function removeContact($pUser) { $pJid = $pUser['jid']; $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['removeContact'] = $pJid; $this->unsubscribed($pJid); $this->requireContacts(); } /* * Vcard */ function newVcard($pVcardUser) { $pVcard = explode("_vkrd_", $pVcardUser['vcard']); $xmlVcard = "".$pVcard[0].""; $xmlVcard .= "".$pVcard[1].""; $xmlVcard .= ""; $xmlVcard .= "".$pVcard[2].""; $xmlVcard .= "".$pVcard[3].""; $xmlVcard .= ""; $xmlVcard .= "".$pVcard[4].""; $xmlVcard .= "".$pVcard[5].""; $xmlVcard .= "".$pVcard[6].""; $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['newVcard'] = $xmlVcard; } function getVcard(array $Vcard) { $div = ""; $_expr = "/<[^].*[^<\/vCard>]>/"; array_shift($Vcard); $div = ''; $jid_user = $_SESSION['phpgw_info']['instant_messenger']['user']; $jid_user .= "@" . $_SESSION['phpgw_info']['instant_messenger']['name_jabber']; for($i = 0 ; $i < count($Vcard[0]) ; $i++) { preg_match($_expr,$Vcard[1][$i],$div_vcard); $div_new[] = $div_vcard; if ( $jid_user == $Vcard[0][$i] ) $_SESSION['phpgw_info']['instant_messenger']['vcard'] = $div_new[$i][0]; $div .= '
'.urldecode($div_new[$i][0]).'
'; } $div .= '
'; return $div; } function Vcard() { $contacts = array(); $contacts = $_SESSION['phpgw_info']['instant_messenger']['contacts']; $jid_user = $this->getJid(); if( count($contacts) > 0 ) { array_unshift($contacts,$jid_user); foreach($contacts as $user) $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['vcard'][] = $user; return "true"; }else{ return "false"; } } function VcardUser() { $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['vcard'][] = $this->getJid();; } function getJid() { $jid = $_SESSION['phpgw_info']['instant_messenger']['user'] . "@" . $_SESSION['phpgw_info']['instant_messenger']['name_jabber']; return $jid; } function subscribe($pJid) { $jid = (is_array($pJid)) ? $pJid['jid'] : $pJid ; $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['Subscribe'] = $jid; } function subscribed($pJid) { $jid = (is_array($pJid)) ? $pJid['jid'] : $pJid ; $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['SubscriptionAcceptRequest'] = $jid; } function unsubscribe($pJid) { $jid = (is_array($pJid)) ? $pJid['jid'] : $pJid ; $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['Unsubscribe'] = $jid; } function unsubscribed($pJid) { $jid = (is_array($pJid)) ? $pJid['jid'] : $pJid ; $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['SubscriptionDenyRequest'] = $jid; } } ?>