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