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

Revision 32, 13.0 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        /*************************************************************
3         *      Classe Jabber - Expresso                                                                                 *
4         *  -------------------------------------------------------  * 
5         *         Com Autenticação no Servidor Jabber                                      *
6         *      Rodrigo Souza - rodsouza@celepar.pr.gov.br                               *
7         *      Alexandre Correia - alexandrecorreia@celepar.pr.gov.br *
8         *************************************************************/
9
10require_once "class.Jabber.inc.php";
11require_once "class.db_im.inc.php";
12
13class Ujabber extends Jabber
14{
15
16        private $server = "localhost";
17        private $port   = "5222";
18       
19        private $username = false;
20        private $password = false;
21        private $resource = false;
22       
23        private $jid = false;
24        private $email = "";
25        private $contacts = "";
26       
27        private $log_error_file = 'erro.log';
28               
29        function __construct()
30        {
31               
32                $this->username = $_SESSION['phpgw_info']['instant_messenger']['user'];
33                $this->password = $_SESSION['phpgw_info']['instant_messenger']['passwd'];
34                $this->server   = $_SESSION['phpgw_info']['instant_messenger']['name_jabber'];
35                $this->port     = $_SESSION['phpgw_info']['instant_messenger']['port_jabber'];
36                $this->resource = $_SESSION['phpgw_info']['instant_messenger']['resource_jabber'];
37                $this->jid      = $this->username."@".$this->server . "/" . $this->resource;
38               
39                parent :: __construct($this->jid, $this->password, $this->port);
40               
41        }
42       
43        function __destruct()
44        {
45                parent :: __destruct();
46        }
47
48        /*
49         * List Users
50         */
51
52        function list_user()
53        {
54                $contacts = $this->UpdateContacts();
55                $group_users = array();
56                $group_nusers = array();
57               
58                for($i=0; $i < count($contacts); $i++){
59                        $group_users[] = strtolower(urldecode($contacts[$i]['group'])).";".urldecode($contacts[$i]['name']).";".$contacts[$i]['jid'].";".$contacts[$i]['subscription'].";".$contacts[$i]['online'];                     
60                }
61       
62                // VCARD - PESSOAL
63                $VCARD_P = $this->getVcard($this->username."@".$this->server);
64               
65                $group_nusers[0]['VCARD_FN'] = $VCARD_P[0]['FN'];
66                $group_nusers[0]['VCARD_NICKNAME'] = $VCARD_P[0]['NICKNAME'];
67                $group_nusers[0]['VCARD_ORGNAME'] = $VCARD_P[0]['ORGNAME'];
68                $group_nusers[0]['VCARD_ORGUNIT'] = $VCARD_P[0]['ORGUNIT'];
69                $group_nusers[0]['VCARD_ROLE'] = $VCARD_P[0]['ROLE'];
70                $group_nusers[0]['VCARD_BDAY'] = $VCARD_P[0]['BDAY'];
71                $group_nusers[0]['VCARD_DESC'] = $VCARD_P[0]['DESC'];
72                $group_nusers[0]['VCARD_JID'] = $VCARD_P[0]['JID'];                             
73       
74                unset($VCARD_P);
75                natcasesort($group_users);             
76                $i=1;
77
78                // VCARD / CONTATOS - USUARIOS
79               
80                foreach($group_users as $tmp){
81                        $aux = explode(";",$tmp);
82                        $group_nusers[$i]['group'] = $aux[0];
83                        $group_nusers[$i]['name'] = $aux[1];
84                        $group_nusers[$i]['jid'] = $aux[2];
85                        $group_nusers[$i]['subscription'] = trim($aux[3]);
86                        $group_nusers[$i]['online'] = $aux[4];
87                        $aux_jid = explode("@",$aux[2]);
88               
89                        $VCARD_U = $this->getVcard($aux_jid[0]."@".$this->server);
90               
91                        $group_nusers[$i]['VCARD_FN'] = $VCARD_U[0]['FN'];
92                        $group_nusers[$i]['VCARD_NICKNAME'] = $VCARD_U[0]['NICKNAME'];
93                        $group_nusers[$i]['VCARD_ORGNAME']  = $VCARD_U[0]['ORGNAME'];
94                        $group_nusers[$i]['VCARD_ORGUNIT']  = $VCARD_U[0]['ORGUNIT'];
95                        $group_nusers[$i]['VCARD_ROLE'] = $VCARD_U[0]['ROLE'];
96                        $group_nusers[$i]['VCARD_BDAY'] = $VCARD_U[0]['BDAY'];
97                        $group_nusers[$i]['VCARD_DESC'] = $VCARD_U[0]['DESC'];
98                        $group_nusers[$i]['VCARD_JID']  = $VCARD_U[0]['JID'];
99                               
100                        unset($VCARD_U);
101                        $i++;                                                                           
102                }
103               
104                return $group_nusers;
105        }
106
107        /*
108         * Contacts - Update - Add - Remove
109         */
110       
111        function UpdateContacts()
112        {
113                $array_contacts = array();
114                $array_db = array();
115                $array_return = array();
116
117                $this->sendIq('get', 'update1', NULL, NULL, 'jabber:iq:roster');
118                usleep(50000);
119                $result = $this->readSocketFromServer();
120               
121                if(count($result['iq'][1]['#']['query'][0]['#']['item']) > 0){
122                        $array_contacts = $result['iq'][1]['#']['query'][0]['#']['item'];
123                }
124               
125                $j = 0;
126               
127                // Consulta no Servidor Jabber;
128                if(count($array_contacts) > 0 ){
129                        for($i=0 ; $i < count($array_contacts); $i++)
130                {
131                                $array_return[$j]['subscription'] = $array_contacts[$i]['@']['subscription'];
132                                $array_return[$j]['name'] = $array_contacts[$i]['@']['name'];                   
133                                $array_return[$j]['jid'] = $array_contacts[$i]['@']['jid'];
134                                $array_return[$j]['group'] = $array_contacts[$i]['#']['group']['0']['#'];
135                                $aux_jid = explode("@",$array_return[$j]['jid']);
136                               
137                                $array_return[$j]['online'] = ($this->Last_access_User(trim($aux_jid[0]."@".$this->server))) ? 1:0;
138                                $j++;                   
139                        }
140                }                       
141               
142                // Consulta no Bd Postgres;
143                $db_im = new db_im();
144                $array_db = $db_im->get_contacts_im(NULL,$this->jid);
145               
146                if(count($array_db) > 0 )
147        {
148                        for($i=0 ; $i < count($array_db); $i++)
149            {
150                                $array_return[$j]['subscription'] = 'to';
151                                $array_return[$j]['name'] = $array_db[$i]['im_from'];
152                                $array_return[$j]['jid'] = $array_db[$i]['im_from'];
153                                $array_return[$j]['group'] = 'sem_grupo';
154                                $array_return[$j]['online'] = 0;
155                                $j++;
156                        }
157                }
158                return $array_return;
159        }
160       
161        function AddNewContacts($pUser)
162        {
163                $jid = explode("@",$pUser['email']);
164                $pJid = $jid[0]."@".$this->server;
165                $pName = $pUser['name'];
166                $pGroup = $pUser['group'];
167               
168                if(trim($jid[0]) != trim($this->username)){
169                        $addid = ( $addid ) ? $addid : "adduser_" . time();
170                        if ( $pJid )
171                        {
172                                $newcontact  = "<item jid='$pJid'";
173                                $newcontact .= ($pName) ? " name='" . urlencode($pName) . "'" : '';
174                                $newcontact .= (($pGroup) ? "><group>" . urlencode($pGroup) . "</group>\n</item" : "/") . ">";
175       
176                                if($this->sendIq('set', $addid, NULL, NULL, "jabber:iq:roster", $newcontact)){
177                                        // Bd;
178                                        $db_im = new db_im();
179                                        if($db_im->delete_contacts($pJid,$this->jid)){
180                                                $this->Subscribe($pJid);
181                                        }
182                    $this->AcceptRequest($request = array("jid" => $pJid,"subscription" => $pUser['subscription']));           
183                                        return true;
184                                }else{
185                                        return false;
186                                }
187                        }
188                        else
189                        {
190                                $this->writeLog("ERROR: RosterAddUser() #1");
191                                return false;
192                        }
193                }else{
194                        return false;
195                }
196        }
197       
198        function UpdateUser($pUser)
199        {
200                $pJid = $pUser['jid'];
201                $pName = $pUser['name'];
202                $pGroup = $pUser['group'];
203       
204                $upid = ( $upid ) ? $upid : "updateuser_" . time();
205
206                if ( $pJid )
207                {
208                        $updatecontact  = "<item jid='$pJid'";
209                        $updatecontact .= ($pName) ? " name='" . urlencode($pName) . "'" : '';
210                        $updatecontact .= (($pGroup) ? "><group>" . urlencode($pGroup) . "</group>\n</item" : "/") . ">";
211
212                        if($this->sendIq('set', $upid, NULL, NULL, "jabber:iq:roster", $updatecontact)){
213                                return true;
214                        }else{
215                                return false;
216                        }
217                }
218                else
219                {
220                        $this->writeLog("ERROR: RosterUpdateUser() #1");
221                        return false;
222                }
223        }
224       
225        function RosterRemoveUser($pUser)
226        {
227                $pJid = $pUser['jid'];
228                $pId  = 'deluser_' . time();
229
230                if ($pJid && $pId)
231                {
232                        $db_im = new db_im();
233                        $db_im->delete_contacts($pJid,$this->jid);
234                        if(!$this->sendIq('set',$pId,NULL,NULL,"jabber:iq:roster","<item jid='$pJid' subscription='remove'/>")){
235                                return false;                           
236                        }
237                        return true;
238                }else{
239                        $this->writeLog("ERROR: RosterRemoveUser() #1");
240                        return false;
241                }
242        }
243       
244        /*
245         * Vcard
246         */
247       
248        function getVcard($pJid)
249        {
250                $array_vcard = array();
251                $id_n = explode("@",$pJid);
252                $id = $id_n[0];
253
254                $this->sendIq('get',$id,$pJid,NULL,"vcard-temp","<vCard xmlns='vcard-temp'/>");         
255               
256                while(($result = $this->readSocketFromServer()) == NULL ){
257                        usleep(40);
258                }
259               
260                if( trim($result['iq'][0]['@']['type']) === trim("result") ){
261
262                                $arr = $result['iq'][0]['#']['vCard'][0]['#'];
263                                       
264                                // vCard
265                                $array_vcard[0]['FN'] = urldecode($arr['FN']['0']['#']);
266                                $array_vcard[0]['NICKNAME'] = urldecode($arr['NICKNAME']['0']['#']);
267                                $array_vcard[0]['ORGNAME'] = urldecode($arr['ORG']['0']['#']['ORGNAME']['0']['#']);
268                                $array_vcard[0]['ORGUNIT'] = urldecode($arr['ORG']['0']['#']['ORGUNIT']['0']['#']);             
269                                $array_vcard[0]['ROLE'] = urldecode($arr['ROLE']['0']['#']);                           
270                                $array_vcard[0]['BDAY'] = urldecode($arr['BDAY']['0']['#']);
271                                $array_vcard[0]['DESC'] = urldecode($arr['DESC']['0']['#']);
272                                $array_vcard[0]['JID'] = $pJid;
273
274                }else{
275                                $array_vcard[0]['FN'] = $pJid;
276                                $array_vcard[0]['NICKNAME'] = $id;
277                                $array_vcard[0]['ORGNAME'] = "";
278                                $array_vcard[0]['ORGUNIT'] = "";               
279                                $array_vcard[0]['ROLE'] = "";                           
280                                $array_vcard[0]['BDAY'] = "";
281                                $array_vcard[0]['DESC'] = "";
282                                $array_vcard[0]['JID'] = $pJid;
283                }
284       
285                return $array_vcard;
286        }
287
288        function newVcard($pVcard_User)
289        {
290                $pVcard = explode(";",$pVcard_User['vcard']);
291                $nid = explode("@",$this->jid);
292                $id = $nid[0];
293                $xmlVcard = "";
294
295                // Vcard                       
296                $xmlVcard  = "<iq id='$id' type='set'>";
297                $xmlVcard .= "<vCard xmlns='vcard-temp'>";             
298                $xmlVcard .= "<FN>".urlencode($pVcard[0])."</FN>";
299                $xmlVcard .= "<NICKNAME>".urlencode($pVcard[1])."</NICKNAME>";                                                                   
300                $xmlVcard .= "<ORG>";
301                $xmlVcard .= "<ORGNAME>".urlencode($pVcard[2])."</ORGNAME>";
302                $xmlVcard .= "<ORGUNIT>".urlencode($pVcard[3])."</ORGUNIT>";
303                $xmlVcard .= "</ORG>";
304                $xmlVcard .= "<ROLE>".urlencode($pVcard[4])."</ROLE>";
305                $xmlVcard .= "<BDAY>".urlencode($pVcard[5])."</BDAY>";
306                $xmlVcard .= "<DESC>".urlencode($pVcard[6])."</DESC>"; 
307                $xmlVcard .= "</vCard></iq>";
308               
309                if(!$this->SendPacket($xmlVcard))
310                        return false;
311                return true;
312               
313         }
314
315        /*
316         * getMessage
317         */
318
319        function getMessages()
320        {
321                $this->presence();
322
323                $this->sendIq('get','roster',NULL,NULL,"jabber:x:offline");
324                $array_return1 = $this->readSocketFromServer();
325
326                $array_message = array();
327                $j = 0;
328                for($i=0; $i < count($array_return1['message']); $i++)
329      {
330                        $array_message[$j]['from'] = $array_return1['message'][$i]['@']['from'];
331                        $array_message[$j]['to']   = $array_return1['message'][$i]['@']['to'];                 
332                        $array_message[$j]['body'] = urldecode(($array_return1['message'][$i]['#']['body'][0]['#']));
333                        $time = explode("T",$array_return1['message'][$i]['#']['x'][0]['@']['stamp']);
334                        $h = substr($time[1],0,2);
335                        $m = substr($time[1],3,2);
336                        $s = substr($time[1],6,2);
337                        // Carregando a hora;
338                        $array_message[$j]['timestamp'] = ($h - 4 ).":".$m.":".$s;
339                        $j++;   
340                }
341
342                return $array_message;
343        }
344       
345        function SendMessage($pSendMessage)
346        {
347                $to = $pSendMessage['to'];
348                $type = "normal";
349                $id = "";
350                $content = array(
351                        "subject" => $pSendMessage['subject'] ? $pSendMessage['subject'] : "",
352                        "thread" => $pSendMessage['thread'] ? $pSendMessage['thread'] : "",
353                        "body" => $pSendMessage['body'] ? urlencode((trim(preg_replace('/^<br>|<br>$/', '', $pSendMessage['body'])))) : ""
354                );
355                $payload = "";
356               
357                if ($to && is_array($content))
358                {
359                        if (!$id)
360                        {
361                                $id = $type . "_" . time();
362                        }
363
364                        $xml = "<message to='$to' type='$type' id='$id'>\n";
365
366                        if ($content['subject'])
367                        {
368                                $xml .= "<subject>" . $content['subject'] . "</subject>\n";
369                        }
370
371                        if ($content['thread'])
372                        {
373                                $xml .= "<thread>" . $content['thread'] . "</thread>\n";
374                        }
375
376                        $xml .= "<body>" . $content['body'] . "</body>\n";
377                        $xml .= $payload;
378                        $xml .= "</message>\n";
379                        unset($id);
380
381                        if ($this->SendPacket($xml))
382                        {
383                                return TRUE;
384                        }
385                        else
386                        {
387                                $this->writeLog("ERROR: SendMessage() #1");
388                                return FALSE;
389                        }
390                }
391                else
392                {
393                        $this->writeLog("ERROR: SendMessage() #2");
394                        return FALSE;
395                }
396        }
397
398        /*
399         *  Log -
400         */
401       
402       
403        /*
404     * Verifica os acessos de cada contato no servidor;
405     */
406   
407    function Last_access_User($pJid)
408    {
409        $id = "last_" . time();         
410        $result = array();
411                $this->sendIq('get',$id,$pJid,NULL,'jabber:iq:last');
412               
413                usleep(50000);
414                $result = $this->readSocketFromServer();
415               
416                $time_offline = $result['iq'][0]['#']['query'][0]['@']['seconds'];
417                return $this->time_off_line($time_offline);
418               
419    }
420   
421    function time_off_line($pTime)
422    {
423        $time_access = $pTime / 15;
424        if($time_access < 2)
425                return TRUE;
426        else
427                return FALSE;   
428    }
429   
430    /*
431         * Permissão - Autorizar /  Negar ;
432         */
433
434        function AcceptRequest($pRequest)
435        {
436                $jid = explode("@",$pRequest['jid']);
437                $pToJid = $jid[0]."@".$this->server;
438
439                if(trim($pRequest['subscription']) == 'none' || trim($pRequest['subscription']) == 'from'){
440                        $db_im = new db_im();
441                        if(count($db_im->get_contacts_im($this->jid, $pToJid)) == 0){
442                                 if(!$db_im->insert_contacts($this->jid , $pToJid))
443                                        return false;
444                                 else
445                                        return $this->SubscriptionAcceptRequest($pToJid);
446                         }else{
447                                return false;                                   
448                         }
449                }else{
450                        if(trim($pRequest['subscription']) == 'to'){
451                                return $this->SubscriptionAcceptRequest($pToJid);
452                        }else{
453                                return false;
454                        }
455                }
456        }
457
458        function SubscriptionAcceptRequest($to = NULL)
459        {
460                return ($to) ? $this->presence("subscribed", $to) : FALSE;
461        }
462
463        function SubscriptionDenyRequest($to = NULL)
464        {
465                return ($to) ? $this->presence("unsubscribed", $to) : FALSE;
466        }
467
468        function Subscribe($to = NULL)
469        {
470                return ($to) ? $this->presence("subscribe", $to) : FALSE;
471        }
472
473        function Unsubscribe($to = NULL)
474        {
475                return ($to) ? $this->presence("unsubscribe", $to) : FALSE;
476        }
477
478// TESTE
479
480   function encode(&$item, $val = 'encode')
481   {
482      switch( gettype($item) )
483      {
484         case 'object' :
485            $item = get_object_vars($item);
486            encode($item);
487         break;
488         case 'array' :
489            array_walk_recursive($item, 'encode');
490         break;
491         default : $item = utf8_encode($item);
492      }
493   }
494}
495?>
Note: See TracBrowser for help on using the repository browser.