]>
Commit | Line | Data |
---|---|---|
910592b4 | 1 | <?php |
95ebe737 | 2 | class Cache_Starred_Images extends Plugin implements IHandler { |
910592b4 | 3 | |
96930889 | 4 | /* @var PluginHost $host */ |
910592b4 AD |
5 | private $host; |
6 | private $cache_dir; | |
75875268 | 7 | private $max_cache_attempts = 5; // per-article |
910592b4 AD |
8 | |
9 | function about() { | |
10 | return array(1.0, | |
88bf000f | 11 | "Automatically cache Starred articles' images and HTML5 video files", |
910592b4 AD |
12 | "fox", |
13 | true); | |
14 | } | |
15 | ||
21ce7d9e AD |
16 | /** |
17 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
18 | */ | |
95ebe737 AD |
19 | function csrf_ignore($method) { |
20 | return false; | |
21 | } | |
22 | ||
21ce7d9e AD |
23 | /** |
24 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
25 | */ | |
95ebe737 AD |
26 | function before($method) { |
27 | return true; | |
28 | } | |
29 | ||
30 | function after() { | |
31 | return true; | |
32 | } | |
33 | ||
910592b4 AD |
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)) { | |
f5967cf8 AD |
44 | |
45 | if (!is_writable($this->cache_dir)) | |
46 | chmod($this->cache_dir, 0777); | |
910592b4 AD |
47 | |
48 | if (is_writable($this->cache_dir)) { | |
49 | $host->add_hook($host::HOOK_UPDATE_TASK, $this); | |
4e5ddeaf | 50 | $host->add_hook($host::HOOK_HOUSE_KEEPING, $this); |
910592b4 | 51 | $host->add_hook($host::HOOK_SANITIZE, $this); |
95ebe737 AD |
52 | $host->add_handler("public", "cache_starred_images_getimage", $this); |
53 | ||
910592b4 AD |
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 | ||
95ebe737 | 63 | function cache_starred_images_getimage() { |
ae3851b1 AD |
64 | ob_end_clean(); |
65 | ||
910592b4 AD |
66 | $hash = basename($_REQUEST["hash"]); |
67 | ||
68 | if ($hash) { | |
69 | ||
c203486e | 70 | $filename = $this->cache_dir . "/" . basename($hash); |
910592b4 AD |
71 | |
72 | if (file_exists($filename)) { | |
c203486e AD |
73 | header("Content-Disposition: attachment; filename=\"$hash\""); |
74 | ||
8b73bd28 | 75 | send_local_file($filename); |
910592b4 AD |
76 | } else { |
77 | header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); | |
78 | echo "File not found."; | |
79 | } | |
80 | } | |
81 | } | |
82 | ||
21ce7d9e AD |
83 | /** |
84 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) | |
85 | */ | |
4e5ddeaf | 86 | function hook_house_keeping() { |
75875268 | 87 | $files = glob($this->cache_dir . "/*.{png,mp4,status}", GLOB_BRACE); |
4e5ddeaf AD |
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; | |
4e5ddeaf | 97 | |
96930889 AD |
98 | $sth = $this->pdo->prepare("SELECT id FROM ttrss_entries WHERE id = ?"); |
99 | $sth->execute([$article_id]); | |
4e5ddeaf | 100 | |
96930889 | 101 | $article_exists = $sth->fetch(); |
4e5ddeaf AD |
102 | } |
103 | ||
104 | if (!$article_exists) { | |
105 | unlink($file); | |
106 | } | |
107 | } | |
108 | } | |
109 | ||
21ce7d9e AD |
110 | /** |
111 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
112 | */ | |
910592b4 AD |
113 | function hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id) { |
114 | $xpath = new DOMXpath($doc); | |
115 | ||
116 | if ($article_id) { | |
88bf000f | 117 | $entries = $xpath->query('(//img[@src])|(//video/source[@src])'); |
910592b4 AD |
118 | |
119 | foreach ($entries as $entry) { | |
120 | if ($entry->hasAttribute('src')) { | |
121 | $src = rewrite_relative_url($site_url, $entry->getAttribute('src')); | |
122 | ||
88bf000f AD |
123 | $extension = $entry->tagName == 'source' ? '.mp4' : '.png'; |
124 | $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension; | |
910592b4 AD |
125 | |
126 | if (file_exists($local_filename)) { | |
127 | $entry->setAttribute("src", get_self_url_prefix() . | |
95ebe737 | 128 | "/public.php?op=cache_starred_images_getimage&method=image&hash=" . |
88bf000f | 129 | $article_id . "-" . sha1($src) . $extension); |
910592b4 AD |
130 | } |
131 | ||
132 | } | |
133 | } | |
134 | } | |
135 | ||
136 | return $doc; | |
137 | } | |
138 | ||
139 | function hook_update_task() { | |
96930889 | 140 | $res = $this->pdo->query("SELECT content, ttrss_user_entries.owner_uid, link, site_url, ttrss_entries.id, plugin_data |
910592b4 AD |
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 | |
88bf000f | 145 | (UPPER(content) LIKE '%<IMG%' OR UPPER(content) LIKE '%<VIDEO%') AND |
910592b4 AD |
146 | site_url != '' AND |
147 | plugin_data NOT LIKE '%starred_cache_images%' | |
148 | ORDER BY ".sql_random_function()." LIMIT 100"); | |
149 | ||
96930889 AD |
150 | $usth = $this->pdo->prepare("UPDATE ttrss_entries SET plugin_data = ? WHERE id = ?"); |
151 | ||
152 | while ($line = $res->fetch()) { | |
910592b4 AD |
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) { | |
96930889 | 157 | $plugin_data = "starred_cache_images,${line['owner_uid']}:" . $line["plugin_data"]; |
910592b4 | 158 | |
96930889 | 159 | $usth->execute([$plugin_data, $line['id']]); |
910592b4 AD |
160 | } |
161 | } | |
162 | } | |
163 | } | |
164 | ||
21ce7d9e AD |
165 | /** |
166 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
167 | */ | |
910592b4 AD |
168 | function cache_article_images($content, $site_url, $owner_uid, $article_id) { |
169 | libxml_use_internal_errors(true); | |
170 | ||
75875268 AD |
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 | ||
910592b4 AD |
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 | ||
88bf000f | 201 | $entries = $xpath->query('(//img[@src])|(//video/source[@src])'); |
910592b4 AD |
202 | |
203 | $success = false; | |
204 | $has_images = false; | |
205 | ||
206 | foreach ($entries as $entry) { | |
88bf000f | 207 | |
5edd605a AD |
208 | if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) { |
209 | ||
910592b4 AD |
210 | $has_images = true; |
211 | $src = rewrite_relative_url($site_url, $entry->getAttribute('src')); | |
212 | ||
88bf000f AD |
213 | $extension = $entry->tagName == 'source' ? '.mp4' : '.png'; |
214 | ||
215 | $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension; | |
910592b4 AD |
216 | |
217 | //_debug("cache_images: downloading: $src to $local_filename"); | |
218 | ||
219 | if (!file_exists($local_filename)) { | |
b14f6d58 | 220 | $file_content = fetch_file_contents(["url" => $src, "max_size" => MAX_CACHE_FILE_SIZE]); |
910592b4 | 221 | |
75875268 AD |
222 | if ($file_content) { |
223 | if (strlen($file_content) > MIN_CACHE_FILE_SIZE) { | |
224 | file_put_contents($local_filename, $file_content); | |
225 | } | |
226 | ||
910592b4 AD |
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 | } |