PageRenderTime 27ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-trackback.php

http://github.com/wordpress/wordpress
PHP | 152 lines | 84 code | 24 blank | 44 comment | 21 complexity | 0b86f565bb77cf6fa412a59ece526446 MD5 | raw file
Possible License(s): 0BSD
  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. /**
  15. * Response to a trackback.
  16. *
  17. * Responds with an error or success XML message.
  18. *
  19. * @since 0.71
  20. *
  21. * @param int|bool $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. }
  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. $tb_id = $posts[0]->ID;
  74. }
  75. if ( ! isset( $tb_id ) || ! intval( $tb_id ) ) {
  76. trackback_response( 1, __( 'I really need an ID for this to work.' ) );
  77. }
  78. if ( empty( $title ) && empty( $tb_url ) && empty( $blog_name ) ) {
  79. // If it doesn't look like a trackback at all.
  80. wp_redirect( get_permalink( $tb_id ) );
  81. exit;
  82. }
  83. if ( ! empty( $tb_url ) && ! empty( $title ) ) {
  84. /**
  85. * Fires before the trackback is added to a post.
  86. *
  87. * @since 4.7.0
  88. *
  89. * @param int $tb_id Post ID related to the trackback.
  90. * @param string $tb_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', $tb_id, $tb_url, $charset, $title, $excerpt, $blog_name );
  97. header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) );
  98. if ( ! pings_open( $tb_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) $tb_id;
  104. $comment_author = $blog_name;
  105. $comment_author_email = '';
  106. $comment_author_url = $tb_url;
  107. $comment_content = "<strong>$title</strong>\n\n$excerpt";
  108. $comment_type = 'trackback';
  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. $result = wp_new_comment( $commentdata );
  115. if ( is_wp_error( $result ) ) {
  116. trackback_response( 1, $result->get_error_message() );
  117. }
  118. $trackback_id = $wpdb->insert_id;
  119. /**
  120. * Fires after a trackback is added to a post.
  121. *
  122. * @since 1.2.0
  123. *
  124. * @param int $trackback_id Trackback ID.
  125. */
  126. do_action( 'trackback_post', $trackback_id );
  127. trackback_response( 0 );
  128. }