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