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 $title = $_POST['title'];
35 $excerpt = $_POST['excerpt'];
36 $blog_name = $_POST['blog_name'];
37 $charset = $_POST['charset'];
40 $charset = strtoupper( trim($charset) );
42 $charset = 'ASCII, UTF-8, ISO-8859-1, JIS, EUC-JP, SJIS';
44 if ( function_exists('mb_convert_encoding') ) { // For international trackbacks
45 $title = mb_convert_encoding($title, get_settings('blog_charset'), $charset);
46 $excerpt = mb_convert_encoding($excerpt, get_settings('blog_charset'), $charset);
47 $blog_name = mb_convert_encoding($blog_name, get_settings('blog_charset'), $charset);
50 if ( is_single() || is_page() )
51 $tb_id = $posts[0]->ID;
53 if ( !intval( $tb_id ) )
54 trackback_response(1, 'I really need an ID for this to work.');
56 if (empty($title) && empty($tb_url) && empty($blog_name)) {
57 // If it doesn't look like a trackback at all...
58 header('Location: ' . get_permalink($tb_id));
62 if ( !empty($tb_url) && !empty($title) && !empty($tb_url) ) {
63 header('Content-Type: text/xml; charset=' . get_option('blog_charset') );
65 $pingstatus = $wpdb->get_var("SELECT ping_status FROM $wpdb->posts WHERE ID = $tb_id");
67 if ( 'open' != $pingstatus )
68 trackback_response(1, 'Sorry, trackbacks are closed for this item.');
70 $title = wp_specialchars( strip_tags( $title ) );
71 $excerpt = strip_tags($excerpt);
72 if ( function_exists('mb_strcut') ) { // For international trackbacks
73 $excerpt = mb_strcut($excerpt, 0, 252, get_settings('blog_charset')) . '...';
74 $title = mb_strcut($title, 0, 250, get_settings('blog_charset')) . '...';
76 $excerpt = (strlen($excerpt) > 255) ? substr($excerpt, 0, 252) . '...' : $excerpt;
77 $title = (strlen($title) > 250) ? substr($title, 0, 250) . '...' : $title;
80 $comment_post_ID = $tb_id;
81 $comment_author = $blog_name;
82 $comment_author_email = '';
83 $comment_author_url = $tb_url;
84 $comment_content = "<strong>$title</strong>\n\n$excerpt";
85 $comment_type = 'trackback';
87 $dupe = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND comment_author_url = '$comment_author_url'");
89 trackback_response(1, 'We already have a ping from that URI for this post.');
91 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type');
93 wp_new_comment($commentdata);
95 do_action('trackback_post', $wpdb->insert_id);
96 trackback_response(0);