воскресенье, 15 апреля 2007 г.

Manual Trackback


Manual Trackback v1.0
by Mark Woodward, July 2003
http://cowpi.com/journal/templates/

Manual Trackback is a php script that allows non-Movable Type users to send a
trackback ping to your weblog. It is called 'manual' because the following
pieces of information must be entered by hand (or copy and pasted) from the
visitor: weblog name, weblog entry title (or date of entry), weblog entry URL,
and weblog excerpt. This script will check to see that each piece of data is
entered into the form, but does not check the validity of that data.

Includes needed: Snoopy.class.inc

Snoopy is a small open-source project "that simulates a web browser. It
automates the task of retrieving web page content and posting forms". Snoopy
can be downloaded from http://snoopy.sourceforge.net/

The following variables need to be sent to this script from your weblog through
either the GET or POST method:

tb_url = trackback URL (required)
tb_title = title of your weblog entry (optional)
tb_entry_url = URL of your weblog entry (optional)

The tb_title and tb_entry_url will be displayed as a reference to your weblog
entry. Both are optional. tb_title can be sent without its tb_entry_url, but
tb_entry_url is useless without its tb_title. The tb_url is the bare minimum
needed for this script to run.

The advantage of the GET method is that a regular link (anchor tag)
can send the required information. The disadvantage is the GET URL is long
and is seen by the visitor. (But so are most GET URL's in Movable Type.)
Here is an example of the GET method using a link and Movable Type tags:


&tb_title=<$MTEntryTitle encode_url="1"$>
&tb_entry_url=<$MTEntryLink encode_url="1"$>">Manual Trackback


Note, this whole example should be one line. No spaces can occur within the
href attribute in a link. Also, the encode_url="1" attribute must used to
for the link to work properly and to have valid HTML.

The advantage of the POST method is that the variables are hidden to the
visitor. The disadvantage is that a HTML form is required with a submit
button. Here is an example using forms and the POST method:








This script is embedded within a simple XHTML 1.0 Transitional document. Some
simple CSS styling has been applied. The following CSS classes are employed
within this script:

p.rule = a horizontal separator after introduction text
div.error = formatting for error messages
p.ping = final message if ping was successful or not

This script can be used as a stand-alone document, or as a Movable Type
template. The filename of this script can be renamed to anything. Also,
this script can be placed in any directory. In either case, set the two global
variables near the beginning of the script. Just be sure that your GET URL
or POST action reflect the filename and directory changes. (Don't forget to
modify the URL to Snoopy.class.inc if needed.)

This script was inspired by Adam Kalsey's SimpleTrack (http://kalsey.com/).
This script would have been impractical or un-user-friendly if it was not
for the clue to use the Snoopy php class.

Please feel free to use, copy and/or modify this script for your needs. You
are not required to place my name or link to my website within this script,
but the acknowledgement would be greatly appreciated. Note also that I am not
responsible for any positive or negative effects of this script.

Happy coding... */


?> "http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd">



Manual Trackback






Manual Trackback




include 'Snoopy.class.inc';

// Global variables
$myHomeURL = 'http://yourweblog.com/'; // or use <$MTBlogURL$> in a Movable Type template
$myBlogName = 'Name of Your Weblog'; // or use <$MTBlogName$> in a Movable Type template

RetrieveTBData($tb);

if ($tb['tbURL'] == '') { // must have a trackback URL to work
ShowNoTrackback();
}
else {
if (!$tb['checkDataFlag']) { // first visit to script
ShowIntro();
ShowTBInfo($tb);
ShowForm($tb);
}
else { // second or more visits to script
$error = CheckData($tb, $errorArray);
if ($error) { // if error in a form field, give user another chance to fix
ShowTBInfo($tb);
ShowErrors($errorArray);
ShowForm($tb);
}
else { // all form fields contain data
ShowTBInfo($tb);
Submit($tb);
WrapUp($tb);
}
}
}
?>





function RetrieveTBData(&$tb) {
// Retrieve data passed thru GET or POST methods.
$tb = array();
$tb['checkDataFlag'] = 0; // if first visit to script
$tb['checkDataFlag'] = $_REQUEST['checkDataFlag'];

// Data submitted from my weblog
$tb['tbURL'] = $_REQUEST['tb_url'];
$tb['myEntryTitle'] = $_REQUEST['tb_title'];
$tb['myEntryURL'] = $_REQUEST['tb_entry_url'];

// Data submitted from the form on this script
$tb['blogName'] = $_REQUEST['blogName'];
$tb['blogEntryTitle'] = $_REQUEST['blogEntryTitle'];
$tb['blogEntryURL'] = $_REQUEST['blogEntryURL'];
$tb['blogExcerpt'] = $_REQUEST['blogExcerpt'];
}


function ShowNoTrackback() {
// Display information when this page is accidently retrieved or no trackback URL has been submitted.
echo '

Somehow, you were sent to this page without any trackback information. Please access this page from an individual entry archive page.

'."\n";
}


function ShowIntro() {
// Display introduction about trackback system on first trip to form.
echo '

Trackbacks were developed by Movable Type to enhance cross-site conversations. They are like comments but are located on someone else’s weblog. If by chance you were inspired by one of my weblog entries and felt compelled to write your own weblog entry on your website (or wrote something similar to my entry), you can send me a trackback instead of writing a comment linking back to your website.

'."\n";
echo '

If you do not use Movable Type or some other system that supports trackback, you can still participate by using this manual trackback form.

'."\n";
echo '

'."\n";
}


function ShowTBInfo($tb) {
// Display trackback URL and if included, title of its weblog entry with corresponding link.
if ($tb['myEntryTitle'] != '') {
if ($tb['myEntryURL'] != '') {
echo '

For entry:  '.$tb['myEntryTitle'].'

'."\n";
}
else {
echo '

For entry:  '.$tb['myEntryTitle'].'

'."\n";
}
}
echo '

Trackback URL:  '.$tb['tbURL'].'

'."\n";
}


function ShowForm($tb) {
// Show form
echo '
'."\n";
echo ''."\n";
echo ''."\n";
echo ''."\n";
echo ''."\n";

echo '

'."\n";
echo ''."\n";

echo '

'."\n";
echo ''."\n";

$link = ($tb['blogEntryURL'] == '' && !$tb['checkDataFlag']) ? 'http://' : $tb['blogEntryURL'];
echo '

'."\n";
echo ''."\n";

echo '

'."\n";
echo '

'."\n";

echo ''."\n";
echo '
'."\n";
}


function CheckData($tb, &$errorArray) {
// Return 0 if no errors and 1 if there are errors with list of errors in $errorArray
$errorArray = array();
if (!$tb['blogName']) $errorArray[] = 'No weblog name entered';
if (!$tb['blogEntryTitle']) $errorArray[] = 'No title or date for weblog entry entered';
if (!$tb['blogEntryURL']) $errorArray[] = 'No weblog URL entered';
else {
ereg('(^http://)(.{0,})', $tb['blogEntryURL'], $pieces);
if ($pieces[1] != 'http://') {
$errorArray[] = 'Weblog URL needs to begin with http://';
}
elseif ($pieces[2] == '') {
$errorArray[] = 'No weblog URL entered';
}
}
if (!$tb['blogExcerpt']) $errorArray[] = 'No excerpt entered';
$errorCount = count($errorArray);
$errorFlag = ($errorCount > 0);
return $errorFlag;
}


function ShowErrors($errorArray) {
// Show errors
echo '

Errors were found:

'."\n";
echo '
    '."\n";
    foreach ($errorArray as $error) {
    echo ''.$error.''."\n";
    }
    echo '
'."\n";
}


function Submit($tb) {
// Send trackback ping and display results
// For specifications, see http://www.movabletype.org/docs/mttrackback.html
$snoopy = new Snoopy;
$submitURL = $tb['tbURL'];
$submitVars['Content-Type'] = 'application/x-www-form-urlencoded';
$submitVars['title'] = $tb['blogEntryTitle'];
$submitVars['url'] = $tb['blogEntryURL'];
$submitVars['blog_name'] = $tb['blogName'];
$submitVars['excerpt'] = $tb['blogExcerpt'];

$snoopy->submit($submitURL, $submitVars);
$pingReply = $snoopy->results;

// search through XML reply for any ping errors
if (ereg('([01])', $pingReply, $pieces)) {
$pingError = $pieces[1];
}
else {
$pingError = 1;
}
if (!$pingError) {
$message = 'Trackback was successful!';
}
else {
if (ereg('(.{0,})', $pingReply, $pieces)) {
$message = 'Error:  '.$pieces[1];
}
else {
$message = 'Error:  Unknown';
}
}
echo '

'.$message.'

'."\n";
}


function WrapUp($tb) {
// Display links back to homepage and/or to originating entry page
global $myHomeURL, $myBlogName;
echo '

Return to '.$myBlogName.' Home';
if ($tb['myEntryURL'] != '' && $tb['myEntryTitle'] != '') {
echo ' or '.$tb['myEntryTitle'].'';
}
echo '

'."\n";
}
?>

Комментариев нет: