]> git.wh0rd.org - tt-rss.git/blob - plugins/cache_starred_images/init.php
make cache starred plugin use hook_house_keeping
[tt-rss.git] / plugins / cache_starred_images / init.php
1 <?php
2 class Cache_Starred_Images extends Plugin {
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 init($host) {
15 $this->host = $host;
16
17 $this->cache_dir = CACHE_DIR . "/starred-images/";
18
19 if (!is_dir($this->cache_dir)) {
20 mkdir($this->cache_dir);
21 }
22
23 if (is_dir($this->cache_dir)) {
24 chmod($this->cache_dir, 0777);
25
26 if (is_writable($this->cache_dir)) {
27 $host->add_hook($host::HOOK_UPDATE_TASK, $this);
28 $host->add_hook($host::HOOK_HOUSE_KEEPING, $this);
29 $host->add_hook($host::HOOK_SANITIZE, $this);
30 } else {
31 user_error("Starred cache directory is not writable.", E_USER_WARNING);
32 }
33
34 } else {
35 user_error("Unable to create starred cache directory.", E_USER_WARNING);
36 }
37 }
38
39 function image() {
40 $hash = basename($_REQUEST["hash"]);
41
42 if ($hash) {
43
44 $filename = $this->cache_dir . "/" . $hash . '.png';
45
46 if (file_exists($filename)) {
47 /* See if we can use X-Sendfile */
48 $xsendfile = false;
49 if (function_exists('apache_get_modules') &&
50 array_search('mod_xsendfile', apache_get_modules()))
51 $xsendfile = true;
52
53 if ($xsendfile) {
54 header("X-Sendfile: $filename");
55 header("Content-type: application/octet-stream");
56 header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
57 } else {
58 header("Content-type: image/png");
59 $stamp = gmdate("D, d M Y H:i:s", filemtime($filename)). " GMT";
60 header("Last-Modified: $stamp", true);
61 ob_clean(); // discard any data in the output buffer (if possible)
62 flush(); // flush headers (if possible)
63 readfile($filename);
64 }
65 } else {
66 header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
67 echo "File not found.";
68 }
69 }
70 }
71
72 function hook_house_keeping() {
73 $files = glob($this->cache_dir . "/*.png");
74
75 $last_article_id = 0;
76 $article_exists = 1;
77
78 foreach ($files as $file) {
79 list ($article_id, $hash) = explode("-", basename($file));
80
81 if ($article_id != $last_article_id) {
82 $last_article_id = $article_id;
83 $article_id = db_escape_string($article_id);
84
85 $result = db_query("SELECT id FROM ttrss_entries WHERE id = " . $article_id);
86
87 $article_exists = db_num_rows($result) > 0;
88 }
89
90 if (!$article_exists) {
91 unlink($file);
92 }
93 }
94 }
95
96 function hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id) {
97 $xpath = new DOMXpath($doc);
98
99 if ($article_id) {
100 $entries = $xpath->query('(//img[@src])');
101
102 foreach ($entries as $entry) {
103 if ($entry->hasAttribute('src')) {
104 $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
105
106 $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . ".png";
107
108 if (file_exists($local_filename)) {
109 $entry->setAttribute("src", get_self_url_prefix() .
110 "/backend.php?op=pluginhandler&plugin=cache_starred_images&method=image&hash=" .
111 $article_id . "-" . sha1($src));
112 }
113
114 }
115 }
116 }
117
118 return $doc;
119 }
120
121 function hook_update_task() {
122 $result = db_query("SELECT content, ttrss_user_entries.owner_uid, link, site_url, ttrss_entries.id, plugin_data
123 FROM ttrss_entries, ttrss_user_entries LEFT JOIN ttrss_feeds ON
124 (ttrss_user_entries.feed_id = ttrss_feeds.id)
125 WHERE ref_id = ttrss_entries.id AND
126 marked = true AND
127 UPPER(content) LIKE '%<IMG%' AND
128 site_url != '' AND
129 plugin_data NOT LIKE '%starred_cache_images%'
130 ORDER BY ".sql_random_function()." LIMIT 100");
131
132
133 while ($line = db_fetch_assoc($result)) {
134 if ($line["site_url"]) {
135 $success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]);
136
137 if ($success) {
138 $plugin_data = db_escape_string("starred_cache_images,${line['owner_uid']}:" . $line["plugin_data"]);
139
140 db_query("UPDATE ttrss_entries SET plugin_data = '$plugin_data' WHERE id = " . $line["id"]);
141 }
142 }
143 }
144 }
145
146 function cache_article_images($content, $site_url, $owner_uid, $article_id) {
147 libxml_use_internal_errors(true);
148
149 $charset_hack = '<head>
150 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
151 </head>';
152
153 $doc = new DOMDocument();
154 $doc->loadHTML($charset_hack . $content);
155 $xpath = new DOMXPath($doc);
156
157 $entries = $xpath->query('(//img[@src])');
158
159 $success = false;
160 $has_images = false;
161
162 foreach ($entries as $entry) {
163 if ($entry->hasAttribute('src')) {
164 $has_images = true;
165 $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
166
167 $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . ".png";
168
169 //_debug("cache_images: downloading: $src to $local_filename");
170
171 if (!file_exists($local_filename)) {
172 $file_content = fetch_file_contents($src);
173
174 if ($file_content && strlen($file_content) > 0) {
175 file_put_contents($local_filename, $file_content);
176 $success = true;
177 }
178 } else {
179 $success = true;
180 }
181 }
182 }
183
184 return $success || !$has_images;
185 }
186
187 function api_version() {
188 return 2;
189 }
190 }
191 ?>