4 require_once('wp-config.php');
8 function trackback_response($error = 0, $error_message = '') {
9 header('Content-Type: text/xml; charset=' . get_option('blog_charset') );
11 echo '<?xml version="1.0" encoding="utf-8"?'.">\n";
13 echo "<error>1</error>\n";
14 echo "<message>$error_message</message>\n";
18 echo '<?xml version="1.0" encoding="utf-8"?'.">\n";
20 echo "<error>0</error>\n";
25 // trackback is done by a POST
26 $request_array = 'HTTP_POST_VARS';
28 if ( !$_GET['tb_id'] ) {
29 $tb_id = explode('/', $_SERVER['REQUEST_URI']);
30 $tb_id = intval( $tb_id[ count($tb_id) - 1 ] );
33 $tb_url = $_POST['url'];
34 $charset = $_POST['charset'];
36 // These three are stripslashed here so that they can be properly escaped after mb_convert_encoding()
37 $title = stripslashes($_POST['title']);
38 $excerpt = stripslashes($_POST['excerpt']);
39 $blog_name = stripslashes($_POST['blog_name']);
42 $charset = strtoupper( trim($charset) );
44 $charset = 'ASCII, UTF-8, ISO-8859-1, JIS, EUC-JP, SJIS';
46 if ( function_exists('mb_convert_encoding') ) { // For international trackbacks
47 $title = mb_convert_encoding($title, get_option('blog_charset'), $charset);
48 $excerpt = mb_convert_encoding($excerpt, get_option('blog_charset'), $charset);
49 $blog_name = mb_convert_encoding($blog_name, get_option('blog_charset'), $charset);
52 // Now that mb_convert_encoding() has been given a swing, we need to escape these three
53 $title = $wpdb->escape($title);
54 $excerpt = $wpdb->escape($excerpt);
55 $blog_name = $wpdb->escape($blog_name);
57 if ( is_single() || is_page() )
58 $tb_id = $posts[0]->ID;
60 if ( !intval( $tb_id ) )
61 trackback_response(1, 'I really need an ID for this to work.');
63 if (empty($title) && empty($tb_url) && empty($blog_name)) {
64 // If it doesn't look like a trackback at all...
65 wp_redirect(get_permalink($tb_id));
69 if ( !empty($tb_url) && !empty($title) && !empty($tb_url) ) {
70 header('Content-Type: text/xml; charset=' . get_option('blog_charset') );
72 $pingstatus = $wpdb->get_var("SELECT ping_status FROM $wpdb->posts WHERE ID = $tb_id");
74 if ( 'open' != $pingstatus )
75 trackback_response(1, 'Sorry, trackbacks are closed for this item.');
77 $title = wp_specialchars( strip_tags( $title ) );
78 $excerpt = strip_tags($excerpt);
79 if ( function_exists('mb_strcut') ) { // For international trackbacks
80 $excerpt = mb_strcut($excerpt, 0, 252, get_settings('blog_charset')) . '...';
81 $title = mb_strcut($title, 0, 250, get_settings('blog_charset')) . '...';
83 $excerpt = (strlen($excerpt) > 255) ? substr($excerpt, 0, 252) . '...' : $excerpt;
84 $title = (strlen($title) > 250) ? substr($title, 0, 250) . '...' : $title;
87 $comment_post_ID = (int) $tb_id;
88 $comment_author = $blog_name;
89 $comment_author_email = '';
90 $comment_author_url = $tb_url;
91 $comment_content = "<strong>$title</strong>\n\n$excerpt";
92 $comment_type = 'trackback';
94 $dupe = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND comment_author_url = '$comment_author_url'");
96 trackback_response(1, 'We already have a ping from that URI for this post.');
98 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type');
100 wp_new_comment($commentdata);
102 do_action('trackback_post', $wpdb->insert_id);
103 trackback_response(0);