]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/Linkback/lib/util.php
Merge branch 'foolproof_file_redirection_branch' into 'nightly'
[quix0rs-gnu-social.git] / plugins / Linkback / lib / util.php
index 95444571dc0ac182239adc4181e517d67c17486d..2f024dd2338a4d4a838d13b1feb94bad40a9d67a 100644 (file)
@@ -5,6 +5,13 @@ function linkback_lenient_target_match($body, $target) {
 }
 
 function linkback_get_source($source, $target) {
+    // Check if we are pinging ourselves and ignore
+    $localprefix = common_config('site', 'server') . '/' . common_config('site', 'path');
+    if(linkback_lenient_target_match($source, $localprefix)) {
+        common_debug('Ignoring self ping from ' . $source . ' to ' . $target);
+        return NULL;
+    }
+
     $request = HTTPClient::start();
 
     try {
@@ -61,6 +68,41 @@ function linkback_get_target($target) {
     return NULL;
 }
 
+function linkback_is_contained_in($entry, $target) {
+    foreach ((array)$entry['properties'] as $key => $values) {
+        if(count(array_filter($values, function($x) use ($target) { return linkback_lenient_target_match($x, $target); })) > 0) {
+            return $entry['properties'];
+        }
+
+        // check included h-* formats and their links
+        foreach ($values as $obj) {
+            if(isset($obj['type']) && array_intersect(array('h-cite', 'h-entry'), $obj['type']) &&
+               isset($obj['properties']) && isset($obj['properties']['url']) &&
+               count(array_filter($obj['properties']['url'],
+                     function($x) use ($target) { return linkback_lenient_target_match($x, $target); })) > 0
+            ) {
+                return $entry['properties'];
+            }
+        }
+
+        // check content for the link
+        if ($key == "content" && preg_match_all("/<a[^>]+?".preg_quote($target, "/")."[^>]*>([^>]+?)<\/a>/i", htmlspecialchars_decode($values[0]['html']), $context)) {
+            return $entry['properties'];
+        // check summary for the link
+        } elseif ($key == "summary" && preg_match_all("/<a[^>]+?".preg_quote($target, "/")."[^>]*>([^>]+?)<\/a>/i", htmlspecialchars_decode($values[0]), $context)) {
+            return $entry['properties'];
+        }
+    }
+
+    foreach((array)$entry['children'] as $mf2) {
+        if(linkback_is_contained_in($mf2, $target)) {
+            return $entry['properties'];
+        }
+    }
+
+    return null;
+}
+
 // Based on https://github.com/acegiak/Semantic-Linkbacks/blob/master/semantic-linkbacks-microformats-handler.php, GPL-2.0+
 function linkback_find_entry($mf2, $target) {
     if(isset($mf2['items'][0]['type']) && in_array("h-feed", $mf2['items'][0]["type"]) && isset($mf2['items'][0]['children'])) {
@@ -70,29 +112,8 @@ function linkback_find_entry($mf2, $target) {
     $entries = array_filter($mf2['items'], function($x) { return isset($x['type']) && in_array('h-entry', $x['type']); });
 
     foreach ($entries as $entry) {
-        foreach ((array)$entry['properties'] as $key => $values) {
-            if(count(array_filter($values, function($x) use ($target) { return linkback_lenient_target_match($x, $target); })) > 0) {
-                return $entry['properties'];
-            }
-
-            // check included h-* formats and their links
-            foreach ($values as $obj) {
-                if(isset($obj['type']) && array_intersect(array('h-cite', 'h-entry'), $obj['type']) &&
-                   isset($obj['properties']) && isset($obj['properties']['url']) &&
-                   count(array_filter($obj['properties']['url'],
-                         function($x) use ($target) { return linkback_lenient_target_match($x, $target); })) > 0
-                ) {
-                    return $entry['properties'];
-                }
-            }
-
-            // check content for the link
-            if ($key == "content" && preg_match_all("/<a[^>]+?".preg_quote($target, "/")."[^>]*>([^>]+?)<\/a>/i", htmlspecialchars_decode($values[0]['html']), $context)) {
-                return $entry['properties'];
-            // check summary for the link
-            } elseif ($key == "summary" && preg_match_all("/<a[^>]+?".preg_quote($target, "/")."[^>]*>([^>]+?)<\/a>/i", htmlspecialchars_decode($values[0]), $context)) {
-                return $entry['properties'];
-            }
+        if($prop = linkback_is_contained_in($entry, $target)) {
+            return $prop;
         }
     }
 
@@ -142,9 +163,8 @@ function linkback_entry_type($entry, $mf2, $target) {
 }
 
 function linkback_is_dupe($key, $url) {
-    $dupe = Notice::getKV('uri', $url);
+    $dupe = Notice::getKV($key, $url);
     if ($dupe instanceof Notice) {
-        common_log(LOG_INFO, "Linkback: ignoring duplicate post: $url");
         return $dupe;
     }
 
@@ -166,10 +186,10 @@ function linkback_hcard($mf2, $url) {
         // We found a match, return it immediately
         if(isset($item['properties']['url']) && in_array($url, $item['properties']['url'])) {
             return $item['properties'];
-      
-            // Let's keep all the hcards for later, to return one of them at least
-            $hcards[] = $item['properties'];
         }
+
+        // Let's keep all the hcards for later, to return one of them at least
+        $hcards[] = $item['properties'];
     }
   
     // No match immediately for the url we expected, but there were h-cards found
@@ -203,7 +223,7 @@ function linkback_notice($source, $notice_or_user, $entry, $author, $mf2) {
 
     $options = array('is_local' => Notice::REMOTE,
                     'url' => $entry['url'][0],
-                    'uri' => $source,
+                    'uri' => $entry['url'][0],
                     'rendered' => $rendered,
                     'replies' => array(),
                     'groups' => array(),
@@ -277,7 +297,10 @@ function linkback_profile($entry, $mf2, $response, $target) {
         return true;
     }
 
-    $profile = Profile::fromUri($author['url'][0]);
+    try {
+        $profile = Profile::fromUri($author['url'][0]);
+    } catch(UnknownUriException $ex) {}
+
     if(!($profile instanceof Profile)) {
         $profile = Profile::getKV('profileurl', $author['url'][0]);
     }
@@ -295,10 +318,10 @@ function linkback_profile($entry, $mf2, $response, $target) {
 }
 
 function linkback_save($source, $target, $response, $notice_or_user) {
-    if($dupe = linkback_is_dupe('uri', $response->getEffectiveUrl())) { return $dupe->getLocalUrl(); }
-    if($dupe = linkback_is_dupe('url', $response->getEffectiveUrl())) { return $dupe->getLocalUrl(); }
-    if($dupe = linkback_is_dupe('uri', $source)) { return $dupe->getLocalUrl(); }
-    if($dupe = linkback_is_dupe('url', $source)) { return $dupe->getLocalUrl(); }
+    $dupe = linkback_is_dupe('uri', $response->getEffectiveUrl());
+    if(!$dupe) { $dupe = linkback_is_dupe('url', $response->getEffectiveUrl()); }
+    if(!$dupe) { $dupe = linkback_is_dupe('uri', $source); }
+    if(!$dupe) { $dupe = linkback_is_dupe('url', $source); }
 
     $mf2 = new Mf2\Parser($response->getBody(), $response->getEffectiveUrl());
     $mf2 = $mf2->parse();
@@ -316,14 +339,43 @@ function linkback_save($source, $target, $response, $notice_or_user) {
         $entry['url'] = array($response->getEffectiveUrl());
     }
 
-    if($dupe = linkback_is_dupe('uri', $entry['url'][0])) { return $dupe->getLocalUrl(); }
-    if($dupe = linkback_is_dupe('url', $entry['url'][0])) { return $dupe->getLocalUrl(); }
+    if(!$dupe) { $dupe = linkback_is_dupe('uri', $entry['url'][0]); }
+    if(!$dupe) { $dupe = linkback_is_dupe('url', $entry['url'][0]); }
 
     $entry['type'] = linkback_entry_type($entry, $mf2, $target);
     list($profile, $author) =  linkback_profile($entry, $mf2, $response, $target);
     list($content, $options) = linkback_notice($source, $notice_or_user, $entry, $author, $mf2);
 
-    if($entry['type'] == 'like' || ($entry['type'] == 'reply' && $entry['rsvp'])) {
+    if($dupe) {
+        $orig = clone($dupe);
+
+        try {
+            // Ignore duplicate save error
+            try { $dupe->saveKnownReplies($options['replies']); } catch (ServerException $ex) {}
+            try { $dupe->saveKnownTags($options['tags']); } catch (ServerException $ex) {}
+            try { $dupe->saveKnownUrls($options['urls']); } catch (ServerException $ex) {}
+
+            if($options['reply_to']) { $dupe->reply_to = $options['reply_to']; }
+            if($options['repeat_of']) { $dupe->repeat_of = $options['repeat_of']; }
+            if($dupe->reply_to != $orig->reply_to || $dupe->repeat_of != $orig->repeat_of) {
+                $parent = Notice::getKV('id', $dupe->repost_of ? $dupe->repost_of : $dupe->reply_to);
+                if($parent instanceof Notice) {
+                    // If we changed the reply_to or repeat_of we might live in a new conversation now
+                    $dupe->conversation = $parent->conversation;
+                }
+            }
+            if($dupe->update($orig)) { $saved = $dupe; }
+            if($dupe->conversation != $orig->conversation && Conversation::noticeCount($orig->conversation) < 1) {
+                // Delete empty conversation
+                $emptyConversation = Conversation::getKV('id', $orig->conversation);
+                $emptyConversation->delete();
+            }
+        } catch (Exception $e) {
+            common_log(LOG_ERR, "Linkback update of remote message $source failed: " . $e->getMessage());
+            return false;
+        }
+        common_log(LOG_INFO, "Linkback updated remote message $source as notice id $saved->id");
+    } else if($entry['type'] == 'like' || ($entry['type'] == 'reply' && $entry['rsvp'])) {
         $act = new Activity();
         $act->type    = ActivityObject::ACTIVITY;
         $act->time    = $options['created'] ? strtotime($options['created']) : time();
@@ -336,7 +388,7 @@ function linkback_save($source, $target, $response, $notice_or_user) {
         //        notice's nickname and %3$s is the content of the favorited notice.)
         $act->content = sprintf(_('%1$s favorited something by %2$s: %3$s'),
                                 $profile->getNickname(), $notice_or_user->getProfile()->getNickname(),
-                                $notice_or_user->rendered ?: $notice_or_user->content);
+                                $notice_or_user->getRendered());
         if($entry['rsvp']) {
             $act->content = $options['rendered'];
         }