root/tags/1.5.1.3/wp-trackback.php

Revision 2563, 3.0 kB (checked in by matt, 3 years ago)

Better charset audo-detection - http://mosquito.wordpress.org/view.php?id=788

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 require_once( dirname(__FILE__) . '/wp-config.php' );
3
4 if ( empty($doing_trackback) ) {
5     $doing_trackback = true;
6     $tb = true;
7     require_once('wp-blog-header.php');
8 }
9
10 function trackback_response($error = 0, $error_message = '') {
11     header('Content-Type: text/xml; charset=' . get_option('blog_charset') );
12     if ($error) {
13         echo '<?xml version="1.0" encoding="utf-8"?'.">\n";
14         echo "<response>\n";
15         echo "<error>1</error>\n";
16         echo "<message>$error_message</message>\n";
17         echo "</response>";
18         die();
19     } else {
20         echo '<?xml version="1.0" encoding="utf-8"?'.">\n";
21         echo "<response>\n";
22         echo "<error>0</error>\n";
23         echo "</response>";
24     }
25 }
26
27 // trackback is done by a POST
28 $request_array = 'HTTP_POST_VARS';
29
30 if ( !$_GET['tb_id'] ) {
31     $tb_id = explode('/', $_SERVER['REQUEST_URI']);
32     $tb_id = intval( $tb_id[ count($tb_id) - 1 ] );
33 }
34
35 $tb_url    = $_POST['url'];
36 $title     = $_POST['title'];
37 $excerpt   = $_POST['excerpt'];
38 $blog_name = $_POST['blog_name'];
39 $charset   = $_POST['charset'];
40
41 if ($charset)
42     $charset = strtoupper( trim($charset) );
43 else
44     $charset = 'ASCII, UTF-8, ISO-8859-1, JIS, EUC-JP, SJIS';
45
46 if ( function_exists('mb_convert_encoding') ) { // For international trackbacks
47     $title     = mb_convert_encoding($title, get_settings('blog_charset'), $charset);
48     $excerpt   = mb_convert_encoding($excerpt, get_settings('blog_charset'), $charset);
49     $blog_name = mb_convert_encoding($blog_name, get_settings('blog_charset'), $charset);
50 }
51
52 if ( is_single() || is_page() )
53     $tb_id = $posts[0]->ID;
54
55 if ( !intval( $tb_id ) )
56     trackback_response(1, 'I really need an ID for this to work.');
57
58 if (empty($title) && empty($tb_url) && empty($blog_name)) {
59     // If it doesn't look like a trackback at all...
60     header('Location: ' . get_permalink($tb_id));
61     exit;
62 }
63
64 if ( !empty($tb_url) && !empty($title) && !empty($tb_url) ) {
65     header('Content-Type: text/xml; charset=' . get_option('blog_charset') );
66
67     $pingstatus = $wpdb->get_var("SELECT ping_status FROM $wpdb->posts WHERE ID = $tb_id");
68
69     if ( 'open' != $pingstatus )
70         trackback_response(1, 'Sorry, trackbacks are closed for this item.');
71
72     $title wp_specialchars( strip_tags( $title ) );
73     $title = (strlen($title) > 250) ? substr($title, 0, 250) . '...' : $title;
74     $excerpt = strip_tags($excerpt);
75     $excerpt = (strlen($excerpt) > 255) ? substr($excerpt, 0, 252) . '...' : $excerpt;
76
77     $comment_post_ID = $tb_id;
78     $comment_author = $blog_name;
79     $comment_author_email = '';
80     $comment_author_url = $tb_url;
81     $comment_content = "<strong>$title</strong>\n\n$excerpt";
82     $comment_type = 'trackback';
83
84     $dupe = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND comment_author_url = '$comment_author_url'");
85     if ( $dupe )
86         trackback_response(1, 'We already have a ping from that URI for this post.');
87
88     $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type');
89
90     wp_new_comment($commentdata);
91
92     do_action('trackback_post', $wpdb->insert_id);
93     trackback_response(0);
94 }
95 ?>
Note: See TracBrowser for help on using the browser.