]>
Commit | Line | Data |
---|---|---|
910592b4 AD |
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)) { | |
f5967cf8 AD |
24 | |
25 | if (!is_writable($this->cache_dir)) | |
26 | chmod($this->cache_dir, 0777); | |
910592b4 AD |
27 | |
28 | if (is_writable($this->cache_dir)) { | |
29 | $host->add_hook($host::HOOK_UPDATE_TASK, $this); | |
4e5ddeaf | 30 | $host->add_hook($host::HOOK_HOUSE_KEEPING, $this); |
910592b4 AD |
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() { | |
ae3851b1 AD |
42 | ob_end_clean(); |
43 | ||
910592b4 AD |
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 | ob_clean(); // discard any data in the output buffer (if possible) | |
66 | flush(); // flush headers (if possible) | |
67 | readfile($filename); | |
68 | } | |
69 | } else { | |
70 | header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); | |
71 | echo "File not found."; | |
72 | } | |
73 | } | |
74 | } | |
75 | ||
4e5ddeaf AD |
76 | function hook_house_keeping() { |
77 | $files = glob($this->cache_dir . "/*.png"); | |
78 | ||
79 | $last_article_id = 0; | |
80 | $article_exists = 1; | |
81 | ||
82 | foreach ($files as $file) { | |
83 | list ($article_id, $hash) = explode("-", basename($file)); | |
84 | ||
85 | if ($article_id != $last_article_id) { | |
86 | $last_article_id = $article_id; | |
87 | $article_id = db_escape_string($article_id); | |
88 | ||
89 | $result = db_query("SELECT id FROM ttrss_entries WHERE id = " . $article_id); | |
90 | ||
91 | $article_exists = db_num_rows($result) > 0; | |
92 | } | |
93 | ||
94 | if (!$article_exists) { | |
95 | unlink($file); | |
96 | } | |
97 | } | |
98 | } | |
99 | ||
910592b4 AD |
100 | function hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id) { |
101 | $xpath = new DOMXpath($doc); | |
102 | ||
103 | if ($article_id) { | |
104 | $entries = $xpath->query('(//img[@src])'); | |
105 | ||
106 | foreach ($entries as $entry) { | |
107 | if ($entry->hasAttribute('src')) { | |
108 | $src = rewrite_relative_url($site_url, $entry->getAttribute('src')); | |
109 | ||
110 | $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . ".png"; | |
111 | ||
112 | if (file_exists($local_filename)) { | |
113 | $entry->setAttribute("src", get_self_url_prefix() . | |
114 | "/backend.php?op=pluginhandler&plugin=cache_starred_images&method=image&hash=" . | |
115 | $article_id . "-" . sha1($src)); | |
116 | } | |
117 | ||
118 | } | |
119 | } | |
120 | } | |
121 | ||
122 | return $doc; | |
123 | } | |
124 | ||
125 | function hook_update_task() { | |
910592b4 AD |
126 | $result = db_query("SELECT content, ttrss_user_entries.owner_uid, link, site_url, ttrss_entries.id, plugin_data |
127 | FROM ttrss_entries, ttrss_user_entries LEFT JOIN ttrss_feeds ON | |
128 | (ttrss_user_entries.feed_id = ttrss_feeds.id) | |
129 | WHERE ref_id = ttrss_entries.id AND | |
130 | marked = true AND | |
131 | UPPER(content) LIKE '%<IMG%' AND | |
132 | site_url != '' AND | |
133 | plugin_data NOT LIKE '%starred_cache_images%' | |
134 | ORDER BY ".sql_random_function()." LIMIT 100"); | |
135 | ||
136 | ||
137 | while ($line = db_fetch_assoc($result)) { | |
138 | if ($line["site_url"]) { | |
139 | $success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]); | |
140 | ||
141 | if ($success) { | |
142 | $plugin_data = db_escape_string("starred_cache_images,${line['owner_uid']}:" . $line["plugin_data"]); | |
143 | ||
144 | db_query("UPDATE ttrss_entries SET plugin_data = '$plugin_data' WHERE id = " . $line["id"]); | |
145 | } | |
146 | } | |
147 | } | |
148 | } | |
149 | ||
150 | function cache_article_images($content, $site_url, $owner_uid, $article_id) { | |
151 | libxml_use_internal_errors(true); | |
152 | ||
153 | $charset_hack = '<head> | |
154 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | |
155 | </head>'; | |
156 | ||
157 | $doc = new DOMDocument(); | |
158 | $doc->loadHTML($charset_hack . $content); | |
159 | $xpath = new DOMXPath($doc); | |
160 | ||
161 | $entries = $xpath->query('(//img[@src])'); | |
162 | ||
163 | $success = false; | |
164 | $has_images = false; | |
165 | ||
166 | foreach ($entries as $entry) { | |
167 | if ($entry->hasAttribute('src')) { | |
168 | $has_images = true; | |
169 | $src = rewrite_relative_url($site_url, $entry->getAttribute('src')); | |
170 | ||
171 | $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . ".png"; | |
172 | ||
173 | //_debug("cache_images: downloading: $src to $local_filename"); | |
174 | ||
175 | if (!file_exists($local_filename)) { | |
176 | $file_content = fetch_file_contents($src); | |
177 | ||
178 | if ($file_content && strlen($file_content) > 0) { | |
179 | file_put_contents($local_filename, $file_content); | |
180 | $success = true; | |
181 | } | |
182 | } else { | |
183 | $success = true; | |
184 | } | |
185 | } | |
186 | } | |
187 | ||
188 | return $success || !$has_images; | |
189 | } | |
190 | ||
191 | function api_version() { | |
192 | return 2; | |
193 | } | |
194 | } | |
195 | ?> |