]> git.wh0rd.org - tt-rss.git/blob - plugins/cache_starred_images/init.php
63637bfd8743820e6cb829d320635d394a94cd73
[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 . "/" . basename($hash);
63 $is_video = strpos($filename, ".mp4") !== FALSE;
64
65 if (file_exists($filename)) {
66 header("Content-Disposition: attachment; filename=\"$hash\"");
67
68 /* See if we can use X-Sendfile */
69 $xsendfile = false;
70 if (function_exists('apache_get_modules') &&
71 array_search('mod_xsendfile', apache_get_modules()))
72 $xsendfile = true;
73
74 if ($xsendfile) {
75 header("X-Sendfile: $filename");
76 header("Content-type: application/octet-stream");
77 header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
78 } else {
79 header("Content-type: " . ($is_video ? "video/mp4" : "image/png"));
80 $stamp = gmdate("D, d M Y H:i:s", filemtime($filename)). " GMT";
81 header("Last-Modified: $stamp", true);
82 readfile($filename);
83 }
84 } else {
85 header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
86 echo "File not found.";
87 }
88 }
89 }
90
91 function hook_house_keeping() {
92 $files = glob($this->cache_dir . "/*.{png,mp4}", GLOB_BRACE);
93
94 $last_article_id = 0;
95 $article_exists = 1;
96
97 foreach ($files as $file) {
98 list ($article_id, $hash) = explode("-", basename($file));
99
100 if ($article_id != $last_article_id) {
101 $last_article_id = $article_id;
102 $article_id = db_escape_string($article_id);
103
104 $result = db_query("SELECT id FROM ttrss_entries WHERE id = " . $article_id);
105
106 $article_exists = db_num_rows($result) > 0;
107 }
108
109 if (!$article_exists) {
110 unlink($file);
111 }
112 }
113 }
114
115 function hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id) {
116 $xpath = new DOMXpath($doc);
117
118 if ($article_id) {
119 $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
120
121 foreach ($entries as $entry) {
122 if ($entry->hasAttribute('src')) {
123 $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
124
125 $extension = $entry->tagName == 'source' ? '.mp4' : '.png';
126 $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension;
127
128 if (file_exists($local_filename)) {
129 $entry->setAttribute("src", get_self_url_prefix() .
130 "/public.php?op=cache_starred_images_getimage&method=image&hash=" .
131 $article_id . "-" . sha1($src) . $extension);
132 }
133
134 }
135 }
136 }
137
138 return $doc;
139 }
140
141 function hook_update_task() {
142 $result = db_query("SELECT content, ttrss_user_entries.owner_uid, link, site_url, ttrss_entries.id, plugin_data
143 FROM ttrss_entries, ttrss_user_entries LEFT JOIN ttrss_feeds ON
144 (ttrss_user_entries.feed_id = ttrss_feeds.id)
145 WHERE ref_id = ttrss_entries.id AND
146 marked = true AND
147 (UPPER(content) LIKE '%<IMG%' OR UPPER(content) LIKE '%<VIDEO%') AND
148 site_url != '' AND
149 plugin_data NOT LIKE '%starred_cache_images%'
150 ORDER BY ".sql_random_function()." LIMIT 100");
151
152 while ($line = db_fetch_assoc($result)) {
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) {
157 $plugin_data = db_escape_string("starred_cache_images,${line['owner_uid']}:" . $line["plugin_data"]);
158
159 db_query("UPDATE ttrss_entries SET plugin_data = '$plugin_data' WHERE id = " . $line["id"]);
160 }
161 }
162 }
163 }
164
165 function cache_article_images($content, $site_url, $owner_uid, $article_id) {
166 libxml_use_internal_errors(true);
167
168 $charset_hack = '<head>
169 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
170 </head>';
171
172 $doc = new DOMDocument();
173 $doc->loadHTML($charset_hack . $content);
174 $xpath = new DOMXPath($doc);
175
176 $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
177
178 $success = false;
179 $has_images = false;
180
181 foreach ($entries as $entry) {
182
183 if ($entry->hasAttribute('src')) {
184 $has_images = true;
185 $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
186
187 $extension = $entry->tagName == 'source' ? '.mp4' : '.png';
188
189 $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension;
190
191 //_debug("cache_images: downloading: $src to $local_filename");
192
193 if (!file_exists($local_filename)) {
194 $file_content = fetch_file_contents($src);
195
196 if ($file_content && strlen($file_content) > 0) {
197 file_put_contents($local_filename, $file_content);
198 $success = true;
199 }
200 } else {
201 $success = true;
202 }
203 }
204 }
205
206 return $success || !$has_images;
207 }
208
209 function api_version() {
210 return 2;
211 }
212 }
213 ?>