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