= 1) {; // check if there is a client trying to connect------------ < if (in_array($sock, $read)) { // accept the client, and add him to the $clients array $clients[] = $newsock = socket_accept($sock); $key = array_search($newsock, $clients); if($X) echo "--New connection made-- $newsock -- C/S Pair:$key\n"; if($X) echo "==== " . date("l, F jS Y - H:i:s") . " ====\n"; // New Connection made Connect to imap server $servers[$key] = $newserv = fsockopen($IMAP_address, $IMAP_port, $errno, $errstr, 30); if (!$newserv) { if($X) echo "Failed to open Socket to server: $errstr ($errno)
\n"; // So lets just shutdown this client then. disconnected_server($servers, $X, $newsock, $clients, $folder); } if($X) echo "--Connected to server-- $newserv -- C/S Pair:$key\n"; if (!stream_set_timeout($servers[$key], 0, 500)){ if($X) echo "####error unable to set timeout for the server socket###"; // Lets just close this server and the client if($X) echo "--Disconnecting server-- $servers[$key] -- C/S Pair:$key \n"; fclose($servers[$key]); disconnected_server($servers, $X, $newsock, $clients, $folder); } if (!socket_set_nonblock($newsock)){ if($X) echo "###error unable to set non-blocking for the client socket###"; // Lets just close this server and the client if($X) echo "--Disconnecting server-- $servers[$key] -- C/S Pair:$key \n"; fclose($servers[$key]); disconnected_server($servers, $X, $newsock, $clients, $folder); } // remove the listening socket from the clients-with-data array $key = array_search($sock, $read); unset($read[$key]); } //End of new connection ----------------------------------- // Read stuff from client to proxy-------------------------- // loop through all the clients that have data to read from foreach ($read as $read_sock) { // read until stop or 1024 bytes // socket_read while show errors when the client is disconnected, so silence the error messages $data = @socket_read($read_sock, 1024, PHP_BINARY_READ); // check if the client is disconnected if (($data === '') && (socket_last_error($read_sock) != 11)) { // remove client for $clients array $key = array_search($read_sock, $clients); unset($clients[$key]); if($X) echo "--Client disconnected-- $read_sock -- C/S Pair:$key\n"; // Disconnect the corresponding server if($X) echo "--Disconnecting server-- $servers[$key] -- C/S Pair:$key \n"; fclose($servers[$key]); unset($servers[$key]); //Remove any possible folder reference unset($folder[$key]); // continue to the next client to read from, if any continue; } // check if there is any data after trimming off the spaces if (!empty($data)) { // send this to the server $key = array_search($read_sock, $clients); if ($status != $key . "client"){ if($X) echo "\n--Client sent-- $read_sock -- C/S Pair:$key\n"; $status = $key . "client"; } if (preg_match( "/l LOGIN \"-\+(.*)\+-(.*\@.*)\" \"(.*)\"/" , $data, $matches) > 0){ if($X) echo "ORIGINAL DATA WAS: $data"; $data = "l LOGIN \"$matches[2]\" \"$matches[3]\"\r\n"; $folder[$key] = $matches[1]; } if ((preg_match( "/s SELECT \"INBOX\"/" , $data, $matches) > 0) && isset($folder[$key])){ if($X) echo "ORIGINAL DATA WAS: $data"; $data = "s SELECT \"" .$namespace. "$folder[$key]\"\r\n"; } if (preg_match( "/a APPEND \"Sent\" \(\\\\Seen\) \{(\d*)\}/" , $data, $matches) > 0){ if($X) echo "ORIGINAL DATA WAS: $data"; $data = "a APPEND \"" .$namespace . $sent_folder."\" (\Seen) \{$matches[1]}\r\n"; } if (preg_match( "/c UID COPY (.*) \"Trash\"/" , $data, $matches) > 0){ if($X) echo "ORIGINAL DATA WAS: $data"; $data = "c UID COPY $matches[1] \"" .$namespace . $trash_folder. "\"\r\n"; } if($X) echo "$data"; fputs($servers[$key], $data); } } // end of reading clients foreach------------------------------- } //end of clients conditional statement //Read stuff from server to proxy--------------------------< // loop through all the servers and check for data foreach ($servers as $read_sock) { // read until stop or 1024 bytes // fgets will show errors when the server is disconnected, so silence the error messages $data = @fgets($read_sock, 1024); // check if the server is disconnected if (strpos($php_errormsg, "SSL: fatal protocol error") !== false){ disconnected_server($servers, $X, $read_sock, $clients, $folder); // continue to the next server to read from, if any continue; } // check if there is any data after trimming off the spaces if (!empty($data)) { // try and figure out which client this goes to? $key = array_search($read_sock, $servers); // send this to the client if ($status != $key . "server"){ if($X) echo "\n--Server sent-- $read_sock -- C/S Pair:$key \n"; $status = $key . "server"; } if (strpos($data, $list_end) !== false){ $append = '* LIST (\Marked \HasNoChildren) "." "Sent"' . "\r\n"; $append .= '* LIST (\Marked \HasNoChildren) "." "Trash"' . "\r\n"; if($X) echo $append; socket_write($clients[$key], $append, strlen($append)); } if($X) echo "$data"; socket_write($clients[$key], $data, strlen($data)); } } // end of reading servers foreach------------------------------- usleep(1000); } socket_close($sock); function disconnected_server($servers, $X, $read_sock, $clients, $folder){ // remove server from $servers array $key = array_search($read_sock, $servers); unset($servers[$key]); if($X) echo "--Connection to server lost-- $read_sock -- C/S Pair:$key \n"; // Disconnect the corresponding client if($X) echo "-Disconnecting client--- $clients[$key] -- C/S Pair:$key \n"; socket_close($clients[$key]); unset($clients[$key]); //Remove any possible folder reference unset($folder[$key]); } ?>