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