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

Revision 57, 15.2 KB checked in by niltonneto, 17 years ago (diff)

* empty log message *

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