WordPress.org

Make WordPress Core

Changeset 2619


Ignore:
Timestamp:
06/04/2005 10:15:36 AM (13 years ago)
Author:
matt
Message:

Better pingback extraction, fixes #1268

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions-formatting.php

    r2582 r2619  
    591591    }
    592592}
    593 
    594 
    595 function strip_all_but_one_link($text, $mylink) {
    596     $match_link = '#(<a.+?href.+?'.'>)(.+?)(</a>)#';
    597     preg_match_all($match_link, $text, $matches);
    598     $count = count($matches[0]);
    599     for ($i=0; $i<$count; $i++) {
    600         if (!strstr($matches[0][$i], $mylink)) {
    601             $text = str_replace($matches[0][$i], $matches[2][$i], $text);
    602         }
    603     }
    604     return $text;
    605 }
    606 
    607593
    608594// used by wp-mail to handle charsets in email subjects
  • trunk/xmlrpc.php

    r2606 r2619  
    10761076    /* pingback.ping gets a pingback and registers it */
    10771077    function pingback_ping($args) {
    1078         // original code by Mort (http://mort.mine.nu:8080 -- site seems dead)
    1079         // refactored to return error codes and avoid deep ifififif headaches
    10801078        global $wpdb, $wp_version;
    10811079
     
    10921090        // Check if the page linked to is in our site
    10931091        $pos1 = strpos($pagelinkedto, str_replace('http://', '', str_replace('www.', '', get_settings('home'))));
    1094         if(!$pos1) {
    1095             return new IXR_Error(0, '');
    1096         }
    1097 
     1092        if( !$pos1 )
     1093            return new IXR_Error(0, 'Is there no link to us?');
    10981094
    10991095        // let's find which post is linked to
     
    11251121            } elseif (is_string($urltest['fragment'])) {
    11261122                // ...or a string #title, a little more complicated
    1127                 $title = preg_replace('/[^a-zA-Z0-9]/', '.', $urltest['fragment']);
     1123                $title = preg_replace('/[^a-z0-9]/i', '.', $urltest['fragment']);
    11281124                $sql = "SELECT ID FROM $wpdb->posts WHERE post_title RLIKE '$title'";
    11291125                if (! ($post_ID = $wpdb->get_var($sql)) ) {
     
    11371133            return new IXR_Error(33, 'The specified target URI cannot be used as a target. It either doesn\'t exist, or it is not a pingback-enabled resource.');
    11381134        }
     1135        $post_ID = (int) $post_ID;
    11391136
    11401137
    11411138        logIO("O","(PB) URI='$pagelinkedto' ID='$post_ID' Found='$way'");
    11421139
    1143         $sql = 'SELECT post_author FROM '.$wpdb->posts.' WHERE ID = '.$post_ID;
    1144         $result = $wpdb->get_results($sql);
    1145 
    1146         if (!$wpdb->num_rows) {
    1147             // Post_ID not found
     1140        $post = $wpdb->get_row("SELECT post_author FROM $wpdb->posts WHERE ID = '$post_ID'");
     1141
     1142        if ( !$post ) // Post_ID not found
    11481143            return new IXR_Error(33, 'The specified target URI cannot be used as a target. It either doesn\'t exist, or it is not a pingback-enabled resource.');
    1149         }
    1150 
     1144
     1145        // Check if pings are on
     1146        if ( 'closed' == $post->ping_status )
     1147            return new IXR_Error(33, 'The specified target URI cannot be used as a target. It either doesn\'t exist, or it is not a pingback-enabled resource.');
    11511148
    11521149        // Let's check that the remote site didn't already pingback this entry
    11531150        $result = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post_ID' AND comment_author_url = '$pagelinkedfrom'");
    11541151
    1155         if ($wpdb->num_rows) {
    1156             // We already have a Pingback from this URL
     1152        if ( $wpdb->num_rows ) // We already have a Pingback from this URL
    11571153            return new IXR_Error(48, 'The pingback has already been registered.');
    1158         }
    1159 
    11601154
    11611155        // very stupid, but gives time to the 'from' server to publish !
     
    11681162
    11691163        // Work around bug in strip_tags():
    1170         $linea = str_replace('<!DOCTYPE','<DOCTYPE',$linea);
    1171         $linea = strip_tags($linea, '<title><a>');
    1172         $linea = strip_all_but_one_link($linea, $pagelinkedto);
    1173         // I don't think we need this? -- emc3
    1174         //$linea = preg_replace('#&([^amp\;])#is', '&amp;$1', $linea);
    1175         if ( empty($matchtitle) ) {
    1176             preg_match('|<title>([^<]*?)</title>|is', $linea, $matchtitle);
     1164        $linea = str_replace('<!DOC', '<DOC', $linea);
     1165        $linea = preg_replace( '/[\s\r\n\t]+/', ' ', $linea ); // normalize spaces
     1166        $linea = preg_replace( "/ <(h1|h2|h3|h4|h5|h6|p|th|td|li|dt|dd|pre|caption|input|textarea|button|body)[^>]*>/", "\n\n", $linea );
     1167
     1168        preg_match('|<title>([^<]*?)</title>|is', $linea, $matchtitle);
     1169        $title = $matchtitle[1];
     1170        if ( empty( $title ) )
     1171            return new IXR_Error(32, 'We cannot find a title on that page.');
     1172
     1173        $linea = strip_tags( $linea, '<a>' ); // just keep the tag we need
     1174
     1175        $p = explode( "\n\n", $linea );
     1176       
     1177        $sem_regexp_pb = "/(\\/|\\\|\*|\?|\+|\.|\^|\\$|\(|\)|\[|\]|\||\{|\})/";
     1178        $sem_regexp_fix = "\\\\$1";
     1179        $link = preg_replace( $sem_regexp_pb, $sem_regexp_fix, $pagelinkedfrom );
     1180       
     1181        $finished = false;
     1182        foreach ( $p as $para ) {
     1183            if ( $finished )
     1184                continue;
     1185            if ( strstr( $para, $pagelinkedto ) ) {
     1186                $context = preg_replace( "/.*<a[^>]+".$link."[^>]*>([^>]+)<\/a>.*/", "$1", $para );
     1187                $excerpt = strip_tags( $para );
     1188                $excerpt = trim( $excerpt );
     1189                $use     = preg_quote( $context );
     1190                $excerpt = preg_replace("|.*?\s(.{0,100}$use.{0,100})\s|s", "$1", $excerpt);
     1191                $finished = true;
     1192            }
    11771193        }
    1178         $pos2 = strpos($linea, $pagelinkedto);
    1179         $pos3 = strpos($linea, str_replace('http://www.', 'http://', $pagelinkedto));
    1180         if (is_integer($pos2) || is_integer($pos3)) {
    1181             // The page really links to us :)
    1182             $pos4 = (is_integer($pos2)) ? $pos2 : $pos3;
    1183             $start = $pos4-100;
    1184             $context = substr($linea, $start, 250);
    1185             $context = str_replace("\n", ' ', $context);
    1186             $context = str_replace('&amp;', '&', $context);
    1187         }
    1188 
    1189         if (empty($context)) {
    1190             // URL pattern not found
    1191             return new IXR_Error(17, 'The source URI does not contain a link to the target URI, and so cannot be used as a source.');
    1192         }
    1193 
    1194 
    1195         // Check if pings are on
    1196         $pingstatus = $wpdb->get_var("SELECT ping_status FROM $wpdb->posts WHERE ID = $post_ID");
    1197         if ('closed' == $pingstatus) {
    1198             return new IXR_Error(33, 'The specified target URI cannot be used as a target. It either doesn\'t exist, or it is not a pingback-enabled resource.');
    1199         }
    1200 
    12011194
    12021195        $pagelinkedfrom = preg_replace('#&([^amp\;])#is', '&amp;$1', $pagelinkedfrom);
    1203         $title = (!strlen($matchtitle[1])) ? $pagelinkedfrom : $matchtitle[1];
    1204         $original_context = strip_tags($context);
    1205         $context = '[...] ';
    1206         $context .= wp_specialchars($original_context);
    1207         $context .= ' [...]';
     1196
     1197        $context = '[...] ' . wp_specialchars( $excerpt ) . ' [...]';
    12081198        $original_pagelinkedfrom = $pagelinkedfrom;
    1209         $pagelinkedfrom = addslashes($pagelinkedfrom);
     1199        $pagelinkedfrom = addslashes( $pagelinkedfrom );
    12101200        $original_title = $title;
    12111201
     
    12161206        $comment_type = 'pingback';
    12171207
    1218         $pingstatus = $wpdb->get_var("SELECT ping_status FROM $wpdb->posts WHERE ID = $post_ID");
    1219    
    1220         if ('open' != $pingstatus)
    1221             die('Sorry, pingbacks are closed for this item.');
    1222 
    12231208        $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_content', 'comment_type');
    12241209
Note: See TracChangeset for help on using the changeset viewer.