summaryrefslogtreecommitdiff
path: root/classes/diskcache.php
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2020-04-29 19:29:36 +0300
committerAndrew Dolgov <[email protected]>2020-04-29 19:29:36 +0300
commit3a4b9249a99972798ce6285bf79b8ce86971bde7 (patch)
treec04d00b13eea07a4005b0a6b454eddfc25a3ca34 /classes/diskcache.php
parente934e9f05e9315907dfebb83685510c0961c34aa (diff)
DiskCache: properly deal with srcset attributes
Diffstat (limited to 'classes/diskcache.php')
-rw-r--r--classes/diskcache.php33
1 files changed, 25 insertions, 8 deletions
diff --git a/classes/diskcache.php b/classes/diskcache.php
index ba6aef858..696a7dda9 100644
--- a/classes/diskcache.php
+++ b/classes/diskcache.php
@@ -79,6 +79,7 @@ class DiskCache {
// 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
+ // NOTE: URLs should be already absolutized because this is called after sanitize()
static public function rewriteUrls($str)
{
$res = trim($str);
@@ -89,29 +90,45 @@ class DiskCache {
$xpath = new DOMXPath($doc);
$cache = new DiskCache("images");
- $entries = $xpath->query('(//img[@src]|//picture/source[@src]|//video[@poster]|//video[@src]|//video/source[@src]|//audio/source[@src])');
+ $entries = $xpath->query('(//img[@src]|//source[@src|@srcset]|//video[@poster|@src])');
$need_saving = false;
foreach ($entries as $entry) {
-
foreach (array('src', 'poster') as $attr) {
if ($entry->hasAttribute($attr)) {
- // should be already absolutized because this is called after sanitize()
- $src = $entry->getAttribute($attr);
- $cached_filename = sha1($src);
+ $url = $entry->getAttribute($attr);
+ $cached_filename = sha1($url);
if ($cache->exists($cached_filename)) {
+ $url = $cache->getUrl($cached_filename);
- $src = $cache->getUrl(sha1($src));
-
- $entry->setAttribute($attr, $src);
+ $entry->setAttribute($attr, $url);
$entry->removeAttribute("srcset");
$need_saving = true;
}
}
}
+
+ if ($entry->hasAttribute("srcset")) {
+ $tokens = explode(",", $entry->getAttribute('srcset'));
+
+ for ($i = 0; $i < count($tokens); $i++) {
+ $token = trim($tokens[$i]);
+
+ list ($url, $width) = explode(" ", $token, 2);
+ $cached_filename = sha1($url);
+
+ if ($cache->exists($cached_filename)) {
+ $tokens[$i] = $cache->getUrl($cached_filename) . " " . $width;
+
+ $need_saving = true;
+ }
+ }
+
+ $entry->setAttribute("srcset", implode(", ", $tokens));
+ }
}
if ($need_saving) {