<?php
/************************************************************
* *
* Pindorama Core Configuration v. 0.11 *
* Guilherme Capilé <hide@address.com> *
* *
************************************************************/
/* construção de um cliente XML-RPC simples para a publicação de documentos e
comunicação com outros serviços/servidores. Leva em conta apenas três
parâmetros e retorna a resposta como uma string em xml:
$xml_string = xmlrpc_c( $server, $msg, $headers );
caso queira passar os valores da resposta para variáveis em php, é fornecido
um método para analisá-las:
$result = xmlrpc_decode( $xml_string );
dependendo do tipo de resposta dado pelo servidor XML-RPC, o resultado pode
ser do tipo array, string ou boolean
versão 0.1 - 08/2003
*/
require_once($c["components"]."xmlrpc/common.php");
function xmlrpc_request ( $server, $method, $params = NULL, $headers = "" )
{
global $xmlrpc_err, $c, $l;
/* gera uma requisição xmlrpc_c com o método $method e os parâmetros
passados na array $params */
if (is_array($params)) {
$param = "";
foreach ($params as $tmp) {
$param .= "<param><value>".xmlrpc_values($tmp)."</value></param>";
}
} else if (isset($params)) {
$param = "<param><value>".xmlrpc_values($params)."</value></param>";
}
$msg = <<<FIM
<?xml version="1.0" encoding="iso-8859-1" ?>
<methodCall>
<methodName>{$method}</methodName>
<params>{$param}</params>
</methodCall>
FIM;
$result = xmlrpc_c ( $server, $msg, $headers );
if ($result == "") {
$xmlrpc_err["faultCode"] = 1;
return false;
}
$res = xmlrpc_decode($result);
if ($xmlrpc_err["faultCode"] > 0) {
return false;
}
return $res;
}
function xmlrpc_c ( $server, $msg, $headers = "" )
{
global $xmlrpc_err, $c, $l;
/* configurações básicas */
$x = $c["xmlrpc_c"];
$s = array();
/* extrai as informações da string de texto do servidor:
[scheme][user][pass][host][port][path][query][fragment]?
http://xmlrpc_robot:hide@address.com:80/index.html?teste=2
$s["scheme"] = "http";
$s["user"] = "xmlrpc_robot";
$s["pass"] = "xmlrpc_password";
$s["host"] = "pindorama";
$s["port"] = "80";
$s["path"] = "/index.html";
$s["query"] = "teste=2";
$s["fragment"]= ""; //?
pelo menos [scheme][host][path] são necessários
*/
if (preg_match("/\:\/\//", $server)) {
$s = parse_url ($server);
if ($s["port"] != "") {
$s["port"] = ":".$s["port"];
}
}
$x = array_merge($x, $s);
/* tratando da mensagem */
if (!preg_match("/\<\?xml /", $msg)) {
$msg = '<?xml version="1.0" encoding="iso-8859-1" ?>'.$msg;
}
/* tratando dos headers */
if ($headers != "") {
$old_headers = $headers;
if (!preg_match("/POST [^ ]* HTTP\/[0-9]\.[0-9]/", $headers)) {
$h = "POST ".$x["path"]." HTTP/1.0\r\n";
}
if (!preg_match("/Host\: .*\r?\n/", $headers)){
$h .= "Host: ".$x["host"]."\r\n";
}
if (!preg_match("/User\-Agent\: .*\r?\n/", $headers)) {
$h .= "User-Agent: ".$x["user-agent"]."\r\n";
}
if (!preg_match("/Content\-Type\: .*\r?\n/", $headers)) {
$h .= "Content-Type: text/xml\r\n";
} else {
$headers = preg_replace("/Content\-Type\: .*\r?\n/", "Content-Type: text/xml\r\n", $headers);
}
if (!preg_match("/Content\-Length\: [0-9]*\r?\n/", $headers)) {
$h .= "Content-Length: ".strlen($msg)."\r\n";
}
$headers = $h.$headers;
} else {
$headers = "POST ".$x["path"]." HTTP/1.0\r\n".
"Host: ".$x["host"]."\r\n".
"User-Agent: ".$x["user-agent"]."\r\n".
"Content-Type: text/xml\r\n".
"Content-Length: ".strlen($msg)."\r\n";
}
/* utilizando o curl para enviar as informações */
$curl = curl_init($x["scheme"]."://". $x["host"].$x["port"].$x["path"]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// guarda os resultados em uma variável
curl_setopt($curl, CURLOPT_USERAGENT, $x["user-agent"]);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $msg);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
/* SSL -- não está configurado
if ($cert) {
curl_setopt($curl, CURLOPT_SSLCERT, $cert);
}
if ($certpass){
curl_setopt($curl, CURLOPT_SSLCERTPASSWD,$certpass);
}
*/
if ($x["user"] && $x["pass"]) {
curl_setopt($curl, CURLOPT_USERPWD,$x["user"].":".$x["pass"]);
}
$result = curl_exec($curl);
curl_close($curl);
if (!$result) {
return xmlrpc_error ( 0, $l["xmlrpc"]["connection_error"].$x["host"] );
} else {
if(ereg("^HTTP",$result) && !ereg("^HTTP/[0-9\.]+ 200 ", $result)) {
return xmlrpc_error ( 1, $l["xmlrpc"]["error"].htmlspecialchars($result) );
}
if (ereg("^HTTP", $result)){
$ar = split("\r\n", $result);
while (($line = array_shift($ar))){
if (strlen($line) < 1){
break;
}
//$headers .= $line;
}
$result = join("\r\n", $ar);
}
return $result;
}
}
?>