📄 trackback_cls.php
字号:
<?php
/**
* PHP Class to handle TrackBacks (send/ping, receive, retreive, detect, seed, etc...)
*
* <code><?php
* include('trackback_cls.php');
* $trackback = new Trackback('BLOGish', 'Ran Aroussi', 'UTF-8');
* ?></code>
*
* ==============================================================================
*
* @version $Id: trackback_cls.php,v 1.2 2004/12/11 18:54:32 Ran Exp $
* @copyright Copyright (c) 2004 Ran Aroussi (http://www.blogish.org)
* @author Ran Aroussi <ran@blogish.org>
* @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 $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 Trackback($blog_name, $author, $encoding = "UTF-8")
{
$this->blog_name = $blog_name;
$this->author = $author;
$this->encoding = $encoding;
// Gather $_POST information
if (isset($_GET['e_id'])) {
$this->get_e_id = $_GET['e_id'];
}
if (isset($_POST['e_id'])) {
$this->post_e_id = $_POST['e_id'];
}
if (isset($_POST['url'])) {
$this->url = $_POST['url'];
}
if (isset($_POST['title'])) {
$this->title = $_POST['title'];
}
if (isset($_POST['excerpt'])) {
$this->excerpt = $_POST['excerpt'];
}
}
/**
* Sends a trackback ping to a specified trackback URL.
* allowing clients to auto-discover the TrackBack Ping URL.
*
* <code><?php
* include('trackback_cls.php');
* $trackback = new Trackback('BLOGish', 'Ran Aroussi', 'UTF-8');
* if ($trackback->ping('http://tracked-blog.com', 'http://your-url.com', 'Your entry title')) {
* echo "Trackback sent successfully...";
* } else {
* echo "Error sending trackback....";
* }
* ?></code>
*
* @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, '<error>0</error>')) {
$return = "2";
} else {
$return = "3";
}
// send result
return $return;
}
/**
* Produces XML response for trackbackers with ok/error message.
*
* <code><?php
* // Set page header to XML
* header('Content-Type: text/xml'); // MUST be the 1st line
* //
* // Instantiate the class
* //
* include('trackback_cls.php');
* $trackback = new Trackback('BLOGish', 'Ran Aroussi', 'UTF-8');
* //
* // Get trackback information
* //
* $tb_e_id = $trackback->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');
* }
* ?></code>
*
* @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 = '<?xml version="1.0" encoding="' . $this->encoding . '"?>' . "\n";
$return .= "<response> \n";
// Send back response...
if ($success) {
// Trackback received successfully...
$return .= " <error>0</error> \n";
} else {
// Something went wrong...
$return .= " <error>1</error> \n";
$return .= " <message>" . $this->xml_safe($err_response) . "</message>\n";
}
// End response to trackbacker...
$return .= "</response>";
return $return;
}
/**
* Feteched trackback information and produces an RSS-0.91 feed.
*
* <code><?php
* // 1
* header('Content-Type: text/xml'); // MUST be the 1st line
* // 2
* include('trackback_cls.php');
* $trackback = new Trackback('BLOGish', 'Ran Aroussi', 'UTF-8');
* // 3
* $tb_e_id = $trackback->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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -