diff --git a/cache/images/.empty b/cache/images/.empty new file mode 100644 index 000000000..e69de29bb diff --git a/classes/feeds.php b/classes/feeds.php index 6b498ac00..f9f82cd16 100644 --- a/classes/feeds.php +++ b/classes/feeds.php @@ -184,9 +184,10 @@ class Feeds extends Protected_Handler { catchupArticlesById($this->link, $ids, $cmode); } */ - //if ($method == "ForceUpdate" && $feed && is_numeric($feed) > 0) { - // update_rss_feed($this->link, $feed, true); - //} + if ($method == "ForceUpdate" && $feed && is_numeric($feed) > 0) { + include "rssfuncs.php"; + update_rss_feed($this->link, $feed, true); + } if ($method == "MarkAllRead") { catchup_feed($this->link, $feed, $cat_view); diff --git a/classes/pref_feeds.php b/classes/pref_feeds.php index b83abd789..5937bfc8a 100644 --- a/classes/pref_feeds.php +++ b/classes/pref_feeds.php @@ -448,12 +448,10 @@ class Pref_Feeds extends Protected_Handler { $checked = ""; } - if (SIMPLEPIE_CACHE_IMAGES) { - print "
 "; - } + __('Cache images locally').""; $mark_unread_on_update = sql_bool_to_bool(db_fetch_result($result, 0, "mark_unread_on_update")); @@ -655,16 +653,13 @@ class Pref_Feeds extends Protected_Handler { print " "; $this->batch_edit_cbox("always_display_enclosures", "always_display_enclosures_l"); - if (SIMPLEPIE_CACHE_IMAGES) { - print "
 "; + print "
 "; - - print " "; $this->batch_edit_cbox("cache_images", "cache_images_l"); - } + print " "; $this->batch_edit_cbox("cache_images", "cache_images_l"); print "
diff --git a/include/rssfuncs.php b/include/rssfuncs.php index 0fc462c48..821d6b175 100644 --- a/include/rssfuncs.php +++ b/include/rssfuncs.php @@ -136,6 +136,8 @@ WHERE id IN (%s)", implode(',', $feed_ids))); } + expire_cached_files($debug); + // For each feed, we call the feed update function. while ($line = array_pop($feeds_to_update)) { @@ -364,15 +366,6 @@ $rss->set_output_encoding('UTF-8'); $rss->force_feed(true); - if (SIMPLEPIE_CACHE_IMAGES && $cache_images) { - - if ($debug_enabled) { - _debug("enabling image cache"); - } - - $rss->set_image_handler("image.php", 'i'); - } - if ($debug_enabled) { _debug("feed update interval (sec): " . get_feed_update_interval($link, $feed)*60); @@ -685,9 +678,13 @@ } } + if ($cache_images) + $entry_content = cache_images($entry_content, $site_url, $debug_enabled); + if ($_REQUEST["xdebug"] == 2) { print "update_rss_feed: content: "; print_r(htmlspecialchars($entry_content)); + print "\n"; } $entry_content_unescaped = $entry_content; @@ -735,7 +732,7 @@ $entry_content = db_escape_string($entry_content, false); - $content_hash = "SHA1:" . sha1(strip_tags($entry_content)); + $content_hash = "SHA1:x" . sha1(strip_tags($entry_content)); $entry_title = db_escape_string($entry_title); $entry_link = db_escape_string($entry_link); @@ -1311,7 +1308,71 @@ } + function cache_images($html, $site_url, $debug) { + $cache_dir = CACHE_DIR . "/images"; + libxml_use_internal_errors(true); + $charset_hack = ' + + '; + + $doc = new DOMDocument(); + $doc->loadHTML($charset_hack . $html); + $xpath = new DOMXPath($doc); + + $entries = $xpath->query('(//img[@src])'); + + foreach ($entries as $entry) { + if ($entry->hasAttribute('src')) { + $src = rewrite_relative_url($site_url, $entry->getAttribute('src')); + + $local_filename = CACHE_DIR . "/images/" . sha1($src) . ".png"; + + if ($debug) _debug("cache_images: downloading: $src to $local_filename"); + + if (!file_exists($local_filename)) { + $file_content = fetch_file_contents($src); + + if ($file_content) { + file_put_contents($local_filename, $file_content); + } + } + + if (file_exists($local_filename)) { + $entry->setAttribute('src', SELF_URL_PATH . '/image.php?url=' . + htmlspecialchars($src)); + } + } + } + + $node = $doc->getElementsByTagName('body')->item(0); + + return $doc->saveXML($node); + } + + function expire_cached_files($debug) { + foreach (array("magpie", "simplepie", "images") as $dir) { + $cache_dir = CACHE_DIR . "/$dir"; + + if ($debug) _debug("Expiring $cache_dir"); + + $num_deleted = 0; + + if (is_writable($cache_dir)) { + $files = glob("$cache_dir/*"); + + foreach ($files as $file) { + if (time() - filemtime($file) > 86400*7) { + unlink($file); + + ++$num_deleted; + } + } + } + + if ($debug) _debug("Removed $num_deleted files."); + } + } ?> diff --git a/mobile/image.php b/mobile/image.php index 1fb8368b2..eec72947f 100644 --- a/mobile/image.php +++ b/mobile/image.php @@ -1,6 +1,19 @@