]>
git.wh0rd.org - tt-rss.git/blob - plugins/cache_starred_images/init.php
82e7d1fbd6b46a0a675d51cf06a9122794da4172
2 class Cache_Starred_Images
extends Plugin
implements IHandler
{
9 "Automatically cache Starred articles' images and HTML5 video files",
15 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
17 function csrf_ignore($method) {
22 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
24 function before($method) {
32 function init($host) {
35 $this->cache_dir
= CACHE_DIR
. "/starred-images/";
37 if (!is_dir($this->cache_dir
)) {
38 mkdir($this->cache_dir
);
41 if (is_dir($this->cache_dir
)) {
43 if (!is_writable($this->cache_dir
))
44 chmod($this->cache_dir
, 0777);
46 if (is_writable($this->cache_dir
)) {
47 $host->add_hook($host::HOOK_UPDATE_TASK
, $this);
48 $host->add_hook($host::HOOK_HOUSE_KEEPING
, $this);
49 $host->add_hook($host::HOOK_SANITIZE
, $this);
50 $host->add_handler("public", "cache_starred_images_getimage", $this);
53 user_error("Starred cache directory is not writable.", E_USER_WARNING
);
57 user_error("Unable to create starred cache directory.", E_USER_WARNING
);
61 function cache_starred_images_getimage() {
64 $hash = basename($_REQUEST["hash"]);
68 $filename = $this->cache_dir
. "/" . basename($hash);
70 if (file_exists($filename)) {
71 header("Content-Disposition: attachment; filename=\"$hash\"");
73 send_local_file($filename);
75 header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
76 echo "File not found.";
82 * @SuppressWarnings(PHPMD.UnusedLocalVariable)
84 function hook_house_keeping() {
85 $files = glob($this->cache_dir
. "/*.{png,mp4}", GLOB_BRACE
);
90 foreach ($files as $file) {
91 list ($article_id, $hash) = explode("-", basename($file));
93 if ($article_id != $last_article_id) {
94 $last_article_id = $article_id;
95 $article_id = db_escape_string($article_id);
97 $result = db_query("SELECT id FROM ttrss_entries WHERE id = " . $article_id);
99 $article_exists = db_num_rows($result) > 0;
102 if (!$article_exists) {
109 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
111 function hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id) {
112 $xpath = new DOMXpath($doc);
115 $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
117 foreach ($entries as $entry) {
118 if ($entry->hasAttribute('src')) {
119 $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
121 $extension = $entry->tagName
== 'source' ?
'.mp4' : '.png';
122 $local_filename = $this->cache_dir
. $article_id . "-" . sha1($src) . $extension;
124 if (file_exists($local_filename)) {
125 $entry->setAttribute("src", get_self_url_prefix() .
126 "/public.php?op=cache_starred_images_getimage&method=image&hash=" .
127 $article_id . "-" . sha1($src) . $extension);
137 function hook_update_task() {
138 $result = db_query("SELECT content, ttrss_user_entries.owner_uid, link, site_url, ttrss_entries.id, plugin_data
139 FROM ttrss_entries, ttrss_user_entries LEFT JOIN ttrss_feeds ON
140 (ttrss_user_entries.feed_id = ttrss_feeds.id)
141 WHERE ref_id = ttrss_entries.id AND
143 (UPPER(content) LIKE '%<IMG%' OR UPPER(content) LIKE '%<VIDEO%') AND
145 plugin_data NOT LIKE '%starred_cache_images%'
146 ORDER BY ".sql_random_function()." LIMIT 100");
148 while ($line = db_fetch_assoc($result)) {
149 if ($line["site_url"]) {
150 $success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]);
153 $plugin_data = db_escape_string("starred_cache_images,${line['owner_uid']}:" . $line["plugin_data"]);
155 db_query("UPDATE ttrss_entries SET plugin_data = '$plugin_data' WHERE id = " . $line["id"]);
162 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
164 function cache_article_images($content, $site_url, $owner_uid, $article_id) {
165 libxml_use_internal_errors(true);
167 $charset_hack = '<head>
168 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
171 $doc = new DOMDocument();
172 $doc->loadHTML($charset_hack . $content);
173 $xpath = new DOMXPath($doc);
175 $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
180 foreach ($entries as $entry) {
182 if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) {
185 $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
187 $extension = $entry->tagName
== 'source' ?
'.mp4' : '.png';
189 $local_filename = $this->cache_dir
. $article_id . "-" . sha1($src) . $extension;
191 //_debug("cache_images: downloading: $src to $local_filename");
193 if (!file_exists($local_filename)) {
194 $file_content = fetch_file_contents($src);
196 if ($file_content && strlen($file_content) > MIN_CACHE_FILE_SIZE
) {
197 file_put_contents($local_filename, $file_content);
206 return $success ||
!$has_images;
209 function api_version() {