]> git.wh0rd.org - tt-rss.git/blob - plugins/cache_starred_images/init.php
cache_starred_images: also handle html5 mp4 video files
[tt-rss.git] / plugins / cache_starred_images / init.php
1 <?php
2 class Cache_Starred_Images extends Plugin implements IHandler {
3
4 private $host;
5 private $cache_dir;
6
7 function about() {
8 return array(1.0,
9 "Automatically cache Starred articles' images and HTML5 video files",
10 "fox",
11 true);
12 }
13
14 function csrf_ignore($method) {
15 return false;
16 }
17
18 function before($method) {
19 return true;
20 }
21
22 function after() {
23 return true;
24 }
25
26 function init($host) {
27 $this->host = $host;
28
29 $this->cache_dir = CACHE_DIR . "/starred-images/";
30
31 if (!is_dir($this->cache_dir)) {
32 mkdir($this->cache_dir);
33 }
34
35 if (is_dir($this->cache_dir)) {
36
37 if (!is_writable($this->cache_dir))
38 chmod($this->cache_dir, 0777);
39
40 if (is_writable($this->cache_dir)) {
41 $host->add_hook($host::HOOK_UPDATE_TASK, $this);
42 $host->add_hook($host::HOOK_HOUSE_KEEPING, $this);
43 $host->add_hook($host::HOOK_SANITIZE, $this);
44 $host->add_handler("public", "cache_starred_images_getimage", $this);
45
46 } else {
47 user_error("Starred cache directory is not writable.", E_USER_WARNING);
48 }
49
50 } else {
51 user_error("Unable to create starred cache directory.", E_USER_WARNING);
52 }
53 }
54
55 function cache_starred_images_getimage() {
56 ob_end_clean();
57
58 $hash = basename($_REQUEST["hash"]);
59
60 if ($hash) {
61
62 $filename = $this->cache_dir . "/" . $hash;
63 $is_video = strpos($filename, ".mp4") !== FALSE;
64
65 if (file_exists($filename)) {
66 /* See if we can use X-Sendfile */
67 $xsendfile = false;
68 if (function_exists('apache_get_modules') &&
69 array_search('mod_xsendfile', apache_get_modules()))
70 $xsendfile = true;
71
72 if ($xsendfile) {
73 header("X-Sendfile: $filename");
74 header("Content-type: application/octet-stream");
75 header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
76 } else {
77 header("Content-type: " . ($is_video ? "video/mp4" : "image/png"));
78 $stamp = gmdate("D, d M Y H:i:s", filemtime($filename)). " GMT";
79 header("Last-Modified: $stamp", true);
80 readfile($filename);
81 }
82 } else {
83 header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
84 echo "File not found.";
85 }
86 }
87 }
88
89 function hook_house_keeping() {
90 $files = glob($this->cache_dir . "/*.{png,mp4}", GLOB_BRACE);
91
92 $last_article_id = 0;
93 $article_exists = 1;
94
95 foreach ($files as $file) {
96 list ($article_id, $hash) = explode("-", basename($file));
97
98 if ($article_id != $last_article_id) {
99 $last_article_id = $article_id;
100 $article_id = db_escape_string($article_id);
101
102 $result = db_query("SELECT id FROM ttrss_entries WHERE id = " . $article_id);
103
104 $article_exists = db_num_rows($result) > 0;
105 }
106
107 if (!$article_exists) {
108 unlink($file);
109 }
110 }
111 }
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 $result = db_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 while ($line = db_fetch_assoc($result)) {
151 if ($line["site_url"]) {
152 $success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]);
153
154 if ($success) {
155 $plugin_data = db_escape_string("starred_cache_images,${line['owner_uid']}:" . $line["plugin_data"]);
156
157 db_query("UPDATE ttrss_entries SET plugin_data = '$plugin_data' WHERE id = " . $line["id"]);
158 }
159 }
160 }
161 }
162
163 function cache_article_images($content, $site_url, $owner_uid, $article_id) {
164 libxml_use_internal_errors(true);
165
166 $charset_hack = '<head>
167 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
168 </head>';
169
170 $doc = new DOMDocument();
171 $doc->loadHTML($charset_hack . $content);
172 $xpath = new DOMXPath($doc);
173
174 $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
175
176 $success = false;
177 $has_images = false;
178
179 foreach ($entries as $entry) {
180
181 if ($entry->hasAttribute('src')) {
182 $has_images = true;
183 $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
184
185 $extension = $entry->tagName == 'source' ? '.mp4' : '.png';
186
187 $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension;
188
189 //_debug("cache_images: downloading: $src to $local_filename");
190
191 if (!file_exists($local_filename)) {
192 $file_content = fetch_file_contents($src);
193
194 if ($file_content && strlen($file_content) > 0) {
195 file_put_contents($local_filename, $file_content);
196 $success = true;
197 }
198 } else {
199 $success = true;
200 }
201 }
202 }
203
204 return $success || !$has_images;
205 }
206
207 function api_version() {
208 return 2;
209 }
210 }
211 ?>