* * ============================================================================== * * @version $Id: class_blog_trackback.php 1 2009-01-10 12:24:57Z john $ * @copyright Copyright (c) 2004 Ran Aroussi (http://www.blogish.org) * @author Ran Aroussi * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL) * * ============================================================================== */ /** * Trackback - The main class * * @param string $blog_name * @param string $author * @param string $encoding */ class Trackback { var $blog_name = ''; // Default blog name used throughout the class (ie. BLOGish) var $author = ''; // Default author name used throughout the class (ie. Ran Aroussi) var $encoding = ''; // Default encoding used throughout the class (ie. UTF-8) var $e_id = NULL; var $bname = NULL; var $get_e_id = ''; // Retreives and holds $_GET['e_id'] (if not empty) var $post_e_id = ''; // Retreives and holds $_POST['e_id'] (if not empty) var $url = ''; // Retreives and holds $_POST['url'] (if not empty) var $title = ''; // Retreives and holds $_POST['title'] (if not empty) var $excerpt = ''; // Retreives and holds $_POST['excerpt'] (if not empty) /** * Class Constructure * * @param string $blog_name * @param string $author * @param string $encoding * @return */ function __construct($blog_name, $author, $encoding = "UTF-8") { $this->blog_name = $blog_name; $this->author = $author; $this->encoding = $encoding; // Gather $_POST information $this->e_id = ( !empty($_POST['e_id']) ? $_POST['e_id'] : ( !empty($_GET['e_id']) ? $_GET['e_id'] : NULL ) ); $this->url = ( !empty($_POST['url']) ? $_POST['url'] : ( !empty($_GET['url']) ? $_GET['url'] : NULL ) ); $this->title = ( !empty($_POST['title']) ? $_POST['title'] : ( !empty($_GET['title']) ? $_GET['title'] : NULL ) ); $this->excerpt = ( !empty($_POST['excerpt']) ? $_POST['excerpt'] : ( !empty($_GET['excerpt']) ? $_GET['excerpt'] : NULL ) ); $this->bname = ( !empty($_POST['blog_name']) ? $_POST['blog_name'] : ( !empty($_GET['blog_name']) ? $_GET['blog_name'] : NULL ) ); // Old if( isset($_GET['e_id']) ) $this->get_e_id = $_GET['e_id']; if( isset($_POST['e_id']) ) $this->post_e_id = $_POST['e_id']; } /** * Sends a trackback ping to a specified trackback URL. * allowing clients to auto-discover the TrackBack Ping URL. * * ping('http://tracked-blog.com', 'http://your-url.com', 'Your entry title')) { * echo "Trackback sent successfully..."; * } else { * echo "Error sending trackback...."; * } * ?> * * @param string $tb * @param string $url * @param string $title * @param string $excerpt * @return boolean */ function ping($tb, $url, $title = "", $excerpt = "") { $response = ""; $reason = ""; // Set default values if (empty($title)) { $title = "Trackbacking your entry..."; } if (empty($excerpt)) { $excerpt = "I found your entry interesting do I've added a Trackback to it on my weblog :)"; } // Parse the target $target = parse_url($tb); if ((isset($target["query"])) && ($target["query"] != "")) { $target["query"] = "?" . $target["query"]; } else { $target["query"] = ""; } if ((isset($target["port"]) && !is_numeric($target["port"])) || (!isset($target["port"]))) { $target["port"] = 80; } // Open the socket $tb_sock = @fsockopen($target["host"], $target["port"]); // Something didn't work out, return if (!is_resource($tb_sock)) { return "1"; exit; } // Put together the things we want to send $tb_send = "url=" . rawurlencode($url) . "&title=" . rawurlencode($title) . "&blog_name=" . rawurlencode($this->blog_name) . "&excerpt=" . rawurlencode($excerpt); $encoding = $this->encoding; // Send the trackback fputs($tb_sock, "POST " . $target["path"] . $target["query"] . " HTTP/1.1\r\n"); fputs($tb_sock, "Host: " . $target["host"] . "\r\n"); fputs($tb_sock, "Content-type: application/x-www-form-urlencoded; charset=$encoding\r\n"); fputs($tb_sock, "Content-length: " . strlen($tb_send) . "\r\n"); fputs($tb_sock, "Connection: close\r\n\r\n"); fputs($tb_sock, $tb_send); // Gather result while (!feof($tb_sock)) { $response .= fgets($tb_sock, 128); } // Close socket fclose($tb_sock); // Did the trackback ping work if( strpos($response, '0') ) { $return = "2"; } else { $return = "3"; } // send result return $return; } /** * Produces XML response for trackbackers with ok/error message. * * post_e_id; // The e_id of the item being trackbacked * $tb_url = $trackback->url; // The URL from which we got the trackback * $tb_title = $trackback->title; // Subject/title send by trackback * $tb_excerpt = $trackback->excerpt; // Short text send by trackback * // * // Do whatever to log the trackback (save in DB, flatfile, etc...) * // * if (TRACKBACK_LOGGED_SUCCESSFULLY) { * // Logged successfully... * echo $trackback->recieve(true); * } else { * // Something went wrong... * echo $trackback->recieve(false, 'Explain why you return error'); * } * ?> * * @param boolean $success * @param string $err_response * @return boolean */ function recieve($success = false, $err_response = "") { // Default error response in case of problems... if (!$success && empty($err_response)) { $err_response = "An error occured while tring to log your trackback..."; } // Start response to trackbacker... $return = 'encoding . '"?>' . "\n"; $return .= " \n"; // Send back response... if ($success) { // Trackback received successfully... $return .= " 0 \n"; } else { // Something went wrong... $return .= " 1 \n"; $return .= " " . $this->xml_safe($err_response) . "\n"; } // End response to trackbacker... $return .= ""; return $return; } /** * Feteched trackback information and produces an RSS-0.91 feed. * * get_e_id; * // 4 * Do whatever to get trackback information by ID (search db, etc...) * if (GOT_TRACKBACK_INFO) { * // Successful - pass trackback info as array()... * $tb_info = array('title' => string TRACKBACK_TITLE, * 'excerpt' => string TRACKBACK_EXCERPT, * 'permalink' => string PERMALINK_URL, * 'trackback' => string TRACKBACK_URL * ); * echo $trackback->fetch(true, $tb_info); * } else { * // Something went wrong - tell my why... * echo $trackback->fetch(false, string RESPONSE); * } * ?> * * @param boolean $success * @param string $response * @return string XML response to the caller */ function fetch($success = false, $response = "") { if (!$success && empty($response)) { $response = "An error occured while tring to retreive trackback information..."; } // Start response to caller $return = 'encoding . '"?>' . "\n"; $return .= " \n"; // Send back response... if ($success) { // Trackback retreived successfully... // Sending back an RSS (0.91) - trackback information from $response (array)... $return .= " 0 \n"; $return .= " \n"; $return .= " \n"; $return .= " " . $this->xml_safe($response['title']) . " \n"; $return .= " " . $this->xml_safe($response['trackback']) . " \n"; $return .= " " . $this->xml_safe($response['excerpt']) . " \n"; $return .= " \n"; $return .= " " . $this->xml_safe($response['title']) . " \n"; $return .= " " . $this->xml_safe($response['permalink']) . " \n"; $return .= " " . $this->xml_safe($response['excerpt']) . " \n"; $return .= " \n"; $return .= " \n"; $return .= " \n"; } else { // Something went wrong - provide reason from $response (string)... $return .= " 1 \n"; $return .= " " . $this->xml_safe($response) . "\n"; } // End response to trackbacker $return .= ""; return $return; } /** * Produces embedded RDF representing metadata about the entry, * allowing clients to auto-discover the TrackBack Ping URL. * * NOTE: DATE should be string in RFC822 Format - Use RFC822_from_datetime(). * * rdf_autodiscover(string DATE, string TITLE, string EXCERPT, string PERMALINK, string TRACKBACK [, string AUTHOR]); * ?> * * @param string $RFC822_date * @param string $title * @param string $excerpt * @param string $permalink * @param string $trackback * @param string $author * @return string */ function rdf_autodiscover($RFC822_date, $title, $excerpt, $permalink, $trackback, $author = "") { if ( !$author ) $author = $this->author; $return = " \n"; return $return; } /** * Search text for links, and searches links for trackback URLs. * * auto_discovery(string TEXT)) { * // Found trackbacks in TEXT. Looping... * foreach($tb_array as $tb_key => $tb_url) { * // Attempt to ping each one... * if ($trackback->ping($tb_url, string URL, [string TITLE], [string EXCERPT])) { * // Successful ping... * echo "Trackback sent to $tb_url...\n"; * } else { * // Error pinging... * echo "Trackback to $tb_url failed....\n"; * } * } * } else { * // No trackbacks in TEXT... * echo "No trackbacks were auto-discover...\n" * } * ?> * * @param string $text * @return array Trackback URLs. */ function auto_discovery($text) { // Get a list of UNIQUE links from text... // --------------------------------------- // RegExp to look for (0=>link, 4=>host in 'replace') $reg_exp = "/(http)+(s)?:(\\/\\/)((\\w|\\.)+)(\\/)?(\\S+)?/i"; // Make sure each link ends with [sapce] $text = preg_replace("/www\./", "http://www.", $text); //$text = preg_replace("www.", "http://www.", $text); $text = preg_replace("/http:\/\/http:\/\//", "http://", $text); //$text = preg_replace("http://http://", "http://", $text); $text = preg_replace('/"/', ' " ', $text); //$text = preg_replace("\"", " \"", $text); $text = preg_replace("/'/", " '", $text); //$text = preg_replace("'", " '", $text); $text = preg_replace("/>/", " >", $text); //$text = preg_replace(">", " >", $text); // Create an array with unique links $uri_array = array(); if( preg_match_all($reg_exp, strip_tags($text, ""), $array, PREG_PATTERN_ORDER) ) { foreach( $array[0] as $key => $link ) { foreach( (array(",", ".", ":", ";")) as $t_key => $t_value ) { $link = trim($link, $t_value); } $uri_array[] = ($link); } $uri_array = array_unique($uri_array); } // Get the trackback URIs from those links... // ------------------------------------------ // Loop through the URIs array and extract RDF segments $rdf_array = array(); // <- holds list of RDF segments foreach( $uri_array as $key => $link ) { if(is_array(file($link))){ if( $link_content = implode("", file($link)) ) { preg_match_all('/()/sm', $link_content, $link_rdf, PREG_SET_ORDER); for ($i = 0; $i < count($link_rdf); $i++) { if (preg_match('|dc:identifier="' . preg_quote($link) . '"|ms', $link_rdf[$i][1])) { $rdf_array[] = trim($link_rdf[$i][1]); } } } } } // Loop through the RDFs array and extract trackback URIs $tb_array = array(); // <- holds list of trackback URIs if (!empty($rdf_array)) { for ($i = 0; $i < count($rdf_array); $i++) { if (preg_match('/trackback:ping="([^"]+)"/', $rdf_array[$i], $array)) { $tb_array[] = trim($array[1]); } } } // Return Trackbacks return $tb_array; } /** * Other Useful functions used in this class */ /** * Converts MySQL datetime to a standart RFC 822 date format * * @param string $datetime * @return string RFC 822 date */ function RFC822_from_datetime($datetime) { $timestamp = mktime( substr($datetime, 8, 2), substr($datetime, 10, 2), substr($datetime, 12, 2), substr($datetime, 4, 2), substr($datetime, 6, 2), substr($datetime, 0, 4) ); return date("r", $timestamp); } /** * Converts a string into an XML-safe string (replaces &, <, >, " and ') * * @param string $string * @return string */ function xml_safe($string) { return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); } /** * Cuts a string short (with "...") accroding to $max_length... * * @param string $string * @param integer $max_length * @return string */ function cut_short($string, $max_length = 255) { if( strlen($string)>$max_length ) { $string = substr($string, 0, $max_length) . '...'; } return $string; } } ?>