Aquarionics

/home/a/aquarion/sites/www.aquarionics.com/epistula/chapters/trackback.inc.php

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
/*******************************************************************************
    Chapter - Trackback
********************************************************************************

    Recieve trackback pings for articles

    $Id: trackback.inc.php,v 1.1.1.1 2004/05/16 17:39:12 aquarion Exp $

    $log$

*******************************************************************************/

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 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"){
            $details = mysql_fetch_assoc(safequery("select id from $request[0] where title = \"".addslashes($request[2])."\""));
            $id = $details['id'];
        } elseif ($request[1] == "id"){
            $id = $request[2];
        } 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

    $check = safequery("select * from crossreference where attachtotype = \"".$pageLinkedFrom."\" and attachfromtype = \"".$request[0]."\" and attachfromid = ".$id, false); // Send query, don't panic on failure.


    $ip = $_SERVER['REMOTE_ADDR'];

    $spammer = 0;
    if (isItSpam("ip",$ip)){$spammer++;}
    if (isItSpam("url",$pageLinkedFrom)){$spammer++;}
    if (isItSpam("text",$title)){$spammer++;}

    if ($spammer > 0){
        $panic = "I think you're a spammer, sorry";
    }


    if ($panic == ""){
        if (!$check){
            $panic .= "Internal SQL error, Admin has been notified";
        }elseif (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, ip) "
                ."values ('".htmlentities($title, ENT_QUOTES)."', '$pageLinkedFrom', 0, '".$request[0]."', '".$id."', ".$type.", NOW(), '$ip')";
    } 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;
}

# http://www.aquarionics.com/trackback/journal/505

/* End Prackback includes */
header("Allow: POST");
if (isset($_REQUEST['url'])){


    $out = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n"
        . "<response>\n"
        . "<error>1</error>\n"
        . "<message>Trackbacks disabled until further notice, sorry. \n\nNicholas 'Aquarion' Avenell, 2006-02-18</message>\n"
        #. print_r_to_var($_REQUEST)."\nRequest:".$_SERVER['REQUEST_URI']
        . "</response>";
    die($out);
    

    global $error;

    $pageLinkedFrom = $_REQUEST['url'];

    if (isset($_REQUEST['title'])){
        $title = $_REQUEST['title'];
    }else{
        $title = grabtitle($pageLinkedFrom, $pageLinkedTo);
    }

    $pageLinkedTo = $_EP['url']."/".$wanted[1]."/id/".$wanted[2];

    $query = makeQuery($pageLinkedTo, $pageLinkedFrom, $title, 2);



    #Stuff to deal with ping
    if (!$error) {
        #print($query);
        safequery($query);
        mailAdmin("Trackback", "Trackback was sent with these details: \n".print_r_to_var($_REQUEST)."\n\n Error was:\n".$error);
        echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n<response>\n<error>0</error>\n</response>";
    } else {
        $out = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n"
            . "<response>\n"
            . "<error>1</error>\n"
            . "<message>".$error."\n on page ".$pageLinkedTo."\n</message>\n"
            #. print_r_to_var($_REQUEST)."\nRequest:".$_SERVER['REQUEST_URI']
            . "</response>";
        echo $out;
        //mailAdmin("Trackback", "Trackback was sent with these details: \n".print_r_to_var($_REQUEST)."\n\n Error was:\n".$error);
    }
    $finalOutput = true;
} else {
    
    $page->status = 405;
    $page->title="Trackback URL";
    $page->content = "This URL is for ".$page->ulink("http://www.movabletype.org/docs/mttrackback.html", "trackback", "The Trackback Specification")." pings only.";
}
?>


Using a heavily customised version of Tom's PHPCode2ValidXHTML Thing

Nicholas 'Aquarion' Avenell is a web developer in London, you can find out more about him or how to get in touch.

There are more Articles, Projects, Journal Entries, Photographs and things that defy description here, too.

If you're looking for something specific, there are Calendar & Category -based lists of everything.

And if you want to follow stuff that appears here, try a Syndication Feed, or the generic Feed of everything.

Aquarionics on Livejournal


Aquarion [updating]
Twitter last updated


© 2000 to 2008 inclusive Nicholas Avenell
All comments are the property of their creators, published with permission
(Unless otherwise indicated, the opinions and sentiments expressed on this site are those of the author and not of any organisation of which he is an affiliate, including his employer. Caveat Lector, E&OE. sigh)
0.854 seconds, 1 queries, 2.7Mb on Thu, 20 Nov 2008 16:00:19 +0000, Not cached
Generated by Epistula Version 2.0.3