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

Revision 311, 5.5 KB checked in by niltonneto, 16 years ago (diff)

Correções críticas para funcionalidades já existentes.

Line 
1<?php
2#error_reporting(E_ALL);
3require_once 'Jabberd2.abstract.php';
4
5class Ujabber extends Jabberd2
6{
7        public final function __construct($pConnectionType = 'write')
8        {
9                /*session_start();
10
11                $this->_user     = $_SESSION['phpgw_info']['instant_messenger']['user'];
12                //$this->_user     = 'niltonneto';//$_SESSION['phpgw_info']['instant_messenger']['user'];
13                //$this->_pass     = 'nine59ever';//$_SESSION['phpgw_info']['instant_messenger']['passwd'];
14                //$this->_pass     = 'senha';//$_SESSION['phpgw_info']['instant_messenger']['passwd'];
15                $this->_pass     = $_SESSION['phpgw_info']['instant_messenger']['passwd'];
16                $this->_server   = $_SESSION['phpgw_info']['instant_messenger']['name_jabber'];
17                $this->_port     = '8883';//$_SESSION['phpgw_info']['instant_messenger']['port_jabber'];
18                $this->_resource = $_SESSION['phpgw_info']['instant_messenger']['resource_jabber'];
19                //$webjabber       = 'im.pr.gov.br';//$_SESSION['phpgw_info']['instant_messenger']['webjabber'];
20                $webjabber       = '10.15.22.236';//$_SESSION['phpgw_info']['instant_messenger']['webjabber'];
21
22                session_write_close();*/
23
24                try
25                {
26                        if ( !file_exists(dirname(__FILE__) . '/../instant_messenger.define.php') )
27                                throw new Exception(__CLASS__ . '[ ERROR #1 ] : Not found configuration file');
28
29                        require_once dirname(__FILE__) . '/../instant_messenger.define.php';
30
31                        if ( !(defined('JABBER_URL') && defined('JABBER_RESOURCE')) )
32                                throw new Exception(__CLASS__ . '[ ERROR #2 ] : Jabber server is not cofigured');
33
34                        if ( !(defined('WEBJABBER_URL') && defined('WEBJABBER_PORT')) )
35                                throw new Exception(__CLASS__ . '[ ERROR #3 ] : Webabber server is not cofigured');
36
37                        $this->_server = constant('JABBER_URL');
38                        $this->_resource = constant('JABBER_RESOURCE');
39
40                        $this->_port = constant('WEBJABBER_PORT');
41
42                        session_start();
43                        $this->_user = $_SESSION['phpgw_info']['instant_messenger']['user'];
44                        $this->_pass = $_SESSION['phpgw_info']['instant_messenger']['passwd'];
45                        session_write_close();
46
47                        $access_string = $this->_user . '@' . $this->_server . '/' . $this->_resource . ':' . $this->_port;
48
49                        $this->connected = false;
50                        if ( $this->connect($access_string, $this->_pass, $pConnectionType, constant('WEBJABBER_URL')) )
51                                $this->connected = true;
52                }
53                catch(Exception $e)
54                {
55                        echo $e->getMessage();
56                }
57        }
58
59        public final function __destruct()
60        {
61                $this->closeSocket();
62                ob_end_flush();
63                flush();
64        }
65
66        public final function isConnected()
67        {
68                return $this->connected;
69        }
70
71        public final function listen()
72        {
73                if ( !$this->isConnected() )
74                        return "disconnected";
75
76                $this->get_last_access_user(array("jid" => "this"));
77
78                if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') )
79                {
80                        sleep(1);
81                        $xml = $this->readSocket();
82                        printf("%s", $xml);
83                }
84                else
85                {
86                        ob_end_clean();
87                        ignore_user_abort(true);
88                        ob_start();
89
90                        $init = time();
91                        $buffer = '';
92
93                        while ( ( connection_aborted() === 0 ) && time() - $init < 30 )
94                        {
95                                # read from server and write in the client
96                                $xml = $this->readSocket();
97                                //var_dump($xml);
98                                if ( strlen($xml) && $xml != ' ' )
99                                {
100                                        if ( $xml[strlen($xml) - 1 ] != '>' )
101                                        {
102                                                $buffer .= $xml;
103                                                $xml = '';
104                                        }
105                                        else
106                                        {
107                                                $xml = $buffer . $xml;
108                                                $buffer = '';
109                                                //var_dump($xml);
110                                                //strlen($xml);
111                                                //exit;
112                                        }
113                                }
114                                $xml = ( strlen($xml) ) ? $xml : ' ';
115                                printf("%s", $xml);
116                                ob_flush();
117                                flush();
118                                usleep(6000);
119                        }
120                }
121        }
122
123        public final function SendMessage($pSendMessage)
124        {
125                if ( !$this->isConnected() )
126                        return "disconnected";
127
128                $pSendMessage['body'] = stripslashes($pSendMessage['body']);
129
130                $patterns = array('/&nbsp;| +/i', '/<br[^>]*>/i');
131                $replace = array(' ', '<br />');
132                $pSendMessage['body'] = preg_replace($patterns, $replace, $pSendMessage['body']);
133
134                $_emotions  = '/<img emotion="(\S+)?"[^>]+>/';
135                $_emotions = preg_match_all($_emotions, $pSendMessage['body'], $emotions);
136
137                foreach ( $emotions[0] as $key => $val )
138                        $emotions[0][$key] = '/' . addcslashes($val, './()[]{}^$*&?') . '/i';
139
140                $pSendMessage['body'] = preg_replace($emotions[0], $emotions[1], $pSendMessage['body']);
141
142                $to = $pSendMessage['to'];
143                $type = "chat";
144                $id = "";
145                $content = array(
146                        "subject" => $pSendMessage['subject'] ? $pSendMessage['subject'] : "",
147                        "thread"  => $pSendMessage['thread'] ? $pSendMessage['thread'] : "",
148                        "body"    => $pSendMessage['body'] ? $pSendMessage['body'] : ""
149                );
150
151                $payload = "";
152
153                if ($to && is_array($content))
154                {
155                        if (!$id)
156                        {
157                                $id = $type . "_" . time();
158                        }
159
160                        $xml = "<message to='$to' type='$type' id='$id'>";
161
162                        if ($content['subject'])
163                        {
164                                $xml .= "<subject>" . $content['subject'] . "</subject>";
165                        }
166
167                        if ($content['thread'])
168                        {
169                                $xml .= "<thread>" . $content['thread'] . "</thread>";
170                        }
171
172                        //Linha abaixo dentro do padrão xmpp, entretanto clientes como
173                        //o PSI não seguem o padrão assim ocorre problemas de comunicação
174                        //descomentar e retirar a seguinte para voltar ao padrão assim
175                        //que possí­vel
176                        //24/10/2007 - problema verificado por usuários do SERPRO na
177                        //plataforma Windows ocorrendo também no cliente Linux
178                        //por Alexandre e Rodrigo
179
180                        //$xml .= "<html><body>" . $content['body'] . "</body></html>";
181
182                        //retirar a linha abaixo para voltar ao padrão conforme acima
183                        $xml .= "<body><![CDATA[" . $content['body'] . "]]></body>";
184                        $xml .= $payload;
185                        $xml .= "</message>";
186                        unset($id);
187
188                        echo $xml;
189                        $this->writeSocket($xml);
190                        $this->close();
191                }
192                else
193                {
194                        //$this->writeLog("ERROR: SendMessage() #2");
195                        return false;
196                }
197        }
198}
199?>
Note: See TracBrowser for help on using the repository browser.