]>
git.wh0rd.org - tt-rss.git/blob - plugins/cache_starred_images/init.php
2366a1e2b0b3e46dd5b4bfe0024f31cd0e449e5e
2 class Cache_Starred_Images
extends Plugin
{
9 "Automatically cache images in Starred articles",
14 function init($host) {
17 $this->cache_dir
= CACHE_DIR
. "/starred-images/";
19 if (!is_dir($this->cache_dir
)) {
20 mkdir($this->cache_dir
);
23 if (is_dir($this->cache_dir
)) {
24 chmod($this->cache_dir
, 0777);
26 if (is_writable($this->cache_dir
)) {
27 $host->add_hook($host::HOOK_UPDATE_TASK
, $this);
28 $host->add_hook($host::HOOK_SANITIZE
, $this);
30 user_error("Starred cache directory is not writable.", E_USER_WARNING
);
34 user_error("Unable to create starred cache directory.", E_USER_WARNING
);
39 $hash = basename($_REQUEST["hash"]);
43 $filename = $this->cache_dir
. "/" . $hash . '.png';
45 if (file_exists($filename)) {
46 /* See if we can use X-Sendfile */
48 if (function_exists('apache_get_modules') &&
49 array_search('mod_xsendfile', apache_get_modules()))
53 header("X-Sendfile: $filename");
54 header("Content-type: application/octet-stream");
55 header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
57 header("Content-type: image/png");
58 $stamp = gmdate("D, d M Y H:i:s", filemtime($filename)). " GMT";
59 header("Last-Modified: $stamp", true);
60 ob_clean(); // discard any data in the output buffer (if possible)
61 flush(); // flush headers (if possible)
65 header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
66 echo "File not found.";
71 function hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id) {
72 $xpath = new DOMXpath($doc);
75 $entries = $xpath->query('(//img[@src])');
77 foreach ($entries as $entry) {
78 if ($entry->hasAttribute('src')) {
79 $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
81 $local_filename = $this->cache_dir
. $article_id . "-" . sha1($src) . ".png";
83 if (file_exists($local_filename)) {
84 $entry->setAttribute("src", get_self_url_prefix() .
85 "/backend.php?op=pluginhandler&plugin=cache_starred_images&method=image&hash=" .
86 $article_id . "-" . sha1($src));
96 function hook_update_task() {
97 header("Content-type: text/plain");
99 $result = db_query("SELECT content, ttrss_user_entries.owner_uid, link, site_url, ttrss_entries.id, plugin_data
100 FROM ttrss_entries, ttrss_user_entries LEFT JOIN ttrss_feeds ON
101 (ttrss_user_entries.feed_id = ttrss_feeds.id)
102 WHERE ref_id = ttrss_entries.id AND
104 UPPER(content) LIKE '%<IMG%' AND
106 plugin_data NOT LIKE '%starred_cache_images%'
107 ORDER BY ".sql_random_function()." LIMIT 100");
110 while ($line = db_fetch_assoc($result)) {
111 if ($line["site_url"]) {
112 $success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]);
115 $plugin_data = db_escape_string("starred_cache_images,${line['owner_uid']}:" . $line["plugin_data"]);
117 db_query("UPDATE ttrss_entries SET plugin_data = '$plugin_data' WHERE id = " . $line["id"]);
123 function cache_article_images($content, $site_url, $owner_uid, $article_id) {
124 libxml_use_internal_errors(true);
126 $charset_hack = '<head>
127 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
130 $doc = new DOMDocument();
131 $doc->loadHTML($charset_hack . $content);
132 $xpath = new DOMXPath($doc);
134 $entries = $xpath->query('(//img[@src])');
139 foreach ($entries as $entry) {
140 if ($entry->hasAttribute('src')) {
142 $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
144 $local_filename = $this->cache_dir
. $article_id . "-" . sha1($src) . ".png";
146 //_debug("cache_images: downloading: $src to $local_filename");
148 if (!file_exists($local_filename)) {
149 $file_content = fetch_file_contents($src);
151 if ($file_content && strlen($file_content) > 0) {
152 file_put_contents($local_filename, $file_content);
161 return $success ||
!$has_images;
164 function api_version() {