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