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 *
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']['timeClient'] = time();
14      $_SESSION['phpgw_info']['instant_messenger']['socket']['block'] = true;
15      $xml = $_SESSION['phpgw_info']['instant_messenger']['socket']['in'];
16
17      unset($_SESSION['phpgw_info']['instant_messenger']['socket']['in']);
18      $_SESSION['phpgw_info']['instant_messenger']['socket']['block'] = false;
19
20      ### Arranjo Tecnico ###
21      $vkrd = $xml;
22     
23      if(isset($_SESSION['phpgw_info']['instant_messenger']['contacts']))
24         unset($_SESSION['phpgw_info']['instant_messenger']['contacts']);
25
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 - ###
35
36      ### - contacts - ###
37      $_contacts = "/<iq[^>]+id='contacts'[^>]*>.*?<\/iq>/";
38      $_contacts = preg_match_all($_contacts, $xml, $_xml);
39
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 - ###
49
50      ### - presence - ###
51      $xml = $_SESSION['phpgw_info']['instant_messenger']['presences'] . $xml;
52
53      $_presence  = "/<presence(?(?=[^>]+\/>)[^>]+\/>|[^>]+>.*?<\/presence>)/";
54      $_presence = preg_match_all($_presence, $xml, $presence);
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]));
63
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);
95
96         $return .= $this->getPresence($presence);
97      }
98      ### - Fim presence - ###
99
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;
113
114      return $return;
115   }
116   
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   
138   /*
139    * Contacts
140    */
141
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;
151
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 .= '" />';
157
158         $_group[utf8_decode(strtolower(!empty($pContacts[4][$key]) ? $pContacts[4][$key] : 'default'))][] = $item;
159      }
160     
161      ksort($_group);
162
163      foreach ( $_group as $key => $val )
164         $return .= '<group name="' . $key . '">' . implode('', $val) . '</group>';
165
166      return '<contacts>' . $return . '</contacts>';
167   }
168
169   function requireContacts()
170   {
171      $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['getContacts'] = NULL;
172   }
173   
174  /*
175   * Presence
176   */
177
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')
183        {
184                $_SESSION['phpgw_info']['instant_messenger']['MyPresence'] = $presence;
185        }
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;
201
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;
206
207         if ( $_offline )
208            $offline[$key] = $val;
209      }
210
211      $online = array();
212      foreach ( $pPresence[3] as $key => $val )
213      {
214         if ( empty($val) )
215            continue;
216
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;
221
222         if ( $_online )
223            $online[$key] = $val;
224      }
225
226      $return = NULL;
227
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;
237
238         if ( empty($pPresence[4][$key]) )
239            $pPresence[4][$key] = ( empty($pPresence[5][$key]) ) ?
240               'available' : $pPresence[5][$key];
241
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>";
247
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      }
256
257      $_SESSION['phpgw_info']['instant_messenger']['presences'] = $session;
258
259      return $return;
260   }
261
262   /*
263    * GetMessage
264    */
265   
266   function getMessages(array $pMessages)
267   {
268      $_messages = array();
269      $return = NULL;
270
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      }
284
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      }
292
293      return $return;
294   }
295
296   /*
297    * SendMessage
298    */
299
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);
305
306      foreach ( $emotions[0] as $key => $val )
307         $emotions[0][$key] = '/' . addcslashes($val, './()[]{}^$*&?') . '/i';
308
309      $pSendMessage['body'] = preg_replace($emotions[0], $emotions[1], $pSendMessage['body']);
310
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      );
319
320      $payload = "";
321
322      if ($to && is_array($content))
323      {
324         if (!$id)
325         {
326            $id = $type . "_" . time();
327         }
328
329         $xml = "<message to='$to' type='$type' id='$id'>";
330
331         if ($content['subject'])
332         {
333            $xml .= "<subject>" . $content['subject'] . "</subject>";
334         }
335
336         if ($content['thread'])
337         {
338            $xml .= "<thread>" . $content['thread'] . "</thread>";
339         }
340
341         $xml .= "<html><body>" . $content['body'] . "</body></html>";
342         $xml .= $payload;
343         $xml .= "</message>";
344         unset($id);
345
346         $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['message'] .= $xml;
347      }
348      else
349      {
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'];
385
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)
405    {
406         $pJid = $pUser['jid'];
407         $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['removeContact'] = $pJid;
408         $this->unsubscribed($pJid);
409         $this->requireContacts();
410    }
411
412   /*
413    * Vcard
414    */
415
416   function newVcard($pVcardUser)
417   {
418      $pVcard = explode("_vkrd_", $pVcardUser['vcard']);
419
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;
431
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;
446
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   
457   function Vcard()
458   {
459      $contacts = array();
460      $contacts = $_SESSION['phpgw_info']['instant_messenger']['contacts'];
461      $jid_user = $this->getJid();
462
463      if( count($contacts) > 0 )
464      {
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";
472      }
473   }
474   
475   function VcardUser()
476   {
477        $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['vcard'][] = $this->getJid();;
478   }
479   
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   }
509}
510?>
Note: See TracBrowser for help on using the repository browser.