PageRenderTime 463ms CodeModel.GetById 24ms RepoModel.GetById 8ms app.codeStats 0ms

/new/wp-trackback.php

https://bitbucket.org/derekeder/thoughtyouknew.us
PHP | 111 lines | 74 code | 21 blank | 16 comment | 19 complexity | 7280ebf7905973b3f152a578136e255d MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1, GPL-2.0, Apache-2.0, GPL-3.0
  1. <?php
  2. /**
  3. * Handle Trackbacks and Pingbacks sent to WordPress
  4. *
  5. * @package WordPress
  6. */
  7. if (empty($wp)) {
  8. require_once('./wp-load.php');
  9. wp( array( 'tb' => '1' ) );
  10. }
  11. /**
  12. * trackback_response() - Respond with error or success XML message
  13. *
  14. * @param int|bool $error Whether there was an error
  15. * @param string $error_message Error message if an error occurred
  16. */
  17. function trackback_response($error = 0, $error_message = '') {
  18. header('Content-Type: text/xml; charset=' . get_option('blog_charset') );
  19. if ($error) {
  20. echo '<?xml version="1.0" encoding="utf-8"?'.">\n";
  21. echo "<response>\n";
  22. echo "<error>1</error>\n";
  23. echo "<message>$error_message</message>\n";
  24. echo "</response>";
  25. die();
  26. } else {
  27. echo '<?xml version="1.0" encoding="utf-8"?'.">\n";
  28. echo "<response>\n";
  29. echo "<error>0</error>\n";
  30. echo "</response>";
  31. }
  32. }
  33. // trackback is done by a POST
  34. $request_array = 'HTTP_POST_VARS';
  35. if ( !isset($_GET['tb_id']) || !$_GET['tb_id'] ) {
  36. $tb_id = explode('/', $_SERVER['REQUEST_URI']);
  37. $tb_id = intval( $tb_id[ count($tb_id) - 1 ] );
  38. }
  39. $tb_url = isset($_POST['url']) ? $_POST['url'] : '';
  40. $charset = isset($_POST['charset']) ? $_POST['charset'] : '';
  41. // These three are stripslashed here so that they can be properly escaped after mb_convert_encoding()
  42. $title = isset($_POST['title']) ? stripslashes($_POST['title']) : '';
  43. $excerpt = isset($_POST['excerpt']) ? stripslashes($_POST['excerpt']) : '';
  44. $blog_name = isset($_POST['blog_name']) ? stripslashes($_POST['blog_name']) : '';
  45. if ($charset)
  46. $charset = str_replace( array(',', ' '), '', strtoupper( trim($charset) ) );
  47. else
  48. $charset = 'ASCII, UTF-8, ISO-8859-1, JIS, EUC-JP, SJIS';
  49. // No valid uses for UTF-7
  50. if ( false !== strpos($charset, 'UTF-7') )
  51. die;
  52. if ( function_exists('mb_convert_encoding') ) { // For international trackbacks
  53. $title = mb_convert_encoding($title, get_option('blog_charset'), $charset);
  54. $excerpt = mb_convert_encoding($excerpt, get_option('blog_charset'), $charset);
  55. $blog_name = mb_convert_encoding($blog_name, get_option('blog_charset'), $charset);
  56. }
  57. // Now that mb_convert_encoding() has been given a swing, we need to escape these three
  58. $title = $wpdb->escape($title);
  59. $excerpt = $wpdb->escape($excerpt);
  60. $blog_name = $wpdb->escape($blog_name);
  61. if ( is_single() || is_page() )
  62. $tb_id = $posts[0]->ID;
  63. if ( !isset($tb_id) || !intval( $tb_id ) )
  64. trackback_response(1, 'I really need an ID for this to work.');
  65. if (empty($title) && empty($tb_url) && empty($blog_name)) {
  66. // If it doesn't look like a trackback at all...
  67. wp_redirect(get_permalink($tb_id));
  68. exit;
  69. }
  70. if ( !empty($tb_url) && !empty($title) ) {
  71. header('Content-Type: text/xml; charset=' . get_option('blog_charset') );
  72. if ( !pings_open($tb_id) )
  73. trackback_response(1, 'Sorry, trackbacks are closed for this item.');
  74. $title = wp_html_excerpt( $title, 250 ).'...';
  75. $excerpt = wp_html_excerpt( $excerpt, 252 ).'...';
  76. $comment_post_ID = (int) $tb_id;
  77. $comment_author = $blog_name;
  78. $comment_author_email = '';
  79. $comment_author_url = $tb_url;
  80. $comment_content = "<strong>$title</strong>\n\n$excerpt";
  81. $comment_type = 'trackback';
  82. $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) );
  83. if ( $dupe )
  84. trackback_response(1, 'We already have a ping from that URL for this post.');
  85. $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type');
  86. wp_new_comment($commentdata);
  87. do_action('trackback_post', $wpdb->insert_id);
  88. trackback_response(0);
  89. }
  90. ?>