summaryrefslogtreecommitdiff
path: root/classes/rssutils.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/rssutils.php
parente934e9f05e9315907dfebb83685510c0961c34aa (diff)
DiskCache: properly deal with srcset attributes
Diffstat (limited to 'classes/rssutils.php')
-rwxr-xr-xclasses/rssutils.php57
1 files changed, 36 insertions, 21 deletions
diff --git a/classes/rssutils.php b/classes/rssutils.php
index fd6d47375..f307efa4b 100755
--- a/classes/rssutils.php
+++ b/classes/rssutils.php
@@ -1226,6 +1226,32 @@ class RSSUtils {
}
}
+ static function cache_media_url($cache, $url, $site_url) {
+ $url = rewrite_relative_url($site_url, $url);
+ $local_filename = sha1($url);
+
+ Debug::log("cache_media: checking $url", Debug::$LOG_VERBOSE);
+
+ if (!$cache->exists($local_filename)) {
+ Debug::log("cache_media: downloading: $url to $local_filename", Debug::$LOG_VERBOSE);
+
+ global $fetch_last_error_code;
+ global $fetch_last_error;
+
+ $file_content = fetch_file_contents(array("url" => $url,
+ "http_referrer" => $url,
+ "max_size" => MAX_CACHE_FILE_SIZE));
+
+ if ($file_content) {
+ $cache->put($local_filename, $file_content);
+ } else {
+ Debug::log("cache_media: failed with $fetch_last_error_code: $fetch_last_error");
+ }
+ } else if ($cache->isWritable($local_filename)) {
+ $cache->touch($local_filename);
+ }
+ }
+
static function cache_media($html, $site_url) {
$cache = new DiskCache("images");
@@ -1234,35 +1260,24 @@ class RSSUtils {
if ($doc->loadHTML($html)) {
$xpath = new DOMXPath($doc);
- $entries = $xpath->query('(//img[@src])|(//video/source[@src])|(//audio/source[@src])|(//video[@poster])|(//video[@src])');
+ $entries = $xpath->query('(//img[@src]|//source[@src|@srcset]|//video[@poster|@src])');
foreach ($entries as $entry) {
foreach (array('src', 'poster') as $attr) {
if ($entry->hasAttribute($attr) && strpos($entry->getAttribute($attr), "data:") !== 0) {
- $src = rewrite_relative_url($site_url, $entry->getAttribute($attr));
-
- $local_filename = sha1($src);
-
- Debug::log("cache_media: checking $src", Debug::$LOG_VERBOSE);
+ RSSUtils::cache_media_url($cache, $entry->getAttribute($attr), $site_url);
+ }
+ }
- if (!$cache->exists($local_filename)) {
- Debug::log("cache_media: downloading: $src to $local_filename", Debug::$LOG_VERBOSE);
+ if ($entry->hasAttribute("srcset")) {
+ $tokens = explode(",", $entry->getAttribute('srcset'));
- global $fetch_last_error_code;
- global $fetch_last_error;
+ for ($i = 0; $i < count($tokens); $i++) {
+ $token = trim($tokens[$i]);
- $file_content = fetch_file_contents(array("url" => $src,
- "http_referrer" => $src,
- "max_size" => MAX_CACHE_FILE_SIZE));
+ list ($url, $width) = explode(" ", $token, 2);
- if ($file_content) {
- $cache->put($local_filename, $file_content);
- } else {
- Debug::log("cache_media: failed with $fetch_last_error_code: $fetch_last_error");
- }
- } else if ($cache->isWritable($local_filename)) {
- $cache->touch($local_filename);
- }
+ RSSUtils::cache_media_url($cache, $url, $site_url);
}
}
}