From: brockhaus <brockhaus>
Date: Mon, 20 Aug 2007 20:56:06 +0000 (+0000)
Subject: Made pingback receiving better working with WordPress Pingbacks
X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=63308dbf261c0889dcc8379aa5e47ba4d5588a9f;p=s9y.git

Made pingback receiving better working with WordPress Pingbacks
---

diff --git a/include/functions_trackbacks.inc.php b/include/functions_trackbacks.inc.php
index b09d236..8b8582a 100644
--- a/include/functions_trackbacks.inc.php
+++ b/include/functions_trackbacks.inc.php
@@ -83,11 +83,9 @@ global $serendipity;
   <methodName>pingback.ping</methodName>
   <params>
     <param>
-      <name>sourceURI</name>
       <value><string>$url</string></value>
     </param>
     <param>
-      <name>targetURI</name>
       <value><string>$loc</string></value>
     </param>
   </params>
@@ -377,13 +375,17 @@ function add_trackback ($id, $title, $url, $name, $excerpt) {
  */
 function add_pingback ($id, $postdata) {
     global $serendipity;
-    $sourceURI = getPingbackParam('sourceURI', $postdata);
-    $targetURI = getPingbackParam('targetURI', $postdata);
-    if (isset($sourceURI) && isset($targetURI)) {
-        $path = parse_url($sourceURI);
-        $local              = $targetURI;
+    log_pingback("Reached add_pingback. ID:[$id]");
+    
+    // XML-RPC Method call without named parameter. This seems to be the default way using XML-RPC
+    if(preg_match('@<methodCall>\s*<methodName>\s*pingback.ping\s*</methodName>\s*<params>\s*<param>\s*<value>\s*<string>([^<]*)</string>\s*</value>\s*</param>\s*<param>\s*<value>\s*<string>([^<]*)</string>\s*</value>\s*</param>\s*</params>\s*</methodCall>@is', $postdata, $matches)) {
+        log_pingback("Pingback wp structure.");
+        $remote             = $matches[1];
+        $local              = $matches[2];
+        log_pingback("remote=$remote, local=$local");
+        $path = parse_url($remote);
         $comment['title']   = 'PingBack';
-        $comment['url']     = $sourceURI;
+        $comment['url']     = $remote;
         $comment['comment'] = '';
         $comment['name']    = $path['host'];
         #Temporarily disabled until made configurable
@@ -391,19 +393,28 @@ function add_pingback ($id, $postdata) {
 
         // if no ID parameter was given, try to get one from targetURI
         if (!isset($id) || $id==0) {
-            if (preg_match('@/(\d+)_[^/]*$@', $local, $matches)) {
-                $id = (int)$matches[1];
-            }
+            log_pingback("ID not found");
+            $id = evaluateIdByLocalUrl($local);
+            log_pingback("ID set to $id");
+        }
+        
+        if ($id>0) {
+            serendipity_saveComment($id, $comment, 'PINGBACK');
+            return 1;
+        } else {
+            return 0;
         }
-        serendipity_saveComment($id, $comment, 'PINGBACK');
-        return 1;
     }
-    if(preg_match('@<methodCall>\s*<methodName>\s*pingback.ping\s*</methodName>\s*<params>\s*<param>\s*<value>\s*<string>([^<]*)</string>\s*</value>\s*</param>\s*<param>\s*<value>\s*<string>([^<]*)</string>\s*</value>\s*</param>\s*</params>\s*</methodCall>@is', $postdata, $matches)) {
-        $remote             = $matches[1];
-        $local              = $matches[2];
-        $path = parse_url($remote);
+
+    // XML-RPC Method call with named parameter. I'm not sure, if XML-RPC supports this, but just to be sure
+    $sourceURI = getPingbackParam('sourceURI', $postdata);
+    $targetURI = getPingbackParam('targetURI', $postdata);
+    if (isset($sourceURI) && isset($targetURI)) {
+        log_pingback("Pingback spec structure.");
+        $path = parse_url($sourceURI);
+        $local              = $targetURI;
         $comment['title']   = 'PingBack';
-        $comment['url']     = $remote;
+        $comment['url']     = $sourceURI;
         $comment['comment'] = '';
         $comment['name']    = $path['host'];
         #Temporarily disabled until made configurable
@@ -411,17 +422,42 @@ function add_pingback ($id, $postdata) {
 
         // if no ID parameter was given, try to get one from targetURI
         if (!isset($id) || $id==0) {
-            if (preg_match('@/(\d+)_[^/]*$@', $local, $matches)) {
-                $id = (int)$matches[1];
-            }
+            log_pingback("ID not found");
+            $id = evaluateIdByLocalUrl($local);
+            log_pingback("ID set to $id");
+        }
+        if ($id>0) {
+            serendipity_saveComment($id, $comment, 'PINGBACK');
+            return 1;
+        } else {
+            return 0;
         }
-
-        serendipity_saveComment($id, $comment, 'PINGBACK');
-        return 1;
     }
+
     return 0;
 }
 
+function evaluateIdByLocalUrl($localUrl) {
+    global $serendipity;
+    
+    // Build an ID searchpattern in configured permaling structure:
+    $permalink_article = $serendipity['permalinkStructure'];
+    log_pingback("perma: $permalink_article");
+    $permalink_article = str_replace('.','\.',$permalink_article);
+    $permalink_article = str_replace('+','\+',$permalink_article);
+    $permalink_article = str_replace('?','\?',$permalink_article);
+    $permalink_article = str_replace('%id%','(\d+)',$permalink_article);
+    $permalink_article = str_replace('%title%','[^/]*',$permalink_article);
+    $permalink_article_regex = '@' . $permalink_article . '$@'; 
+    log_pingback("regex: $permalink_article_regex");
+
+    if (preg_match($permalink_article_regex, $localUrl, $matches)) {
+        return (int)$matches[1];
+    } else {
+        return 0;
+    }
+}
+
 /**
  * Gets a XML-RPC pingback.ping value by given parameter name
  * @access private
@@ -463,7 +499,7 @@ function fetchPingbackData( &$comment) {
         $fContent = $req->getResponseBody();
 
         // Get a title
-        if (preg_match('@<head>.*?<title>(.*?)</title>.*?</head>@is',$fContent,$matches)) {
+        if (preg_match('@<head[^>]*>.*?<title[^>]*>(.*?)</title>.*?</head>@is',$fContent,$matches)) {
             $comment['title'] = strip_tags($matches[1]);
         }
         
@@ -789,4 +825,5 @@ function is_utf8($string) {
          . '|[\xF1-\xF3][\x80-\xBF]{3}'          # planes 4-15
          . '|\xF4[\x80-\x8F][\x80-\xBF]{2}'      # plane 16
          . ')*$%xs', $string);
-}
\ No newline at end of file
+}
+