Dotclear

Changeset 1671:7c0168380f80


Ignore:
Timestamp:
08/31/13 14:37:33 (11 years ago)
Author:
Florent Cotton <florent.cotton@…>
Branch:
pingbacks
Message:

Commit intermédiaire portant uniquement sur la découverte et le ping des URLs de pingback (see #1370)

File:
1 edited

Legend:

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

    r1179 r1671  
    8080          } 
    8181           
    82           $data = array( 
    83                'title' => $post_title, 
    84                'excerpt' => $post_excerpt, 
    85                'url' => $post_url, 
    86                'blog_name' => trim(html::escapeHTML(html::clean($this->core->blog->name))) 
    87                //,'__debug' => false 
    88           ); 
    89            
    90           # Ping 
    91           try 
    92           { 
    93                $http = self::initHttp($url,$path); 
    94                $http->post($path,$data,'UTF-8'); 
    95                $res = $http->getContent(); 
    96           } 
    97           catch (Exception $e) 
    98           { 
    99                throw new Exception(__('Unable to ping URL')); 
    100           } 
    101            
    102           $pattern = 
    103           '|<response>.*<error>(.*)</error>(.*)'. 
    104           '(<message>(.*)</message>(.*))?'. 
    105           '</response>|msU'; 
    106            
    107           if (!preg_match($pattern,$res,$match)) 
    108           { 
    109                throw new Exception(sprintf(__('%s is not a ping URL'),$url)); 
    110           } 
    111            
    112           $ping_error = trim($match[1]); 
    113           $ping_msg = (!empty($match[4])) ? $match[4] : ''; 
     82          $ping_parts = explode('|',$url); 
     83           
     84          # Let's walk by the trackback way 
     85          if (count($ping_parts) < 2) { 
     86               $data = array( 
     87                    'title' => $post_title, 
     88                    'excerpt' => $post_excerpt, 
     89                    'url' => $post_url, 
     90                    'blog_name' => trim(html::escapeHTML(html::clean($this->core->blog->name))) 
     91                    //,'__debug' => false 
     92               ); 
     93                
     94               # Ping 
     95               try 
     96               { 
     97                    $http = self::initHttp($url,$path); 
     98                    $http->post($path,$data,'UTF-8'); 
     99                    $res = $http->getContent(); 
     100               } 
     101               catch (Exception $e) 
     102               { 
     103                    throw new Exception(__('Unable to ping URL')); 
     104               } 
     105                
     106               $pattern = 
     107               '|<response>.*<error>(.*)</error>(.*)'. 
     108               '(<message>(.*)</message>(.*))?'. 
     109               '</response>|msU'; 
     110                
     111               if (!preg_match($pattern,$res,$match)) 
     112               { 
     113                    throw new Exception(sprintf(__('%s is not a ping URL'),$url)); 
     114               } 
     115                
     116               $ping_error = trim($match[1]); 
     117               $ping_msg = (!empty($match[4])) ? $match[4] : ''; 
     118          } 
     119          # Damnit ! Let's play pingback 
     120          else { 
     121               try { 
     122                    $xmlrpc = new xmlrpcClient($ping_parts[0]); 
     123                    $res = $xmlrpc->query('pingback.ping', $post_url, $ping_parts[1]); 
     124                    $ping_error = '0'; 
     125               } 
     126               catch (xmlrpcException $e) { 
     127                    $ping_error = $e->getCode(); 
     128                    $ping_msg = $e->getMessage();  
     129               } 
     130               catch (Exception $e) { 
     131                    throw new Exception(__('Unable to ping URL')); 
     132               } 
     133          } 
    114134           
    115135          if ($ping_error != '0') { 
     
    346366               for ($i = 0; $i<count($match); $i++) 
    347367               { 
    348                     if (preg_match('/href="(http:\/\/[^"]+)"/ms', $match[$i][1], $matches)) { 
     368                    if (preg_match('/href="(https?:\/\/[^"]+)"/ms', $match[$i][1], $matches)) { 
    349369                         $res[$matches[1]] = 1; 
    350370                    } 
     
    358378               for ($i = 0; $i<count($match); $i++) 
    359379               { 
    360                     if (preg_match('/cite="(http:\/\/[^"]+)"/ms', $match[$i][2], $matches)) { 
     380                    if (preg_match('/cite="(https?:\/\/[^"]+)"/ms', $match[$i][2], $matches)) { 
    361381                         $res[$matches[1]] = 1; 
    362382                    } 
     
    374394               $http->get($path); 
    375395               $page_content = $http->getContent(); 
     396               $pb_url = $http->getHeader('x-pingback'); 
    376397          } 
    377398          catch (Exception $e) 
     
    380401          } 
    381402           
     403          # If we've got a X-Pingback header and it's a valid URL, it will be enough 
     404          if ($pb_url && filter_var($pb_url,FILTER_VALIDATE_URL) && preg_match('!^https?:!',$pb_url)) { 
     405               return $pb_url.'|'.$url; 
     406          } 
     407           
     408          # No X-Pingback header ? OK, let's check for a trackback data chunk... 
    382409          $pattern_rdf = 
    383410          '/<rdf:RDF.*?>.*?'. 
     
    388415          preg_match_all($pattern_rdf,$page_content,$rdf_all,PREG_SET_ORDER); 
    389416           
     417          $url_path = parse_url($url, PHP_URL_PATH); 
     418          $sanitized_url = str_replace($url_path, html::sanitizeURL($url_path), $url); 
     419           
    390420          for ($i=0; $i<count($rdf_all); $i++) 
    391421          { 
    392422               $rdf = $rdf_all[$i][1]; 
    393                 
    394                if (preg_match('/dc:identifier="'.preg_quote($url,'/').'"/msi',$rdf)) { 
     423               if (preg_match('/dc:identifier="'.preg_quote($url,'/').'"/msi',$rdf) || 
     424                    preg_match('/dc:identifier="'.preg_quote($sanitized_url,'/').'"/msi',$rdf)) { 
    395425                    if (preg_match('/trackback:ping="(.*?)"/msi',$rdf,$tb_link)) { 
    396426                         return $tb_link[1]; 
     
    399429          } 
    400430           
     431          # Last call before the point of no return : a link rel=pingback, maybe ? 
     432          $pattern_pingback = '!<link rel="pingback" href="(.*?)"( /)?>!msi'; 
     433           
     434          if (preg_match($pattern_pingback,$page_content,$m)) { 
     435               $pb_url = $m[1]; 
     436               if (filter_var($pb_url,FILTER_VALIDATE_URL) && preg_match('!^https?:!',$pb_url)) { 
     437                    return $pb_url.'|'.$url; 
     438               } 
     439          } 
     440           
    401441          return null; 
    402442     } 
Note: See TracChangeset for help on using the changeset viewer.

Sites map