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