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