]>
git.wh0rd.org - tt-rss.git/blob - plugins/cache_starred_images/init.php
2 class Cache_Starred_Images
extends Plugin
implements IHandler
{
9 "Automatically cache Starred articles' images and HTML5 video files",
14 function csrf_ignore($method) {
18 function before($method) {
26 function init($host) {
29 $this->cache_dir
= CACHE_DIR
. "/starred-images/";
31 if (!is_dir($this->cache_dir
)) {
32 mkdir($this->cache_dir
);
35 if (is_dir($this->cache_dir
)) {
37 if (!is_writable($this->cache_dir
))
38 chmod($this->cache_dir
, 0777);
40 if (is_writable($this->cache_dir
)) {
41 $host->add_hook($host::HOOK_UPDATE_TASK
, $this);
42 $host->add_hook($host::HOOK_HOUSE_KEEPING
, $this);
43 $host->add_hook($host::HOOK_SANITIZE
, $this);
44 $host->add_handler("public", "cache_starred_images_getimage", $this);
47 user_error("Starred cache directory is not writable.", E_USER_WARNING
);
51 user_error("Unable to create starred cache directory.", E_USER_WARNING
);
55 function cache_starred_images_getimage() {
58 $hash = basename($_REQUEST["hash"]);
62 $filename = $this->cache_dir
. "/" . basename($hash);
63 $is_video = strpos($filename, ".mp4") !== FALSE;
65 if (file_exists($filename)) {
66 header("Content-Disposition: attachment; filename=\"$hash\"");
68 /* See if we can use X-Sendfile */
70 if (function_exists('apache_get_modules') &&
71 array_search('mod_xsendfile', apache_get_modules()))
75 header("X-Sendfile: $filename");
76 header("Content-type: application/octet-stream");
77 header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
79 header("Content-type: " . ($is_video ?
"video/mp4" : "image/png"));
80 $stamp = gmdate("D, d M Y H:i:s", filemtime($filename)). " GMT";
81 header("Last-Modified: $stamp", true);
85 header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
86 echo "File not found.";
91 function hook_house_keeping() {
92 $files = glob($this->cache_dir
. "/*.{png,mp4}", GLOB_BRACE
);
97 foreach ($files as $file) {
98 list ($article_id, $hash) = explode("-", basename($file));
100 if ($article_id != $last_article_id) {
101 $last_article_id = $article_id;
102 $article_id = db_escape_string($article_id);
104 $result = db_query("SELECT id FROM ttrss_entries WHERE id = " . $article_id);
106 $article_exists = db_num_rows($result) > 0;
109 if (!$article_exists) {
115 function hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id) {
116 $xpath = new DOMXpath($doc);
119 $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
121 foreach ($entries as $entry) {
122 if ($entry->hasAttribute('src')) {
123 $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
125 $extension = $entry->tagName
== 'source' ?
'.mp4' : '.png';
126 $local_filename = $this->cache_dir
. $article_id . "-" . sha1($src) . $extension;
128 if (file_exists($local_filename)) {
129 $entry->setAttribute("src", get_self_url_prefix() .
130 "/public.php?op=cache_starred_images_getimage&method=image&hash=" .
131 $article_id . "-" . sha1($src) . $extension);
141 function hook_update_task() {
142 $result = db_query("SELECT content, ttrss_user_entries.owner_uid, link, site_url, ttrss_entries.id, plugin_data
143 FROM ttrss_entries, ttrss_user_entries LEFT JOIN ttrss_feeds ON
144 (ttrss_user_entries.feed_id = ttrss_feeds.id)
145 WHERE ref_id = ttrss_entries.id AND
147 (UPPER(content) LIKE '%<IMG%' OR UPPER(content) LIKE '%<VIDEO%') AND
149 plugin_data NOT LIKE '%starred_cache_images%'
150 ORDER BY ".sql_random_function()." LIMIT 100");
152 while ($line = db_fetch_assoc($result)) {
153 if ($line["site_url"]) {
154 $success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]);
157 $plugin_data = db_escape_string("starred_cache_images,${line['owner_uid']}:" . $line["plugin_data"]);
159 db_query("UPDATE ttrss_entries SET plugin_data = '$plugin_data' WHERE id = " . $line["id"]);
165 function cache_article_images($content, $site_url, $owner_uid, $article_id) {
166 libxml_use_internal_errors(true);
168 $charset_hack = '<head>
169 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
172 $doc = new DOMDocument();
173 $doc->loadHTML($charset_hack . $content);
174 $xpath = new DOMXPath($doc);
176 $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
181 foreach ($entries as $entry) {
183 if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) {
186 $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
188 $extension = $entry->tagName
== 'source' ?
'.mp4' : '.png';
190 $local_filename = $this->cache_dir
. $article_id . "-" . sha1($src) . $extension;
192 //_debug("cache_images: downloading: $src to $local_filename");
194 if (!file_exists($local_filename)) {
195 $file_content = fetch_file_contents($src);
197 if ($file_content && strlen($file_content) > 0) {
198 file_put_contents($local_filename, $file_content);
207 return $success ||
!$has_images;
210 function api_version() {