Dotclear

Changeset 3521:b8a16a351d6d for inc


Ignore:
Timestamp:
01/20/17 14:14:22 (9 years ago)
Author:
franck <carnet.franck.paul@…>
Branch:
default
Message:

backlinks:

  • retrieving ping URLs, let trackback first, then pingback, then finally webmention
  • get source post content to compose webmention excerpt and retrieve title
  • use source post title as blog name if this one is unknown (Anonymous blog is used if neither title nor blog name are known)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • inc/core/class.dc.trackback.php

    r3415 r3521  
    356356     /** 
    357357     Receives a webmention and insert it as a comment of given post. 
    358       
     358 
    359359     NB: plugin Fair Trackback check source content to find url. 
    360360 
     
    387387 
    388388               # Create a comment for received webmention 
    389                // maybe better to try to fetch a title from the source ?... 
    390                $title = __('A website mention this entry.'); 
    391                $comment = ''; 
    392                $excerpt = sprintf('<a href="%s" rel="nofollow">%s</a>',$from_url,$from_url); 
     389               $remote_content = $this->getRemoteContent($from_url); 
     390 
     391               # We want a title... 
     392               if (!preg_match('!<title>([^<].*?)</title>!mis',$remote_content,$m)) { 
     393                    throw new Exception(__('Where\'s your title?'), 0); 
     394               } 
     395               $title = trim(html::clean($m[1])); 
     396               $title = html::decodeEntities($title); 
     397               $title = html::escapeHTML($title); 
     398               $title = text::cutString($title,60); 
     399 
     400               preg_match('!<body[^>]*?>(.*)?</body>!msi',$remote_content,$m); 
     401               $source = $m[1]; 
     402               $source = preg_replace('![\r\n\s]+!ms',' ',$source); 
     403               $source = preg_replace( "/<\/*(h\d|p|th|td|li|dt|dd|pre|caption|input|textarea|button)[^>]*>/","\n\n",$source ); 
     404               $source = strip_tags($source,'<a>'); 
     405               $source = explode("\n\n",$source); 
     406 
     407               $excerpt = ''; 
     408               foreach ($source as $line) { 
     409                    if (strpos($line, $to_url) !== false) { 
     410                         if (preg_match("!<a[^>]+?".$to_url."[^>]*>([^>]+?)</a>!",$line,$m)) { 
     411                              $excerpt = strip_tags($line); 
     412                              break; 
     413                         } 
     414                    } 
     415               } 
     416               if ($excerpt) { 
     417                    $excerpt = '(&#8230;) '.text::cutString(html::escapeHTML($excerpt),200).' (&#8230;)'; 
     418               } 
     419               else { 
     420                    $excerpt = '(&#8230;)'; 
     421               } 
     422 
    393423               $this->addBacklink($post_id,$from_url,'',$title,$excerpt,$comment); 
    394424 
     
    443473     { 
    444474          if (empty($blog_name)) { 
    445                $blog_name = 'Anonymous blog'; 
     475               // Let use title as text link for this backlink 
     476               $blog_name = ($title ?: 'Anonymous blog'); 
    446477          } 
    447478 
     
    522553 
    523554     /** 
    524      Retreive local post from a given IRL 
     555     Retreive local post from a given URL 
    525556 
    526557     @param    to_url         <b>string</b>       Target URL 
     
    692723          } 
    693724 
    694           # If we've got a X-Pingback header and it's a valid URL, it will be enough 
    695           if ($pb_url && filter_var($pb_url,FILTER_VALIDATE_URL) && preg_match('!^https?:!',$pb_url)) { 
    696                return $pb_url.'|'.$url; 
    697           } 
    698  
    699           # No X-Pingback header. A link rel=pingback, maybe ? 
    700           $pattern_pingback = '!<link rel="pingback" href="(.*?)"( /)?>!msi'; 
    701  
    702           if (preg_match($pattern_pingback,$page_content,$m)) { 
    703                $pb_url = $m[1]; 
    704                if (filter_var($pb_url,FILTER_VALIDATE_URL) && preg_match('!^https?:!',$pb_url)) { 
    705                     return $pb_url.'|'.$url; 
    706                } 
    707           } 
    708  
    709           # No pingback ? OK, let's check for a trackback data chunk... 
     725          # Let's check for an elderly trackback data chunk... 
    710726          $pattern_rdf = 
    711727          '/<rdf:RDF.*?>.*?'. 
     
    730746          } 
    731747 
     748          # No trackback ? OK, let see if we've got a X-Pingback header and it's a valid URL, it will be enough 
     749          if ($pb_url && filter_var($pb_url,FILTER_VALIDATE_URL) && preg_match('!^https?:!',$pb_url)) { 
     750               return $pb_url.'|'.$url; 
     751          } 
     752 
     753          # No X-Pingback header. A link rel=pingback, maybe ? 
     754          $pattern_pingback = '!<link rel="pingback" href="(.*?)"( /)?>!msi'; 
     755 
     756          if (preg_match($pattern_pingback,$page_content,$m)) { 
     757               $pb_url = $m[1]; 
     758               if (filter_var($pb_url,FILTER_VALIDATE_URL) && preg_match('!^https?:!',$pb_url)) { 
     759                    return $pb_url.'|'.$url; 
     760               } 
     761          } 
     762 
    732763          # Nothing, let's try webmention. Only support x/html content 
    733764          if ($wm_url) { 
     
    792823     public static function checkURLs($from_url,$to_url) 
    793824     { 
    794           if (!(filter_var($from_url, FILTER_VALIDATE_URL) && preg_match('!^https?://!',$from_url))) { 
     825          if (!(filter_var($from_url,FILTER_VALIDATE_URL) && preg_match('!^https?://!',$from_url))) { 
    795826               throw new Exception(__('No valid source URL provided? Try again!'), 0); 
    796827          } 
    797828 
    798           if (!(filter_var($to_url, FILTER_VALIDATE_URL) && preg_match('!^https?://!',$to_url))) { 
     829          if (!(filter_var($to_url,FILTER_VALIDATE_URL) && preg_match('!^https?://!',$to_url))) { 
    799830               throw new Exception(__('No valid target URL provided? Try again!'), 0); 
    800831          } 
Note: See TracChangeset for help on using the changeset viewer.

Sites map