]> git.wh0rd.org - tt-rss.git/blame - plugins/cache_starred_images/init.php
plugins/embed_original: use PDO
[tt-rss.git] / plugins / cache_starred_images / init.php
CommitLineData
910592b4 1<?php
95ebe737 2class Cache_Starred_Images extends Plugin implements IHandler {
910592b4
AD
3
4 private $host;
5 private $cache_dir;
6
7 function about() {
8 return array(1.0,
88bf000f 9 "Automatically cache Starred articles' images and HTML5 video files",
910592b4
AD
10 "fox",
11 true);
12 }
13
21ce7d9e
AD
14 /**
15 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
16 */
95ebe737
AD
17 function csrf_ignore($method) {
18 return false;
19 }
20
21ce7d9e
AD
21 /**
22 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
23 */
95ebe737
AD
24 function before($method) {
25 return true;
26 }
27
28 function after() {
29 return true;
30 }
31
910592b4
AD
32 function init($host) {
33 $this->host = $host;
34
35 $this->cache_dir = CACHE_DIR . "/starred-images/";
36
37 if (!is_dir($this->cache_dir)) {
38 mkdir($this->cache_dir);
39 }
40
41 if (is_dir($this->cache_dir)) {
f5967cf8
AD
42
43 if (!is_writable($this->cache_dir))
44 chmod($this->cache_dir, 0777);
910592b4
AD
45
46 if (is_writable($this->cache_dir)) {
47 $host->add_hook($host::HOOK_UPDATE_TASK, $this);
4e5ddeaf 48 $host->add_hook($host::HOOK_HOUSE_KEEPING, $this);
910592b4 49 $host->add_hook($host::HOOK_SANITIZE, $this);
95ebe737
AD
50 $host->add_handler("public", "cache_starred_images_getimage", $this);
51
910592b4
AD
52 } else {
53 user_error("Starred cache directory is not writable.", E_USER_WARNING);
54 }
55
56 } else {
57 user_error("Unable to create starred cache directory.", E_USER_WARNING);
58 }
59 }
60
95ebe737 61 function cache_starred_images_getimage() {
ae3851b1
AD
62 ob_end_clean();
63
910592b4
AD
64 $hash = basename($_REQUEST["hash"]);
65
66 if ($hash) {
67
c203486e 68 $filename = $this->cache_dir . "/" . basename($hash);
910592b4
AD
69
70 if (file_exists($filename)) {
c203486e
AD
71 header("Content-Disposition: attachment; filename=\"$hash\"");
72
8b73bd28 73 send_local_file($filename);
910592b4
AD
74 } else {
75 header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
76 echo "File not found.";
77 }
78 }
79 }
80
21ce7d9e
AD
81 /**
82 * @SuppressWarnings(PHPMD.UnusedLocalVariable)
83 */
4e5ddeaf 84 function hook_house_keeping() {
88bf000f 85 $files = glob($this->cache_dir . "/*.{png,mp4}", GLOB_BRACE);
4e5ddeaf
AD
86
87 $last_article_id = 0;
88 $article_exists = 1;
89
90 foreach ($files as $file) {
91 list ($article_id, $hash) = explode("-", basename($file));
92
93 if ($article_id != $last_article_id) {
94 $last_article_id = $article_id;
95 $article_id = db_escape_string($article_id);
96
97 $result = db_query("SELECT id FROM ttrss_entries WHERE id = " . $article_id);
98
99 $article_exists = db_num_rows($result) > 0;
100 }
101
102 if (!$article_exists) {
103 unlink($file);
104 }
105 }
106 }
107
21ce7d9e
AD
108 /**
109 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
110 */
910592b4
AD
111 function hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id) {
112 $xpath = new DOMXpath($doc);
113
114 if ($article_id) {
88bf000f 115 $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
910592b4
AD
116
117 foreach ($entries as $entry) {
118 if ($entry->hasAttribute('src')) {
119 $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
120
88bf000f
AD
121 $extension = $entry->tagName == 'source' ? '.mp4' : '.png';
122 $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension;
910592b4
AD
123
124 if (file_exists($local_filename)) {
125 $entry->setAttribute("src", get_self_url_prefix() .
95ebe737 126 "/public.php?op=cache_starred_images_getimage&method=image&hash=" .
88bf000f 127 $article_id . "-" . sha1($src) . $extension);
910592b4
AD
128 }
129
130 }
131 }
132 }
133
134 return $doc;
135 }
136
137 function hook_update_task() {
910592b4
AD
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
88bf000f 143 (UPPER(content) LIKE '%<IMG%' OR UPPER(content) LIKE '%<VIDEO%') AND
910592b4
AD
144 site_url != '' AND
145 plugin_data NOT LIKE '%starred_cache_images%'
146 ORDER BY ".sql_random_function()." LIMIT 100");
147
910592b4
AD
148 while ($line = db_fetch_assoc($result)) {
149 if ($line["site_url"]) {
150 $success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]);
151
152 if ($success) {
153 $plugin_data = db_escape_string("starred_cache_images,${line['owner_uid']}:" . $line["plugin_data"]);
154
155 db_query("UPDATE ttrss_entries SET plugin_data = '$plugin_data' WHERE id = " . $line["id"]);
156 }
157 }
158 }
159 }
160
21ce7d9e
AD
161 /**
162 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
163 */
910592b4
AD
164 function cache_article_images($content, $site_url, $owner_uid, $article_id) {
165 libxml_use_internal_errors(true);
166
167 $charset_hack = '<head>
168 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
169 </head>';
170
171 $doc = new DOMDocument();
172 $doc->loadHTML($charset_hack . $content);
173 $xpath = new DOMXPath($doc);
174
88bf000f 175 $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
910592b4
AD
176
177 $success = false;
178 $has_images = false;
179
180 foreach ($entries as $entry) {
88bf000f 181
5edd605a
AD
182 if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) {
183
910592b4
AD
184 $has_images = true;
185 $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
186
88bf000f
AD
187 $extension = $entry->tagName == 'source' ? '.mp4' : '.png';
188
189 $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension;
910592b4
AD
190
191 //_debug("cache_images: downloading: $src to $local_filename");
192
193 if (!file_exists($local_filename)) {
194 $file_content = fetch_file_contents($src);
195
6fd03996 196 if ($file_content && strlen($file_content) > MIN_CACHE_FILE_SIZE) {
910592b4
AD
197 file_put_contents($local_filename, $file_content);
198 $success = true;
199 }
200 } else {
201 $success = true;
202 }
203 }
204 }
205
206 return $success || !$has_images;
207 }
208
209 function api_version() {
210 return 2;
211 }
212}