Files
WordPress/wp-trackback.php
T

111 lines
3.6 KiB
PHP
Raw Normal View History

2003-12-11 00:22:36 +00:00
<?php
/**
* Handle Trackbacks and Pingbacks sent to WordPress
*
* @package WordPress
*/
2004-09-18 22:47:43 +00:00
if (empty($wp)) {
require_once('./wp-load.php');
wp( array( 'tb' => '1' ) );
2004-09-18 22:47:43 +00:00
}
/**
* trackback_response() - Respond with error or success XML message
*
* @param int|bool $error Whether there was an error
* @param string $error_message Error message if an error occurred
*/
2004-09-07 02:34:12 +00:00
function trackback_response($error = 0, $error_message = '') {
header('Content-Type: text/xml; charset=' . get_option('blog_charset') );
if ($error) {
echo '<?xml version="1.0" encoding="utf-8"?'.">\n";
echo "<response>\n";
echo "<error>1</error>\n";
echo "<message>$error_message</message>\n";
echo "</response>";
2004-09-23 12:27:52 +00:00
die();
2004-09-07 02:34:12 +00:00
} else {
echo '<?xml version="1.0" encoding="utf-8"?'.">\n";
echo "<response>\n";
echo "<error>0</error>\n";
echo "</response>";
}
}
// trackback is done by a POST
$request_array = 'HTTP_POST_VARS';
2004-12-16 02:57:05 +00:00
if ( !isset($_GET['tb_id']) || !$_GET['tb_id'] ) {
2004-09-07 02:34:12 +00:00
$tb_id = explode('/', $_SERVER['REQUEST_URI']);
$tb_id = intval( $tb_id[ count($tb_id) - 1 ] );
2004-09-07 02:34:12 +00:00
}
2004-12-16 02:57:05 +00:00
$tb_url = isset($_POST['url']) ? $_POST['url'] : '';
$charset = isset($_POST['charset']) ? $_POST['charset'] : '';
// These three are stripslashed here so that they can be properly escaped after mb_convert_encoding()
$title = isset($_POST['title']) ? stripslashes($_POST['title']) : '';
$excerpt = isset($_POST['excerpt']) ? stripslashes($_POST['excerpt']) : '';
$blog_name = isset($_POST['blog_name']) ? stripslashes($_POST['blog_name']) : '';
if ($charset)
$charset = str_replace( array(',', ' '), '', strtoupper( trim($charset) ) );
else
$charset = 'ASCII, UTF-8, ISO-8859-1, JIS, EUC-JP, SJIS';
2008-03-28 06:51:19 +00:00
// No valid uses for UTF-7
if ( false !== strpos($charset, 'UTF-7') )
die;
2004-12-16 02:57:05 +00:00
if ( function_exists('mb_convert_encoding') ) { // For international trackbacks
$title = mb_convert_encoding($title, get_option('blog_charset'), $charset);
$excerpt = mb_convert_encoding($excerpt, get_option('blog_charset'), $charset);
$blog_name = mb_convert_encoding($blog_name, get_option('blog_charset'), $charset);
}
// Now that mb_convert_encoding() has been given a swing, we need to escape these three
$title = $wpdb->escape($title);
$excerpt = $wpdb->escape($excerpt);
$blog_name = $wpdb->escape($blog_name);
if ( is_single() || is_page() )
$tb_id = $posts[0]->ID;
2004-09-07 02:34:12 +00:00
if ( !isset($tb_id) || !intval( $tb_id ) )
2004-09-07 02:34:12 +00:00
trackback_response(1, 'I really need an ID for this to work.');
if (empty($title) && empty($tb_url) && empty($blog_name)) {
// If it doesn't look like a trackback at all...
2006-06-27 05:38:56 +00:00
wp_redirect(get_permalink($tb_id));
2004-09-07 02:34:12 +00:00
exit;
2003-12-11 00:22:36 +00:00
}
if ( !empty($tb_url) && !empty($title) ) {
2004-09-07 02:34:12 +00:00
header('Content-Type: text/xml; charset=' . get_option('blog_charset') );
2003-12-11 00:22:36 +00:00
if ( !pings_open($tb_id) )
2003-12-11 00:22:36 +00:00
trackback_response(1, 'Sorry, trackbacks are closed for this item.');
$title = wp_html_excerpt( $title, 250 ).'...';
$excerpt = wp_html_excerpt( $excerpt, 252 ).'...';
2003-12-11 00:22:36 +00:00
2007-03-23 00:59:21 +00:00
$comment_post_ID = (int) $tb_id;
2004-09-22 19:44:35 +00:00
$comment_author = $blog_name;
2004-09-07 02:34:12 +00:00
$comment_author_email = '';
2003-12-11 00:22:36 +00:00
$comment_author_url = $tb_url;
2004-09-07 02:34:12 +00:00
$comment_content = "<strong>$title</strong>\n\n$excerpt";
$comment_type = 'trackback';
2003-12-11 00:22:36 +00:00
$dupe = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s", $comment_post_ID, $comment_author_url) );
2005-01-11 02:56:43 +00:00
if ( $dupe )
2006-08-30 16:40:17 +00:00
trackback_response(1, 'We already have a ping from that URL for this post.');
2005-01-11 02:56:43 +00:00
2004-09-22 19:44:35 +00:00
$commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type');
2003-12-11 00:22:36 +00:00
2004-09-07 02:34:12 +00:00
wp_new_comment($commentdata);
2003-12-11 00:22:36 +00:00
2004-09-07 02:34:12 +00:00
do_action('trackback_post', $wpdb->insert_id);
2005-02-12 07:30:21 +00:00
trackback_response(0);
2003-12-11 00:22:36 +00:00
}
?>