| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * Handle Trackbacks and Pingbacks sent to WordPress |
|---|
| 4 | * |
|---|
| 5 | * @package WordPress |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | if (empty($wp)) { |
|---|
| 9 | require_once('./wp-load.php'); |
|---|
| 10 | wp( array( 'tb' => '1' ) ); |
|---|
| 11 | } |
|---|
| 12 | |
|---|
| 13 | /** |
|---|
| 14 | * trackback_response() - Respond with error or success XML message |
|---|
| 15 | * |
|---|
| 16 | * @param int|bool $error Whether there was an error |
|---|
| 17 | * @param string $error_message Error message if an error occurred |
|---|
| 18 | */ |
|---|
| 19 | function trackback_response($error = 0, $error_message = '') { |
|---|
| 20 | header('Content-Type: text/xml; charset=' . get_option('blog_charset') ); |
|---|
| 21 | if ($error) { |
|---|
| 22 | echo '<?xml version="1.0" encoding="utf-8"?'.">\n"; |
|---|
| 23 | echo "<response>\n"; |
|---|
| 24 | echo "<error>1</error>\n"; |
|---|
| 25 | echo "<message>$error_message</message>\n"; |
|---|
| 26 | echo "</response>"; |
|---|
| 27 | die(); |
|---|
| 28 | } else { |
|---|
| 29 | echo '<?xml version="1.0" encoding="utf-8"?'.">\n"; |
|---|
| 30 | echo "<response>\n"; |
|---|
| 31 | echo "<error>0</error>\n"; |
|---|
| 32 | echo "</response>"; |
|---|
| 33 | } |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | // trackback is done by a POST |
|---|
| 37 | $request_array = 'HTTP_POST_VARS'; |
|---|
| 38 | |
|---|
| 39 | if ( !isset($_GET['tb_id']) || !$_GET['tb_id'] ) { |
|---|
| 40 | $tb_id = explode('/', $_SERVER['REQUEST_URI']); |
|---|
| 41 | $tb_id = intval( $tb_id[ count($tb_id) - 1 ] ); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | $tb_url = isset($_POST['url']) ? $_POST['url'] : ''; |
|---|
| 45 | $charset = isset($_POST['charset']) ? $_POST['charset'] : ''; |
|---|
| 46 | |
|---|
| 47 | // These three are stripslashed here so that they can be properly escaped after mb_convert_encoding() |
|---|
| 48 | $title = isset($_POST['title']) ? stripslashes($_POST['title']) : ''; |
|---|
| 49 | $excerpt = isset($_POST['excerpt']) ? stripslashes($_POST['excerpt']) : ''; |
|---|
| 50 | $blog_name = isset($_POST['blog_name']) ? stripslashes($_POST['blog_name']) : ''; |
|---|
| 51 | |
|---|
| 52 | if ($charset) |
|---|
| 53 | $charset = str_replace( array(',', ' '), '', strtoupper( trim($charset) ) ); |
|---|
| 54 | else |
|---|
| 55 | $charset = 'ASCII, UTF-8, ISO-8859-1, JIS, EUC-JP, SJIS'; |
|---|
| 56 | |
|---|
| 57 | // No valid uses for UTF-7 |
|---|
| 58 | if ( false !== strpos($charset, 'UTF-7') ) |
|---|
| 59 | die; |
|---|
| 60 | |
|---|
| 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); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 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); |
|---|
| 71 | |
|---|
| 72 | if ( is_single() || is_page() ) |
|---|
| 73 | $tb_id = $posts[0]->ID; |
|---|
| 74 | |
|---|
| 75 | if ( !isset($tb_id) || !intval( $tb_id ) ) |
|---|
| 76 | trackback_response(1, 'I really need an ID for this to work.'); |
|---|
| 77 | |
|---|
| 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)); |
|---|
| 81 | exit; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | if ( !empty($tb_url) && !empty($title) ) { |
|---|
| 85 | header('Content-Type: text/xml; charset=' . get_option('blog_charset') ); |
|---|
| 86 | |
|---|
| 87 | if ( !pings_open($tb_id) ) |
|---|
| 88 | trackback_response(1, 'Sorry, trackbacks are closed for this item.'); |
|---|
| 89 | |
|---|
| 90 | $title = wp_html_excerpt( $title, 250 ).'...'; |
|---|
| 91 | $excerpt = wp_html_excerpt( $excerpt, 252 ).'...'; |
|---|
| 92 | |
|---|
| 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'; |
|---|
| 99 | |
|---|
| 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) ); |
|---|
| 101 | if ( $dupe ) |
|---|
| 102 | trackback_response(1, 'We already have a ping from that URL for this post.'); |
|---|
| 103 | |
|---|
| 104 | $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type'); |
|---|
| 105 | |
|---|
| 106 | wp_new_comment($commentdata); |
|---|
| 107 | |
|---|
| 108 | do_action('trackback_post', $wpdb->insert_id); |
|---|
| 109 | trackback_response(0); |
|---|
| 110 | } |
|---|
| 111 | ?> |
|---|