wp-trackback.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. if ( empty( $wp ) ) {
  11. require_once __DIR__ . '/wp-load.php';
  12. wp( array( 'tb' => '1' ) );
  13. }
  14. // Always run as an unauthenticated user.
  15. wp_set_current_user( 0 );
  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', true or false.
  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. if ( ! isset( $_GET['tb_id'] ) || ! $_GET['tb_id'] ) {
  44. $post_id = explode( '/', $_SERVER['REQUEST_URI'] );
  45. $post_id = (int) $post_id[ count( $post_id ) - 1 ];
  46. }
  47. $trackback_url = isset( $_POST['url'] ) ? $_POST['url'] : '';
  48. $charset = isset( $_POST['charset'] ) ? $_POST['charset'] : '';
  49. // These three are stripslashed here so they can be properly escaped after mb_convert_encoding().
  50. $title = isset( $_POST['title'] ) ? wp_unslash( $_POST['title'] ) : '';
  51. $excerpt = isset( $_POST['excerpt'] ) ? wp_unslash( $_POST['excerpt'] ) : '';
  52. $blog_name = isset( $_POST['blog_name'] ) ? wp_unslash( $_POST['blog_name'] ) : '';
  53. if ( $charset ) {
  54. $charset = str_replace( array( ',', ' ' ), '', strtoupper( trim( $charset ) ) );
  55. } else {
  56. $charset = 'ASCII, UTF-8, ISO-8859-1, JIS, EUC-JP, SJIS';
  57. }
  58. // No valid uses for UTF-7.
  59. if ( false !== strpos( $charset, 'UTF-7' ) ) {
  60. die;
  61. }
  62. // For international trackbacks.
  63. if ( function_exists( 'mb_convert_encoding' ) ) {
  64. $title = mb_convert_encoding( $title, get_option( 'blog_charset' ), $charset );
  65. $excerpt = mb_convert_encoding( $excerpt, get_option( 'blog_charset' ), $charset );
  66. $blog_name = mb_convert_encoding( $blog_name, get_option( 'blog_charset' ), $charset );
  67. }
  68. // Now that mb_convert_encoding() has been given a swing, we need to escape these three.
  69. $title = wp_slash( $title );
  70. $excerpt = wp_slash( $excerpt );
  71. $blog_name = wp_slash( $blog_name );
  72. if ( is_single() || is_page() ) {
  73. $post_id = $posts[0]->ID;
  74. }
  75. if ( ! isset( $post_id ) || ! (int) $post_id ) {
  76. trackback_response( 1, __( 'I really need an ID for this to work.' ) );
  77. }
  78. if ( empty( $title ) && empty( $trackback_url ) && empty( $blog_name ) ) {
  79. // If it doesn't look like a trackback at all.
  80. wp_redirect( get_permalink( $post_id ) );
  81. exit;
  82. }
  83. if ( ! empty( $trackback_url ) && ! empty( $title ) ) {
  84. /**
  85. * Fires before the trackback is added to a post.
  86. *
  87. * @since 4.7.0
  88. *
  89. * @param int $post_id Post ID related to the trackback.
  90. * @param string $trackback_url Trackback URL.
  91. * @param string $charset Character set.
  92. * @param string $title Trackback title.
  93. * @param string $excerpt Trackback excerpt.
  94. * @param string $blog_name Blog name.
  95. */
  96. do_action( 'pre_trackback_post', $post_id, $trackback_url, $charset, $title, $excerpt, $blog_name );
  97. header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) );
  98. if ( ! pings_open( $post_id ) ) {
  99. trackback_response( 1, __( 'Sorry, trackbacks are closed for this item.' ) );
  100. }
  101. $title = wp_html_excerpt( $title, 250, '&#8230;' );
  102. $excerpt = wp_html_excerpt( $excerpt, 252, '&#8230;' );
  103. $comment_post_id = (int) $post_id;
  104. $comment_author = $blog_name;
  105. $comment_author_email = '';
  106. $comment_author_url = $trackback_url;
  107. $comment_content = "<strong>$title</strong>\n\n$excerpt";
  108. $comment_type = 'trackback';
  109. $dupe = $wpdb->get_results(
  110. $wpdb->prepare(
  111. "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s",
  112. $comment_post_id,
  113. $comment_author_url
  114. )
  115. );
  116. if ( $dupe ) {
  117. trackback_response( 1, __( 'There is already a ping from that URL for this post.' ) );
  118. }
  119. $commentdata = array(
  120. 'comment_post_ID' => $comment_post_id,
  121. );
  122. $commentdata += compact(
  123. 'comment_author',
  124. 'comment_author_email',
  125. 'comment_author_url',
  126. 'comment_content',
  127. 'comment_type'
  128. );
  129. $result = wp_new_comment( $commentdata );
  130. if ( is_wp_error( $result ) ) {
  131. trackback_response( 1, $result->get_error_message() );
  132. }
  133. $trackback_id = $wpdb->insert_id;
  134. /**
  135. * Fires after a trackback is added to a post.
  136. *
  137. * @since 1.2.0
  138. *
  139. * @param int $trackback_id Trackback ID.
  140. */
  141. do_action( 'trackback_post', $trackback_id );
  142. trackback_response( 0 );
  143. }