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

Revision 64, 15.1 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               
[63]13                $_SESSION['phpgw_info']['instant_messenger']['time'] = 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   
117   /*
118    * Contacts
119    */
[41]120
[55]121   function getContacts(array $pContacts)
122   {
123      $_group = array();
124      $return = NULL;
125      $jid = array_unique($pContacts[3]);
126     
127      foreach ( $jid as $key => $val )
128      {
129         $_SESSION['phpgw_info']['instant_messenger']['contacts'][] = $val;
[41]130
[55]131         $item  = NULL;
132         $item .= '<item name="' . (( !empty($pContacts[1][$key]) ) ? $pContacts[1][$key] : $val);
133         $item .= '" jid="' . $val;
134         $item .= '" subscription="' . $pContacts[2][$key];
135         $item .= '" />';
[20]136
[55]137         $_group[utf8_decode(strtolower(!empty($pContacts[4][$key]) ? $pContacts[4][$key] : 'default'))][] = $item;
138      }
139     
140      ksort($_group);
[41]141
[55]142      foreach ( $_group as $key => $val )
143         $return .= '<group name="' . $key . '">' . implode('', $val) . '</group>';
[41]144
[55]145      return '<contacts>' . $return . '</contacts>';
146   }
[41]147
[55]148   function requireContacts()
149   {
150      $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['getContacts'] = NULL;
151   }
152   
153  /*
154   * Presence
155   */
[41]156
[55]157   function setPresence($pPresence)
158   {
159      $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['setPresence'] = $pPresence;
160      foreach($pPresence as $key=>$presence)
161        if($key == 'show' || $key == 'type')
[57]162        {
[55]163                $_SESSION['phpgw_info']['instant_messenger']['MyPresence'] = $presence;
[57]164        }
[55]165   }
166   
167   function MyPresence()
168   {
169        if(isset($_SESSION['phpgw_info']['instant_messenger']['MyPresence']))
170                return $_SESSION['phpgw_info']['instant_messenger']['MyPresence'];
171   }
172   
173   function getPresence(array $pPresence)
174   {
175      $offline = array();
176      foreach ( $pPresence[1] as $key => $val )
177      {
178         if ( empty($val) )
179            continue;
[41]180
[55]181         $_offline = true;
182         for ( $i = $key; $i < sizeof($pPresence[1]); $i++ )
183            if ( $pPresence[1][$key] == $pPresence[3][$i] || $pPresence[1][$key] == $pPresence[5][$i] )
184               $_offline = false;
[20]185
[55]186         if ( $_offline )
187            $offline[$key] = $val;
188      }
[41]189
[55]190      $online = array();
191      foreach ( $pPresence[3] as $key => $val )
192      {
193         if ( empty($val) )
194            continue;
[20]195
[55]196         $_online = true;
197         for ( $i = ($key + 1); $i < sizeof($pPresence[3]); $i++ )
198            if ( in_array($val, $offline) || $pPresence[3][$key] == $pPresence[3][$i] )
199               $_online = false;
[41]200
[55]201         if ( $_online )
202            $online[$key] = $val;
203      }
[41]204
[55]205      $return = NULL;
[41]206
[55]207      foreach ( $offline as $key => $val )
208      {
209         $val = ( strpos($val, '/') ) ? substr($val, 0, strpos($val, '/')) : $val;
210         $return .= "<presence from='" . $val . "' type='unavailable' />";
211      }
212     
213      foreach ( $online as $key => $val )
214      {
215         $val = ( strpos($val, '/') ) ? substr($val, 0, strpos($val, '/')) : $val;
[41]216
[55]217         if ( empty($pPresence[4][$key]) )
218            $pPresence[4][$key] = ( empty($pPresence[5][$key]) ) ?
219               'available' : $pPresence[5][$key];
[41]220
[55]221         $return .= "<presence from='" . $val . "' type='" . $pPresence[4][$key];
222         if ( empty($pPresence[6][$key]) )
223            $return .= "' />";
224         else
225            $return .= "'>" . $pPresence[6][$key] . "</presence>";
[41]226
[55]227         if ( strpos($pPresence[4][$key], 'subscribe') === false )
228         {
229            $session .= "<presence from='" . $val . "'><show>" . $pPresence[4][$key] . "</show>";
230            if ( !empty($pPresence[6][$key]) )
231               $session .= "<status>" . $pPresence[6][$key] . "</status>";
232            $session .= "</presence>";
233         }
234      }
[41]235
[55]236      $_SESSION['phpgw_info']['instant_messenger']['presences'] = $session;
[41]237
[55]238      return $return;
239   }
[41]240
[55]241   /*
242    * GetMessage
243    */
244   
245   function getMessages(array $pMessages)
246   {
247      $_messages = array();
248      $return = NULL;
[41]249
[55]250      foreach ( $pMessages[1] as $key => $val )
251      {
252         trim($pMessages[3][$key]);
253         if ( !empty($pMessages[3][$key]) )
254            $_messages[$val][] = $pMessages[3][$key];
255           
256         if ( !empty($pMessages[2][$key]) )
257         {
258            $return .= '<' . $pMessages[2][$key] . ' from="';
259            $return .= ( strpos($val, '/') ) ? substr($val, 0, strpos($val, '/')) : $val;
260            $return .= '" />';
261         }
262      }
[41]263
[55]264      foreach ( $_messages as $key => $val )
265      {
266         $return .= '<messages from="' . $key . '">';
267         foreach ( $val as $msg )
268            $return .= '<message>' . $msg . '</message>';
269         $return .= '</messages>';
270      }
[20]271
[55]272      return $return;
273   }
[20]274
[55]275   /*
276    * SendMessage
277    */
[41]278
[55]279   function SendMessage($pSendMessage)
280   {
281      $pSendMessage['body'] = stripslashes($pSendMessage['body']);
282      $_emotions  = '/<img emotion="(\S+)?"[^>]+>/';
283      $_emotions = preg_match_all($_emotions, $pSendMessage['body'], $emotions);
[20]284
[55]285      foreach ( $emotions[0] as $key => $val )
286         $emotions[0][$key] = '/' . addcslashes($val, './()[]{}^$*&?') . '/i';
[41]287
[55]288      $pSendMessage['body'] = preg_replace($emotions[0], $emotions[1], $pSendMessage['body']);
[41]289
[55]290      $to = $pSendMessage['to'];
291      $type = "normal";
292      $id = "";
293      $content = array(
294         "subject" => $pSendMessage['subject'] ? $pSendMessage['subject'] : "",
295         "thread" => $pSendMessage['thread'] ? $pSendMessage['thread'] : "",
296         "body" => $pSendMessage['body'] ? $pSendMessage['body'] : ""
297      );
[20]298
[55]299      $payload = "";
[41]300
[55]301      if ($to && is_array($content))
302      {
303         if (!$id)
304         {
305            $id = $type . "_" . time();
306         }
[32]307
[55]308         $xml = "<message to='$to' type='$type' id='$id'>";
[41]309
[55]310         if ($content['subject'])
311         {
312            $xml .= "<subject>" . $content['subject'] . "</subject>";
313         }
[32]314
[55]315         if ($content['thread'])
316         {
317            $xml .= "<thread>" . $content['thread'] . "</thread>";
318         }
[41]319
[55]320         $xml .= "<html><body>" . $content['body'] . "</body></html>";
321         $xml .= $payload;
322         $xml .= "</message>";
323         unset($id);
[20]324
[55]325         $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['message'] .= $xml;
326      }
327      else
[27]328      {
[55]329         $this->writeLog("ERROR: SendMessage() #2");
330         return FALSE;
331      }
332   }
333   
334   /*
335    * UpdateContact
336    */
337   
338   function updateContact($pUser)
339   {
340      $jid   = $pUser['jid'];
341      $name  = $pUser['name'];
342      $group = $pUser['group'];
343     
344      if ( $jid )
345      {
346         $updatecontact  = "<item jid='$jid'";
347         $updatecontact .= " name='" . $name . "'";
348         $updatecontact .= "><group>" . $group . "</group></item>";
349         $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['updateContact'] = $updatecontact;
350      }
351      $this->requireContacts();
352   }
353   
354   /*
355    * AddContact
356    */
357   
358   function addContacts($pUser)
359   {
360      $jid = explode("@",$pUser['email']);
361      $pJid = $jid[0]."@".$_SESSION['phpgw_info']['instant_messenger']['name_jabber'];
362      $pName = $pUser['name'];
363      $pGroup = $pUser['group'];
[27]364
[55]365      if(trim($jid[0]) != trim($this->username))
366      {
367         if ( $pJid )
368         {
369            $newcontact  = "<item jid='$pJid'";
370            $newcontact .= " name='" . $pName . "'";
371            $newcontact .= "><group>" . $pGroup . "</group></item>";
372            $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['addContacts'] = $newcontact;
373            $this->subscribe($pJid);
374            $this->requireContacts();           
375         }
376      }
377   }
378   
379   /*
380    * RemoveContact
381    */
382   
383    function removeContact($pUser)
[20]384    {
[55]385         $pJid = $pUser['jid'];
386         $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['removeContact'] = $pJid;
387         $this->unsubscribed($pJid);
388         $this->requireContacts();
[20]389    }
[41]390
[55]391   /*
392    * Vcard
393    */
[41]394
[55]395   function newVcard($pVcardUser)
396   {
397      $pVcard = explode("_vkrd_", $pVcardUser['vcard']);
[20]398
[55]399      $xmlVcard  = "<FN>".$pVcard[0]."</FN>";
400      $xmlVcard .= "<NICKNAME>".$pVcard[1]."</NICKNAME>";
401      $xmlVcard .= "<ORG>";
402      $xmlVcard .= "<ORGNAME>".$pVcard[2]."</ORGNAME>";
403      $xmlVcard .= "<ORGUNIT>".$pVcard[3]."</ORGUNIT>";
404      $xmlVcard .= "</ORG>";
405      $xmlVcard .= "<ROLE>".$pVcard[4]."</ROLE>";
406      $xmlVcard .= "<BDAY>".$pVcard[5]."</BDAY>";
407      $xmlVcard .= "<DESC>".$pVcard[6]."</DESC>";
408     
409      $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['newVcard'] = $xmlVcard;
[20]410
[55]411   }
412   
413   function getVcard(array $Vcard)
414   {
415      $div = "";
416      $_expr = "/<[^<vCard xmlns=\'vcard\-temp\'>].*[^<\/vCard>]>/";
417      array_shift($Vcard);
418      $div = '<vcard>';
419      $jid_user = $_SESSION['phpgw_info']['instant_messenger']['user'];
420      $jid_user .= "@" . $_SESSION['phpgw_info']['instant_messenger']['name_jabber'];
421      for($i = 0 ; $i < count($Vcard[0]) ; $i++)
422      {
423         preg_match($_expr,$Vcard[1][$i],$div_vcard);
424         $div_new[] = $div_vcard;
[20]425
[55]426         if ( $jid_user == $Vcard[0][$i] )
427            $_SESSION['phpgw_info']['instant_messenger']['vcard'] = $div_new[$i][0];
428         
429         $div .= '<div id="vcard_'.$Vcard[0][$i].'">'.urldecode($div_new[$i][0]).'</div>';
430      }
431      $div .= '</vcard>';
432         
433      return $div;
434   }
435   
436   function requireVcard()
[41]437   {
[64]438
[55]439      $contacts = array();
440      $contacts = $_SESSION['phpgw_info']['instant_messenger']['contacts'];
441      $jid_user = $_SESSION['phpgw_info']['instant_messenger']['user'];
442      $jid_user .= "@" . $_SESSION['phpgw_info']['instant_messenger']['name_jabber'];
443     
444      if( count($contacts) > 0 )
[41]445      {
[55]446         array_unshift($contacts,$jid_user);
447
448         foreach($contacts as $user)
449            $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['vcard'][] = $user;
450         return "true";
451      }else{
452         return "false";
[41]453      }
454   }
[55]455   
456   function getJid()
457   {
458      $jid = $_SESSION['phpgw_info']['instant_messenger']['user'] . "@" . $_SESSION['phpgw_info']['instant_messenger']['name_jabber'];
459      return $jid;   
460   }
461   
462   function subscribe($pJid)
463   {
464      $jid = (is_array($pJid)) ? $pJid['jid'] : $pJid ;
465      $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['Subscribe'] = $jid;
466   }
467   
468   function subscribed($pJid)
469   {
470      $jid = (is_array($pJid)) ? $pJid['jid'] : $pJid ;
471      $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['SubscriptionAcceptRequest'] = $jid;
472   }
473   
474   function unsubscribe($pJid)
475   {
476      $jid = (is_array($pJid)) ? $pJid['jid'] : $pJid ;
477      $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['Unsubscribe'] = $jid;
478   }
479   
480   function unsubscribed($pJid)
481   {
482      $jid = (is_array($pJid)) ? $pJid['jid'] : $pJid ;
483      $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['SubscriptionDenyRequest'] = $jid;   
484   }
[20]485}
486?>
Note: See TracBrowser for help on using the repository browser.