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

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