]> git.wh0rd.org - tt-rss.git/blob - plugins/cache_starred_images/init.php
cache_starred_articles: limit maximum amount of download attempts per-article, consid...
[tt-rss.git] / plugins / cache_starred_images / init.php
1 <?php
2 class Cache_Starred_Images extends Plugin implements IHandler {
3
4 /* @var PluginHost $host */
5 private $host;
6 private $cache_dir;
7 private $max_cache_attempts = 5; // per-article
8
9 function about() {
10 return array(1.0,
11 "Automatically cache Starred articles' images and HTML5 video files",
12 "fox",
13 true);
14 }
15
16 /**
17 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
18 */
19 function csrf_ignore($method) {
20 return false;
21 }
22
23 /**
24 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
25 */
26 function before($method) {
27 return true;
28 }
29
30 function after() {
31 return true;
32 }
33
34 function init($host) {
35 $this->host = $host;
36
37 $this->cache_dir = CACHE_DIR . "/starred-images/";
38
39 if (!is_dir($this->cache_dir)) {
40 mkdir($this->cache_dir);
41 }
42
43 if (is_dir($this->cache_dir)) {
44
45 if (!is_writable($this->cache_dir))
46 chmod($this->cache_dir, 0777);
47
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);
53
54 } else {
55 user_error("Starred cache directory is not writable.", E_USER_WARNING);
56 }
57
58 } else {
59 user_error("Unable to create starred cache directory.", E_USER_WARNING);
60 }
61 }
62
63 function cache_starred_images_getimage() {
64 ob_end_clean();
65
66 $hash = basename($_REQUEST["hash"]);
67
68 if ($hash) {
69
70 $filename = $this->cache_dir . "/" . basename($hash);
71
72 if (file_exists($filename)) {
73 header("Content-Disposition: attachment; filename=\"$hash\"");
74
75 send_local_file($filename);
76 } else {
77 header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
78 echo "File not found.";
79 }
80 }
81 }
82
83 /**
84 * @SuppressWarnings(PHPMD.UnusedLocalVariable)
85 */
86 function hook_house_keeping() {
87 $files = glob($this->cache_dir . "/*.{png,mp4,status}", GLOB_BRACE);
88
89 $last_article_id = 0;
90 $article_exists = 1;
91
92 foreach ($files as $file) {
93 list ($article_id, $hash) = explode("-", basename($file));
94
95 if ($article_id != $last_article_id) {
96 $last_article_id = $article_id;
97
98 $sth = $this->pdo->prepare("SELECT id FROM ttrss_entries WHERE id = ?");
99 $sth->execute([$article_id]);
100
101 $article_exists = $sth->fetch();
102 }
103
104 if (!$article_exists) {
105 unlink($file);
106 }
107 }
108 }
109
110 /**
111 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
112 */
113 function hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id) {
114 $xpath = new DOMXpath($doc);
115
116 if ($article_id) {
117 $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
118
119 foreach ($entries as $entry) {
120 if ($entry->hasAttribute('src')) {
121 $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
122
123 $extension = $entry->tagName == 'source' ? '.mp4' : '.png';
124 $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension;
125
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);
130 }
131
132 }
133 }
134 }
135
136 return $doc;
137 }
138
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
144 marked = true AND
145 (UPPER(content) LIKE '%<IMG%' OR UPPER(content) LIKE '%<VIDEO%') AND
146 site_url != '' AND
147 plugin_data NOT LIKE '%starred_cache_images%'
148 ORDER BY ".sql_random_function()." LIMIT 100");
149
150 $usth = $this->pdo->prepare("UPDATE ttrss_entries SET plugin_data = ? WHERE id = ?");
151
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"]);
155
156 if ($success) {
157 $plugin_data = "starred_cache_images,${line['owner_uid']}:" . $line["plugin_data"];
158
159 $usth->execute([$plugin_data, $line['id']]);
160 }
161 }
162 }
163 }
164
165 /**
166 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
167 */
168 function cache_article_images($content, $site_url, $owner_uid, $article_id) {
169 libxml_use_internal_errors(true);
170
171 $status_filename = $this->cache_dir . $article_id . "-" . sha1($site_url) . ".status";
172
173 //_debug("status: $status_filename"); return;
174
175 if (file_exists($status_filename))
176 $status = json_decode(file_get_contents($status_filename), true);
177 else
178 $status = [];
179
180 $status["attempt"] += 1;
181
182 // only allow several download attempts for article
183 if ($status["attempt"] > $this->max_cache_attempts) {
184 _debug("too many attempts for $site_url");
185 return;
186 }
187
188 if (!file_put_contents($status_filename, json_encode($status))) {
189 user_error("unable to write status file: $status_filename", E_USER_WARNING);
190 return;
191 }
192
193 $charset_hack = '<head>
194 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
195 </head>';
196
197 $doc = new DOMDocument();
198 $doc->loadHTML($charset_hack . $content);
199 $xpath = new DOMXPath($doc);
200
201 $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
202
203 $success = false;
204 $has_images = false;
205
206 foreach ($entries as $entry) {
207
208 if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) {
209
210 $has_images = true;
211 $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
212
213 $extension = $entry->tagName == 'source' ? '.mp4' : '.png';
214
215 $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension;
216
217 //_debug("cache_images: downloading: $src to $local_filename");
218
219 if (!file_exists($local_filename)) {
220 $file_content = fetch_file_contents(["url" => $src, "max_size" => MAX_CACHE_FILE_SIZE]);
221
222 if ($file_content) {
223 if (strlen($file_content) > MIN_CACHE_FILE_SIZE) {
224 file_put_contents($local_filename, $file_content);
225 }
226
227 $success = true;
228 }
229 } else {
230 $success = true;
231 }
232 }
233 }
234
235 return $success || !$has_images;
236 }
237
238 function api_version() {
239 return 2;
240 }
241 }