source: trunk/instant_messenger/inc/class.Ujabber.inc.php @ 72

Revision 72, 15.6 KB checked in by niltonneto, 17 years ago (diff)

* empty log message *

  • Property svn:eol-style set to native
  • Property svn:executable set to *
RevLine 
[20]1<?php
2
[55]3class Ujabber
[20]4{
[55]5   /*
6    * Listen
7    */
8   
9   final function listen()
10   {
11      $return = null;
[64]12               
[67]13           $_SESSION['phpgw_info']['instant_messenger']['timeClient'] = time();
[55]14      $_SESSION['phpgw_info']['instant_messenger']['socket']['block'] = true;
15      $xml = $_SESSION['phpgw_info']['instant_messenger']['socket']['in'];
[64]16
[55]17      unset($_SESSION['phpgw_info']['instant_messenger']['socket']['in']);
18      $_SESSION['phpgw_info']['instant_messenger']['socket']['block'] = false;
[41]19
[55]20      ### Arranjo Tecnico ###
21      $vkrd = $xml;
22     
23      if(isset($_SESSION['phpgw_info']['instant_messenger']['contacts']))
24         unset($_SESSION['phpgw_info']['instant_messenger']['contacts']);
[41]25
[55]26      ### - messages - ###
27      $_messages = "/<message[^>]+from='(\S+)'[^>]*>(?(?=.*?<composing |.*?<paused ).*?<(composing|paused) )(?(?=.*?<html).*?<html[^>]*><body[^>]*>(.*?)<\/body><\/html>).*?<\/message>/s";
28      $_messages = preg_match_all($_messages, $xml, $messages);
29      if ( $_messages !== false && $_messages > 0 )
30      {
31         $xml = preg_replace("/<message.*?<\/message>/", "", $xml);         
32         $return .= $this->getMessages($messages);
33      }
34      ### - Fim messages - ###
[41]35
[55]36      ### - contacts - ###
37      $_contacts = "/<iq[^>]+id='contacts'[^>]*>.*?<\/iq>/";
38      $_contacts = preg_match_all($_contacts, $xml, $_xml);
[41]39
[55]40      $_contacts = "/<item.*?(?(?=[^>]+name)[^>]+name='(.*?)')[^>]+subscription='(.*?)'[^>]+jid='(.*?)'";
41      $_contacts .= "(?(?=[^>]*\/>)[^>]*\/>|[^>]*><group>(.*?)<\/group><\/item>)/";
42      $_contacts = preg_match_all($_contacts, $_xml[0][0], $contacts);
43      if ( $_contacts !== false && $_contacts > 0 )
44      {
45         $xml = preg_replace("/<iq[^>]+id='contacts'[^>]*>.*?<\/iq>/", "", $xml);
46         $return .= rawurldecode(urldecode($this->getContacts($contacts)));
47      }
48      ### - Fim contacts - ###
[41]49
[55]50      ### - presence - ###
51      $xml = $_SESSION['phpgw_info']['instant_messenger']['presences'] . $xml;
[41]52
[55]53      $_presence  = "/<presence(?(?=[^>]+\/>)[^>]+\/>|[^>]+>.*?<\/presence>)/";
54      $_presence = preg_match_all($_presence, $xml, $presence);
[41]55
[55]56      $patterns[0] = '/<priority>[^<]*<\/priority>|<(\S) (?(?=[^>]*\/>)[^>]*\/>|[^>]*>.*?<\/\1>)/';
57      $patterns[1] = '/ xmlns=\'.*?\'| to=\'.*?\'/';
58      $patterns[2] = '/> +</';
59      $replacements[0] = '';
60      $replacements[1] = '';
61      $replacements[2] = '><';
62      $_xml = preg_replace($patterns, $replacements, implode('', $presence[0]));
[20]63
[55]64      $_presence  = "/<presence[^>]+from='(\S+)'[^>]*type='(unavailable)'(?(?=[^>]*\/>)[^>]*\/>|[^>]*>.*?<\/presence>)|";
65      $_presence .= "<presence[^>]+from='(\S+)'(?(?=[^>]+type)[^>]+type='(\S+)')[^>]*";
66      $_presence .= "(?(?=\/>)\/>|>";
67      $_presence .= "(?(?=<s[^>]*?>)<s[^>]*?>(.*?)<\/s[^>]*?>(?(?=<s[^>]*?>)<s[^>]*?>(.*?)<\/s[^>]*?>))";
68      $_presence .= "<\/presence>)/";
69      $_presence = preg_match_all($_presence, $_xml, $presence);
70      if ( $_presence !== false && $_presence > 0 )
71      {
72         foreach ( $presence[5] as $key => $val )
73         {
74            if ( !empty($presence[1][$key]) && (strpos($presence[1][$key], '/') !== false) )
75               $presence[1][$key] = substr($presence[1][$key], 0, strpos($presence[1][$key], '/'));
76           
77            if ( !empty($presence[3][$key]) && strpos($presence[3][$key], '/') !== false )
78               $presence[3][$key] = substr($presence[3][$key], 0, strpos($presence[3][$key], '/'));
79   
80            if ( !empty($val) && !preg_match("/^(away|chat|dnd|xa)$/", $val) )
81               if ( preg_match("/^(away|chat|dnd|xa)$/", $presence[6][$key]) )
82               {
83                  $aux = $presence[5][$key];
84                  $presence[5][$key] = $presence[6][$key];
85                  $presence[6][$key] = $aux;
86               }
87               else
88               {
89                  if ( empty($presence[6][$key]) )         
90                     $presence[6][$key] = $presence[5][$key];
91                  $presence[5][$key] = '';
92               }
93         }
94         $xml = preg_replace("/<presence[^>]\/?>|<presence[^>]>.*?<\/presence>/", "", $xml);
[20]95
[55]96         $return .= $this->getPresence($presence);
97      }
98      ### - Fim presence - ###
[41]99
[55]100     
101      ### - vcard - ###
102      ### - Arranjo Tecnico - ###
103      $_xml = $vkrd;
104      $_vcard = "/<iq.*? id='(.*?)' .*?(<vCard [^>]+\/>|<vCard.*?<\/vCard>)<\/iq>/";
105      $_vcard = preg_match_all($_vcard, $_xml, $vcard);
106      if( $_vcard !== false && $_vcard > 0)
107      {
108         $xml = preg_replace("/<iq.*?<vCard.*?<\/iq>/", "", $xml);
109         $return .= urldecode($this->getVcard($vcard));
110      }
111      ### - Fim vcard - ###
112      ## Final do Listen;
[41]113
[55]114      return $return;
115   }
116   
[72]117   function reconnect()
118   {
119      require_once("IMConfigure.php");
120
121      $_transport = TRANSPORT_NON_SECURITY;
122      $_port = PORT_NON_SECURITY;
123
124                $SERVER = "127.0.0.1";
125
126                $fp  = "";
127                @$fp = stream_socket_client($_transport . "://" . $SERVER . ":" . $_port, $errno, $errstr, 10);
128
129                if ( $fp )
130                {
131                        fwrite($fp, "GET /" . CONNECT . ";" . session_id() . " HTTP/1.1\r\nHost: " . $SERVER . ":" . $_port . "\r\nAccept: */*\r\n\r\n");
132                        return 'true';
133                }
134                else
135                        return 'false';
136   }
137   
[55]138   /*
139    * Contacts
140    */
[41]141
[55]142   function getContacts(array $pContacts)
143   {
144      $_group = array();
145      $return = NULL;
146      $jid = array_unique($pContacts[3]);
147     
148      foreach ( $jid as $key => $val )
149      {
150         $_SESSION['phpgw_info']['instant_messenger']['contacts'][] = $val;
[41]151
[55]152         $item  = NULL;
153         $item .= '<item name="' . (( !empty($pContacts[1][$key]) ) ? $pContacts[1][$key] : $val);
154         $item .= '" jid="' . $val;
155         $item .= '" subscription="' . $pContacts[2][$key];
156         $item .= '" />';
[20]157
[55]158         $_group[utf8_decode(strtolower(!empty($pContacts[4][$key]) ? $pContacts[4][$key] : 'default'))][] = $item;
159      }
160     
161      ksort($_group);
[41]162
[55]163      foreach ( $_group as $key => $val )
164         $return .= '<group name="' . $key . '">' . implode('', $val) . '</group>';
[41]165
[55]166      return '<contacts>' . $return . '</contacts>';
167   }
[41]168
[55]169   function requireContacts()
170   {
171      $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['getContacts'] = NULL;
172   }
173   
174  /*
175   * Presence
176   */
[41]177
[55]178   function setPresence($pPresence)
179   {
180      $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['setPresence'] = $pPresence;
181      foreach($pPresence as $key=>$presence)
182        if($key == 'show' || $key == 'type')
[57]183        {
[55]184                $_SESSION['phpgw_info']['instant_messenger']['MyPresence'] = $presence;
[57]185        }
[55]186   }
187   
188   function MyPresence()
189   {
190        if(isset($_SESSION['phpgw_info']['instant_messenger']['MyPresence']))
191                return $_SESSION['phpgw_info']['instant_messenger']['MyPresence'];
192   }
193   
194   function getPresence(array $pPresence)
195   {
196      $offline = array();
197      foreach ( $pPresence[1] as $key => $val )
198      {
199         if ( empty($val) )
200            continue;
[41]201
[55]202         $_offline = true;
203         for ( $i = $key; $i < sizeof($pPresence[1]); $i++ )
204            if ( $pPresence[1][$key] == $pPresence[3][$i] || $pPresence[1][$key] == $pPresence[5][$i] )
205               $_offline = false;
[20]206
[55]207         if ( $_offline )
208            $offline[$key] = $val;
209      }
[41]210
[55]211      $online = array();
212      foreach ( $pPresence[3] as $key => $val )
213      {
214         if ( empty($val) )
215            continue;
[20]216
[55]217         $_online = true;
218         for ( $i = ($key + 1); $i < sizeof($pPresence[3]); $i++ )
219            if ( in_array($val, $offline) || $pPresence[3][$key] == $pPresence[3][$i] )
220               $_online = false;
[41]221
[55]222         if ( $_online )
223            $online[$key] = $val;
224      }
[41]225
[55]226      $return = NULL;
[41]227
[55]228      foreach ( $offline as $key => $val )
229      {
230         $val = ( strpos($val, '/') ) ? substr($val, 0, strpos($val, '/')) : $val;
231         $return .= "<presence from='" . $val . "' type='unavailable' />";
232      }
233     
234      foreach ( $online as $key => $val )
235      {
236         $val = ( strpos($val, '/') ) ? substr($val, 0, strpos($val, '/')) : $val;
[41]237
[55]238         if ( empty($pPresence[4][$key]) )
239            $pPresence[4][$key] = ( empty($pPresence[5][$key]) ) ?
240               'available' : $pPresence[5][$key];
[41]241
[55]242         $return .= "<presence from='" . $val . "' type='" . $pPresence[4][$key];
243         if ( empty($pPresence[6][$key]) )
244            $return .= "' />";
245         else
246            $return .= "'>" . $pPresence[6][$key] . "</presence>";
[41]247
[55]248         if ( strpos($pPresence[4][$key], 'subscribe') === false )
249         {
250            $session .= "<presence from='" . $val . "'><show>" . $pPresence[4][$key] . "</show>";
251            if ( !empty($pPresence[6][$key]) )
252               $session .= "<status>" . $pPresence[6][$key] . "</status>";
253            $session .= "</presence>";
254         }
255      }
[41]256
[55]257      $_SESSION['phpgw_info']['instant_messenger']['presences'] = $session;
[41]258
[55]259      return $return;
260   }
[41]261
[55]262   /*
263    * GetMessage
264    */
265   
266   function getMessages(array $pMessages)
267   {
268      $_messages = array();
269      $return = NULL;
[41]270
[55]271      foreach ( $pMessages[1] as $key => $val )
272      {
273         trim($pMessages[3][$key]);
274         if ( !empty($pMessages[3][$key]) )
275            $_messages[$val][] = $pMessages[3][$key];
276           
277         if ( !empty($pMessages[2][$key]) )
278         {
279            $return .= '<' . $pMessages[2][$key] . ' from="';
280            $return .= ( strpos($val, '/') ) ? substr($val, 0, strpos($val, '/')) : $val;
281            $return .= '" />';
282         }
283      }
[41]284
[55]285      foreach ( $_messages as $key => $val )
286      {
287         $return .= '<messages from="' . $key . '">';
288         foreach ( $val as $msg )
289            $return .= '<message>' . $msg . '</message>';
290         $return .= '</messages>';
291      }
[20]292
[55]293      return $return;
294   }
[20]295
[55]296   /*
297    * SendMessage
298    */
[41]299
[55]300   function SendMessage($pSendMessage)
301   {
302      $pSendMessage['body'] = stripslashes($pSendMessage['body']);
303      $_emotions  = '/<img emotion="(\S+)?"[^>]+>/';
304      $_emotions = preg_match_all($_emotions, $pSendMessage['body'], $emotions);
[20]305
[55]306      foreach ( $emotions[0] as $key => $val )
307         $emotions[0][$key] = '/' . addcslashes($val, './()[]{}^$*&?') . '/i';
[41]308
[55]309      $pSendMessage['body'] = preg_replace($emotions[0], $emotions[1], $pSendMessage['body']);
[41]310
[55]311      $to = $pSendMessage['to'];
312      $type = "normal";
313      $id = "";
314      $content = array(
315         "subject" => $pSendMessage['subject'] ? $pSendMessage['subject'] : "",
316         "thread" => $pSendMessage['thread'] ? $pSendMessage['thread'] : "",
317         "body" => $pSendMessage['body'] ? $pSendMessage['body'] : ""
318      );
[20]319
[55]320      $payload = "";
[41]321
[55]322      if ($to && is_array($content))
323      {
324         if (!$id)
325         {
326            $id = $type . "_" . time();
327         }
[32]328
[55]329         $xml = "<message to='$to' type='$type' id='$id'>";
[41]330
[55]331         if ($content['subject'])
332         {
333            $xml .= "<subject>" . $content['subject'] . "</subject>";
334         }
[32]335
[55]336         if ($content['thread'])
337         {
338            $xml .= "<thread>" . $content['thread'] . "</thread>";
339         }
[41]340
[55]341         $xml .= "<html><body>" . $content['body'] . "</body></html>";
342         $xml .= $payload;
343         $xml .= "</message>";
344         unset($id);
[20]345
[55]346         $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['message'] .= $xml;
347      }
348      else
[27]349      {
[55]350         $this->writeLog("ERROR: SendMessage() #2");
351         return FALSE;
352      }
353   }
354   
355   /*
356    * UpdateContact
357    */
358   
359   function updateContact($pUser)
360   {
361      $jid   = $pUser['jid'];
362      $name  = $pUser['name'];
363      $group = $pUser['group'];
364     
365      if ( $jid )
366      {
367         $updatecontact  = "<item jid='$jid'";
368         $updatecontact .= " name='" . $name . "'";
369         $updatecontact .= "><group>" . $group . "</group></item>";
370         $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['updateContact'] = $updatecontact;
371      }
372      $this->requireContacts();
373   }
374   
375   /*
376    * AddContact
377    */
378   
379   function addContacts($pUser)
380   {
381      $jid = explode("@",$pUser['email']);
382      $pJid = $jid[0]."@".$_SESSION['phpgw_info']['instant_messenger']['name_jabber'];
383      $pName = $pUser['name'];
384      $pGroup = $pUser['group'];
[27]385
[55]386      if(trim($jid[0]) != trim($this->username))
387      {
388         if ( $pJid )
389         {
390            $newcontact  = "<item jid='$pJid'";
391            $newcontact .= " name='" . $pName . "'";
392            $newcontact .= "><group>" . $pGroup . "</group></item>";
393            $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['addContacts'] = $newcontact;
394            $this->subscribe($pJid);
395            $this->requireContacts();           
396         }
397      }
398   }
399   
400   /*
401    * RemoveContact
402    */
403   
404    function removeContact($pUser)
[20]405    {
[55]406         $pJid = $pUser['jid'];
407         $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['removeContact'] = $pJid;
408         $this->unsubscribed($pJid);
409         $this->requireContacts();
[20]410    }
[41]411
[55]412   /*
413    * Vcard
414    */
[41]415
[55]416   function newVcard($pVcardUser)
417   {
418      $pVcard = explode("_vkrd_", $pVcardUser['vcard']);
[20]419
[55]420      $xmlVcard  = "<FN>".$pVcard[0]."</FN>";
421      $xmlVcard .= "<NICKNAME>".$pVcard[1]."</NICKNAME>";
422      $xmlVcard .= "<ORG>";
423      $xmlVcard .= "<ORGNAME>".$pVcard[2]."</ORGNAME>";
424      $xmlVcard .= "<ORGUNIT>".$pVcard[3]."</ORGUNIT>";
425      $xmlVcard .= "</ORG>";
426      $xmlVcard .= "<ROLE>".$pVcard[4]."</ROLE>";
427      $xmlVcard .= "<BDAY>".$pVcard[5]."</BDAY>";
428      $xmlVcard .= "<DESC>".$pVcard[6]."</DESC>";
429     
430      $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['newVcard'] = $xmlVcard;
[20]431
[55]432   }
433   
434   function getVcard(array $Vcard)
435   {
436      $div = "";
437      $_expr = "/<[^<vCard xmlns=\'vcard\-temp\'>].*[^<\/vCard>]>/";
438      array_shift($Vcard);
439      $div = '<vcard>';
440      $jid_user = $_SESSION['phpgw_info']['instant_messenger']['user'];
441      $jid_user .= "@" . $_SESSION['phpgw_info']['instant_messenger']['name_jabber'];
442      for($i = 0 ; $i < count($Vcard[0]) ; $i++)
443      {
444         preg_match($_expr,$Vcard[1][$i],$div_vcard);
445         $div_new[] = $div_vcard;
[20]446
[55]447         if ( $jid_user == $Vcard[0][$i] )
448            $_SESSION['phpgw_info']['instant_messenger']['vcard'] = $div_new[$i][0];
449         
450         $div .= '<div id="vcard_'.$Vcard[0][$i].'">'.urldecode($div_new[$i][0]).'</div>';
451      }
452      $div .= '</vcard>';
453         
454      return $div;
455   }
456   
[68]457   function Vcard()
[41]458   {
[55]459      $contacts = array();
460      $contacts = $_SESSION['phpgw_info']['instant_messenger']['contacts'];
[68]461      $jid_user = $this->getJid();
462
[55]463      if( count($contacts) > 0 )
[41]464      {
[55]465         array_unshift($contacts,$jid_user);
466
467         foreach($contacts as $user)
468            $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['vcard'][] = $user;
469         return "true";
470      }else{
471         return "false";
[41]472      }
473   }
[55]474   
[68]475   function VcardUser()
476   {
477        $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['vcard'][] = $this->getJid();;
478   }
479   
[55]480   function getJid()
481   {
482      $jid = $_SESSION['phpgw_info']['instant_messenger']['user'] . "@" . $_SESSION['phpgw_info']['instant_messenger']['name_jabber'];
483      return $jid;   
484   }
485   
486   function subscribe($pJid)
487   {
488      $jid = (is_array($pJid)) ? $pJid['jid'] : $pJid ;
489      $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['Subscribe'] = $jid;
490   }
491   
492   function subscribed($pJid)
493   {
494      $jid = (is_array($pJid)) ? $pJid['jid'] : $pJid ;
495      $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['SubscriptionAcceptRequest'] = $jid;
496   }
497   
498   function unsubscribe($pJid)
499   {
500      $jid = (is_array($pJid)) ? $pJid['jid'] : $pJid ;
501      $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['Unsubscribe'] = $jid;
502   }
503   
504   function unsubscribed($pJid)
505   {
506      $jid = (is_array($pJid)) ? $pJid['jid'] : $pJid ;
507      $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['SubscriptionDenyRequest'] = $jid;   
508   }
[20]509}
510?>
Note: See TracBrowser for help on using the repository browser.