]>
git.wh0rd.org - tt-rss.git/blob - plugins/cache_starred_images/init.php
8a3464e0d758abb41817d018016d1e67e967cbec
2 class Cache_Starred_Images
extends Plugin
implements IHandler
{
4 /* @var PluginHost $host */
10 "Automatically cache Starred articles' images and HTML5 video files",
16 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
18 function csrf_ignore($method) {
23 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
25 function before($method) {
33 function init($host) {
36 $this->cache_dir
= CACHE_DIR
. "/starred-images/";
38 if (!is_dir($this->cache_dir
)) {
39 mkdir($this->cache_dir
);
42 if (is_dir($this->cache_dir
)) {
44 if (!is_writable($this->cache_dir
))
45 chmod($this->cache_dir
, 0777);
47 if (is_writable($this->cache_dir
)) {
48 $host->add_hook($host::HOOK_UPDATE_TASK
, $this);
49 $host->add_hook($host::HOOK_HOUSE_KEEPING
, $this);
50 $host->add_hook($host::HOOK_SANITIZE
, $this);
51 $host->add_handler("public", "cache_starred_images_getimage", $this);
54 user_error("Starred cache directory is not writable.", E_USER_WARNING
);
58 user_error("Unable to create starred cache directory.", E_USER_WARNING
);
62 function cache_starred_images_getimage() {
65 $hash = basename($_REQUEST["hash"]);
69 $filename = $this->cache_dir
. "/" . basename($hash);
71 if (file_exists($filename)) {
72 header("Content-Disposition: attachment; filename=\"$hash\"");
74 send_local_file($filename);
76 header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
77 echo "File not found.";
83 * @SuppressWarnings(PHPMD.UnusedLocalVariable)
85 function hook_house_keeping() {
86 $files = glob($this->cache_dir
. "/*.{png,mp4}", GLOB_BRACE
);
91 foreach ($files as $file) {
92 list ($article_id, $hash) = explode("-", basename($file));
94 if ($article_id != $last_article_id) {
95 $last_article_id = $article_id;
97 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_entries WHERE id = ?");
98 $sth->execute([$article_id]);
100 $article_exists = $sth->fetch();
103 if (!$article_exists) {
110 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
112 function hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id) {
113 $xpath = new DOMXpath($doc);
116 $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
118 foreach ($entries as $entry) {
119 if ($entry->hasAttribute('src')) {
120 $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
122 $extension = $entry->tagName
== 'source' ?
'.mp4' : '.png';
123 $local_filename = $this->cache_dir
. $article_id . "-" . sha1($src) . $extension;
125 if (file_exists($local_filename)) {
126 $entry->setAttribute("src", get_self_url_prefix() .
127 "/public.php?op=cache_starred_images_getimage&method=image&hash=" .
128 $article_id . "-" . sha1($src) . $extension);
138 function hook_update_task() {
139 $res = $this->pdo
->query("SELECT content, ttrss_user_entries.owner_uid, link, site_url, ttrss_entries.id, plugin_data
140 FROM ttrss_entries, ttrss_user_entries LEFT JOIN ttrss_feeds ON
141 (ttrss_user_entries.feed_id = ttrss_feeds.id)
142 WHERE ref_id = ttrss_entries.id AND
144 (UPPER(content) LIKE '%<IMG%' OR UPPER(content) LIKE '%<VIDEO%') AND
146 plugin_data NOT LIKE '%starred_cache_images%'
147 ORDER BY ".sql_random_function()." LIMIT 100");
149 $usth = $this->pdo
->prepare("UPDATE ttrss_entries SET plugin_data = ? WHERE id = ?");
151 while ($line = $res->fetch()) {
152 if ($line["site_url"]) {
153 $success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]);
156 $plugin_data = "starred_cache_images,${line['owner_uid']}:" . $line["plugin_data"];
158 $usth->execute([$plugin_data, $line['id']]);
165 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
167 function cache_article_images($content, $site_url, $owner_uid, $article_id) {
168 libxml_use_internal_errors(true);
170 $charset_hack = '<head>
171 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
174 $doc = new DOMDocument();
175 $doc->loadHTML($charset_hack . $content);
176 $xpath = new DOMXPath($doc);
178 $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
183 foreach ($entries as $entry) {
185 if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) {
188 $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
190 $extension = $entry->tagName
== 'source' ?
'.mp4' : '.png';
192 $local_filename = $this->cache_dir
. $article_id . "-" . sha1($src) . $extension;
194 //_debug("cache_images: downloading: $src to $local_filename");
196 if (!file_exists($local_filename)) {
197 $file_content = fetch_file_contents(["url" => $src, "max_size" => MAX_CACHE_FILE_SIZE
]);
199 if ($file_content && strlen($file_content) > MIN_CACHE_FILE_SIZE
) {
200 file_put_contents($local_filename, $file_content);
209 return $success ||
!$has_images;
212 function api_version() {