source: tags/instant_messenger/inc/class.Ujabber.inc.php @ 327

Revision 327, 5.6 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                $this->get_last_access_user();
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                        $send_last_access = time();
79
80                        while ( (connection_aborted() === 0) && (time() - $init < 50) )
81                        {
82                                if ( (time() - $send_last_access) > 15 )
83                                {
84                                        $this->get_last_access_user();
85                                        $send_last_access = time();
86                                }
87
88                                # read from server and write in the client
89                                $xml = $this->readSocket();
90                                //var_dump($xml);
91                                if ( strlen($xml) && $xml != ' ' )
92                                {
93                                        if ( $xml[strlen($xml) - 1 ] != '>' )
94                                        {
95                                                $buffer .= $xml;
96                                                $xml = '';
97                                        }
98                                        else
99                                        {
100                                                $xml = $buffer . $xml;
101                                                $buffer = '';
102                                                //var_dump($xml);
103                                                //strlen($xml);
104                                                //exit;
105                                        }
106                                }
107                                $xml = ( strlen($xml) ) ? $xml : ' ';
108                                printf("%s", $xml);
109                                ob_flush();
110                                flush();
111                                usleep(6000);
112                        }
113                }
114        }
115
116        public final function update()
117        {
118                $presence = $_COOKIE['IM_presence'];
119
120                if ( !$presence )
121                        $presence = 'available';
122
123                switch ( $presence )
124                {
125                        case 'away':
126                        case 'dnd':
127                        case 'xa':
128                                $presence = array('show' => $presence);
129                        break;
130                        case 'available':
131                        case 'unavailable':
132                                $presence = array('type' => $presence);
133                        break;
134                }
135
136                $this->getContacts();
137                $this->setPresence(array('type'=>'unavailable'));
138                $this->setPresence($presence);
139
140                print_r($presence);
141        }
142
143        public final function Composing($pJid)
144        {
145                $pJid = $pJid['jid'];
146                $id = 'chat' . "_" . time();
147                $xml = "<message to='{$pJid}' type='chat' id='{$id}'><composing/></message>";
148                $this->writeSocket($xml);
149        }
150
151        public final function Paused($pJid)
152        {
153                $pJid = $pJid['jid'];
154                $id = 'chat' . "_" . time();
155                $xml = "<message to='{$pJid}' type='chat' id='{$id}'><paused/></message>";
156                $this->writeSocket($xml);
157        }
158
159        public final function SendMessage($pSendMessage)
160        {
161                if ( !$this->isConnected() )
162                        return "disconnected";
163
164                $pSendMessage['body'] = stripslashes($pSendMessage['body']);
165
166                $patterns = array('/&nbsp;| +/i', '/<br[^>]*>/i');
167                $replace = array(' ', '<br />');
168                $pSendMessage['body'] = preg_replace($patterns, $replace, $pSendMessage['body']);
169
170                $_emotions  = '/<img emotion="(\S+)?"[^>]+>/';
171                $_emotions = preg_match_all($_emotions, $pSendMessage['body'], $emotions);
172
173                foreach ( $emotions[0] as $key => $val )
174                        $emotions[0][$key] = '/' . addcslashes($val, './()[]{}^$*&?') . '/i';
175
176                $pSendMessage['body'] = preg_replace($emotions[0], $emotions[1], $pSendMessage['body']);
177
178                $to = $pSendMessage['to'];
179                $type = "chat";
180                $id = "";
181                $content = array(
182                        "subject" => $pSendMessage['subject'] ? $pSendMessage['subject'] : "",
183                        "thread"  => $pSendMessage['thread'] ? $pSendMessage['thread'] : "",
184                        "body"    => $pSendMessage['body'] ? $pSendMessage['body'] : ""
185                );
186
187                $payload = "";
188
189                if ($to && is_array($content))
190                {
191                        if (!$id)
192                        {
193                                $id = $type . "_" . time();
194                        }
195
196                        $xml = "<message to='$to' type='$type' id='$id'>";
197
198                        if ($content['subject'])
199                        {
200                                $xml .= "<subject>" . $content['subject'] . "</subject>";
201                        }
202
203                        if ($content['thread'])
204                        {
205                                $xml .= "<thread>" . $content['thread'] . "</thread>";
206                        }
207
208                        //Linha abaixo dentro do padrão xmpp, entretanto clientes como
209                        //o PSI não seguem o padrão assim ocorre problemas de comunicação
210                        //descomentar e retirar a seguinte para voltar ao padrão assim
211                        //que possí­vel
212                        //24/10/2007 - problema verificado por usuários do SERPRO na
213                        //plataforma Windows ocorrendo também no cliente Linux
214                        //por Alexandre e Rodrigo
215
216                        //$xml .= "<html><body>" . $content['body'] . "</body></html>";
217
218                        //retirar a linha abaixo para voltar ao padrão conforme acima
219                        $xml .= "<body><![CDATA[" . $content['body'] . "]]></body>";
220                        $xml .= $payload;
221                        $xml .= "</message>";
222                        unset($id);
223
224                        //echo $xml;
225                        $this->writeSocket($xml);
226                        $this->close();
227                }
228                else
229                {
230                        //$this->writeLog("ERROR: SendMessage() #2");
231                        return false;
232                }
233        }
234}
235?>
Note: See TracBrowser for help on using the repository browser.