]>
git.wh0rd.org - tt-rss.git/blob - plugins/cache_starred_images/init.php
6e62bde768edcb64cb0654c4d75bdf266c87218f
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
)) {
25 if (!is_writable($this->cache_dir
))
26 chmod($this->cache_dir
, 0777);
28 if (is_writable($this->cache_dir
)) {
29 $host->add_hook($host::HOOK_UPDATE_TASK
, $this);
30 $host->add_hook($host::HOOK_HOUSE_KEEPING
, $this);
31 $host->add_hook($host::HOOK_SANITIZE
, $this);
33 user_error("Starred cache directory is not writable.", E_USER_WARNING
);
37 user_error("Unable to create starred cache directory.", E_USER_WARNING
);
44 $hash = basename($_REQUEST["hash"]);
48 $filename = $this->cache_dir
. "/" . $hash . '.png';
50 if (file_exists($filename)) {
51 /* See if we can use X-Sendfile */
53 if (function_exists('apache_get_modules') &&
54 array_search('mod_xsendfile', apache_get_modules()))
58 header("X-Sendfile: $filename");
59 header("Content-type: application/octet-stream");
60 header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
62 header("Content-type: image/png");
63 $stamp = gmdate("D, d M Y H:i:s", filemtime($filename)). " GMT";
64 header("Last-Modified: $stamp", true);
65 ob_clean(); // discard any data in the output buffer (if possible)
66 flush(); // flush headers (if possible)
70 header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
71 echo "File not found.";
76 function hook_house_keeping() {
77 $files = glob($this->cache_dir
. "/*.png");
82 foreach ($files as $file) {
83 list ($article_id, $hash) = explode("-", basename($file));
85 if ($article_id != $last_article_id) {
86 $last_article_id = $article_id;
87 $article_id = db_escape_string($article_id);
89 $result = db_query("SELECT id FROM ttrss_entries WHERE id = " . $article_id);
91 $article_exists = db_num_rows($result) > 0;
94 if (!$article_exists) {
100 function hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id) {
101 $xpath = new DOMXpath($doc);
104 $entries = $xpath->query('(//img[@src])');
106 foreach ($entries as $entry) {
107 if ($entry->hasAttribute('src')) {
108 $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
110 $local_filename = $this->cache_dir
. $article_id . "-" . sha1($src) . ".png";
112 if (file_exists($local_filename)) {
113 $entry->setAttribute("src", get_self_url_prefix() .
114 "/backend.php?op=pluginhandler&plugin=cache_starred_images&method=image&hash=" .
115 $article_id . "-" . sha1($src));
125 function hook_update_task() {
126 $result = db_query("SELECT content, ttrss_user_entries.owner_uid, link, site_url, ttrss_entries.id, plugin_data
127 FROM ttrss_entries, ttrss_user_entries LEFT JOIN ttrss_feeds ON
128 (ttrss_user_entries.feed_id = ttrss_feeds.id)
129 WHERE ref_id = ttrss_entries.id AND
131 UPPER(content) LIKE '%<IMG%' AND
133 plugin_data NOT LIKE '%starred_cache_images%'
134 ORDER BY ".sql_random_function()." LIMIT 100");
137 while ($line = db_fetch_assoc($result)) {
138 if ($line["site_url"]) {
139 $success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]);
142 $plugin_data = db_escape_string("starred_cache_images,${line['owner_uid']}:" . $line["plugin_data"]);
144 db_query("UPDATE ttrss_entries SET plugin_data = '$plugin_data' WHERE id = " . $line["id"]);
150 function cache_article_images($content, $site_url, $owner_uid, $article_id) {
151 libxml_use_internal_errors(true);
153 $charset_hack = '<head>
154 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
157 $doc = new DOMDocument();
158 $doc->loadHTML($charset_hack . $content);
159 $xpath = new DOMXPath($doc);
161 $entries = $xpath->query('(//img[@src])');
166 foreach ($entries as $entry) {
167 if ($entry->hasAttribute('src')) {
169 $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
171 $local_filename = $this->cache_dir
. $article_id . "-" . sha1($src) . ".png";
173 //_debug("cache_images: downloading: $src to $local_filename");
175 if (!file_exists($local_filename)) {
176 $file_content = fetch_file_contents($src);
178 if ($file_content && strlen($file_content) > 0) {
179 file_put_contents($local_filename, $file_content);
188 return $success ||
!$has_images;
191 function api_version() {