]>
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; | |
7 | ||
8 | function about() { | |
9 | return array(1.0, | |
88bf000f | 10 | "Automatically cache Starred articles' images and HTML5 video files", |
910592b4 AD |
11 | "fox", |
12 | true); | |
13 | } | |
14 | ||
21ce7d9e AD |
15 | /** |
16 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
17 | */ | |
95ebe737 AD |
18 | function csrf_ignore($method) { |
19 | return false; | |
20 | } | |
21 | ||
21ce7d9e AD |
22 | /** |
23 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
24 | */ | |
95ebe737 AD |
25 | function before($method) { |
26 | return true; | |
27 | } | |
28 | ||
29 | function after() { | |
30 | return true; | |
31 | } | |
32 | ||
910592b4 AD |
33 | function init($host) { |
34 | $this->host = $host; | |
35 | ||
36 | $this->cache_dir = CACHE_DIR . "/starred-images/"; | |
37 | ||
38 | if (!is_dir($this->cache_dir)) { | |
39 | mkdir($this->cache_dir); | |
40 | } | |
41 | ||
42 | if (is_dir($this->cache_dir)) { | |
f5967cf8 AD |
43 | |
44 | if (!is_writable($this->cache_dir)) | |
45 | chmod($this->cache_dir, 0777); | |
910592b4 AD |
46 | |
47 | if (is_writable($this->cache_dir)) { | |
48 | $host->add_hook($host::HOOK_UPDATE_TASK, $this); | |
4e5ddeaf | 49 | $host->add_hook($host::HOOK_HOUSE_KEEPING, $this); |
910592b4 | 50 | $host->add_hook($host::HOOK_SANITIZE, $this); |
95ebe737 AD |
51 | $host->add_handler("public", "cache_starred_images_getimage", $this); |
52 | ||
910592b4 AD |
53 | } else { |
54 | user_error("Starred cache directory is not writable.", E_USER_WARNING); | |
55 | } | |
56 | ||
57 | } else { | |
58 | user_error("Unable to create starred cache directory.", E_USER_WARNING); | |
59 | } | |
60 | } | |
61 | ||
95ebe737 | 62 | function cache_starred_images_getimage() { |
ae3851b1 AD |
63 | ob_end_clean(); |
64 | ||
910592b4 AD |
65 | $hash = basename($_REQUEST["hash"]); |
66 | ||
67 | if ($hash) { | |
68 | ||
c203486e | 69 | $filename = $this->cache_dir . "/" . basename($hash); |
910592b4 AD |
70 | |
71 | if (file_exists($filename)) { | |
c203486e AD |
72 | header("Content-Disposition: attachment; filename=\"$hash\""); |
73 | ||
8b73bd28 | 74 | send_local_file($filename); |
910592b4 AD |
75 | } else { |
76 | header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); | |
77 | echo "File not found."; | |
78 | } | |
79 | } | |
80 | } | |
81 | ||
21ce7d9e AD |
82 | /** |
83 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) | |
84 | */ | |
4e5ddeaf | 85 | function hook_house_keeping() { |
88bf000f | 86 | $files = glob($this->cache_dir . "/*.{png,mp4}", GLOB_BRACE); |
4e5ddeaf AD |
87 | |
88 | $last_article_id = 0; | |
89 | $article_exists = 1; | |
90 | ||
91 | foreach ($files as $file) { | |
92 | list ($article_id, $hash) = explode("-", basename($file)); | |
93 | ||
94 | if ($article_id != $last_article_id) { | |
95 | $last_article_id = $article_id; | |
4e5ddeaf | 96 | |
96930889 AD |
97 | $sth = $this->pdo->prepare("SELECT id FROM ttrss_entries WHERE id = ?"); |
98 | $sth->execute([$article_id]); | |
4e5ddeaf | 99 | |
96930889 | 100 | $article_exists = $sth->fetch(); |
4e5ddeaf AD |
101 | } |
102 | ||
103 | if (!$article_exists) { | |
104 | unlink($file); | |
105 | } | |
106 | } | |
107 | } | |
108 | ||
21ce7d9e AD |
109 | /** |
110 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
111 | */ | |
910592b4 AD |
112 | function hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id) { |
113 | $xpath = new DOMXpath($doc); | |
114 | ||
115 | if ($article_id) { | |
88bf000f | 116 | $entries = $xpath->query('(//img[@src])|(//video/source[@src])'); |
910592b4 AD |
117 | |
118 | foreach ($entries as $entry) { | |
119 | if ($entry->hasAttribute('src')) { | |
120 | $src = rewrite_relative_url($site_url, $entry->getAttribute('src')); | |
121 | ||
88bf000f AD |
122 | $extension = $entry->tagName == 'source' ? '.mp4' : '.png'; |
123 | $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension; | |
910592b4 AD |
124 | |
125 | if (file_exists($local_filename)) { | |
126 | $entry->setAttribute("src", get_self_url_prefix() . | |
95ebe737 | 127 | "/public.php?op=cache_starred_images_getimage&method=image&hash=" . |
88bf000f | 128 | $article_id . "-" . sha1($src) . $extension); |
910592b4 AD |
129 | } |
130 | ||
131 | } | |
132 | } | |
133 | } | |
134 | ||
135 | return $doc; | |
136 | } | |
137 | ||
138 | function hook_update_task() { | |
96930889 | 139 | $res = $this->pdo->query("SELECT content, ttrss_user_entries.owner_uid, link, site_url, ttrss_entries.id, plugin_data |
910592b4 AD |
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 | |
143 | marked = true AND | |
88bf000f | 144 | (UPPER(content) LIKE '%<IMG%' OR UPPER(content) LIKE '%<VIDEO%') AND |
910592b4 AD |
145 | site_url != '' AND |
146 | plugin_data NOT LIKE '%starred_cache_images%' | |
147 | ORDER BY ".sql_random_function()." LIMIT 100"); | |
148 | ||
96930889 AD |
149 | $usth = $this->pdo->prepare("UPDATE ttrss_entries SET plugin_data = ? WHERE id = ?"); |
150 | ||
151 | while ($line = $res->fetch()) { | |
910592b4 AD |
152 | if ($line["site_url"]) { |
153 | $success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]); | |
154 | ||
155 | if ($success) { | |
96930889 | 156 | $plugin_data = "starred_cache_images,${line['owner_uid']}:" . $line["plugin_data"]; |
910592b4 | 157 | |
96930889 | 158 | $usth->execute([$plugin_data, $line['id']]); |
910592b4 AD |
159 | } |
160 | } | |
161 | } | |
162 | } | |
163 | ||
21ce7d9e AD |
164 | /** |
165 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
166 | */ | |
910592b4 AD |
167 | function cache_article_images($content, $site_url, $owner_uid, $article_id) { |
168 | libxml_use_internal_errors(true); | |
169 | ||
170 | $charset_hack = '<head> | |
171 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | |
172 | </head>'; | |
173 | ||
174 | $doc = new DOMDocument(); | |
175 | $doc->loadHTML($charset_hack . $content); | |
176 | $xpath = new DOMXPath($doc); | |
177 | ||
88bf000f | 178 | $entries = $xpath->query('(//img[@src])|(//video/source[@src])'); |
910592b4 AD |
179 | |
180 | $success = false; | |
181 | $has_images = false; | |
182 | ||
183 | foreach ($entries as $entry) { | |
88bf000f | 184 | |
5edd605a AD |
185 | if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) { |
186 | ||
910592b4 AD |
187 | $has_images = true; |
188 | $src = rewrite_relative_url($site_url, $entry->getAttribute('src')); | |
189 | ||
88bf000f AD |
190 | $extension = $entry->tagName == 'source' ? '.mp4' : '.png'; |
191 | ||
192 | $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension; | |
910592b4 AD |
193 | |
194 | //_debug("cache_images: downloading: $src to $local_filename"); | |
195 | ||
196 | if (!file_exists($local_filename)) { | |
b14f6d58 | 197 | $file_content = fetch_file_contents(["url" => $src, "max_size" => MAX_CACHE_FILE_SIZE]); |
910592b4 | 198 | |
6fd03996 | 199 | if ($file_content && strlen($file_content) > MIN_CACHE_FILE_SIZE) { |
910592b4 AD |
200 | file_put_contents($local_filename, $file_content); |
201 | $success = true; | |
202 | } | |
203 | } else { | |
204 | $success = true; | |
205 | } | |
206 | } | |
207 | } | |
208 | ||
209 | return $success || !$has_images; | |
210 | } | |
211 | ||
212 | function api_version() { | |
213 | return 2; | |
214 | } | |
215 | } |