/home/a/aquarion/sites/www.aquarionics.com/epistula/include/prackback.inc
All my code (That is, anything not in the "Others" list on the right) is BSD licenced.
You can also view this page as text/plain or colour-coded source
<?PHP
/*******************************************************************************
Prackback library
********************************************************************************
Library of response format functions, including Pingback and Trackback
$Id: prackback.inc,v 1.1.1.1 2004/05/16 17:39:16 aquarion Exp $
$log$
*******************************************************************************/
include($_EP['codedir']."/include/IXR_Library.inc.php");
/* Send Ping/Trackback Functions */
function liftLinks($data){
unset($location);
$links = array(); // the contents of the anchors (highlighted text)
$hrefs = array(); // the URLs corresponding to each element of $links
$pos = 0;
while (!(($pos = strpos($data,"<",$pos)) === false)) {
$pos++;
$endpos = strpos($data,">",$pos);
$tag = substr($data,$pos,$endpos-$pos);
$tag = trim($tag);
if (isset($location)) { // look for a </A>
if (!strcasecmp(strtok($tag," "),"/A")) {
$link = substr($data,$linkpos,$pos-1-$linkpos);
$links[] = $link;
$hrefs[] = $location;
unset($location);
}
$pos = $endpos+1;
} else { // look for a <A ...>
if (!strcasecmp(strtok($tag," "),"A")) {
if (eregi("HREF[ \t\n\r\v]*=[ \t\n\r\v]*\"([^\"]*)\"",$tag,$regs));
else if (eregi("HREF[ \t\n\r\v]*=[ \t\n\r\v]*([^ \t\n\r\v]*)",$tag,$regs));
else $regs[1] = "";
if ($regs[1]) { // Only use it if it seems to be reasonable
$location = $regs[1];
}
$pos = $endpos+1;
$linkpos = $pos;
} else {
$pos = $endpos+1;
}
}
}
return $hrefs;
}
function pingback($links, $type, $post_id, $debug=false){
$output = "Parsing Links:\n<dl>";
$output .="<dl>\n";
foreach ($links as $link){
$output .= "<dt><a href=\"$link\">$link</a></dt>\n\t<dd>";
if (!preg_match("/^http:\/\//i", $link)){
$output .= "Not external link<br>";
} else {
$fd = @fopen ($link, "r");
if ($fd) {
#print "Woohoo! Connection Established!";
$carryon = true;
$i = 0;
while ($carryon == true && !feof ($fd) && $i <= 50) {
$output .= ".";
#set_time_limit(60);
$buffer = fgets($fd, 4096);
$pregex = "\<link rel\=\"pingback\" href\=\"http:\/\/(.*)\"(.*)(\/?)>";
$hpregex = "X-Pingback: (.*)";
if (preg_match("/$pregex/i", $buffer, $matches) || preg_match("/$hpregex/i", $buffer, $matches)) {
$carryon = false;
$output .= "<br>\n";
$output .= "Found <Link> as ".htmlentities($matches[0])."<br>\n";
$output .= "Found href as ".htmlentities($matches[1])."<br>\n";
$output .= send_pingback($matches[1], getPermalink($type, $post_id), $link); # Ping their server
}; // End If preg_match
$i++;
} // End While
if ($carryon == true || feof ($fd) || $i > 50){
$output .= "<link> not found :-|";
}// End if
} else{
$output .= "Can't access $link";
}// End if pregmatch
#fclose ($fd);
} // End foreach
$output .= "</dd>";
}
$output .= "</dl>\n";
if ($debug){
return $output;
}
}
function trackback($url, $type, $id) {
if (!checkExists($type, $id)){
return "Can't find $type $id";
} else {
global $_EP;
/*$data = array (
"title" => "Trackback Test",
"excerpt" => "this is a test of the trackback system",
"url" => "http://water.gkhs.net/~aquarion/type/blog/archives/000005.html",
"blog_name" => "Aquarionics"
);*/
#http://www.aquarionics.com/trackback/journal/505
#$url = "http://www.aquarionics.com/trackback/journal/505";
$q = "select *, unix_timestamp(date_created) as epoch from $type where id = $id";
$res = safequery($q);
$item = mysql_fetch_assoc($res);
$link = getPermalink($type, $id, $item);
if ($item['description'] == ""){
$text = strip_tags(process($item['content'], $item['format']));
$desc = htmlentities(break_string(strtr($text,"’","'"),400));
} else {
$desc = strip_tags(process($item['description'], $item['format']));
}
$data = array (
"title" => $item['title'],
"url" => $link,
"blog_name" => $_EP['book'],
"excerpt" => $desc
);
/*if (strncasecmp ($url, "http://", 7) == 0) $url = substr ($url, 7);
if (strncasecmp ($url, "https://", 8) == 0) $url = substr ($url, 8);
$p = strpos ($url, '/');
if ($p == 0){
$host = $url;
$script = "/";
} else {
$host = substr ($url, 0, $p);
$script = substr ($url, $p);
}*/
$url = parse_url($url);
$host = $url['scheme']."://".$url['host'];
$script = $url['path'];
$query = explode("&",$url['query']);
foreach($query as $datum){
$datum = explode("=", $datum);
$data[$datum[0]] = $datum[1];
}
$reply = post_data($host, $script, $data);
return $reply;
}
}
function send_pingback($pingback, $permalink, $link){
global $_EP;
$cout .= "<h3>Sending Pingback:</h3>";
$cout .= "URL is $pingback<br>";
$client = new IXR_Client("http://".$pingback);
#$client->debug = true;
if (!$client->query('pingback.ping', $permalink, $link)) {
$cout .= nl2br('Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage());
$client->debug = true;
#die("</div></body></html>");
} else {
$cout .= nl2br($client->getResponse());
}
}
/* Recieve Ping/Trackback Functions */
function pBgetArray($pageLinkedTo){
global $error;
global $_EP;
$regex = "/^".preg_quote($_EP['url'],"/")."/i";
if (!preg_match($regex, $pageLinkedTo)){
return false;
} else {
$count = strlen($_EP['url']);
return explode("/",substr ($pageLinkedTo, $count+1));
}
}
function makeQuery($pageLinkedTo, $pageLinkedFrom, $title, $type){
// str LinkedTo: http://www.aquarionics.com/journal/id/213
// str LinkedFrom: http://reef.water.gkhs.net/~aquarion/blog/whatever
// str Title: Title of the link
// int Type: See Crossreference for details, but Zero is internal, 1 is Pingback, 2 is Trackback.
global $error;
global $_EP;
global $types;
$panic = "";
// Basic validation
if (!$request = pBgetArray($pageLinkedTo)){
$panic .= "Help! Confused! Return link needs to be a fully valid URL. Mine, for preference.\n";
# } elseif (count($request) > 3){
# $panic .= "No idea how to pingback to that at all (Failed explode)\n";
} elseif (! in_array($request[0],$types)){
$panic .= "Um. What's a ".$request[0]."?";
} elseif (preg_match("/^".preg_quote($_EP['url'],"/")."/i", $pageLinkedFrom)){
$panic .= "That's an internal link, Not my department guv.\n";
}
// Get the ID of the post, if we can.
if ($panic == ""){
if ($request[1] == "name"){
$query = "select id from ".$request[0]." where title = \"".addslashes(ep_decode($request[2]))."\"";
$details = mysql_fetch_assoc(safequery($query));
$id = $details['id'];
} elseif ($request[1] == "id"){
$id = $request[2];
// journal/1981/01/26/happy_birthday_aq
} elseif (is_numeric($request[1]) && is_numeric($request[2]) && is_numeric($request[3]) && $request[4]) {
$q = "select id from ".$request[0]." where"
." (MONTH(date_created) = '".$request[2]."') "
."and (YEAR(date_created) = '".$request[1]."') "
."and (DAYOFMONTH(date_created) = '".$request[3]."') "
."and title = \"".ep_decode($request[4])."\" ";
$details = mysql_fetch_assoc(safequery($q));
$id = $details['id'];
} else {
$panic .= "I don't understand how to get an article from ".$request[1];
}
} // end panic check
if ($panic == ""){
if (!checkExists($request[0], $id)){
$panic .= "That don't exist, tha don't.";
}
} // end panic check
if ($panic == ""){
$check = safequery("select id from crossreference where attachtotype = \"".$pageLinkedFrom."\" and attachfromtype = \"".$request[0]."\" and attachfromid = ".$id);
if (mysql_num_rows($check) != 0){ // If there is already a link with those parameters
$panic .= "I've already had a crossreference between those two points.";
}
} // end panic check
/*if (!checkExists($attachment, $attach_id)){
$error .= "Not pingable.\n";
}*/
/*---------------+-------------------+
| Field | Type |
+----------------+-------------------+
| id | int(10) unsigned |
| title | tinytext |
| date_created | datetime |
| date_edited | timestamp(14) |
| attachfromtype | tinytext |
| attachfromid | int(10) unsigned |
| attachtotype | tinytext |
| attachtoid | int(10) unsigned |
| type | tinyint(4) |
+----------------+------------------*/
if ($panic == ""){
deleteCache($id,$request[0]);
return "insert into crossreference (title, attachtotype, attachtoid, attachfromtype, attachfromid, type, date_created) "
."values ('".htmlentities($title, ENT_QUOTES)."', '$pageLinkedFrom', 0, '".$request[0]."', '".$id."', ".$type.", NOW())";
} else {
$error .= $panic;
return false;
}
}
function grabtitle($pageLinkedFrom, $pageLinkedTo){
global $error;
$fd = @fopen ($pageLinkedFrom, "r");
if ($fd) {
$found = 0;
$linkfound = false;
$titleFound = false;
$i = 0;
while ($found != 2 && !feof ($fd) && $i <= 1000) {
$buffer = fgets($fd, 4096);
$linkrex = "/".preg_quote($pageLinkedTo,"/")."/i";
if (preg_match($linkrex, $buffer, $matches)) {
$found++;
$linkfound = true;
};
$titleregex = "/<title>(.*)<\/title>/i";
if (preg_match($titleregex, $buffer, $matches)) {
$found++;
$title = $matches[1];
$titleFound = true;
};
$i++;
}
} else{
$error .= "Couldn't open link\n";
}
if (!$linkfound){
$error .= "Couldn't find a link back to $pageLinkedTo on $pageLinkedFrom\n";
}
if ($titleFound == false){
$title = $pageLinkedFrom;
}
fclose ($fd);
return $title;
}
?>
Using a heavily customised version of Tom's PHPCode2ValidXHTML Thing