📄 trackback_cls.php
字号:
* );
* echo $trackback->fetch(true, $tb_info);
* } else {
* // Something went wrong - tell my why...
* echo $trackback->fetch(false, string RESPONSE);
* }
* ?></code>
*
* @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 = '<?xml version="1.0" encoding="' . $this->encoding . '"?>' . "\n";
$return .= "<response> \n";
// Send back response...
if ($success) {
// Trackback retreived successfully...
// Sending back an RSS (0.91) - trackback information from $response (array)...
$return .= " <error>0</error> \n";
$return .= " <rss version=\"0.91\"> \n";
$return .= " <channel> \n";
$return .= " <title>" . $this->xml_safe($response['title']) . "</title> \n";
$return .= " <link>" . $this->xml_safe($response['trackback']) . "</link> \n";
$return .= " <description>" . $this->xml_safe($response['excerpt']) . "</description> \n";
$return .= " <item> \n";
$return .= " <title>" . $this->xml_safe($response['title']) . "</title> \n";
$return .= " <link>" . $this->xml_safe($response['permalink']) . "</link> \n";
$return .= " <description>" . $this->xml_safe($response['excerpt']) . "</description> \n";
$return .= " </item> \n";
$return .= " </channel> \n";
$return .= " </rss> \n";
} else {
// Something went wrong - provide reason from $response (string)...
$return .= " <error>1</error> \n";
$return .= " <message>" . $this->xml_safe($response) . "</message>\n";
}
// End response to trackbacker
$return .= "</response>";
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().
*
* <code><?php
* include('trackback_cls.php');
* $trackback = new Trackback('BLOGish', 'Ran Aroussi', 'UTF-8');
*
* echo $trackback->rdf_autodiscover(string DATE, string TITLE, string EXCERPT, string PERMALINK, string TRACKBACK [, string AUTHOR]);
* ?></code>
*
* @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 .= "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" \n";
$return .= " xmlns:dc=\"http://purl.org/dc/elements/1.1/\" \n";
$return .= " xmlns:trackback=\"http://madskills.com/public/xml/rss/module/trackback/\"> \n";
$return .= "<rdf:Description \n";
$return .= " rdf:about=\"" . $this->xml_safe($permalink) . "\" \n";
$return .= " dc:identifier=\"" . $this->xml_safe($permalink) . "\" \n";
$return .= " trackback:ping=\"" . $this->xml_safe($trackback) . "\" \n";
$return .= " dc:title=\"" . $this->xml_safe($title) . "\" \n";
$return .= " dc:subject=\"TrackBack\" \n";
$return .= " dc:description=\"" . $this->xml_safe($this->cut_short($excerpt)) . "\" \n";
$return .= " dc:creator=\"" . $this->xml_safe($author) . "\" \n";
$return .= " dc:date=\"" . $RFC822_date . "\" /> \n";
$return .= "</rdf:RDF> \n";
$return .= "--> \n";
return $return;
}
/**
* Search text for links, and searches links for trackback URLs.
*
* <code><?php
*
* include('trackback_cls.php');
* $trackback = new Trackback('BLOGish', 'Ran Aroussi', 'UTF-8');
*
* if ($tb_array = $trackback->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 <i>$tb_url</i>...\n";
* } else {
* // Error pinging...
* echo "Trackback to <i>$tb_url</i> failed....\n";
* }
* }
* } else {
* // No trackbacks in TEXT...
* echo "No trackbacks were auto-discover...\n"
* }
* ?></code>
*
* @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 = eregi_replace("www.", "http://www.", $text);
$text = eregi_replace("http://http://", "http://", $text);
$text = eregi_replace("\"", " \"", $text);
$text = eregi_replace("'", " '", $text);
$text = eregi_replace(">", " >", $text);
// Create an array with unique links
$uri_array = array();
if (preg_match_all($reg_exp, strip_tags($text, "<a>"), $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 ($link_content = implode("", @file($link))) {
preg_match_all('/(<rdf:RDF.*?<\/rdf:RDF>)/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);
}
/**
* 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;
}
}
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -