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

Revision 318, 5.0 KB checked in by niltonneto, 16 years ago (diff)

Commit feito pelo desenvolvedor (rodrigosouza).

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