header("Content-Type: application/xml");
require_once('xmlrpc/xmlrpc.inc');
require_once('xmlrpc/xmlrpcs.inc');
require_once("config.php");
//require_once (DOC_ROOT.'frame/DB.php');
//require_once(DOC_ROOT."frame/managedata.php");
//require_once(DOC_ROOT."frame/main_controller.php");
//$m = new xmlrpcmsg('examples.getStateName',
// array(new xmlrpcval($HTTP_POST_VARS["stateno"], "int")));
//print_r($m);
//pbprocess($m);
function pbprocess($m) {
global $xmlrpcerruser;
$x1 = $m->getParam(0);
$x2 = $m->getParam(1);
$source = $x1->scalarval(); # their article
$dest = $x2->scalarval(); # your article
mysql_connect(DB_HOST, DB_USER, DB_PWD) or die(mysql_error());
mysql_select_db(DB_DB) or die(mysql_error());
# INSERT CODE
# here we can check for valid urls in source and dest, security
# lookup the dest article in our database etc..
if (empty($source)) { # source uri does not exist
return new xmlrpcresp(0, 16, "Source uri does not exist");
}
if (!preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i',$source)) { # source uri does not have a link to target uri
return new xmlrpcresp(0, 17, "Source uri does have link to target uri");
}
if (empty($dest)) { # target uri does not exist
return new xmlrpcresp(0, 32, "Target uri does not exist");
}
if (!preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i',$dest)) { # target uri cannot be used as target
return new xmlrpcresp(0, 33, "Target uri cannot be used as target");
}
$result = mysql_query("SELECT * FROM pingback WHERE source='$source'") or die(mysql_error());
if (mysql_num_rows($result)>0) { # Pingback already registered
return new xmlrpcresp(0, 48, "Target uri cannot be used as target");
}
/* if (..) { # Access denied
return new xmlrpcresp(0, 49, "Access denied");
}
if (..) { # Could not communicate with upstream server or got error
return new xmlrpcresp(0, 50, "Problem with upstream server");
}
if (..) { # Generic fault code if not applicable to above
return new xmlrpcresp(0, 50, "Unkown error");
}*/
$date = date("Y-m-d h:i:s");
mysql_query("INSERT INTO pingback (source, destination, ping_date) VALUES ('$source', '$dest', '$date')") or die(mysql_error());
return new xmlrpcresp(new xmlrpcval("You just sent a ping, please only ping when you update.", "string"));
}
$a = array( "pingback.ping" => array( "function" => "pbprocess" ));
$s = new xmlrpc_server($a, false);
$s->setdebug(1);
$s->service();
//print_r($s);
function do_send_pingback($myarticle, $url, $pdebug = 0) {
$parts = parse_url($url);
if (!isset($parts['scheme'])) {
print "do_send_pingback: failed to get url scheme [".$url."]
\n";
return(1);
}
if ($parts['scheme'] != 'http') {
print "do_send_pingback: url scheme is not http [".$url."]
\n";
return(1);
}
if (!isset($parts['host'])) {
print "do_send_pingback: could not get host [".$url."]
\n";
return(1);
}
$host = $parts['host'];
$port = 80;
if (isset($parts['port'])) $port = $parts['port'];
$path = "/";
if (isset($parts['path'])) $path = $parts['path'];
if (isset($parts['query'])) $path .="?".$parts['query'];
if (isset($parts['fragment'])) $path .="#".$parts['fragment'];
$fp = fsockopen($host, $port);
fwrite($fp, "GET $path HTTP/1.0\r\nHost: $host\r\n\r\n");
$response = "";
while (is_resource($fp) && $fp && (!feof($fp))) {
$response .= fread($fp, 1024);
}
fclose($fp);
$lines = explode("\r\n", $response);
foreach ($lines as $line) {
if (ereg("X-Pingback: ", $line)) {
list($pburl) = sscanf($line, "X-Pingback: %s");
#print "pingback url is $pburl
\n";
}
}
if (empty($pburl)) {
print "Could not get pingback url from [$url].
\n";
return(1);
}
if (!isset($parts['scheme'])) {
print "do_send_pingback: failed to get pingback url scheme [".$pburl."]
\n";
return(1);
}
if ($parts['scheme'] != 'http') {
print "do_send_pingback: pingback url scheme is not http[".$pburl."]
\n";
return(1);
}
if (!isset($parts['host'])) {
print "do_send_pingback: could not get pingback host [".$pburl."]
\n";
return(1);
}
$host = $parts['host'];
$port = 80;
if (isset($parts['port'])) $port = $parts['port'];
$path = "/";
if (isset($parts['path'])) $path = $parts['path'];
if (isset($parts['query'])) $path .="?".$parts['query'];
if (isset($parts['fragment'])) $path .="#".$parts['fragment'];
$m = new xmlrpcmsg("pingback.ping", array(new xmlrpcval($myarticle, "string"), new xmlrpcval($url, "string")));
$c = new xmlrpc_client($path, $host, $port);
$c->setRequestCompression(null);
$c->setAcceptedCompression(null);
if ($pdebug) $c->setDebug(2);
$r = $c->send($m);
if (!$r->faultCode()) {
print "Pingback to $url succeeded.
\n";
} else {
$err = "code ".$r->faultCode()." message ".$r->faultString();
print "Pingback to $url failed with error $err.
\n";
}
}
# call send_pingback() from your blog after adding a new post,
# $text will be the full text of your post
# $myurl will be the full url of your posting
function send_pingback($text, $myurl) {
$m = array();
preg_match_all ("/]*href=[\"']([^\"']*)[\"'][^>]*>(.*?)<\/a>/i", $text, $m);
$c = count($m[0]);
for ($i = 0; $i < $c; $i++) {
$ret = valid_url($m[1][$i]);
if ($ret) do_send_pingback($myurl, $m[1][$i]);
}
}
function valid_url($url) {
if (!ereg('[()"\'<>]', $url)) return(1);
return(0);
}
?>