1
0
mirror of https://git.tt-rss.org/git/tt-rss.git synced 2025-12-14 03:45:54 +00:00

split transparent rewriting of locally cached media URLs to execute after both sanitize() and HOOK_RENDER_ARTICLE to allow plugins work on original source URLs consistently

This commit is contained in:
Andrew Dolgov
2018-08-20 12:12:32 +03:00
parent c5c3a0a2a8
commit 2aef804f4b
4 changed files with 65 additions and 24 deletions

View File

@@ -1564,6 +1564,62 @@
return false;
}
// 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($str); if (!$res) return '';
$doc = new DOMDocument();
$doc->loadHTML($charset_hack . $res);
$xpath = new DOMXPath($doc);
$entries = $xpath->query('(//img[@src]|//video/source[@src]|//audio/source[@src])');
$need_saving = false;
foreach ($entries as $entry) {
if ($entry->hasAttribute('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)) {
// this is strictly cosmetic
if ($entry->tagName == 'img') {
$suffix = ".png";
} else if ($entry->parentNode && $entry->parentNode->tagName == "video") {
$suffix = ".mp4";
} else if ($entry->parentNode && $entry->parentNode->tagName == "audio") {
$suffix = ".ogg";
} else {
$suffix = "";
}
$src = get_self_url_prefix() . '/public.php?op=cached_url&hash=' . sha1($src) . $suffix;
$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"];
@@ -1596,31 +1652,8 @@
if ($entry->hasAttribute('src')) {
$src = rewrite_relative_url($rewrite_base_url, $entry->getAttribute('src'));
$cached_filename = CACHE_DIR . '/images/' . sha1($src);
if (file_exists($cached_filename)) {
// this is strictly cosmetic
if ($entry->tagName == 'img') {
$suffix = ".png";
} else if ($entry->parentNode && $entry->parentNode->tagName == "video") {
$suffix = ".mp4";
} else if ($entry->parentNode && $entry->parentNode->tagName == "audio") {
$suffix = ".ogg";
} else {
$suffix = "";
}
$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');
}
}
// cache stuff has gone to rewrite_cached_urls()
$entry->setAttribute('src', $src);
}