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 *
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      $_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   /*
118    * Contacts
119    */
120
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;
130
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 .= '" />';
136
137         $_group[utf8_decode(strtolower(!empty($pContacts[4][$key]) ? $pContacts[4][$key] : 'default'))][] = $item;
138      }
139     
140      ksort($_group);
141
142      foreach ( $_group as $key => $val )
143         $return .= '<group name="' . $key . '">' . implode('', $val) . '</group>';
144
145      return '<contacts>' . $return . '</contacts>';
146   }
147
148   function requireContacts()
149   {
150      $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['getContacts'] = NULL;
151   }
152   
153  /*
154   * Presence
155   */
156
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')
162        {
163                $_SESSION['phpgw_info']['instant_messenger']['MyPresence'] = $presence;
164        }
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
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 )
445      {
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";
453      }
454   }
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   }
485}
486?>
Note: See TracBrowser for help on using the repository browser.