function call_epi($method) { if (!extension_loaded("xmlrpc")) { return $this->raiseError("xmlrpc extension is not loaded"); } $server_channel = $this->config->get('default_channel'); $channel = $this->_registry->getChannel($server_channel); if (!PEAR::isError($channel)) { $mirror = $this->config->get('preferred_mirror'); if ($channel->getMirror($mirror)) { if ($channel->supports('xmlrpc', $method, $mirror)) { $server_channel = $server_host = $mirror; // use the preferred mirror $server_port = $channel->getPort($mirror); } elseif (!$channel->supports('xmlrpc', $method)) { return $this->raiseError("Channel {$server_channel} does not " . "support xml-rpc method {$method}"); } } if (!isset($server_host)) { if (!$channel->supports('xmlrpc', $method)) { return $this->raiseError("Channel {$server_channel} does not support " . "xml-rpc method {$method}"); } else { $server_host = $server_channel; $server_port = $channel->getPort(); } } } else { return $this->raiseError("Unknown channel '{$server_channel}'"); } $params = func_get_args(); array_shift($params); $method = str_replace("_", ".", $method); $request = xmlrpc_encode_request($method, $params); if ($http_proxy = $this->config->get('http_proxy')) { $proxy = parse_url($http_proxy); $proxy_host = $proxy_port = $proxy_user = $proxy_pass = ''; $proxy_host = isset($proxy['host']) ? $proxy['host'] : null; if (isset($proxy['scheme']) && $proxy['scheme'] == 'https') { $proxy_host = 'https://' . $proxy_host; } $proxy_port = isset($proxy['port']) ? $proxy['port'] : null; $proxy_user = isset($proxy['user']) ? urldecode($proxy['user']) : null; $proxy_pass = isset($proxy['pass']) ? urldecode($proxy['pass']) : null; $fp = @fsockopen($proxy_host, $proxy_port); $use_proxy = true; if ($channel->getSSL()) { $server_host = "https://{$server_host}"; } } else { $use_proxy = false; $ssl = $channel->getSSL(); $fp = @fsockopen(($ssl ? 'ssl://' : '') . $server_host, $server_port); if (!$fp) { $server_host = "{$ssl}{$server_host}"; // for error-reporting } } if (!$fp && $http_proxy) { return $this->raiseError("PEAR_Remote::call: fsockopen(`{$proxy_host}', {$proxy_port}) failed"); } elseif (!$fp) { return $this->raiseError("PEAR_Remote::call: fsockopen(`{$server_host}', {$server_port}) failed"); } $len = strlen($request); $req_headers = "Host: {$server_host}:{$server_port}\r\n" . "Content-type: text/xml\r\n" . "Content-length: {$len}\r\n"; $username = $this->config->get('username'); $password = $this->config->get('password'); if ($username && $password) { $req_headers .= "Cookie: PEAR_USER={$username}; PEAR_PW={$password}\r\n"; $tmp = base64_encode("{$username}:{$password}"); $req_headers .= "Authorization: Basic {$tmp}\r\n"; } if ($this->cache !== null) { $maxAge = '?maxAge=' . $this->cache['lastChange']; } else { $maxAge = ''; } if ($use_proxy && $proxy_host != '' && $proxy_user != '') { $req_headers .= 'Proxy-Authorization: Basic ' . base64_encode($proxy_user . ':' . $proxy_pass) . "\r\n"; } if ($this->config->get('verbose') > 3) { print "XMLRPC REQUEST HEADERS:\n"; var_dump($req_headers); print "XMLRPC REQUEST BODY:\n"; var_dump($request); } if ($use_proxy && $proxy_host != '') { $post_string = "POST http://" . $server_host; if ($proxy_port > '') { $post_string .= ':' . $server_port; } } else { $post_string = "POST "; } $path = '/' . $channel->getPath('xmlrpc'); fwrite($fp, $post_string . $path . "{$maxAge} HTTP/1.0\r\n{$req_headers}\r\n{$request}"); $response = ''; $line1 = fgets($fp, 2048); if (!preg_match('!^HTTP/[0-9\\.]+ (\\d+) (.*)!', $line1, $matches)) { return $this->raiseError("PEAR_Remote: invalid HTTP response from XML-RPC server"); } switch ($matches[1]) { case "200": // OK break; case "304": // Not Modified return $this->cache['content']; case "401": // Unauthorized if ($username && $password) { return $this->raiseError("PEAR_Remote ({$server_host}:{$server_port}) " . ": authorization failed", 401); } else { return $this->raiseError("PEAR_Remote ({$server_host}:{$server_port}) " . ": authorization required, please log in first", 401); } default: return $this->raiseError("PEAR_Remote ({$server_host}:{$server_port}) : " . "unexpected HTTP response", (int) $matches[1], null, null, "{$matches['1']} {$matches['2']}"); } while (trim(fgets($fp, 2048)) != '') { } // skip rest of headers while ($chunk = fread($fp, 10240)) { $response .= $chunk; } fclose($fp); if ($this->config->get('verbose') > 3) { print "XMLRPC RESPONSE:\n"; var_dump($response); } $ret = xmlrpc_decode($response); if (is_array($ret) && isset($ret['__PEAR_TYPE__'])) { if ($ret['__PEAR_TYPE__'] == 'error') { if (isset($ret['__PEAR_CLASS__'])) { $class = $ret['__PEAR_CLASS__']; } else { $class = "PEAR_Error"; } if ($ret['code'] === '') { $ret['code'] = null; } if ($ret['message'] === '') { $ret['message'] = null; } if ($ret['userinfo'] === '') { $ret['userinfo'] = null; } if (strtolower($class) == 'db_error') { $ret = $this->raiseError(PEAR::errorMessage($ret['code']), $ret['code'], null, null, $ret['userinfo']); } else { $ret = $this->raiseError($ret['message'], $ret['code'], null, null, $ret['userinfo']); } } } elseif (is_array($ret) && sizeof($ret) == 1 && isset($ret[0]) && is_array($ret[0]) && !empty($ret[0]['faultString']) && !empty($ret[0]['faultCode'])) { extract($ret[0]); $faultString = "XML-RPC Server Fault: " . str_replace("\n", " ", $faultString); return $this->raiseError($faultString, $faultCode); } elseif (is_array($ret) && sizeof($ret) == 2 && !empty($ret['faultString']) && !empty($ret['faultCode'])) { extract($ret); $faultString = "XML-RPC Server Fault: " . str_replace("\n", " ", $faultString); return $this->raiseError($faultString, $faultCode); } return $ret; }
function call_epi($method) { do { if (extension_loaded("xmlrpc")) { break; } if (OS_WINDOWS) { $ext = 'dll'; } elseif (PHP_OS == 'HP-UX') { $ext = 'sl'; } elseif (PHP_OS == 'AIX') { $ext = 'a'; } else { $ext = 'so'; } $ext = OS_WINDOWS ? 'dll' : 'so'; @dl("xmlrpc-epi.{$ext}"); if (extension_loaded("xmlrpc")) { break; } @dl("xmlrpc.{$ext}"); if (extension_loaded("xmlrpc")) { break; } return $this->raiseError("unable to load xmlrpc extension"); } while (false); $params = func_get_args(); array_shift($params); $method = str_replace("_", ".", $method); $request = xmlrpc_encode_request($method, $params); $server_host = $this->config->get("master_server"); if (empty($server_host)) { return $this->raiseError("PEAR_Remote::call: no master_server configured"); } $server_port = 80; if ($http_proxy = $this->config->get('http_proxy')) { $proxy = parse_url($http_proxy); $proxy_host = $proxy_port = $proxy_user = $proxy_pass = ''; $proxy_host = @$proxy['host']; $proxy_port = @$proxy['port']; $proxy_user = @urldecode(@$proxy['user']); $proxy_pass = @urldecode(@$proxy['pass']); $fp = @fsockopen($proxy_host, $proxy_port); $use_proxy = true; } else { $use_proxy = false; $fp = @fsockopen($server_host, $server_port); } if (!$fp && $http_proxy) { return $this->raiseError("PEAR_Remote::call: fsockopen(`{$proxy_host}', {$proxy_port}) failed"); } elseif (!$fp) { return $this->raiseError("PEAR_Remote::call: fsockopen(`{$server_host}', {$server_port}) failed"); } $len = strlen($request); $req_headers = "Host: {$server_host}:{$server_port}\r\n" . "Content-type: text/xml\r\n" . "Content-length: {$len}\r\n"; $username = $this->config->get('username'); $password = $this->config->get('password'); if ($username && $password) { $req_headers .= "Cookie: PEAR_USER={$username}; PEAR_PW={$password}\r\n"; $tmp = base64_encode("{$username}:{$password}"); $req_headers .= "Authorization: Basic {$tmp}\r\n"; } if ($this->cache !== null) { $maxAge = '?maxAge=' . $this->cache['lastChange']; } else { $maxAge = ''; } if ($use_proxy && $proxy_host != '' && $proxy_user != '') { $req_headers .= 'Proxy-Authorization: Basic ' . base64_encode($proxy_user . ':' . $proxy_pass) . "\r\n"; } if ($this->config->get('verbose') > 3) { print "XMLRPC REQUEST HEADERS:\n"; var_dump($req_headers); print "XMLRPC REQUEST BODY:\n"; var_dump($request); } if ($use_proxy && $proxy_host != '') { $post_string = "POST http://" . $server_host; if ($proxy_port > '') { $post_string .= ':' . $server_port; } } else { $post_string = "POST "; } fwrite($fp, $post_string . "/xmlrpc.php{$maxAge} HTTP/1.0\r\n{$req_headers}\r\n{$request}"); $response = ''; $line1 = fgets($fp, 2048); if (!preg_match('!^HTTP/[0-9\\.]+ (\\d+) (.*)!', $line1, $matches)) { return $this->raiseError("PEAR_Remote: invalid HTTP response from XML-RPC server"); } switch ($matches[1]) { case "200": // OK break; case "304": // Not Modified return $this->cache['content']; case "401": // Unauthorized if ($username && $password) { return $this->raiseError("PEAR_Remote: authorization failed", 401); } else { return $this->raiseError("PEAR_Remote: authorization required, please log in first", 401); } default: return $this->raiseError("PEAR_Remote: unexpected HTTP response", (int) $matches[1], null, null, "{$matches['1']} {$matches['2']}"); } while (trim(fgets($fp, 2048)) != '') { } // skip rest of headers while ($chunk = fread($fp, 10240)) { $response .= $chunk; } fclose($fp); if ($this->config->get('verbose') > 3) { print "XMLRPC RESPONSE:\n"; var_dump($response); } $ret = xmlrpc_decode($response); if (is_array($ret) && isset($ret['__PEAR_TYPE__'])) { if ($ret['__PEAR_TYPE__'] == 'error') { if (isset($ret['__PEAR_CLASS__'])) { $class = $ret['__PEAR_CLASS__']; } else { $class = "PEAR_Error"; } if ($ret['code'] === '') { $ret['code'] = null; } if ($ret['message'] === '') { $ret['message'] = null; } if ($ret['userinfo'] === '') { $ret['userinfo'] = null; } if (strtolower($class) == 'db_error') { $ret = $this->raiseError(PEAR::errorMessage($ret['code']), $ret['code'], null, null, $ret['userinfo']); } else { $ret = $this->raiseError($ret['message'], $ret['code'], null, null, $ret['userinfo']); } } } elseif (is_array($ret) && sizeof($ret) == 1 && isset($ret[0]) && is_array($ret[0]) && !empty($ret[0]['faultString']) && !empty($ret[0]['faultCode'])) { extract($ret[0]); $faultString = "XML-RPC Server Fault: " . str_replace("\n", " ", $faultString); return $this->raiseError($faultString, $faultCode); } return $ret; }