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