3 * Handle Trackbacks and Pingbacks sent to WordPress
9 require_once('./wp-load.php');
14 * trackback_response() - Respond with error or success XML message
16 * @param int|bool $error Whether there was an error or not
17 * @param string $error_message Error message if an error occurred
19 function trackback_response($error = 0, $error_message = '') {
20 header('Content-Type: text/xml; charset=' . get_option('blog_charset') );
22 echo '<?xml version="1.0" encoding="utf-8"?'.">\n";
24 echo "<error>1</error>\n";
25 echo "<message>$error_message</message>\n";
29 echo '<?xml version="1.0" encoding="utf-8"?'.">\n";
31 echo "<error>0</error>\n";
36 // trackback is done by a POST
37 $request_array = 'HTTP_POST_VARS';
39 if ( !$_GET['tb_id'] ) {
40 $tb_id = explode('/', $_SERVER['REQUEST_URI']);
41 $tb_id = intval( $tb_id[ count($tb_id) - 1 ] );
44 $tb_url = $_POST['url'];
45 $charset = $_POST['charset'];
47 // These three are stripslashed here so that they can be properly escaped after mb_convert_encoding()
48 $title = stripslashes($_POST['title']);
49 $excerpt = stripslashes($_POST['excerpt']);
50 $blog_name = stripslashes($_POST['blog_name']);
53 $charset = strtoupper( trim($charset) );
55 $charset = 'ASCII, UTF-8, ISO-8859-1, JIS, EUC-JP, SJIS';
57 // No valid uses for UTF-7
58 if ( false !== strpos($charset, 'UTF-7') )
61 if ( function_exists('mb_convert_encoding') ) { // For international trackbacks
62 $title = mb_convert_encoding($title, get_option('blog_charset'), $charset);
63 $excerpt = mb_convert_encoding($excerpt, get_option('blog_charset'), $charset);
64 $blog_name = mb_convert_encoding($blog_name, get_option('blog_charset'), $charset);
67 // Now that mb_convert_encoding() has been given a swing, we need to escape these three
68 $title = $wpdb->escape($title);
69 $excerpt = $wpdb->escape($excerpt);
70 $blog_name = $wpdb->escape($blog_name);
72 if ( is_single() || is_page() )
73 $tb_id = $posts[0]->ID;
75 if ( !intval( $tb_id ) )
76 trackback_response(1, 'I really need an ID for this to work.');
78 if (empty($title) && empty($tb_url) && empty($blog_name)) {
79 // If it doesn't look like a trackback at all...
80 wp_redirect(get_permalink($tb_id));
84 if ( !empty($tb_url) && !empty($title) ) {
85 header('Content-Type: text/xml; charset=' . get_option('blog_charset') );
87 if ( !pings_open($tb_id) )
88 trackback_response(1, 'Sorry, trackbacks are closed for this item.');
90 $title = wp_html_excerpt( $title, 250 ).'...';
91 $excerpt = wp_html_excerpt( $excerpt, 252 ).'...';
93 $comment_post_ID = (int) $tb_id;
94 $comment_author = $blog_name;
95 $comment_author_email = '';
96 $comment_author_url = $tb_url;
97 $comment_content = "<strong>$title</strong>\n\n$excerpt";
98 $comment_type = 'trackback';
100 $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) );
102 trackback_response(1, 'We already have a ping from that URL for this post.');
104 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type');
106 wp_new_comment($commentdata);
108 do_action('trackback_post', $wpdb->insert_id);
109 trackback_response(0);