Make WordPress Core

Changeset 9012


Ignore:
Timestamp:
09/27/2008 09:40:09 PM (17 years ago)
Author:
westi
Message:

Move the pingbacks and trackbacks to the new HTTP api. See #7793 props jacobsantos.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/comment.php

    r8956 r9012  
    10521052 *
    10531053 * @since 1.5.0
    1054  * @uses $wp_version
    10551054 *
    10561055 * @param string $url URL to ping.
    1057  * @param int $timeout_bytes Number of bytes to timeout at. Prevents big file downloads, default is 2048.
     1056 * @param int $deprecated Not Used.
    10581057 * @return bool|string False on failure, string containing URI on success.
    10591058 */
    1060 function discover_pingback_server_uri($url, $timeout_bytes = 2048) {
    1061     global $wp_version;
    1062 
    1063     $byte_count = 0;
    1064     $contents = '';
    1065     $headers = '';
     1059function discover_pingback_server_uri($url, $deprecated = 2048) {
     1060
    10661061    $pingback_str_dquote = 'rel="pingback"';
    10671062    $pingback_str_squote = 'rel=\'pingback\'';
    1068     $x_pingback_str = 'x-pingback: ';
    1069 
    1070     extract(parse_url($url), EXTR_SKIP);
    1071 
    1072     if ( !isset($host) ) // Not an URL. This should never happen.
     1063
     1064    /** @todo Should use Filter Extension or custom preg_match instead. */
     1065    $parsed_url = parse_url($url);
     1066
     1067    if ( ! isset( $parsed_url['host'] ) ) // Not an URL. This should never happen.
    10731068        return false;
    10741069
    1075     $path  = ( !isset($path) ) ? '/'          : $path;
    1076     $path .= ( isset($query) ) ? '?' . $query : '';
    1077     $port  = ( isset($port)  ) ? $port        : 80;
    1078 
    1079     // Try to connect to the server at $host
    1080     $fp = @fsockopen($host, $port, $errno, $errstr, 2);
    1081     if ( !$fp ) // Couldn't open a connection to $host
     1070    $response = wp_remote_get( $url, array( 'timeout' => 2, 'httpversion' => '1.1' ) );
     1071
     1072    if ( is_wp_error( $response ) )
    10821073        return false;
    10831074
    1084     // Send the GET request
    1085     $request = "GET $path HTTP/1.1\r\nHost: $host\r\nUser-Agent: WordPress/$wp_version \r\n\r\n";
    1086     // ob_end_flush();
    1087     fputs($fp, $request);
    1088 
    1089     // Let's check for an X-Pingback header first
    1090     while ( !feof($fp) ) {
    1091         $line = fgets($fp, 512);
    1092         if ( trim($line) == '' )
    1093             break;
    1094         $headers .= trim($line)."\n";
    1095         $x_pingback_header_offset = strpos(strtolower($headers), $x_pingback_str);
    1096         if ( $x_pingback_header_offset ) {
    1097             // We got it!
    1098             preg_match('#x-pingback: (.+)#is', $headers, $matches);
    1099             $pingback_server_url = trim($matches[1]);
     1075    if ( isset( $response['headers']['x-pingback'] ) )
     1076        return $response['headers']['x-pingback'];
     1077
     1078    // Not an (x)html, sgml, or xml page, no use going further.
     1079    if ( isset( $response['headers']['content-type'] ) && preg_match('#(image|audio|video|model)/#is', $response['headers']['content-type']) )
     1080        return false;
     1081
     1082    $contents = $response['body'];
     1083
     1084    $pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote);
     1085    $pingback_link_offset_squote = strpos($contents, $pingback_str_squote);
     1086    if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) {
     1087        $quote = ($pingback_link_offset_dquote) ? '"' : '\'';
     1088        $pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
     1089        $pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset);
     1090        $pingback_href_start = $pingback_href_pos+6;
     1091        $pingback_href_end = @strpos($contents, $quote, $pingback_href_start);
     1092        $pingback_server_url_len = $pingback_href_end - $pingback_href_start;
     1093        $pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len);
     1094
     1095        // We may find rel="pingback" but an incomplete pingback URL
     1096        if ( $pingback_server_url_len > 0 ) { // We got it!
    11001097            return $pingback_server_url;
    11011098        }
    1102         if ( strpos(strtolower($headers), 'content-type: ') ) {
    1103             preg_match('#content-type: (.+)#is', $headers, $matches);
    1104             $content_type = trim($matches[1]);
    1105         }
    1106     }
    1107 
    1108     if ( preg_match('#(image|audio|video|model)/#is', $content_type) ) // Not an (x)html, sgml, or xml page, no use going further
    1109         return false;
    1110 
    1111     while ( !feof($fp) ) {
    1112         $line = fgets($fp, 1024);
    1113         $contents .= trim($line);
    1114         $pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote);
    1115         $pingback_link_offset_squote = strpos($contents, $pingback_str_squote);
    1116         if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) {
    1117             $quote = ($pingback_link_offset_dquote) ? '"' : '\'';
    1118             $pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
    1119             $pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset);
    1120             $pingback_href_start = $pingback_href_pos+6;
    1121             $pingback_href_end = @strpos($contents, $quote, $pingback_href_start);
    1122             $pingback_server_url_len = $pingback_href_end - $pingback_href_start;
    1123             $pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len);
    1124             // We may find rel="pingback" but an incomplete pingback URL
    1125             if ( $pingback_server_url_len > 0 ) { // We got it!
    1126                 fclose($fp);
    1127                 return $pingback_server_url;
    1128             }
    1129         }
    1130         $byte_count += strlen($line);
    1131         if ( $byte_count > $timeout_bytes ) {
    1132             // It's no use going further, there probably isn't any pingback
    1133             // server to find in this file. (Prevents loading large files.)
    1134             fclose($fp);
    1135             return false;
    1136         }
    1137     }
    1138 
    1139     // We didn't find anything.
    1140     fclose($fp);
     1099    }
     1100
    11411101    return false;
    11421102}
     
    13331293 * @since 0.71
    13341294 * @uses $wpdb
    1335  * @uses $wp_version WordPress version
    13361295 *
    13371296 * @param string $trackback_url URL to send trackbacks.
     
    13421301 */
    13431302function trackback($trackback_url, $title, $excerpt, $ID) {
    1344     global $wpdb, $wp_version;
     1303    global $wpdb;
    13451304
    13461305    if ( empty($trackback_url) )
    13471306        return;
    13481307
    1349     $title = urlencode($title);
    1350     $excerpt = urlencode($excerpt);
    1351     $blog_name = urlencode(get_option('blogname'));
    1352     $tb_url = $trackback_url;
    1353     $url = urlencode(get_permalink($ID));
    1354     $query_string = "title=$title&url=$url&blog_name=$blog_name&excerpt=$excerpt";
    1355     $trackback_url = parse_url($trackback_url);
    1356     $http_request = 'POST ' . $trackback_url['path'] . ($trackback_url['query'] ? '?'.$trackback_url['query'] : '') . " HTTP/1.0\r\n";
    1357     $http_request .= 'Host: '.$trackback_url['host']."\r\n";
    1358     $http_request .= 'Content-Type: application/x-www-form-urlencoded; charset='.get_option('blog_charset')."\r\n";
    1359     $http_request .= 'Content-Length: '.strlen($query_string)."\r\n";
    1360     $http_request .= "User-Agent: WordPress/" . $wp_version;
    1361     $http_request .= "\r\n\r\n";
    1362     $http_request .= $query_string;
    1363     if ( '' == $trackback_url['port'] )
    1364         $trackback_url['port'] = 80;
    1365     $fs = @fsockopen($trackback_url['host'], $trackback_url['port'], $errno, $errstr, 4);
    1366     @fputs($fs, $http_request);
    1367     @fclose($fs);
    1368 
    1369     $tb_url = addslashes( $tb_url );
     1308    $options = array();
     1309    $options['timeout'] = 4;
     1310    $options['body'] = array(
     1311        'title' => urlencode($title),
     1312        'url' => urlencode(get_permalink($ID)),
     1313        'blog_name' => urlencode(get_option('blogname')),
     1314        'excerpt' => urlencode($excerpt)
     1315    );
     1316
     1317    wp_remote_post($trackback_url, $options);
     1318
     1319    $tb_url = addslashes( $trackback_url );
    13701320    $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', '$tb_url') WHERE ID = %d", $ID) );
    13711321    return $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_url', '')) WHERE ID = %d", $ID) );
Note: See TracChangeset for help on using the changeset viewer.