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