/** * Implements Net_Finger::query() function using PEAR's socket functions * * @param string $server The finger-server to query * @param string $query The finger database object to lookup * @return mixed The data returned from the finger-server as string * or a PEAR_Error ( see Net_Socket for error codes) */ function query($server, $query) { $socket = new Net_Socket(); if (PEAR::isError($sockerror = $socket->connect($server, 79))) { $data = new PEAR_Error("Error connecting to {$server} ( Net_Socket says: " . $sockerror->getMessage() . ")", $sockerror->getCode()); } else { $query .= "\n"; $socket->write($query); $data = $socket->read(16384); $socket->disconnect(); } return $data; }
/** * Send a command to the IMAP server and retrieve * the response * * @param string $command The command to send * * @param string $data If we are issuing a command * that means the server will * consequently expect literal * data sent then this is that data. * * @return mixed Server response string on success * or bool false on error */ function sendCmd($command, $data = null, $fh = false) { if (!is_resource($this->socket->fp)) { $this->lastError = IC_NOT_CONNECTED; return false; } if (feof($this->socket->fp)) { $this->lastError = IC_NOT_CONNECTED; return false; } $cid = $this->cid; $this->socket->writeLine("{$this->cid} {$command}"); $this->cid++; if ($this->debug) { $this->debugOutput("C: {$cid} {$command}\n"); } $resp = ''; $loop = 0; $lines = 0; $bytesRead = 0; $respSize = 0; $code = ''; $fetch = substr($command, 0, 5) == 'FETCH'; $select = substr($command, 0, 6) == 'SELECT'; while (true) { // Check we are still connected if (!is_resource($this->socket->fp)) { return $resp; } if (feof($this->socket->fp)) { $this->lastError = IC_NOT_CONNECTED; return false; } $line = $this->socket->gets(8192); // 8192 same buffer used in Pear Net::POP3 if ($this->debug) { $this->debugOutput("S: {$line}"); } if (preg_match("/^{$cid} (OK|BAD|NO)/", $line, $m)) { $code = $m[1]; } elseif (!$fetch && !$select && preg_match("/^\\* (BAD|NO|BYE)/", $line, $m)) { $code = $m[1]; } // UW-IMAP been brain dead for 'status' command on a mailbox if ($code == 'NO' && preg_match("/NO CLIENT BUG DETECTED/", $line)) { $code = ''; continue; } else { if ($code == 'BAD' || $code == 'NO' || $code == 'BYE') { return false; } else { if ($code == 'OK') { return !empty($resp) ? $resp : $line; } } } $lines++; // If writing to the filehandle, get msg size and skip the first line response from the IMAP server if (is_resource($fh) && preg_match('/^\\* /', $line) && $lines == '1') { preg_match('/\\{(\\d+)\\}/', $line, $m); $respSize = $m[1]; continue; } // Check if we are sending literal data // and server is ready for literal data if ($data && preg_match('/^\\+/', $line)) { $size = $this->socket->write($data); if ($this->debug) { $this->debugOutput("C: {$cid} {$data}\n"); } sleep(1); $this->socket->write("\r\n"); continue; } // Double check that we are not hung on a // command (ie not enough data sent) if ($data && $line == '') { // Send some new lines to try and // reach expected bytes $this->socket->writeLine(''); if ($this->debug) { $this->debugOutput("C: {$cid} \n"); } } // Replace/fix - Under cyrus, multiple IMAP logins, mailbox locks, sockets are closed automatically if ($line == '' && $loop > 1000) { return $resp; } else { if ($line == '') { $loop++; } } // Print to the filehandle only up to $respSize bytes so we do not append the last IMAP header on the message if ($stop) { continue; } elseif (is_resource($fh)) { $bytesRead += strlen($line); // Strip Control-M chars $line = preg_replace('/\\cM+$/', '', $line); fwrite($fh, $line); // Only read up to expected byte size if ($bytesRead == $respSize) { $stop = true; } } else { $resp .= "{$line}"; } } }
function get_gg_status($numer_gg, $haslo_gg, $szukany_numer, &$error, &$gg_status_widocznosc) { define("GG_WELCOME", 0x1); define("GG_LOGIN", 0xc); define("GG_LOGIN60", 0x15); define("GG_LOGIN_OK", 0x3); define("GG_LOGIN_FAILED", 0x9); define("GG_NEW_STATUS", 0x2); define("GG_STATUS", 0x2); define("GG_STATUS_NOT_AVAIL", 0x1); define("GG_STATUS_NOT_AVAIL_DESCR", 0x15); define("GG_STATUS_AVAIL", 0x2); define("GG_STATUS_AVAIL_DESCR", 0x4); define("GG_STATUS_BUSY", 0x3); define("GG_STATUS_BUSY_DESCR", 0x5); define("GG_STATUS_INVISIBLE", 0x14); define("GG_NOTIFY", 0x10); define("GG_NOTIFY_REPLY", 0xc); define("GG_NOTIFY_REPLY60", 0x11); define("GG_USER_NORMAL", 0x3); define("GG_USER_BLOCKED", 0x4); define("GG_SEND_MSG", 0xb); define("GG_CLASS_MSG", 0x4); define("GG_CLASS_CHAT", 0x8); define("GG_CLASS_ACK", 0x20); define("GG_SEND_MSG_ACK", 0x5); define("GG_ACK_DELIVERED", 0x2); define("GG_ACK_QUEUED", 0x3); define("GG_RECV_MSG", 0xa); define("GG_LOGIN_FAILED2", 0xb); define("GG_ACK_MBOXFULL", 0x4); define("DISCONNECTED", 0x100); define("GG_PUBDIR50_REQUEST", 0x14); define("GG_PUBDIR50_REPLY", 0xe); define("GG_PUBDIR50_SEARCH", 0x3); // // Getting a logon server // require_once S9Y_PEAR_PATH . 'HTTP/Request.php'; serendipity_request_start(); $req = new HTTP_Request('http://appmsg.gadu-gadu.pl:80/appsvc/appmsg.asp?fmnumber=<' . $numer_gg . '>'); if (PEAR::isError($req->sendRequest()) || $req->getResponseCode() != '200') { $error = PLUGIN_GGOPIS_MSG_NOCONNTOAPPMSG . $errno . " - " . $errstr . "\n"; serendipity_request_end(); return false; } else { $buf = $req->getResponseBody(); preg_match("/\\s([\\d\\.]{8,16})\\:([\\d]{1,5})\\s/", $buf, $adres); $host = $adres[1]; $port = $adres[2]; serendipity_request_end(); } // // Connecting to a server // require_once S9Y_PEAR_PATH . 'Net/Socket.php'; $conn = new Net_Socket(); if (!$conn->connect($host, $port, null, 10)) { $error = PLUGIN_GGOPIS_MSG_CONNERROR . ": {$errno} - {$errstr}\n\n"; return false; } // // Getting data from a server - // receiving a key needed to calculate // a hash from your password // if (!($data = $conn->read(12))) { $error = PLUGIN_GGOPIS_MSG_CONNUNEXPCLOSED . "\n\n"; $conn->disconnect(); return false; } $tab = unpack("Vtyp/Vrozmiar/Vklucz", $data); // Calculating a password hash $hash = $this->calculate_hash($haslo_gg, $tab['klucz']); $data = pack("VVVVVVvVvVvCCa" . strlen(""), GG_LOGIN60, 0x20 + strlen(""), $numer_gg, $hash, GG_STATUS_AVAIL, 0x20, 0, 0, 0, 0, 0, 0x14, 0xbe, ""); // Sending a password hash - logging to a GG server $conn->write($data); if (!($data1 = $conn->read(8))) { $error = PLUGIN_GGOPIS_MSG_UNKNOWNERROR . "\n"; $conn->disconnect(); return false; } // Checking a login status $tab = unpack("Vlogin_status/Vrozmiar", $data1); if ($tab['login_status'] != GG_LOGIN_OK) { $error = PLUGIN_GGOPIS_MSG_INCORRPASSWD . "\n\n"; $conn->disconnect(); return false; } // Sending a contact list with one contact $data = pack("VVVC", GG_NOTIFY, 5, $szukany_numer, GG_USER_NORMAL); if (!$conn->write($data)) { $error = PLUGIN_GGOPIS_MSG_SENDCONTACTSERROR . "\n\n"; $conn->disconnect(); return false; } // Receiving a packet with the next packet specification $gg_opis = ''; $data = $conn->read(8); if (strlen($data) > 0) { $tab = unpack("Vtyp/Vrozmiar", $data); // Pobranie pakietu opisu // DEBUG: echo $tab['rozmiar']; $data = $conn->read($tab['rozmiar']); if ($tab['rozmiar'] > 14) { $tablica = unpack("Iuin/Cstatus/Iremoteip/Sremoteport/Cversion/Cimagesize/Cunknown/Cdescription_size/a*description", $data); // Getting a status description, and converting it from CP1250 (that's how it's encoded) to UTF8 $gg_opis = $this->cp1250_to_utf8($tablica['description']); // Getting a status itself $gg_status_flaga = $tablica['status']; } else { $tablica = unpack("Iuin/Cstatus", $data); // Getting a status $gg_status_flaga = $tablica['status']; } if (empty($gg_opis)) { $gg_opis = PLUGIN_GGOPIS_MSG_NOSTATUSDESC; } // Choosing a status icon to display switch ($gg_status_flaga) { case GG_STATUS_NOT_AVAIL: case GG_STATUS_NOT_AVAIL_DESCR: $gg_status_widocznosc = 'gg11'; break; case GG_STATUS_AVAIL: case GG_STATUS_AVAIL_DESCR: $gg_status_widocznosc = 'gg12'; break; case GG_STATUS_BUSY: case GG_STATUS_BUSY_DESCR: $gg_status_widocznosc = 'gg13'; break; default: $gg_status_widocznosc = 'gg11'; } } else { $gg_opis = PLUGIN_GGOPIS_MSG_NOSTATUSDESC; } // Closing a connection to the server $conn->disconnect(); return $gg_opis; }
/** * Sends the request * * @access public * @param bool Whether to store response body in Response object property, * set this to false if downloading a LARGE file and using a Listener * @return mixed PEAR error on error, true otherwise */ function sendRequest($saveBody = true) { if (!is_a($this->_url, 'Net_URL')) { return PEAR::raiseError('No URL given', HTTP_REQUEST_ERROR_URL); } $host = isset($this->_proxy_host) ? $this->_proxy_host : $this->_url->host; $port = isset($this->_proxy_port) ? $this->_proxy_port : $this->_url->port; // 4.3.0 supports SSL connections using OpenSSL. The function test determines // we running on at least 4.3.0 if (strcasecmp($this->_url->protocol, 'https') == 0 AND function_exists('file_get_contents') AND extension_loaded('openssl')) { if (isset($this->_proxy_host)) { return PEAR::raiseError('HTTPS proxies are not supported', HTTP_REQUEST_ERROR_PROXY); } $host = 'ssl://' . $host; } // magic quotes may f**k up file uploads and chunked response processing $magicQuotes = ini_get('magic_quotes_runtime'); ini_set('magic_quotes_runtime', false); // RFC 2068, section 19.7.1: A client MUST NOT send the Keep-Alive // connection token to a proxy server... if (isset($this->_proxy_host) && !empty($this->_requestHeaders['connection']) && 'Keep-Alive' == $this->_requestHeaders['connection']) { $this->removeHeader('connection'); } $keepAlive = (HTTP_REQUEST_HTTP_VER_1_1 == $this->_http && empty($this->_requestHeaders['connection'])) || (!empty($this->_requestHeaders['connection']) && 'Keep-Alive' == $this->_requestHeaders['connection']); $sockets = &PEAR::getStaticProperty('HTTP_Request', 'sockets'); $sockKey = $host . ':' . $port; unset($this->_sock); // There is a connected socket in the "static" property? if ($keepAlive && !empty($sockets[$sockKey]) && !empty($sockets[$sockKey]->fp)) { $this->_sock =& $sockets[$sockKey]; $err = null; } else { $this->_notify('connect'); $this->_sock = new Net_Socket(); $err = $this->_sock->connect($host, $port, null, $this->_timeout, $this->_socketOptions); } PEAR::isError($err) or $err = $this->_sock->write($this->_buildRequest()); if (!PEAR::isError($err)) { if (!empty($this->_readTimeout)) { $this->_sock->setTimeout($this->_readTimeout[0], $this->_readTimeout[1]); } $this->_notify('sentRequest'); // Read the response $this->_response = new HTTP_Response($this->_sock, $this->_listeners); $err = $this->_response->process( $this->_saveBody && $saveBody, HTTP_REQUEST_METHOD_HEAD != $this->_method ); if ($keepAlive) { $keepAlive = (isset($this->_response->_headers['content-length']) || (isset($this->_response->_headers['transfer-encoding']) && strtolower($this->_response->_headers['transfer-encoding']) == 'chunked')); if ($keepAlive) { if (isset($this->_response->_headers['connection'])) { $keepAlive = strtolower($this->_response->_headers['connection']) == 'keep-alive'; } else { $keepAlive = 'HTTP/'.HTTP_REQUEST_HTTP_VER_1_1 == $this->_response->_protocol; } } } } ini_set('magic_quotes_runtime', $magicQuotes); if (PEAR::isError($err)) { return $err; } if (!$keepAlive) { $this->disconnect(); // Store the connected socket in "static" property } elseif (empty($sockets[$sockKey]) || empty($sockets[$sockKey]->fp)) { $sockets[$sockKey] =& $this->_sock; } // Check for redirection if ( $this->_allowRedirects AND $this->_redirects <= $this->_maxRedirects AND $this->getResponseCode() > 300 AND $this->getResponseCode() < 399 AND !empty($this->_response->_headers['location'])) { $redirect = $this->_response->_headers['location']; // Absolute URL if (preg_match('/^https?:\/\//i', $redirect)) { $this->_url = new Net_URL($redirect); $this->addHeader('Host', $this->_generateHostHeader()); // Absolute path } elseif ($redirect{0} == '/') { $this->_url->path = $redirect; // Relative path } elseif (substr($redirect, 0, 3) == '../' OR substr($redirect, 0, 2) == './') { if (substr($this->_url->path, -1) == '/') { $redirect = $this->_url->path . $redirect; } else { $redirect = dirname($this->_url->path) . '/' . $redirect; } $redirect = Net_URL::resolvePath($redirect); $this->_url->path = $redirect; // Filename, no path } else { if (substr($this->_url->path, -1) == '/') { $redirect = $this->_url->path . $redirect; } else { $redirect = dirname($this->_url->path) . '/' . $redirect; } $this->_url->path = $redirect; } $this->_redirects++; return $this->sendRequest($saveBody); // Too many redirects } elseif ($this->_allowRedirects AND $this->_redirects > $this->_maxRedirects) { return PEAR::raiseError('Too many redirects', HTTP_REQUEST_ERROR_REDIRECTS); } return true; }
/** * Sends the request * * @access public * @param bool Whether to store response body in Response object property, * set this to false if downloading a LARGE file and using a Listener * @return mixed PEAR error on error, true otherwise */ function sendRequest($saveBody = true) { if (!is_a($this->_url, 'Net_URL')) { return PEAR::raiseError('No URL given', HTTP_REQUEST_ERROR_URL); } $host = isset($this->_proxy_host) ? $this->_proxy_host : $this->_url->host; $port = isset($this->_proxy_port) ? $this->_proxy_port : $this->_url->port; if (strcasecmp($this->_url->protocol, 'https') == 0) { // Bug #14127, don't try connecting to HTTPS sites without OpenSSL if (version_compare(PHP_VERSION, '4.3.0', '<') || !extension_loaded('openssl')) { return PEAR::raiseError('Need PHP 4.3.0 or later with OpenSSL support for https:// requests', HTTP_REQUEST_ERROR_URL); } elseif (isset($this->_proxy_host)) { return PEAR::raiseError('HTTPS proxies are not supported', HTTP_REQUEST_ERROR_PROXY); } $host = 'ssl://' . $host; } // magic quotes may f**k up file uploads and chunked response processing $magicQuotes = ini_get('magic_quotes_runtime'); ini_set('magic_quotes_runtime', false); // RFC 2068, section 19.7.1: A client MUST NOT send the Keep-Alive // connection token to a proxy server... if (isset($this->_proxy_host) && !empty($this->_requestHeaders['connection']) && 'Keep-Alive' == $this->_requestHeaders['connection']) { $this->removeHeader('connection'); } $keepAlive = HTTP_REQUEST_HTTP_VER_1_1 == $this->_http && empty($this->_requestHeaders['connection']) || !empty($this->_requestHeaders['connection']) && 'Keep-Alive' == $this->_requestHeaders['connection']; $sockets =& PEAR::getStaticProperty('HTTP_Request', 'sockets'); $sockKey = $host . ':' . $port; unset($this->_sock); // There is a connected socket in the "static" property? if ($keepAlive && !empty($sockets[$sockKey]) && !empty($sockets[$sockKey]->fp)) { $this->_sock =& $sockets[$sockKey]; $err = null; } else { $this->_notify('connect'); $this->_sock = new Net_Socket(); $err = $this->_sock->connect($host, $port, null, $this->_timeout, $this->_socketOptions); } PEAR::isError($err) or $err = $this->_sock->write($this->_buildRequest()); if (!PEAR::isError($err)) { if (!empty($this->_readTimeout)) { $this->_sock->setTimeout($this->_readTimeout[0], $this->_readTimeout[1]); } $this->_notify('sentRequest'); // Read the response $this->_response = new HTTP_Response($this->_sock, $this->_listeners); $err = $this->_response->process($this->_saveBody && $saveBody, HTTP_REQUEST_METHOD_HEAD != $this->_method); if ($keepAlive) { $keepAlive = isset($this->_response->_headers['content-length']) || isset($this->_response->_headers['transfer-encoding']) && strtolower($this->_response->_headers['transfer-encoding']) == 'chunked'; if ($keepAlive) { if (isset($this->_response->_headers['connection'])) { $keepAlive = strtolower($this->_response->_headers['connection']) == 'keep-alive'; } else { $keepAlive = 'HTTP/' . HTTP_REQUEST_HTTP_VER_1_1 == $this->_response->_protocol; } } } } ini_set('magic_quotes_runtime', $magicQuotes); if (PEAR::isError($err)) { return $err; } if (!$keepAlive) { $this->disconnect(); // Store the connected socket in "static" property } elseif (empty($sockets[$sockKey]) || empty($sockets[$sockKey]->fp)) { $sockets[$sockKey] =& $this->_sock; } // Check for redirection if ($this->_allowRedirects and $this->_redirects <= $this->_maxRedirects and $this->getResponseCode() > 300 and $this->getResponseCode() < 399 and !empty($this->_response->_headers['location'])) { $redirect = $this->_response->_headers['location']; // Absolute URL if (preg_match('/^https?:\\/\\//i', $redirect)) { $this->_url = new Net_URL($redirect); $this->addHeader('Host', $this->_generateHostHeader()); // Absolute path } elseif ($redirect[0] == '/') { $this->_url->path = $redirect; // Relative path } elseif (substr($redirect, 0, 3) == '../' or substr($redirect, 0, 2) == './') { if (substr($this->_url->path, -1) == '/') { $redirect = $this->_url->path . $redirect; } else { $redirect = dirname($this->_url->path) . '/' . $redirect; } $redirect = Net_URL::resolvePath($redirect); $this->_url->path = $redirect; // Filename, no path } else { if (substr($this->_url->path, -1) == '/') { $redirect = $this->_url->path . $redirect; } else { $redirect = dirname($this->_url->path) . '/' . $redirect; } $this->_url->path = $redirect; } // handle cookes on redirect... if (!empty($this->_response->_cookies)) { foreach ($this->_response->_cookies as $c) { $this->_cookies[] = $c; } } if (isset($this->_requestHeaders['cookie'])) { unset($this->_requestHeaders['cookie']); } //print_r($this->_cookies); $cookies = array(); foreach ($this->_cookies as $c) { if (substr($this->_url->host, -1 * strlen($c['domain']) == $c['domain'])) { $cookies[$c['name']] = $c['value']; } } foreach ($cookies as $k => $v) { $this->addCookie($k, $v); } $this->_redirects++; return $this->sendRequest($saveBody); // Too many redirects } elseif ($this->_allowRedirects and $this->_redirects > $this->_maxRedirects) { return PEAR::raiseError('Too many redirects', HTTP_REQUEST_ERROR_REDIRECTS); } return true; }
function checkout() { //$repo_url, $repo_sub_path, $current_rev) { $repo_url = $this->state->get_repository_root(); $repo_sub_path = $this->state->get_repository_path(); $current_rev = $this->state->get_revision(); $src_path = $repo_url . $repo_sub_path; // figure out where to send our http REPORT request $bits = parse_url($repo_url . "/!svn/vcc/default"); $repo_host = $bits['host']; $repo_port = @$bits['port']; if (!$repo_port) { $repo_port = 80; } $repo_path = $bits['path']; $this->out("Connecting to host {$repo_host}:{$repo_port},<br>sending HTTP REPORT {$repo_path} for src-path {$src_path}.\n"); $xml = '<S:update-report send-all="true" xmlns:S="svn:"><S:src-path>' . $src_path . '</S:src-path><S:entry rev="' . $current_rev . '"></S:entry></S:update-report>'; $xml_len = strlen($xml); $query = "REPORT {$repo_path} HTTP/1.0\nHost: {$repo_host}\nContent-Type: text/xml\nContent-Length: {$xml_len}\nDepth: 0\n\n{$xml}"; $this->out($query); $sock = new Net_Socket(); $this->check_err($sock->connect($repo_host, $repo_port, null, 60, null)); // 60 sec timeout $this->check_err($sock->write($query)); // read response $this->out("Downloading patch ...\n"); $this->fp = fopen($this->diff_fn, "wt"); if (!$this->fp) { throw new Subversion_Failure("can't open {$this->diff_fn} file"); } $listeners = array($this); $this->received_bytes = 0; $resp = new HTTP_Response($sock, $listeners); $this->check_err($resp->process(false)); fclose($this->fp); $this->out("\nFinished downloading update ({$this->received_bytes} bytes).\n"); }