function query() { $args = func_get_args(); $method = array_shift($args); $request = new IXR_Request($method, $args); $length = $request->getLength(); $xml = $request->getXml(); $r = "\r\n"; $request = "POST {$this->path} HTTP/1.0{$r}"; $this->headers['Host'] = $this->server; $this->headers['Content-Type'] = 'text/xml'; $this->headers['User-Agent'] = $this->useragent; $this->headers['Content-Length'] = $length; foreach ($this->headers as $header => $value) { $request .= "{$header}: {$value}{$r}"; } $request .= $r; $request .= $xml; // Now send the request if ($this->debug) { echo '<pre class="ixr_request">' . htmlspecialchars($request) . "\n</pre>\n\n"; } if ($this->timeout) { $fp = @fsockopen($this->server, $this->port, $errno, $errstr, $this->timeout); } else { $fp = @fsockopen($this->server, $this->port, $errno, $errstr); } if (!$fp) { $this->error = new IXR_Error(-32300, "transport error - could not open socket: {$errno} {$errstr}"); return false; } fputs($fp, $request); $contents = ''; $debug_contents = ''; $gotFirstLine = false; $gettingHeaders = true; while (!feof($fp)) { $line = fgets($fp, 4096); if (!$gotFirstLine) { // Check line for '200' if (strstr($line, '200') === false) { $this->error = new IXR_Error(-32301, 'transport error - HTTP status code was not 200'); return false; } $gotFirstLine = true; } if (trim($line) == '') { $gettingHeaders = false; } if (!$gettingHeaders) { $contents .= trim($line); } if ($this->debug) { $debug_contents .= $line; } } if ($this->debug) { echo '<pre class="ixr_response">' . htmlspecialchars($debug_contents) . "\n</pre>\n\n"; } // Now parse what we've got back $this->message = new IXR_Message($contents); if (!$this->message->parse()) { // XML error $this->error = new IXR_Error(-32700, 'parse error. not well formed'); return false; } // Is the message a fault? if ($this->message->messageType == 'fault') { $this->error = new IXR_Error($this->message->faultCode, $this->message->faultString); return false; } // Message must be OK return true; }
/** * Set the query to send to the XML-RPC Server * @since 0.1.0 */ function query() { $args = func_get_args(); $method = array_shift($args); $request = new IXR_Request($method, $args); $length = $request->getLength(); $xml = $request->getXml(); if ($this->debug) { echo '<pre>' . htmlspecialchars($xml) . "\n</pre>\n\n"; } //This is where we deviate from the normal query() //Rather than open a normal sock, we will actually use the cURL //extensions to make the calls, and handle the SSL stuff. //Since 04Aug2004 (0.1.3) - Need to include the port (duh...) //Since 06Oct2004 (0.1.4) - Need to include the colon!!! // (I swear I've fixed this before... ESP in live... But anyhu...) $curl = curl_init('https://' . $this->server . ':' . $this->port . $this->path); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //Since 23Jun2004 (0.1.2) - Made timeout a class field curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout); if ($this->debug) { curl_setopt($curl, CURLOPT_VERBOSE, 1); } curl_setopt($curl, CURLOPT_HEADER, 1); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $xml); curl_setopt($curl, CURLOPT_PORT, $this->port); curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml", "Content-length: {$length}")); // Process the SSL certificates, etc. to use if (!($this->_certFile === false)) { // We have a certificate file set, so add these to the cURL handler curl_setopt($curl, CURLOPT_SSLCERT, $this->_certFile); curl_setopt($curl, CURLOPT_SSLKEY, $this->_keyFile); if ($this->debug) { echo "SSL Cert at : " . $this->_certFile . "\n"; echo "SSL Key at : " . $this->_keyFile . "\n"; } // See if we need to give a passphrase if (!($this->_passphrase === '')) { curl_setopt($curl, CURLOPT_SSLCERTPASSWD, $this->_passphrase); } if ($this->_caFile === false) { // Don't verify their certificate, as we don't have a CA to verify against curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); } else { // Verify against a CA curl_setopt($curl, CURLOPT_CAINFO, $this->_caFile); } } // Call cURL to do it's stuff and return us the content $contents = curl_exec($curl); curl_close($curl); // Check for 200 Code in $contents if (!strstr($contents, '200 OK')) { //There was no "200 OK" returned - we failed $this->error = new IXR_Error(-32300, 'transport error - HTTP status code was not 200'); return false; } if ($this->debug) { echo '<pre>' . htmlspecialchars($contents) . "\n</pre>\n\n"; } // Now parse what we've got back // Since 20Jun2004 (0.1.1) - We need to remove the headers first // Why I have only just found this, I will never know... // So, remove everything before the first < $contents = substr($contents, strpos($contents, '<')); $this->message = new IXR_Message($contents); if (!$this->message->parse()) { // XML error $this->error = new IXR_Error(-32700, 'parse error. not well formed'); return false; } // Is the message a fault? if ($this->message->messageType == 'fault') { $this->error = new IXR_Error($this->message->faultCode, $this->message->faultString); return false; } // Message must be OK return true; }
function query() { $args = func_get_args(); $method = array_shift($args); $request = new IXR_Request($method, $args); $length = $request->getLength(); $xml = $request->getXml(); $r = "\r\n"; $request = "POST {$this->path} HTTP/1.0{$r}"; $request .= "Host: {$this->server}{$r}"; $request .= "Content-Type: text/xml{$r}"; $request .= "User-Agent: {$this->useragent}{$r}"; $request .= "Content-length: {$length}{$r}{$r}"; $request .= $xml; // Now send the request if ($this->debug) { echo '<pre>' . htmlspecialchars($request) . "\n</pre>\n\n"; } $fp = @fsockopen($this->server, $this->port); if (!$fp) { $this->error = new IXR_Error(-32300, 'transport error - could not open socket'); return false; } fputs($fp, $request); $contents = ''; $gotFirstLine = false; $gettingHeaders = true; while (!feof($fp)) { $line = fgets($fp, 4096); if (!$gotFirstLine) { // Check line for '200' if (strstr($line, '200') === false) { $this->error = new IXR_Error(-32300, 'transport error - HTTP status code was not 200'); return false; } $gotFirstLine = true; } if (trim($line) == '') { $gettingHeaders = false; } if (!$gettingHeaders) { $contents .= trim($line) . "\n"; } } if ($this->debug) { echo '<pre>' . htmlspecialchars($contents) . "\n</pre>\n\n"; } // Now parse what we've got back $this->message = new IXR_Message($contents); if (!$this->message->parse()) { // XML error $this->error = new IXR_Error(-32700, 'parse error. not well formed'); return false; } // Is the message a fault? if ($this->message->messageType == 'fault') { $this->error = new IXR_Error($this->message->faultCode, $this->message->faultString); return false; } // Message must be OK return true; }
function query() { $args = func_get_args(); $method = array_shift($args); $request = new IXR_Request($method, $args); $length = $request->getLength(); $xml = $request->getXml(); $r = "\r\n"; $request = "POST {$this->path} HTTP/1.0{$r}"; $this->headers['Host'] = preg_replace('#^ssl://#', '', $this->server); $this->headers['Content-Type'] = 'text/xml'; $this->headers['User-Agent'] = $this->useragent; $this->headers['Content-Length'] = $length; if (class_exists('WP_Http')) { $args = array('method' => 'POST', 'body' => $xml, 'headers' => $this->headers, 'sslverify' => false); if ($this->timeout) { $args['timeout'] = $this->timeout; } $http = new WP_Http(); if ($this->ssl) { $url = sprintf('https://%s%s', $this->server, $this->path); } else { $url = sprintf('http://%s%s', $this->server, $this->path); } $result = $http->request($url, $args); if (is_wp_error($result)) { foreach ($result->errors as $type => $messages) { $this->error = new IXR_Error(-32702, sprintf('WP_Http error: %s, %s', $type, $messages[0])); break; } return false; } else { if ($result['response']['code'] > 299 || $result['response']['code'] < 200) { $this->error = new IXR_Error(-32701, sprintf('Server rejected request (HTTP response: %s %s)', $result['response']['code'], $result['response']['message'])); return false; } } // Now parse what we've got back $this->message = new IXR_Message($result['body']); } else { foreach ($this->headers as $header => $value) { $request .= "{$header}: {$value}{$r}"; } $request .= $r; $request .= $xml; // Now send the request if ($this->ssl) { $host = 'ssl://' . $this->server; } else { $host = $this->server; } if ($this->timeout) { $fp = @fsockopen($host, $this->port, $errno, $errstr, $this->timeout); } else { $fp = @fsockopen($host, $this->port, $errno, $errstr); } if (!$fp) { $this->error = new IXR_Error(-32300, "Transport error - could not open socket: {$errno} {$errstr}"); return false; } fputs($fp, $request); $contents = ''; $gotFirstLine = false; $gettingHeaders = true; while (!feof($fp)) { $line = fgets($fp, 4096); if (!$gotFirstLine) { // Check line for '200' if (strstr($line, '200') === false) { $this->error = new IXR_Error(-32301, 'transport error - HTTP status code was not 200'); return false; } $gotFirstLine = true; } if (trim($line) == '') { $gettingHeaders = false; } if (!$gettingHeaders) { $contents .= trim($line); } } // Now parse what we've got back $this->message = new IXR_Message($contents); } if (!$this->message->parse()) { // XML error $this->error = new IXR_Error(-32700, 'parse error. not well formed'); return false; } // Is the message a fault? if ($this->message->messageType == 'fault') { $this->error = new IXR_Error($this->message->faultCode, $this->message->faultString); return false; } // Message must be OK return true; }
function query() { $args = func_get_args(); $method = array_shift($args); $request = new IXR_Request($method, $args); $length = $request->getLength(); $xml = $request->getXml(); $r = "\r\n"; $request = "POST {$this->path} HTTP/1.0{$r}"; $request .= "Host: {$this->server}{$r}"; $request .= "Content-Type: text/xml{$r}"; $request .= "User-Agent: {$this->useragent}{$r}"; // Accept gzipped response if zlib and if php4.3+ (fgets turned binary safe) if (extension_loaded('zlib') && preg_match('#^(4\\.[3-9])|([5-9])#', phpversion())) { $request .= "Accept-Encoding: gzip{$r}"; } $request .= "Content-length: {$length}{$r}{$r}"; $request .= $xml; // Now send the request if ($this->debug) { echo '<pre>' . htmlspecialchars($request) . "\n</pre>\n\n"; } // This is to find out when the script unexpectedly dies due to fsockopen ob_start(NULL, 2048); echo "Trying to connect to an RPC Server..."; $fp = is_callable('fsockopen') ? fsockopen($this->server, $this->port, $errno, $errstr, 45) : false; ob_end_clean(); if (!$fp) { $this->error = new IXR_Error(-32300, 'transport error - could not open socket (' . $errstr . ')'); return false; } fputs($fp, $request); $contents = ''; $gotFirstLine = false; $gettingHeaders = true; $is_gzipped = false; while (!feof($fp)) { $line = fgets($fp, 4096); if (!$gotFirstLine) { // Check line for '200' if (strstr($line, '200') === false) { $this->error = new IXR_Error(-32300, 'transport error - HTTP status code was not 200'); return false; } $gotFirstLine = true; } if ($gettingHeaders && trim($line) == '') { $gettingHeaders = false; continue; } if (!$gettingHeaders) { // We do a binary comparison of the first two bytes, see // rfc1952, to check wether the content is gzipped. if ($contents == '' && strncmp($line, "‹", 2) === 0) { $is_gzipped = true; } $contents .= $is_gzipped ? $line : trim($line) . "\n"; } } # if gzipped, strip the 10 byte header, and pass it to gzinflate (rfc1952) if ($is_gzipped) { $contents = gzinflate(substr($contents, 10)); //simulate trim() for each line; don't know why, but it won't work otherwise $contents = preg_replace('#^[\\x20\\x09\\x0A\\x0D\\x00\\x0B]*(.*)[\\x20\\x09\\x0A\\x0D\\x00\\x0B]*$#m', '\\1', $contents); } if ($this->debug) { echo '<pre>' . htmlspecialchars($contents) . "\n</pre>\n\n"; } // Now parse what we've got back $this->message = new IXR_Message($contents); if (!$this->message->parse()) { // XML error $this->error = new IXR_Error(-32700, 'parse error. not well formed'); return false; } // Is the message a fault? if ($this->message->messageType == 'fault') { $this->error = new IXR_Error($this->message->faultCode, $this->message->faultString); return false; } // Message must be OK return true; }
/** * Set the query to send to the XML-RPC Server * @since 0.1.0 */ function query() { $args = func_get_args(); $method = array_shift($args); $request = new IXR_Request($method, $args); $length = $request->getLength(); $xml = $request->getXml(); if ($this->debug) { echo '<pre>' . htmlspecialchars($xml) . "\n</pre>\n\n"; } //This is where we deviate from the normal query() //Rather than open a normal sock, we will actually use the cURL //extensions to make the calls, and handle the SSL stuff. //Since 04Aug2004 (0.1.3) - Need to include the port (duh...) //Since 06Oct2004 (0.1.4) - Need to include the colon!!! // (I swear I've fixed this before... ESP in live... But anyhu...) $curl = curl_init('https://' . $this->server . ':' . $this->port . $this->path); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //Since 23Jun2004 (0.1.2) - Made timeout a class field curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout); if ($this->debug) { curl_setopt($curl, CURLOPT_VERBOSE, 1); } curl_setopt($curl, CURLOPT_HEADER, 1); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $xml); curl_setopt($curl, CURLOPT_PORT, $this->port); curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml", "Content-length: {$length}")); //Process the SSL certificates, etc. to use if (!($this->_certFile === false)) { //We have a certificate file set, so add these to the cURL handler curl_setopt($curl, CURLOPT_SSLCERT, $this->_certFile); curl_setopt($curl, CURLOPT_SSLKEY, $this->_keyFile); if ($this->debug) { echo "SSL Cert at : " . $this->_certFile . "\n"; echo "SSL Key at : " . $this->_keyFile . "\n"; } //See if we need to give a passphrase if (!($this->_passphrase === '')) { curl_setopt($curl, CURLOPT_SSLCERTPASSWD, $this->_passphrase); } } if ($this->_caFile === false) { //Don't verify their certificate, as we don't have a CA to verify against curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); } else { //Verify against a CA curl_setopt($curl, CURLOPT_CAINFO, $this->_caFile); } //Call cURL to do it's stuff and return us the content $response = curl_exec($curl); curl_close($curl); $contents = ''; $gotFirstLine = false; $gettingHeaders = true; $lines = explode("\n", $response); foreach ($lines as $line) { $line = rtrim($line); if (!$gotFirstLine) { // get response code list($httpVersion, $responseCode, $responseMessage) = split(" ", rtrim($line), 3); $gotFirstLine = true; } if (trim($line) == '') { $gettingHeaders = false; } if (!$gettingHeaders) { $contents .= trim($line); } } // Check line for '200' if ($responseCode != '200') { $this->error = new IXR_Error(-32300, 'transport error - HTTP status code was ' . $responseCode . ' (' . $responseMessage . ')'); return false; } if ($this->debug) { echo '<pre>' . htmlspecialchars($contents) . "\n</pre>\n\n"; } $this->message = new IXR_Message($contents); if (!$this->message->parse()) { // XML error $this->error = new IXR_Error(-32700, 'parse error. not well formed'); error_log(print_r($this, true)); return false; } // Is the message a fault? if ($this->message->messageType == 'fault') { $this->error = new IXR_Error($this->message->faultCode, $this->message->faultString); return false; } // Message must be OK return true; }
function query($args) { //$args = func_get_args(); $method = array_shift($args); $request = new IXR_Request($method, $args); $length = $request->getLength(); $xml = $request->getXml(); if ($this->debug) { echo '<pre>' . htmlspecialchars($xml) . "\n</pre>\n\n"; } $curl = curl_init('http://' . $this->server . ':' . $this->port . $this->path); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //Since 23Jun2004 (0.1.2) - Made timeout a class field curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout); if ($this->debug) { curl_setopt($curl, CURLOPT_VERBOSE, 1); } curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookiePath); curl_setopt($curl, CURLOPT_COOKIEJAR, $this->cookiePath); curl_setopt($curl, CURLOPT_HEADER, 1); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $xml); curl_setopt($curl, CURLOPT_PORT, $this->port); curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml", "Content-length: {$length}")); // Call cURL to do it's stuff and return us the content $contents = curl_exec($curl); curl_close($curl); // Check for 200 Code in $contents if (!strstr($contents, '200 OK')) { //There was no "200 OK" returned - we failed $this->error = new IXR_Error(-32300, 'transport error - HTTP status code was not 200'); return false; } if ($this->debug) { echo '<pre>' . htmlspecialchars($contents) . "\n</pre>\n\n"; } // Now parse what we've got back // Since 20Jun2004 (0.1.1) - We need to remove the headers first // Why I have only just found this, I will never know... // So, remove everything before the first < $contents = substr($contents, strpos($contents, '<')); $this->message = new IXR_Message($contents); if (!$this->message->parse()) { // XML error $this->error = new IXR_Error(-32700, 'parse error. not well formed'); return false; } // Is the message a fault? if ($this->message->messageType == 'fault') { $this->error = new IXR_Error($this->message->faultCode, $this->message->faultString); return false; } // Message must be OK return true; }
function open() { global $xml_rpc_validator_errors; $args = func_get_args(); $method = array_shift($args); $request = new IXR_Request($method, $args); $length = $request->getLength(); $xml = $request->getXml(); $this->headers['Content-Type'] = 'text/xml'; $this->headers['User-Agent'] = $this->useragent; $this->headers['Content-Length'] = $length; $this->headers['Accept'] = '*/*'; if (!empty($this->HTTP_auth_user_login)) { xml_rpc_validator_logIO("I", "HTTP auth header set " . $this->HTTP_auth_user_login . ':' . $this->HTTP_auth_user_pass); $this->headers['Authorization'] = 'Basic ' . base64_encode($this->HTTP_auth_user_login . ':' . $this->HTTP_auth_user_pass); } $requestParameter = array(); $requestParameter = array('headers' => $this->headers); $requestParameter['method'] = 'POST'; $requestParameter['body'] = $xml; $requestParameter['timeout'] = REQUEST_HTTP_TIMEOUT; xml_rpc_validator_logIO("I", "HTTP Request headers: " . print_r($this->headers, TRUE)); xml_rpc_validator_logIO("I", "XML-RPC Request: "); if (strpos($method, 'metaWeblog.newMediaObject') === false) { //do not log the whole picture upload request document xml_rpc_validator_logXML("I", $xml); } else { xml_rpc_validator_logXML("I", substr($xml, 0, 100)); } $xmlrpc_request = new WP_Http(); $this->response = $xmlrpc_request->request($this->URL, $requestParameter); xml_rpc_validator_logIO("O", "Response details below ->"); //xml_rpc_validator_logIO("O", "RAW response: ". print_r ($this->response, TRUE)); xml_rpc_validator_logIO("O", "HTTP Response code: " . print_r($this->response['response']['code'] . ' - ' . $this->response['response']['message'], TRUE)); xml_rpc_validator_logIO("O", "HTTP Response headers: " . print_r($this->response['headers'], TRUE)); // Handle error here. if (is_wp_error($this->response)) { return $this->response; } elseif (strcmp($this->response['response']['code'], '200') != 0) { return new WP_Error($this->response['response']['code'], $this->response['response']['message']); } xml_rpc_validator_logIO("O", "HTTP Response Body:", TRUE); $contents = trim($this->response['body']); xml_rpc_validator_logXML("O", $contents); if (empty($contents)) { $error_obj = $xml_rpc_validator_errors['MISSING_XMLRPC_METHODS']; $this->error = new WP_Error($error_obj['code'], $error_obj['message']); return $this->error; } else { //check the first character if ($contents[0] !== '<') { $error_obj = $xml_rpc_validator_errors['XMLRPC_RESPONSE_MALFORMED_1']; $this->error = new WP_Error($error_obj['code'], $error_obj['message']); return $this->error; } } //check the characters within the response if ($this->check_UTF8($contents) !== true) { $error_obj = $xml_rpc_validator_errors['XMLRPC_RESPONSE_CONTAINS_INVALID_CHARACTERS']; $this->error = new WP_Error($error_obj['code'], $error_obj['message']); return $this->error; } // Now parse what we've got back $this->message = new IXR_Message($contents); if (!$this->message->parse()) { // XML error $error_obj = $xml_rpc_validator_errors['XMLRPC_RESPONSE_MALFORMED_2']; $this->error = new WP_Error($error_obj['code'], $error_obj['message']); return $this->error; } // Is the message a fault? if ($this->message->messageType == 'fault') { $this->error = new WP_Error($this->message->faultCode, $this->message->faultString); return $this->error; } return $this->message->params[0]; }
function queryIgnoreResult() { $args = func_get_args(); $method = array_shift($args); if (!$this->socket || $this->protocol == 0) { $this->error = new IXR_Error(-32300, 'transport error - client not initialized'); return false; } $request = new IXR_Request($method, $args); // Check if the request is greater than 512 Kbytes to avoid errors // If the method is system.multicall, make two calls (possibly recursively) if (($size = $request->getLength()) > 1024 * 1024 - 8) { if ($method = 'system.multicall' && isset($args[0])) { $count = count($args[0]); // If count is 1, query cannot be reduced if ($count < 2) { $this->error = new IXR_Error(-32300, "transport error - request too large ({$size})"); return false; } $length = floor($count / 2); $args1 = array_slice($args[0], 0, $length); $args2 = array_slice($args[0], $length, $count - $length); $res1 = $this->queryIgnoreResult('system.multicall', $args1); $res2 = $this->queryIgnoreResult('system.multicall', $args2); return $res1 && $res2; } else { $this->error = new IXR_Error(-32300, "transport error - request too large ({$size})"); return false; } } // Send request $ok = $this->sendRequest($request); if (!$ok) { $this->error = new IXR_Error(-32300, 'transport error - connection interrupted!'); return false; } return true; }