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

Revision 55, 15.1 KB checked in by niltonneto, 17 years ago (diff)

Nova versão do Instant Messenger.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2
3class Ujabber
4{
5   /*
6    * Listen
7    */
8   
9   final function listen()
10   {
11      $return = null;
12
13      $_SESSION['phpgw_info']['instant_messenger']['time'] = time();
14
15      $_SESSION['phpgw_info']['instant_messenger']['socket']['block'] = true;
16      $xml = $_SESSION['phpgw_info']['instant_messenger']['socket']['in'];
17     
18      unset($_SESSION['phpgw_info']['instant_messenger']['socket']['in']);
19      $_SESSION['phpgw_info']['instant_messenger']['socket']['block'] = false;
20
21      ### Arranjo Tecnico ###
22      $vkrd = $xml;
23     
24      if(isset($_SESSION['phpgw_info']['instant_messenger']['contacts']))
25         unset($_SESSION['phpgw_info']['instant_messenger']['contacts']);
26
27      ### - messages - ###
28      $_messages = "/<message[^>]+from='(\S+)'[^>]*>(?(?=.*?<composing |.*?<paused ).*?<(composing|paused) )(?(?=.*?<html).*?<html[^>]*><body[^>]*>(.*?)<\/body><\/html>).*?<\/message>/s";
29      $_messages = preg_match_all($_messages, $xml, $messages);
30      if ( $_messages !== false && $_messages > 0 )
31      {
32         $xml = preg_replace("/<message.*?<\/message>/", "", $xml);         
33         $return .= $this->getMessages($messages);
34      }
35      ### - Fim messages - ###
36
37      ### - contacts - ###
38      $_contacts = "/<iq[^>]+id='contacts'[^>]*>.*?<\/iq>/";
39      $_contacts = preg_match_all($_contacts, $xml, $_xml);
40
41      $_contacts = "/<item.*?(?(?=[^>]+name)[^>]+name='(.*?)')[^>]+subscription='(.*?)'[^>]+jid='(.*?)'";
42      $_contacts .= "(?(?=[^>]*\/>)[^>]*\/>|[^>]*><group>(.*?)<\/group><\/item>)/";
43      $_contacts = preg_match_all($_contacts, $_xml[0][0], $contacts);
44      if ( $_contacts !== false && $_contacts > 0 )
45      {
46         $xml = preg_replace("/<iq[^>]+id='contacts'[^>]*>.*?<\/iq>/", "", $xml);
47         $return .= rawurldecode(urldecode($this->getContacts($contacts)));
48      }
49      ### - Fim contacts - ###
50
51      ### - presence - ###
52      $xml = $_SESSION['phpgw_info']['instant_messenger']['presences'] . $xml;
53
54      $_presence  = "/<presence(?(?=[^>]+\/>)[^>]+\/>|[^>]+>.*?<\/presence>)/";
55      $_presence = preg_match_all($_presence, $xml, $presence);
56
57      $patterns[0] = '/<priority>[^<]*<\/priority>|<(\S) (?(?=[^>]*\/>)[^>]*\/>|[^>]*>.*?<\/\1>)/';
58      $patterns[1] = '/ xmlns=\'.*?\'| to=\'.*?\'/';
59      $patterns[2] = '/> +</';
60      $replacements[0] = '';
61      $replacements[1] = '';
62      $replacements[2] = '><';
63      $_xml = preg_replace($patterns, $replacements, implode('', $presence[0]));
64
65      $_presence  = "/<presence[^>]+from='(\S+)'[^>]*type='(unavailable)'(?(?=[^>]*\/>)[^>]*\/>|[^>]*>.*?<\/presence>)|";
66      $_presence .= "<presence[^>]+from='(\S+)'(?(?=[^>]+type)[^>]+type='(\S+)')[^>]*";
67      $_presence .= "(?(?=\/>)\/>|>";
68      $_presence .= "(?(?=<s[^>]*?>)<s[^>]*?>(.*?)<\/s[^>]*?>(?(?=<s[^>]*?>)<s[^>]*?>(.*?)<\/s[^>]*?>))";
69      $_presence .= "<\/presence>)/";
70      $_presence = preg_match_all($_presence, $_xml, $presence);
71      if ( $_presence !== false && $_presence > 0 )
72      {
73         foreach ( $presence[5] as $key => $val )
74         {
75            if ( !empty($presence[1][$key]) && (strpos($presence[1][$key], '/') !== false) )
76               $presence[1][$key] = substr($presence[1][$key], 0, strpos($presence[1][$key], '/'));
77           
78            if ( !empty($presence[3][$key]) && strpos($presence[3][$key], '/') !== false )
79               $presence[3][$key] = substr($presence[3][$key], 0, strpos($presence[3][$key], '/'));
80   
81            if ( !empty($val) && !preg_match("/^(away|chat|dnd|xa)$/", $val) )
82               if ( preg_match("/^(away|chat|dnd|xa)$/", $presence[6][$key]) )
83               {
84                  $aux = $presence[5][$key];
85                  $presence[5][$key] = $presence[6][$key];
86                  $presence[6][$key] = $aux;
87               }
88               else
89               {
90                  if ( empty($presence[6][$key]) )         
91                     $presence[6][$key] = $presence[5][$key];
92                  $presence[5][$key] = '';
93               }
94         }
95         $xml = preg_replace("/<presence[^>]\/?>|<presence[^>]>.*?<\/presence>/", "", $xml);
96
97         $return .= $this->getPresence($presence);
98      }
99      ### - Fim presence - ###
100
101     
102      ### - vcard - ###
103      ### - Arranjo Tecnico - ###
104      $_xml = $vkrd;
105      $_vcard = "/<iq.*? id='(.*?)' .*?(<vCard [^>]+\/>|<vCard.*?<\/vCard>)<\/iq>/";
106      $_vcard = preg_match_all($_vcard, $_xml, $vcard);
107      if( $_vcard !== false && $_vcard > 0)
108      {
109         $xml = preg_replace("/<iq.*?<vCard.*?<\/iq>/", "", $xml);
110         $return .= urldecode($this->getVcard($vcard));
111      }
112      ### - Fim vcard - ###
113     
114      ## Final do Listen;
115
116      return $return;
117   }
118   
119   /*
120    * Contacts
121    */
122
123   function getContacts(array $pContacts)
124   {
125      $_group = array();
126      $return = NULL;
127      $jid = array_unique($pContacts[3]);
128     
129      foreach ( $jid as $key => $val )
130      {
131         $_SESSION['phpgw_info']['instant_messenger']['contacts'][] = $val;
132
133         $item  = NULL;
134         $item .= '<item name="' . (( !empty($pContacts[1][$key]) ) ? $pContacts[1][$key] : $val);
135         $item .= '" jid="' . $val;
136         $item .= '" subscription="' . $pContacts[2][$key];
137         $item .= '" />';
138
139         $_group[utf8_decode(strtolower(!empty($pContacts[4][$key]) ? $pContacts[4][$key] : 'default'))][] = $item;
140      }
141     
142      ksort($_group);
143
144      foreach ( $_group as $key => $val )
145         $return .= '<group name="' . $key . '">' . implode('', $val) . '</group>';
146
147      return '<contacts>' . $return . '</contacts>';
148   }
149
150   function requireContacts()
151   {
152      $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['getContacts'] = NULL;
153   }
154   
155  /*
156   * Presence
157   */
158
159   function setPresence($pPresence)
160   {
161      $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['setPresence'] = $pPresence;
162      foreach($pPresence as $key=>$presence)
163        if($key == 'show' || $key == 'type')
164                $_SESSION['phpgw_info']['instant_messenger']['MyPresence'] = $presence;
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;
180
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;
185
186         if ( $_offline )
187            $offline[$key] = $val;
188      }
189
190      $online = array();
191      foreach ( $pPresence[3] as $key => $val )
192      {
193         if ( empty($val) )
194            continue;
195
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;
200
201         if ( $_online )
202            $online[$key] = $val;
203      }
204
205      $return = NULL;
206
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;
216
217         if ( empty($pPresence[4][$key]) )
218            $pPresence[4][$key] = ( empty($pPresence[5][$key]) ) ?
219               'available' : $pPresence[5][$key];
220
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>";
226
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      }
235
236      $_SESSION['phpgw_info']['instant_messenger']['presences'] = $session;
237
238      return $return;
239   }
240
241   /*
242    * GetMessage
243    */
244   
245   function getMessages(array $pMessages)
246   {
247      $_messages = array();
248      $return = NULL;
249
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      }
263
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      }
271
272      return $return;
273   }
274
275   /*
276    * SendMessage
277    */
278
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);
284
285      foreach ( $emotions[0] as $key => $val )
286         $emotions[0][$key] = '/' . addcslashes($val, './()[]{}^$*&?') . '/i';
287
288      $pSendMessage['body'] = preg_replace($emotions[0], $emotions[1], $pSendMessage['body']);
289
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      );
298
299      $payload = "";
300
301      if ($to && is_array($content))
302      {
303         if (!$id)
304         {
305            $id = $type . "_" . time();
306         }
307
308         $xml = "<message to='$to' type='$type' id='$id'>";
309
310         if ($content['subject'])
311         {
312            $xml .= "<subject>" . $content['subject'] . "</subject>";
313         }
314
315         if ($content['thread'])
316         {
317            $xml .= "<thread>" . $content['thread'] . "</thread>";
318         }
319
320         $xml .= "<html><body>" . $content['body'] . "</body></html>";
321         $xml .= $payload;
322         $xml .= "</message>";
323         unset($id);
324
325         $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['message'] .= $xml;
326      }
327      else
328      {
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'];
364
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)
384    {
385         $pJid = $pUser['jid'];
386         $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['removeContact'] = $pJid;
387         $this->unsubscribed($pJid);
388         $this->requireContacts();
389    }
390
391   /*
392    * Vcard
393    */
394
395   function newVcard($pVcardUser)
396   {
397      $pVcard = explode("_vkrd_", $pVcardUser['vcard']);
398
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;
410
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;
425
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()
437   {
438      $contacts = array();
439      $contacts = $_SESSION['phpgw_info']['instant_messenger']['contacts'];
440      $jid_user = $_SESSION['phpgw_info']['instant_messenger']['user'];
441      $jid_user .= "@" . $_SESSION['phpgw_info']['instant_messenger']['name_jabber'];
442     
443      if( count($contacts) > 0 )
444      {
445         array_unshift($contacts,$jid_user);
446
447         foreach($contacts as $user)
448            $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['vcard'][] = $user;
449         return "true";
450      }else{
451         return "false";
452      }
453   }
454   
455   function getJid()
456   {
457      $jid = $_SESSION['phpgw_info']['instant_messenger']['user'] . "@" . $_SESSION['phpgw_info']['instant_messenger']['name_jabber'];
458      return $jid;   
459   }
460   
461   function subscribe($pJid)
462   {
463      $jid = (is_array($pJid)) ? $pJid['jid'] : $pJid ;
464      $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['Subscribe'] = $jid;
465   }
466   
467   function subscribed($pJid)
468   {
469      $jid = (is_array($pJid)) ? $pJid['jid'] : $pJid ;
470      $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['SubscriptionAcceptRequest'] = $jid;
471   }
472   
473   function unsubscribe($pJid)
474   {
475      $jid = (is_array($pJid)) ? $pJid['jid'] : $pJid ;
476      $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['Unsubscribe'] = $jid;
477   }
478   
479   function unsubscribed($pJid)
480   {
481      $jid = (is_array($pJid)) ? $pJid['jid'] : $pJid ;
482      $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['SubscriptionDenyRequest'] = $jid;   
483   }
484}
485?>
Note: See TracBrowser for help on using the repository browser.