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

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