]> git.wh0rd.org - tt-rss.git/commitdiff
split transparent rewriting of locally cached media URLs to execute after both saniti...
authorAndrew Dolgov <noreply@fakecake.org>
Mon, 20 Aug 2018 09:12:32 +0000 (12:12 +0300)
committerAndrew Dolgov <noreply@fakecake.org>
Mon, 20 Aug 2018 09:12:32 +0000 (12:12 +0300)
classes/api.php [changed mode: 0644->0755]
classes/article.php
classes/feeds.php
include/functions.php

old mode 100644 (file)
new mode 100755 (executable)
index 4c321d7..5dbf8dc
@@ -379,6 +379,8 @@ class API extends Handler {
                                        $article = $p->hook_render_article_api(array("article" => $article));
                                }
 
+                               $article['content'] = rewrite_cached_urls($article['content']);
+
                                array_push($articles, $article);
 
                        }
@@ -799,6 +801,8 @@ class API extends Handler {
                                                $headline_row = $p->hook_render_article_api(array("headline" => $headline_row));
                                        }
 
+                                       $headline_row['content'] = rewrite_cached_urls($headline_row['content']);
+
                                        array_push($headlines, $headline_row);
                                }
                        } else if (is_numeric($result) && $result == -1) {
index c8ee5b93171976572e91316dd4b04f65d080198c..71dfdabc4a8f34487acc41ce0634320f268c6bf9 100755 (executable)
@@ -610,6 +610,8 @@ class Article extends Handler_Protected {
                                $line = $p->hook_render_article($line);
                        }
 
+                       $line['content'] = rewrite_cached_urls($line['content']);
+
                        $num_comments = (int) $line["num_comments"];
                        $entry_comments = "";
 
index 47a6b56b87973356161d5129ebeda4020efcbb41..6bf14f45e8d962bdb68cf218de58f391b3b4a582 100755 (executable)
@@ -477,6 +477,8 @@ class Feeds extends Handler_Protected {
                                                $line = $p->hook_render_article_cdm($line);
                                        }
 
+                                       $line['content'] = rewrite_cached_urls($line['content']);
+
                                        if ($vfeed_group_enabled && $line["feed_title"]) {
                                                if ($feed_id != $vgroup_last_feed) {
 
index a4e0e4d0221b70e579086dae9e0187c5406add86..8acea8ef49e652eda2c24f0a79c7dd8a8be7e8d9 100755 (executable)
                return false;
        }
 
-       function sanitize($str, $force_remove_images = false, $owner = false, $site_url = false, $highlight_words = false, $article_id = false) {
-               if (!$owner) $owner = $_SESSION["uid"];
-
-               $res = trim($str); if (!$res) return '';
+       // check for locally cached (media) URLs and rewrite to local versions
+       // this is called separately after sanitize() and plugin render article hooks to allow
+       // plugins work on original source URLs used before caching
 
+       function rewrite_cached_urls($str) {
                $charset_hack = '<head>
                                <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
                        </head>';
 
-               $res = trim($res); if (!$res) return '';
-
-               libxml_use_internal_errors(true);
+               $res = trim($str); if (!$res) return '';
 
                $doc = new DOMDocument();
                $doc->loadHTML($charset_hack . $res);
                $xpath = new DOMXPath($doc);
 
-               $rewrite_base_url = $site_url ? $site_url : get_self_url_prefix();
+               $entries = $xpath->query('(//img[@src]|//video/source[@src]|//audio/source[@src])');
 
-               $entries = $xpath->query('(//a[@href]|//img[@src]|//video/source[@src]|//audio/source[@src])');
+               $need_saving = false;
 
                foreach ($entries as $entry) {
 
-                       if ($entry->hasAttribute('href')) {
-                               $entry->setAttribute('href',
-                                       rewrite_relative_url($rewrite_base_url, $entry->getAttribute('href')));
-
-                               $entry->setAttribute('rel', 'noopener noreferrer');
-                       }
-
                        if ($entry->hasAttribute('src')) {
-                               $src = rewrite_relative_url($rewrite_base_url, $entry->getAttribute('src'));
+
+                               // should be already absolutized because this is called after sanitize()
+                               $src = $entry->getAttribute('src');
                                $cached_filename = CACHE_DIR . '/images/' . sha1($src);
 
                                if (file_exists($cached_filename)) {
 
                                        $src = get_self_url_prefix() . '/public.php?op=cached_url&hash=' . sha1($src) . $suffix;
 
-                                       if ($entry->hasAttribute('srcset')) {
-                                               $entry->removeAttribute('srcset');
-                                       }
-
-                                       if ($entry->hasAttribute('sizes')) {
-                                               $entry->removeAttribute('sizes');
-                                       }
+                                       $entry->setAttribute('src', $src);
+                                       $need_saving = true;
                                }
+                       }
+               }
+
+               if ($need_saving) {
+                       $doc->removeChild($doc->firstChild); //remove doctype
+                       $res = $doc->saveHTML();
+               }
+
+               return $res;
+       }
+
+       function sanitize($str, $force_remove_images = false, $owner = false, $site_url = false, $highlight_words = false, $article_id = false) {
+               if (!$owner) $owner = $_SESSION["uid"];
+
+               $res = trim($str); if (!$res) return '';
+
+               $charset_hack = '<head>
+                               <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
+                       </head>';
+
+               $res = trim($res); if (!$res) return '';
+
+               libxml_use_internal_errors(true);
+
+               $doc = new DOMDocument();
+               $doc->loadHTML($charset_hack . $res);
+               $xpath = new DOMXPath($doc);
+
+               $rewrite_base_url = $site_url ? $site_url : get_self_url_prefix();
+
+               $entries = $xpath->query('(//a[@href]|//img[@src]|//video/source[@src]|//audio/source[@src])');
+
+               foreach ($entries as $entry) {
+
+                       if ($entry->hasAttribute('href')) {
+                               $entry->setAttribute('href',
+                                       rewrite_relative_url($rewrite_base_url, $entry->getAttribute('href')));
+
+                               $entry->setAttribute('rel', 'noopener noreferrer');
+                       }
+
+                       if ($entry->hasAttribute('src')) {
+                               $src = rewrite_relative_url($rewrite_base_url, $entry->getAttribute('src'));
+
+                               // cache stuff has gone to rewrite_cached_urls()
 
                                $entry->setAttribute('src', $src);
                        }