]> git.wh0rd.org Git - tt-rss.git/blob - plugins/cache_starred_images/init.php
8a3464e0d758abb41817d018016d1e67e967cbec
[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
8         function about() {
9                 return array(1.0,
10                         "Automatically cache Starred articles' images and HTML5 video files",
11                         "fox",
12                         true);
13         }
14
15         /**
16          * @SuppressWarnings(PHPMD.UnusedFormalParameter)
17          */
18         function csrf_ignore($method) {
19                 return false;
20         }
21
22         /**
23          * @SuppressWarnings(PHPMD.UnusedFormalParameter)
24          */
25         function before($method) {
26                 return true;
27         }
28
29         function after() {
30                 return true;
31         }
32
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)) {
43
44                         if (!is_writable($this->cache_dir))
45                                 chmod($this->cache_dir, 0777);
46
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);
52
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
62         function cache_starred_images_getimage() {
63                 ob_end_clean();
64
65                 $hash = basename($_REQUEST["hash"]);
66
67                 if ($hash) {
68
69                         $filename = $this->cache_dir . "/" . basename($hash);
70
71                         if (file_exists($filename)) {
72                                 header("Content-Disposition: attachment; filename=\"$hash\"");
73
74                                 send_local_file($filename);
75                         } else {
76                                 header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
77                                 echo "File not found.";
78                         }
79                 }
80         }
81
82         /**
83          * @SuppressWarnings(PHPMD.UnusedLocalVariable)
84          */
85         function hook_house_keeping() {
86                 $files = glob($this->cache_dir . "/*.{png,mp4}", GLOB_BRACE);
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;
96
97                                 $sth = $this->pdo->prepare("SELECT id FROM ttrss_entries WHERE id = ?");
98                                 $sth->execute([$article_id]);
99
100                                 $article_exists = $sth->fetch();
101                         }
102
103                         if (!$article_exists) {
104                                 unlink($file);
105                         }
106                 }
107         }
108
109         /**
110          * @SuppressWarnings(PHPMD.UnusedFormalParameter)
111          */
112         function hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id) {
113                 $xpath = new DOMXpath($doc);
114
115                 if ($article_id) {
116                         $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
117
118                         foreach ($entries as $entry) {
119                                 if ($entry->hasAttribute('src')) {
120                                         $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
121
122                                         $extension = $entry->tagName == 'source' ? '.mp4' : '.png';
123                                         $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension;
124
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);
129                                         }
130
131                                 }
132                         }
133                 }
134
135                 return $doc;
136         }
137
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
143                                 marked = true AND
144                                 (UPPER(content) LIKE '%<IMG%' OR UPPER(content) LIKE '%<VIDEO%') AND
145                                 site_url != '' AND
146                                 plugin_data NOT LIKE '%starred_cache_images%'
147                         ORDER BY ".sql_random_function()." LIMIT 100");
148
149                 $usth = $this->pdo->prepare("UPDATE ttrss_entries SET plugin_data = ? WHERE id = ?");
150
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"]);
154
155                                 if ($success) {
156                                         $plugin_data = "starred_cache_images,${line['owner_uid']}:" . $line["plugin_data"];
157
158                                         $usth->execute([$plugin_data, $line['id']]);
159                                 }
160                         }
161                 }
162         }
163
164         /**
165          * @SuppressWarnings(PHPMD.UnusedFormalParameter)
166          */
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
178                 $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
179
180                 $success = false;
181                 $has_images = false;
182
183                 foreach ($entries as $entry) {
184
185                         if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) {
186
187                                 $has_images = true;
188                                 $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
189
190                                 $extension = $entry->tagName == 'source' ? '.mp4' : '.png';
191
192                                 $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension;
193
194                                 //_debug("cache_images: downloading: $src to $local_filename");
195
196                                 if (!file_exists($local_filename)) {
197                                         $file_content = fetch_file_contents(["url" => $src, "max_size" => MAX_CACHE_FILE_SIZE]);
198
199                                         if ($file_content && strlen($file_content) > MIN_CACHE_FILE_SIZE) {
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 }