]>
git.wh0rd.org - tt-rss.git/blob - plugins/cache_starred_images/init.php
2 class Cache_Starred_Images
extends Plugin
implements IHandler
{
4 /* @var PluginHost $host */
7 private $max_cache_attempts = 5; // per-article
11 "Automatically cache Starred articles' images and HTML5 video files",
17 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
19 function csrf_ignore($method) {
24 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
26 function before($method) {
34 function init($host) {
37 $this->cache_dir
= CACHE_DIR
. "/starred-images/";
39 if (!is_dir($this->cache_dir
)) {
40 mkdir($this->cache_dir
);
43 if (is_dir($this->cache_dir
)) {
45 if (!is_writable($this->cache_dir
))
46 chmod($this->cache_dir
, 0777);
48 if (is_writable($this->cache_dir
)) {
49 $host->add_hook($host::HOOK_UPDATE_TASK
, $this);
50 $host->add_hook($host::HOOK_HOUSE_KEEPING
, $this);
51 $host->add_hook($host::HOOK_SANITIZE
, $this);
52 $host->add_handler("public", "cache_starred_images_getimage", $this);
55 user_error("Starred cache directory is not writable.", E_USER_WARNING
);
59 user_error("Unable to create starred cache directory.", E_USER_WARNING
);
63 function cache_starred_images_getimage() {
66 $hash = basename($_REQUEST["hash"]);
70 $filename = $this->cache_dir
. "/" . basename($hash);
72 if (file_exists($filename)) {
73 header("Content-Disposition: attachment; filename=\"$hash\"");
75 send_local_file($filename);
77 header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
78 echo "File not found.";
84 * @SuppressWarnings(PHPMD.UnusedLocalVariable)
86 function hook_house_keeping() {
87 $files = glob($this->cache_dir
. "/*.{png,mp4,status}", GLOB_BRACE
);
92 foreach ($files as $file) {
93 list ($article_id, $hash) = explode("-", basename($file));
95 if ($article_id != $last_article_id) {
96 $last_article_id = $article_id;
98 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_entries WHERE id = ?");
99 $sth->execute([$article_id]);
101 $article_exists = $sth->fetch();
104 if (!$article_exists) {
111 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
113 function hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id) {
114 $xpath = new DOMXpath($doc);
117 $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
119 foreach ($entries as $entry) {
120 if ($entry->hasAttribute('src')) {
121 $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
123 $extension = $entry->tagName
== 'source' ?
'.mp4' : '.png';
124 $local_filename = $this->cache_dir
. $article_id . "-" . sha1($src) . $extension;
126 if (file_exists($local_filename)) {
127 $entry->setAttribute("src", get_self_url_prefix() .
128 "/public.php?op=cache_starred_images_getimage&method=image&hash=" .
129 $article_id . "-" . sha1($src) . $extension);
139 function hook_update_task() {
140 $res = $this->pdo
->query("SELECT content, ttrss_user_entries.owner_uid, link, site_url, ttrss_entries.id, plugin_data
141 FROM ttrss_entries, ttrss_user_entries LEFT JOIN ttrss_feeds ON
142 (ttrss_user_entries.feed_id = ttrss_feeds.id)
143 WHERE ref_id = ttrss_entries.id AND
145 (UPPER(content) LIKE '%<IMG%' OR UPPER(content) LIKE '%<VIDEO%') AND
147 plugin_data NOT LIKE '%starred_cache_images%'
148 ORDER BY ".sql_random_function()." LIMIT 100");
150 $usth = $this->pdo
->prepare("UPDATE ttrss_entries SET plugin_data = ? WHERE id = ?");
152 while ($line = $res->fetch()) {
153 if ($line["site_url"]) {
154 $success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]);
157 $plugin_data = "starred_cache_images,${line['owner_uid']}:" . $line["plugin_data"];
159 $usth->execute([$plugin_data, $line['id']]);
166 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
168 function cache_article_images($content, $site_url, $owner_uid, $article_id) {
169 libxml_use_internal_errors(true);
171 $status_filename = $this->cache_dir
. $article_id . "-" . sha1($site_url) . ".status";
173 //_debug("status: $status_filename"); return;
175 if (file_exists($status_filename))
176 $status = json_decode(file_get_contents($status_filename), true);
180 $status["attempt"] +
= 1;
182 // only allow several download attempts for article
183 if ($status["attempt"] > $this->max_cache_attempts
) {
184 _debug("too many attempts for $site_url");
188 if (!file_put_contents($status_filename, json_encode($status))) {
189 user_error("unable to write status file: $status_filename", E_USER_WARNING
);
193 $charset_hack = '<head>
194 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
197 $doc = new DOMDocument();
198 $doc->loadHTML($charset_hack . $content);
199 $xpath = new DOMXPath($doc);
201 $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
206 foreach ($entries as $entry) {
208 if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) {
211 $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
213 $extension = $entry->tagName
== 'source' ?
'.mp4' : '.png';
215 $local_filename = $this->cache_dir
. $article_id . "-" . sha1($src) . $extension;
217 //_debug("cache_images: downloading: $src to $local_filename");
219 if (!file_exists($local_filename)) {
220 $file_content = fetch_file_contents(["url" => $src, "max_size" => MAX_CACHE_FILE_SIZE
]);
223 if (strlen($file_content) > MIN_CACHE_FILE_SIZE
) {
224 file_put_contents($local_filename, $file_content);
235 return $success ||
!$has_images;
238 function api_version() {