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 // No valid uses for UTF-7
47 if ( false !== strpos($charset, 'UTF-7') )
50 if ( function_exists('mb_convert_encoding') ) { // For international trackbacks
51 $title = mb_convert_encoding($title, get_option('blog_charset'), $charset);
52 $excerpt = mb_convert_encoding($excerpt, get_option('blog_charset'), $charset);
53 $blog_name = mb_convert_encoding($blog_name, get_option('blog_charset'), $charset);
56 // Now that mb_convert_encoding() has been given a swing, we need to escape these three
57 $title = $wpdb->escape($title);
58 $excerpt = $wpdb->escape($excerpt);
59 $blog_name = $wpdb->escape($blog_name);
61 if ( is_single() || is_page() )
62 $tb_id = $posts[0]->ID;
64 if ( !intval( $tb_id ) )
65 trackback_response(1, 'I really need an ID for this to work.');
67 if (empty($title) && empty($tb_url) && empty($blog_name)) {
68 // If it doesn't look like a trackback at all...
69 wp_redirect(get_permalink($tb_id));
73 if ( !empty($tb_url) && !empty($title) ) {
74 header('Content-Type: text/xml; charset=' . get_option('blog_charset') );
76 if ( !pings_open($tb_id) )
77 trackback_response(1, 'Sorry, trackbacks are closed for this item.');
79 $title = wp_html_excerpt( $title, 250 ).'...';
80 $excerpt = wp_html_excerpt( $excerpt, 252 ).'...';
82 $comment_post_ID = (int) $tb_id;
83 $comment_author = $blog_name;
84 $comment_author_email = '';
85 $comment_author_url = $tb_url;
86 $comment_content = "<strong>$title</strong>\n\n$excerpt";
87 $comment_type = 'trackback';
89 $dupe = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND comment_author_url = '$comment_author_url'");
91 trackback_response(1, 'We already have a ping from that URL for this post.');
93 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type');
95 wp_new_comment($commentdata);
97 do_action('trackback_post', $wpdb->insert_id);
98 trackback_response(0);