summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2017-03-23 15:19:25 +0300
committerAndrew Dolgov <[email protected]>2017-03-23 15:19:25 +0300
commit388d4dfa88e843237ebe57e9a0376d0cd58c0f51 (patch)
tree1a2be54903bb2e40431a9e4972b1dcf0957d5891 /include
parent9c3c0ace6b149b0e1ed248d6f5b89a469a2971ee (diff)
enable caching of media in article enclosures
Diffstat (limited to 'include')
-rw-r--r--include/functions2.php5
-rw-r--r--include/rssfuncs.php29
2 files changed, 33 insertions, 1 deletions
diff --git a/include/functions2.php b/include/functions2.php
index 9d6eb9330..e00a0ba13 100644
--- a/include/functions2.php
+++ b/include/functions2.php
@@ -1816,6 +1816,11 @@
if (db_num_rows($result) > 0) {
while ($line = db_fetch_assoc($result)) {
+
+ if (file_exists(CACHE_DIR . '/images/' . sha1($line["content_url"]))) {
+ $line["content_url"] = get_self_url_prefix() . '/public.php?op=cached_url&hash=' . sha1($line["content_url"]);
+ }
+
array_push($rv, $line);
}
}
diff --git a/include/rssfuncs.php b/include/rssfuncs.php
index 997ea4594..c03e6681e 100644
--- a/include/rssfuncs.php
+++ b/include/rssfuncs.php
@@ -1083,6 +1083,9 @@
}
}
+ if ($cache_images && is_writable(CACHE_DIR . '/images'))
+ cache_enclosures($enclosures, $site_url, $debug_enabled);
+
if ($debug_enabled) {
_debug("article enclosures:", $debug_enabled);
print_r($enclosures);
@@ -1227,6 +1230,30 @@
return $rss;
}
+ function cache_enclosures($enclosures, $site_url, $debug) {
+ foreach ($enclosures as $enc) {
+
+ if (preg_match("/(image|audio|video)/", $enc[1])) {
+
+ $src = rewrite_relative_url($site_url, $enc[0]);
+
+ $local_filename = CACHE_DIR . "/images/" . sha1($src);
+
+ if ($debug) _debug("cache_enclosures: downloading: $src to $local_filename");
+
+ if (!file_exists($local_filename)) {
+ $file_content = fetch_file_contents($src);
+
+ if ($file_content && strlen($file_content) > _MIN_CACHE_FILE_SIZE) {
+ file_put_contents($local_filename, $file_content);
+ }
+ } else {
+ touch($local_filename);
+ }
+ }
+ }
+ }
+
function cache_media($html, $site_url, $debug) {
libxml_use_internal_errors(true);
@@ -1238,7 +1265,7 @@
$doc->loadHTML($charset_hack . $html);
$xpath = new DOMXPath($doc);
- $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
+ $entries = $xpath->query('(//img[@src])|(//video/source[@src])|(//audio/source[@src])');
foreach ($entries as $entry) {
if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) {