You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

142 lines
4.4 KiB

  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( dirname( __FILE__ ) . '/wp-load.php' );
  12. wp( array( 'tb' => '1' ) );
  13. }
  14. /**
  15. * Response to a trackback.
  16. *
  17. * Responds with an error or success XML message.
  18. *
  19. * @since 0.71
  20. *
  21. * @param mixed $error Whether there was an error.
  22. * Default '0'. Accepts '0' or '1', true or false.
  23. * @param string $error_message Error message if an error occurred.
  24. */
  25. function trackback_response($error = 0, $error_message = '') {
  26. header('Content-Type: text/xml; charset=' . get_option('blog_charset') );
  27. if ($error) {
  28. echo '<?xml version="1.0" encoding="utf-8"?'.">\n";
  29. echo "<response>\n";
  30. echo "<error>1</error>\n";
  31. echo "<message>$error_message</message>\n";
  32. echo "</response>";
  33. die();
  34. } else {
  35. echo '<?xml version="1.0" encoding="utf-8"?'.">\n";
  36. echo "<response>\n";
  37. echo "<error>0</error>\n";
  38. echo "</response>";
  39. }
  40. }
  41. // Trackback is done by a POST.
  42. $request_array = 'HTTP_POST_VARS';
  43. if ( !isset($_GET['tb_id']) || !$_GET['tb_id'] ) {
  44. $tb_id = explode('/', $_SERVER['REQUEST_URI']);
  45. $tb_id = intval( $tb_id[ count($tb_id) - 1 ] );
  46. }
  47. $tb_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. // No valid uses for UTF-7.
  58. if ( false !== strpos($charset, 'UTF-7') )
  59. die;
  60. // For international trackbacks.
  61. if ( function_exists('mb_convert_encoding') ) {
  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. // Now that mb_convert_encoding() has been given a swing, we need to escape these three.
  67. $title = wp_slash($title);
  68. $excerpt = wp_slash($excerpt);
  69. $blog_name = wp_slash($blog_name);
  70. if ( is_single() || is_page() )
  71. $tb_id = $posts[0]->ID;
  72. if ( !isset($tb_id) || !intval( $tb_id ) )
  73. trackback_response( 1, __( 'I really need an ID for this to work.' ) );
  74. if (empty($title) && empty($tb_url) && empty($blog_name)) {
  75. // If it doesn't look like a trackback at all.
  76. wp_redirect(get_permalink($tb_id));
  77. exit;
  78. }
  79. if ( !empty($tb_url) && !empty($title) ) {
  80. /**
  81. * Fires before the trackback is added to a post.
  82. *
  83. * @since 4.7.0
  84. *
  85. * @param int $tb_id Post ID related to the trackback.
  86. * @param string $tb_url Trackback URL.
  87. * @param string $charset Character Set.
  88. * @param string $title Trackback Title.
  89. * @param string $excerpt Trackback Excerpt.
  90. * @param string $blog_name Blog Name.
  91. */
  92. do_action( 'pre_trackback_post', $tb_id, $tb_url, $charset, $title, $excerpt, $blog_name );
  93. header('Content-Type: text/xml; charset=' . get_option('blog_charset') );
  94. if ( !pings_open($tb_id) )
  95. trackback_response( 1, __( 'Sorry, trackbacks are closed for this item.' ) );
  96. $title = wp_html_excerpt( $title, 250, '&#8230;' );
  97. $excerpt = wp_html_excerpt( $excerpt, 252, '&#8230;' );
  98. $comment_post_ID = (int) $tb_id;
  99. $comment_author = $blog_name;
  100. $comment_author_email = '';
  101. $comment_author_url = $tb_url;
  102. $comment_content = "<strong>$title</strong>\n\n$excerpt";
  103. $comment_type = 'trackback';
  104. $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) );
  105. if ( $dupe )
  106. trackback_response( 1, __( 'We already have a ping from that URL for this post.' ) );
  107. $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type');
  108. wp_new_comment($commentdata);
  109. $trackback_id = $wpdb->insert_id;
  110. /**
  111. * Fires after a trackback is added to a post.
  112. *
  113. * @since 1.2.0
  114. *
  115. * @param int $trackback_id Trackback ID.
  116. */
  117. do_action( 'trackback_post', $trackback_id );
  118. trackback_response( 0 );
  119. }