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

Revision 265, 3.7 KB checked in by niltonneto, 16 years ago (diff)
Line 
1<?php
2
3require_once 'Jabberd2.abstract.php';
4
5class Ujabber extends Jabberd2
6{
7        public final function __construct()
8        {       
9                session_start();
10
11                $this->_user     = $_SESSION['phpgw_info']['instant_messenger']['user'];
12                $this->_pass     = $_SESSION['phpgw_info']['instant_messenger']['passwd'];
13                $this->_server   = $_SESSION['phpgw_info']['instant_messenger']['name_jabber'];
14                $this->_port     = $_SESSION['phpgw_info']['instant_messenger']['port_jabber'];
15                $this->_resource = $_SESSION['phpgw_info']['instant_messenger']['resource_jabber'];
16                $webjabber       = $_SESSION['phpgw_info']['instant_messenger']['webjabber'];
17
18                session_write_close();
19
20                if ( $this->connect($this->_user . '@' . $this->_server . '/' . $this->_resource . ':' . $this->_port, $this->_pass, 'read/write', $webjabber) )
21                        $this->connected = true;
22                else
23                        $this->connected = false;
24        }
25
26        public final function __destruct()
27        {
28                $this->closeSocket();
29                ob_end_flush();
30                flush();
31        }
32       
33        public final function isConnected()
34        {
35                return $this->connected;
36        }
37       
38        public final function listen()
39        {
40                if ( !$this->isConnected() )
41                        return "disconnected";
42
43                if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') )
44                {
45                        sleep(1);
46                        $xml = $this->readSocket();
47                        printf("%s", $xml);
48                }
49                else
50                {
51                        ob_end_clean();
52                        ignore_user_abort(true);
53                        ob_start();
54
55                        $init = time();
56
57                        while ( ( connection_aborted() === 0 ) && time() - $init < 50 )
58                        {
59                                # read from server and write in the client
60                                $xml = $this->readSocket();
61                                //var_dump($xml);
62                                $xml = ( strlen($xml) ) ? $xml : ' ';
63                                printf("%s", $xml);
64                                ob_flush();
65                                flush();
66                                usleep(6000);
67                        }
68                }
69        }
70
71        public final function SendMessage($pSendMessage)
72        {
73                if ( !$this->isConnected() )
74                        return "disconnected";
75
76                $pSendMessage['body'] = stripslashes($pSendMessage['body']);
77
78                $patterns = array('/&nbsp;| +/i', '/<br[^>]*>/i');
79                $replace = array(' ', '<br />');
80                $pSendMessage['body'] = preg_replace($patterns, $replace, $pSendMessage['body']);
81
82                $_emotions  = '/<img emotion="(\S+)?"[^>]+>/';
83                $_emotions = preg_match_all($_emotions, $pSendMessage['body'], $emotions);
84
85                foreach ( $emotions[0] as $key => $val )
86                        $emotions[0][$key] = '/' . addcslashes($val, './()[]{}^$*&?') . '/i';
87
88                $pSendMessage['body'] = preg_replace($emotions[0], $emotions[1], $pSendMessage['body']);
89
90                $to = $pSendMessage['to'];
91                $type = "chat";
92                $id = "";
93                $content = array(
94                        "subject" => $pSendMessage['subject'] ? $pSendMessage['subject'] : "",
95                        "thread"  => $pSendMessage['thread'] ? $pSendMessage['thread'] : "",
96                        "body"    => $pSendMessage['body'] ? $pSendMessage['body'] : ""
97                );
98
99                $payload = "";
100
101                if ($to && is_array($content))
102                {
103                        if (!$id)
104                        {
105                                $id = $type . "_" . time();
106                        }
107
108                        $xml = "<message to='$to' type='$type' id='$id'>";
109
110                        if ($content['subject'])
111                        {
112                                $xml .= "<subject>" . $content['subject'] . "</subject>";
113                        }
114
115                        if ($content['thread'])
116                        {
117                                $xml .= "<thread>" . $content['thread'] . "</thread>";
118                        }
119
120                        //Linha abaixo dentro do padrão xmpp, entretanto clientes como
121                        //o PSI não seguem o padrão assim ocorre problemas de comunicação
122                        //descomentar e retirar a seguinte para voltar ao padrão assim
123                        //que possí­vel
124                        //24/10/2007 - problema verificado por usuários do SERPRO na
125                        //plataforma Windows ocorrendo também no cliente Linux
126                        //por Alexandre e Rodrigo
127
128                        //$xml .= "<html><body>" . $content['body'] . "</body></html>";
129
130                        //retirar a linha abaixo para voltar ao padrão conforme acima
131                        $xml .= "<body>" . $content['body'] . "</body>";
132                        $xml .= $payload;
133                        $xml .= "</message>";
134                        unset($id);
135
136                        echo $xml;
137                        $this->writeSocket($xml);
138                        $this->close();
139                }
140                else
141                {
142                        //$this->writeLog("ERROR: SendMessage() #2");
143                        return false;
144                }
145        }
146}
147?>
Note: See TracBrowser for help on using the repository browser.