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

Revision 216, 3.6 KB checked in by niltonneto, 16 years ago (diff)

Correções do módulo instant_messenger.
Ver WiKi? em: http://www.expressolivre.org/dev/wiki/messenger

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