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