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

Revision 323, 5.0 KB checked in by niltonneto, 16 years ago (diff)
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                if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') )
62                {
63                        sleep(1);
64                        $xml = $this->readSocket();
65                        printf("%s", $xml);
66                }
67                else
68                {
69                        ob_end_clean();
70                        ignore_user_abort(true);
71                        ob_start();
72
73                        $init = time();
74                        $buffer = '';
75
76                        while ( ( connection_aborted() === 0 ) && time() - $init < 50 )
77                        {
78                                # read from server and write in the client
79                                $xml = $this->readSocket();
80                                //var_dump($xml);
81                                if ( strlen($xml) && $xml != ' ' )
82                                {
83                                        if ( $xml[strlen($xml) - 1 ] != '>' )
84                                        {
85                                                $buffer .= $xml;
86                                                $xml = '';
87                                        }
88                                        else
89                                        {
90                                                $xml = $buffer . $xml;
91                                                $buffer = '';
92                                                //var_dump($xml);
93                                                //strlen($xml);
94                                                //exit;
95                                        }
96                                }
97                                $xml = ( strlen($xml) ) ? $xml : ' ';
98                                printf("%s", $xml);
99                                ob_flush();
100                                flush();
101                                usleep(6000);
102                        }
103                }
104        }
105
106        public final function checkConnection()
107        {
108                $this->get_last_access_user();
109                return $this->isConnected();
110        }
111
112        public final function Composing($pJid)
113        {
114                $pJid = $pJid['jid'];
115                $id = 'chat' . "_" . time();
116                $xml = "<message to='{$pJid}' type='chat' id='{$id}'><composing/></message>";
117                $this->writeSocket($xml);
118        }
119
120        public final function Paused($pJid)
121        {
122                $pJid = $pJid['jid'];
123                $id = 'chat' . "_" . time();
124                $xml = "<message to='{$pJid}' type='chat' id='{$id}'><paused/></message>";
125                $this->writeSocket($xml);
126        }
127
128        public final function SendMessage($pSendMessage)
129        {
130                if ( !$this->isConnected() )
131                        return "disconnected";
132
133                $pSendMessage['body'] = stripslashes($pSendMessage['body']);
134
135                $patterns = array('/&nbsp;| +/i', '/<br[^>]*>/i');
136                $replace = array(' ', '<br />');
137                $pSendMessage['body'] = preg_replace($patterns, $replace, $pSendMessage['body']);
138
139                $_emotions  = '/<img emotion="(\S+)?"[^>]+>/';
140                $_emotions = preg_match_all($_emotions, $pSendMessage['body'], $emotions);
141
142                foreach ( $emotions[0] as $key => $val )
143                        $emotions[0][$key] = '/' . addcslashes($val, './()[]{}^$*&?') . '/i';
144
145                $pSendMessage['body'] = preg_replace($emotions[0], $emotions[1], $pSendMessage['body']);
146
147                $to = $pSendMessage['to'];
148                $type = "chat";
149                $id = "";
150                $content = array(
151                        "subject" => $pSendMessage['subject'] ? $pSendMessage['subject'] : "",
152                        "thread"  => $pSendMessage['thread'] ? $pSendMessage['thread'] : "",
153                        "body"    => $pSendMessage['body'] ? $pSendMessage['body'] : ""
154                );
155
156                $payload = "";
157
158                if ($to && is_array($content))
159                {
160                        if (!$id)
161                        {
162                                $id = $type . "_" . time();
163                        }
164
165                        $xml = "<message to='$to' type='$type' id='$id'>";
166
167                        if ($content['subject'])
168                        {
169                                $xml .= "<subject>" . $content['subject'] . "</subject>";
170                        }
171
172                        if ($content['thread'])
173                        {
174                                $xml .= "<thread>" . $content['thread'] . "</thread>";
175                        }
176
177                        //Linha abaixo dentro do padrão xmpp, entretanto clientes como
178                        //o PSI não seguem o padrão assim ocorre problemas de comunicação
179                        //descomentar e retirar a seguinte para voltar ao padrão assim
180                        //que possí­vel
181                        //24/10/2007 - problema verificado por usuários do SERPRO na
182                        //plataforma Windows ocorrendo também no cliente Linux
183                        //por Alexandre e Rodrigo
184
185                        //$xml .= "<html><body>" . $content['body'] . "</body></html>";
186
187                        //retirar a linha abaixo para voltar ao padrão conforme acima
188                        $xml .= "<body><![CDATA[" . $content['body'] . "]]></body>";
189                        $xml .= $payload;
190                        $xml .= "</message>";
191                        unset($id);
192
193                        //echo $xml;
194                        $this->writeSocket($xml);
195                        $this->close();
196                }
197                else
198                {
199                        //$this->writeLog("ERROR: SendMessage() #2");
200                        return false;
201                }
202        }
203}
204?>
Note: See TracBrowser for help on using the repository browser.