1 | #!/usr/bin/php -Cq |
---|
2 | <?php |
---|
3 | |
---|
4 | require_once('inc/IMConfigure.php'); |
---|
5 | |
---|
6 | ### |
---|
7 | # definitions of socket - BEGIN |
---|
8 | # |
---|
9 | define('URI', '0.0.0.0'); |
---|
10 | define('TARGET_NON_SECURITY', TRANSPORT_NON_SECURITY . '://' . URI . ':' . PORT_NON_SECURITY); |
---|
11 | define('SOCKET_NON_SECURITY', stream_socket_server(TARGET_NON_SECURITY, $errno_non_security, $errstr_non_security, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN)); |
---|
12 | # |
---|
13 | # definitions of socket - END |
---|
14 | ### |
---|
15 | |
---|
16 | |
---|
17 | if ( !SOCKET_NON_SECURITY ) |
---|
18 | { |
---|
19 | printf("NON SECURITY:\n(%d) %s", $errno_non_security, $errstr_non_security); |
---|
20 | exit(0); |
---|
21 | } |
---|
22 | |
---|
23 | include 'inc/Jabberd2.abstract.php'; |
---|
24 | |
---|
25 | $master = array(SOCKET_NON_SECURITY); |
---|
26 | |
---|
27 | $users = array(); |
---|
28 | |
---|
29 | $send = false; |
---|
30 | |
---|
31 | while ( 1 ) |
---|
32 | { |
---|
33 | $read = $master; |
---|
34 | $_w = NULL; |
---|
35 | $_e = NULL; |
---|
36 | |
---|
37 | $mod_fd = @stream_select($read, $_w , $_e , READ_SOCKET_AFTER_OF ); |
---|
38 | |
---|
39 | if ( $mod_fd === FALSE ) |
---|
40 | break; |
---|
41 | |
---|
42 | for ( $i = 0; $i < $mod_fd; ++$i ) |
---|
43 | { |
---|
44 | if ( $read[$i] === SOCKET_NON_SECURITY ) |
---|
45 | { |
---|
46 | $conn_non_security = stream_socket_accept(SOCKET_NON_SECURITY); |
---|
47 | $master[] = &$conn_non_security; |
---|
48 | } |
---|
49 | else |
---|
50 | { |
---|
51 | $sock_data = fread($read[$i], 1024); |
---|
52 | if ( strlen($sock_data) === 0 ) |
---|
53 | { |
---|
54 | $key_to_del = array_search($read[$i], $master, TRUE); |
---|
55 | fclose($read[$i]); |
---|
56 | unset($master[$key_to_del]); |
---|
57 | } |
---|
58 | else if ( $sock_data === FALSE ) |
---|
59 | { |
---|
60 | $key_to_del = array_search($read[$i], $master, TRUE); |
---|
61 | unset($master[$key_to_del]); |
---|
62 | } |
---|
63 | else |
---|
64 | { |
---|
65 | $headers = (split("\n", $sock_data)); |
---|
66 | |
---|
67 | preg_match('/^(GET|POST) \/(.+) HTTP\/1\.1/U', $headers[0], $request); |
---|
68 | preg_match('/^Host: \w*:(.+)/', $headers[1], $host); |
---|
69 | |
---|
70 | $conn = &$conn_non_security; |
---|
71 | |
---|
72 | $request = explode(';', $request[2]); |
---|
73 | |
---|
74 | if ( $request[0] == CONNECT && isset($request[1]) && !isset($users[$request[1]]) ) |
---|
75 | { |
---|
76 | connect($users, $request[1]); |
---|
77 | } |
---|
78 | |
---|
79 | if ( $request[0] == CLOSE && isset($request[1]) && isset($users[$request[1]]) ) |
---|
80 | { |
---|
81 | close($request[1]); |
---|
82 | } |
---|
83 | |
---|
84 | fclose($read[$i]); |
---|
85 | unset($master[array_search($read[$i], $master)]); |
---|
86 | } |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | foreach ( $users as $key => $val ) |
---|
91 | { |
---|
92 | if($users[$key]['time0'] != $users[$key]['time1'] ) |
---|
93 | { |
---|
94 | $users[$key]['time1'] = $users[$key]['time0']; |
---|
95 | $users[$key]['attempt'] = 0; |
---|
96 | readSocket($key); |
---|
97 | //readSocket($users, $key); |
---|
98 | } |
---|
99 | else |
---|
100 | { |
---|
101 | $users[$key]['attempt']++; |
---|
102 | if( $users[$key]['attempt'] > 5 ) |
---|
103 | { |
---|
104 | $users[$key]['jabber']->disconnect(); |
---|
105 | unset($users[$key]); |
---|
106 | } |
---|
107 | else |
---|
108 | { |
---|
109 | readSocket($key); |
---|
110 | //readSocket($users, $key); |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | //print_r("Session : " . $key . " | Count : " . count($users) . " | time 0 : " . $users[$key]['time0'] . " | time 1 : " . $users[$key]['time1'] . " | tentativas : " . $users[$key]['attempt'] . " \n"); |
---|
115 | } |
---|
116 | |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | exit(0); |
---|
121 | |
---|
122 | |
---|
123 | function connect(&$pUsers, $pSession) |
---|
124 | { |
---|
125 | $sessionpath = explode(';', session_save_path()); |
---|
126 | foreach ( $sessionpath as $path ) |
---|
127 | { |
---|
128 | $path = ( strrpos($path, '/') == strlen($path) - 1 ) ? $path : $path . '/'; |
---|
129 | |
---|
130 | if ( file_exists($path . 'sess_' . $pSession) ) |
---|
131 | { |
---|
132 | session_id($pSession); |
---|
133 | |
---|
134 | ob_start(); |
---|
135 | session_start(); |
---|
136 | ob_clean(); |
---|
137 | ob_end_flush(); |
---|
138 | |
---|
139 | $is_user = isset($_SESSION['phpgw_info']['instant_messenger']['user']) and isset($_SESSION['phpgw_info']['instant_messenger']['passwd']); |
---|
140 | if ( $is_user ) |
---|
141 | { |
---|
142 | $_user = $_SESSION['phpgw_info']['instant_messenger']['user']; |
---|
143 | $_pass = $_SESSION['phpgw_info']['instant_messenger']['passwd']; |
---|
144 | $_server = $_SESSION['phpgw_info']['instant_messenger']['name_jabber']; |
---|
145 | $_port = $_SESSION['phpgw_info']['instant_messenger']['port_jabber']; |
---|
146 | $_resource = $_SESSION['phpgw_info']['instant_messenger']['resource_jabber']; |
---|
147 | $_time = time(); |
---|
148 | $_SESSION['phpgw_info']['instant_messenger']['time'] = $_time; |
---|
149 | $_SESSION['phpgw_info']['instant_messenger']['socket'] = NULL; |
---|
150 | $_SESSION['phpgw_info']['instant_messenger']['socket']['in'] = NULL; |
---|
151 | $_SESSION['phpgw_info']['instant_messenger']['socket']['out'] = array(); |
---|
152 | $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['message'] = NULL; |
---|
153 | $_SESSION['phpgw_info']['instant_messenger']['socket']['block'] = false; |
---|
154 | |
---|
155 | session_write_close(); |
---|
156 | |
---|
157 | $a = new Jabberd2; |
---|
158 | if ( ($a->connect($_user . '@' . $_server . '/' . $_resource . ':' . $_port, $_pass)) ) |
---|
159 | { |
---|
160 | $pUsers[$pSession] = array(); |
---|
161 | $pUsers[$pSession]['jabber'] = $a; |
---|
162 | $pUsers[$pSession]['time0'] = $_time; |
---|
163 | $pUsers[$pSession]['time1'] = 0; |
---|
164 | $pUsers[$pSession]['attempt'] = 0; |
---|
165 | } |
---|
166 | else |
---|
167 | { |
---|
168 | unset($a); |
---|
169 | } |
---|
170 | break; |
---|
171 | } |
---|
172 | session_write_close(); |
---|
173 | } |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | function close($pUser) |
---|
178 | { |
---|
179 | global $users; |
---|
180 | $users[$pUser]['jabber']->disconnect(); |
---|
181 | unset($users[$pUser]); |
---|
182 | } |
---|
183 | |
---|
184 | //function readSocket(&$pUsers, $pSession) |
---|
185 | function readSocket($pSession) |
---|
186 | { |
---|
187 | global $users; |
---|
188 | global $send; |
---|
189 | $sessionpath = explode(';', session_save_path()); |
---|
190 | foreach ( $sessionpath as $path ) |
---|
191 | { |
---|
192 | $path = ( strrpos($path, '/') == strlen($path) - 1 ) ? $path : $path . '/'; |
---|
193 | |
---|
194 | if ( file_exists($path . 'sess_' . $pSession) ) |
---|
195 | { |
---|
196 | session_id($pSession); |
---|
197 | |
---|
198 | ob_start(); |
---|
199 | session_start(); |
---|
200 | ob_clean(); |
---|
201 | ob_end_flush(); |
---|
202 | |
---|
203 | $is_user = isset($_SESSION['phpgw_info']['instant_messenger']['user']) and isset($_SESSION['phpgw_info']['instant_messenger']['passwd']); |
---|
204 | if ( $is_user ) |
---|
205 | { |
---|
206 | $users[$pSession]['time0'] = $_SESSION['phpgw_info']['instant_messenger']['time']; |
---|
207 | $readSocket = $users[$pSession]['jabber']->readSocket(); |
---|
208 | |
---|
209 | if ( $readSocket !== false ) |
---|
210 | { |
---|
211 | if ( !empty($readSocket) ) |
---|
212 | { |
---|
213 | $_SESSION['phpgw_info']['instant_messenger']['socket']['in'] .= $readSocket; |
---|
214 | } |
---|
215 | |
---|
216 | if ( $_SESSION['phpgw_info']['instant_messenger']['socket']['out'] ) |
---|
217 | foreach ( $_SESSION['phpgw_info']['instant_messenger']['socket']['out'] as $action => $value ) |
---|
218 | { |
---|
219 | if ( $action === 'message' ) |
---|
220 | { |
---|
221 | if ( !is_null($value) ) |
---|
222 | { |
---|
223 | $users[$pSession]['jabber']->writeSocket($value); |
---|
224 | $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['message'] = NULL; |
---|
225 | } |
---|
226 | } |
---|
227 | else if ( $action === 'vcard' ) |
---|
228 | { |
---|
229 | if ( !empty($value) ) |
---|
230 | { |
---|
231 | foreach ( $value as $key => $val ) |
---|
232 | { |
---|
233 | $users[$pSession]['jabber']->getVcard($val); |
---|
234 | unset($_SESSION['phpgw_info']['instant_messenger']['socket']['out']['vcard'][$key]); |
---|
235 | } |
---|
236 | } |
---|
237 | } |
---|
238 | else |
---|
239 | { |
---|
240 | if ( method_exists($users[$pSession]['jabber'], $action) ) |
---|
241 | if ( $value ) |
---|
242 | $users[$pSession]['jabber']->$action($value); |
---|
243 | else |
---|
244 | $users[$pSession]['jabber']->$action(); |
---|
245 | unset($_SESSION['phpgw_info']['instant_messenger']['socket']['out'][$action]); |
---|
246 | } |
---|
247 | } |
---|
248 | } |
---|
249 | else |
---|
250 | { |
---|
251 | close($pSession); |
---|
252 | } |
---|
253 | session_write_close(); |
---|
254 | break; |
---|
255 | } |
---|
256 | session_write_close(); |
---|
257 | } |
---|
258 | } |
---|
259 | } |
---|
260 | ?> |
---|