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

Revision 63, 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     
113      ## Final do Listen;
114
115      return $return;
116   }
117   
118   /*
119    * Contacts
120    */
121
122   function getContacts(array $pContacts)
123   {
124      $_group = array();
125      $return = NULL;
126      $jid = array_unique($pContacts[3]);
127     
128      foreach ( $jid as $key => $val )
129      {
130         $_SESSION['phpgw_info']['instant_messenger']['contacts'][] = $val;
131
132         $item  = NULL;
133         $item .= '<item name="' . (( !empty($pContacts[1][$key]) ) ? $pContacts[1][$key] : $val);
134         $item .= '" jid="' . $val;
135         $item .= '" subscription="' . $pContacts[2][$key];
136         $item .= '" />';
137
138         $_group[utf8_decode(strtolower(!empty($pContacts[4][$key]) ? $pContacts[4][$key] : 'default'))][] = $item;
139      }
140     
141      ksort($_group);
142
143      foreach ( $_group as $key => $val )
144         $return .= '<group name="' . $key . '">' . implode('', $val) . '</group>';
145
146      return '<contacts>' . $return . '</contacts>';
147   }
148
149   function requireContacts()
150   {
151      $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['getContacts'] = NULL;
152   }
153   
154  /*
155   * Presence
156   */
157
158   function setPresence($pPresence)
159   {
160      $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['setPresence'] = $pPresence;
161      foreach($pPresence as $key=>$presence)
162        if($key == 'show' || $key == 'type')
163        {
164                $_SESSION['phpgw_info']['instant_messenger']['MyPresence'] = $presence;
165        }
166   }
167   
168   function MyPresence()
169   {
170        if(isset($_SESSION['phpgw_info']['instant_messenger']['MyPresence']))
171                return $_SESSION['phpgw_info']['instant_messenger']['MyPresence'];
172   }
173   
174   function getPresence(array $pPresence)
175   {
176      $offline = array();
177      foreach ( $pPresence[1] as $key => $val )
178      {
179         if ( empty($val) )
180            continue;
181
182         $_offline = true;
183         for ( $i = $key; $i < sizeof($pPresence[1]); $i++ )
184            if ( $pPresence[1][$key] == $pPresence[3][$i] || $pPresence[1][$key] == $pPresence[5][$i] )
185               $_offline = false;
186
187         if ( $_offline )
188            $offline[$key] = $val;
189      }
190
191      $online = array();
192      foreach ( $pPresence[3] as $key => $val )
193      {
194         if ( empty($val) )
195            continue;
196
197         $_online = true;
198         for ( $i = ($key + 1); $i < sizeof($pPresence[3]); $i++ )
199            if ( in_array($val, $offline) || $pPresence[3][$key] == $pPresence[3][$i] )
200               $_online = false;
201
202         if ( $_online )
203            $online[$key] = $val;
204      }
205
206      $return = NULL;
207
208      foreach ( $offline as $key => $val )
209      {
210         $val = ( strpos($val, '/') ) ? substr($val, 0, strpos($val, '/')) : $val;
211         $return .= "<presence from='" . $val . "' type='unavailable' />";
212      }
213     
214      foreach ( $online as $key => $val )
215      {
216         $val = ( strpos($val, '/') ) ? substr($val, 0, strpos($val, '/')) : $val;
217
218         if ( empty($pPresence[4][$key]) )
219            $pPresence[4][$key] = ( empty($pPresence[5][$key]) ) ?
220               'available' : $pPresence[5][$key];
221
222         $return .= "<presence from='" . $val . "' type='" . $pPresence[4][$key];
223         if ( empty($pPresence[6][$key]) )
224            $return .= "' />";
225         else
226            $return .= "'>" . $pPresence[6][$key] . "</presence>";
227
228         if ( strpos($pPresence[4][$key], 'subscribe') === false )
229         {
230            $session .= "<presence from='" . $val . "'><show>" . $pPresence[4][$key] . "</show>";
231            if ( !empty($pPresence[6][$key]) )
232               $session .= "<status>" . $pPresence[6][$key] . "</status>";
233            $session .= "</presence>";
234         }
235      }
236
237      $_SESSION['phpgw_info']['instant_messenger']['presences'] = $session;
238
239      return $return;
240   }
241
242   /*
243    * GetMessage
244    */
245   
246   function getMessages(array $pMessages)
247   {
248      $_messages = array();
249      $return = NULL;
250
251      foreach ( $pMessages[1] as $key => $val )
252      {
253         trim($pMessages[3][$key]);
254         if ( !empty($pMessages[3][$key]) )
255            $_messages[$val][] = $pMessages[3][$key];
256           
257         if ( !empty($pMessages[2][$key]) )
258         {
259            $return .= '<' . $pMessages[2][$key] . ' from="';
260            $return .= ( strpos($val, '/') ) ? substr($val, 0, strpos($val, '/')) : $val;
261            $return .= '" />';
262         }
263      }
264
265      foreach ( $_messages as $key => $val )
266      {
267         $return .= '<messages from="' . $key . '">';
268         foreach ( $val as $msg )
269            $return .= '<message>' . $msg . '</message>';
270         $return .= '</messages>';
271      }
272
273      return $return;
274   }
275
276   /*
277    * SendMessage
278    */
279
280   function SendMessage($pSendMessage)
281   {
282      $pSendMessage['body'] = stripslashes($pSendMessage['body']);
283      $_emotions  = '/<img emotion="(\S+)?"[^>]+>/';
284      $_emotions = preg_match_all($_emotions, $pSendMessage['body'], $emotions);
285
286      foreach ( $emotions[0] as $key => $val )
287         $emotions[0][$key] = '/' . addcslashes($val, './()[]{}^$*&?') . '/i';
288
289      $pSendMessage['body'] = preg_replace($emotions[0], $emotions[1], $pSendMessage['body']);
290
291      $to = $pSendMessage['to'];
292      $type = "normal";
293      $id = "";
294      $content = array(
295         "subject" => $pSendMessage['subject'] ? $pSendMessage['subject'] : "",
296         "thread" => $pSendMessage['thread'] ? $pSendMessage['thread'] : "",
297         "body" => $pSendMessage['body'] ? $pSendMessage['body'] : ""
298      );
299
300      $payload = "";
301
302      if ($to && is_array($content))
303      {
304         if (!$id)
305         {
306            $id = $type . "_" . time();
307         }
308
309         $xml = "<message to='$to' type='$type' id='$id'>";
310
311         if ($content['subject'])
312         {
313            $xml .= "<subject>" . $content['subject'] . "</subject>";
314         }
315
316         if ($content['thread'])
317         {
318            $xml .= "<thread>" . $content['thread'] . "</thread>";
319         }
320
321         $xml .= "<html><body>" . $content['body'] . "</body></html>";
322         $xml .= $payload;
323         $xml .= "</message>";
324         unset($id);
325
326         $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['message'] .= $xml;
327      }
328      else
329      {
330         $this->writeLog("ERROR: SendMessage() #2");
331         return FALSE;
332      }
333   }
334   
335   /*
336    * UpdateContact
337    */
338   
339   function updateContact($pUser)
340   {
341      $jid   = $pUser['jid'];
342      $name  = $pUser['name'];
343      $group = $pUser['group'];
344     
345      if ( $jid )
346      {
347         $updatecontact  = "<item jid='$jid'";
348         $updatecontact .= " name='" . $name . "'";
349         $updatecontact .= "><group>" . $group . "</group></item>";
350         $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['updateContact'] = $updatecontact;
351      }
352      $this->requireContacts();
353   }
354   
355   /*
356    * AddContact
357    */
358   
359   function addContacts($pUser)
360   {
361      $jid = explode("@",$pUser['email']);
362      $pJid = $jid[0]."@".$_SESSION['phpgw_info']['instant_messenger']['name_jabber'];
363      $pName = $pUser['name'];
364      $pGroup = $pUser['group'];
365
366      if(trim($jid[0]) != trim($this->username))
367      {
368         if ( $pJid )
369         {
370            $newcontact  = "<item jid='$pJid'";
371            $newcontact .= " name='" . $pName . "'";
372            $newcontact .= "><group>" . $pGroup . "</group></item>";
373            $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['addContacts'] = $newcontact;
374            $this->subscribe($pJid);
375            $this->requireContacts();           
376         }
377      }
378   }
379   
380   /*
381    * RemoveContact
382    */
383   
384    function removeContact($pUser)
385    {
386         $pJid = $pUser['jid'];
387         $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['removeContact'] = $pJid;
388         $this->unsubscribed($pJid);
389         $this->requireContacts();
390    }
391
392   /*
393    * Vcard
394    */
395
396   function newVcard($pVcardUser)
397   {
398      $pVcard = explode("_vkrd_", $pVcardUser['vcard']);
399
400      $xmlVcard  = "<FN>".$pVcard[0]."</FN>";
401      $xmlVcard .= "<NICKNAME>".$pVcard[1]."</NICKNAME>";
402      $xmlVcard .= "<ORG>";
403      $xmlVcard .= "<ORGNAME>".$pVcard[2]."</ORGNAME>";
404      $xmlVcard .= "<ORGUNIT>".$pVcard[3]."</ORGUNIT>";
405      $xmlVcard .= "</ORG>";
406      $xmlVcard .= "<ROLE>".$pVcard[4]."</ROLE>";
407      $xmlVcard .= "<BDAY>".$pVcard[5]."</BDAY>";
408      $xmlVcard .= "<DESC>".$pVcard[6]."</DESC>";
409     
410      $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['newVcard'] = $xmlVcard;
411
412   }
413   
414   function getVcard(array $Vcard)
415   {
416      $div = "";
417      $_expr = "/<[^<vCard xmlns=\'vcard\-temp\'>].*[^<\/vCard>]>/";
418      array_shift($Vcard);
419      $div = '<vcard>';
420      $jid_user = $_SESSION['phpgw_info']['instant_messenger']['user'];
421      $jid_user .= "@" . $_SESSION['phpgw_info']['instant_messenger']['name_jabber'];
422      for($i = 0 ; $i < count($Vcard[0]) ; $i++)
423      {
424         preg_match($_expr,$Vcard[1][$i],$div_vcard);
425         $div_new[] = $div_vcard;
426
427         if ( $jid_user == $Vcard[0][$i] )
428            $_SESSION['phpgw_info']['instant_messenger']['vcard'] = $div_new[$i][0];
429         
430         $div .= '<div id="vcard_'.$Vcard[0][$i].'">'.urldecode($div_new[$i][0]).'</div>';
431      }
432      $div .= '</vcard>';
433         
434      return $div;
435   }
436   
437   function requireVcard()
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.