summaryrefslogtreecommitdiff
path: root/plugins/cache_starred_images/init.php
blob: 36e8b73f0067e59c09793aa5ffa5eed102557084 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
class Cache_Starred_Images extends Plugin {

	/* @var PluginHost $host */
	private $host;
	/* @var DiskCache $cache */
	private $cache;
	private $max_cache_attempts = 5; // per-article

	function about() {
		return array(null,
			"Automatically cache media files in Starred articles",
			"fox");
	}

	function init($host) {
		$this->host = $host;
		$this->cache = new DiskCache("starred-images");

		if ($this->cache->make_dir())
			chmod($this->cache->get_dir(), 0777);

		if (!$this->cache->exists(".no-auto-expiry"))
			$this->cache->touch(".no-auto-expiry");

		if ($this->cache->is_writable()) {
			$host->add_hook($host::HOOK_HOUSE_KEEPING, $this);
			$host->add_hook($host::HOOK_ENCLOSURE_ENTRY, $this);
			$host->add_hook($host::HOOK_SANITIZE, $this);
		} else {
			user_error("Starred cache directory ".$this->cache->get_dir()." is not writable.", E_USER_WARNING);
		}
	}

	function hook_house_keeping() {
		/* since HOOK_UPDATE_TASK is not available to user plugins, this hook is a next best thing */

		Debug::log("caching media of starred articles for user " . $this->host->get_owner_uid() . "...");

		$sth = $this->pdo->prepare("SELECT content, ttrss_entries.title,
				ttrss_user_entries.owner_uid, link, site_url, ttrss_entries.id, plugin_data
			FROM ttrss_entries, ttrss_user_entries LEFT JOIN ttrss_feeds ON
				(ttrss_user_entries.feed_id = ttrss_feeds.id)
			WHERE ref_id = ttrss_entries.id AND
				marked = true AND
				site_url != '' AND
				ttrss_user_entries.owner_uid = ? AND
				plugin_data NOT LIKE '%starred_cache_images%'
			ORDER BY ".Db::sql_random_function()." LIMIT 100");

		if ($sth->execute([$this->host->get_owner_uid()])) {

			$usth = $this->pdo->prepare("UPDATE ttrss_entries SET plugin_data = ? WHERE id = ?");

			while ($line = $sth->fetch()) {
				Debug::log("processing article " . $line["title"], Debug::$LOG_VERBOSE);

				if ($line["site_url"]) {
					$success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]);

					if ($success) {
						$plugin_data = "starred_cache_images," . $line["owner_uid"] . ":" . $line["plugin_data"];

						$usth->execute([$plugin_data, $line['id']]);
					}
				}
			}
		}

		/* actual housekeeping */

		Debug::log("expiring " . $this->cache->get_dir() . "...");

		$files = array_merge(
				glob($this->cache->get_dir() . "/*.png"),
				glob($this->cache->get_dir() . "/*.mp4"),
				glob($this->cache->get_dir() . "/*.status"));

		$last_article_id = 0;
		$article_exists = 1;

		foreach ($files as $file) {
			list ($article_id, $hash) = explode("-", basename($file));

			if ($article_id != $last_article_id) {
				$last_article_id = $article_id;

				$sth = $this->pdo->prepare("SELECT id FROM ttrss_entries WHERE id = ?");
				$sth->execute([$article_id]);

				$article_exists = $sth->fetch();
			}

			if (!$article_exists) {
				unlink($file);
			}
		}
	}

	function hook_enclosure_entry($enc, $article_id, $rv) {
		$local_filename = $article_id . "-" . sha1($enc["content_url"]);

		if ($this->cache->exists($local_filename)) {
			$enc["content_url"] = $this->cache->get_url($local_filename);
		}

		return $enc;
	}

	function hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id) {
		$xpath = new DOMXPath($doc);

		if ($article_id) {
			$entries = $xpath->query('(//img[@src])|(//video/source[@src])');

			foreach ($entries as $entry) {
				if ($entry->hasAttribute('src')) {
					$src = rewrite_relative_url($site_url, $entry->getAttribute('src'));

					$local_filename = $article_id . "-" . sha1($src);

					if ($this->cache->exists($local_filename)) {
						$entry->setAttribute("src", $this->cache->get_url($local_filename));
						$entry->removeAttribute("srcset");
					}
				}
			}
		}

		return $doc;
	}

	private function cache_url($article_id, $url) {
		$local_filename = $article_id . "-" . sha1($url);

		if (!$this->cache->exists($local_filename)) {
			Debug::log("cache_images: downloading: $url to $local_filename", Debug::$LOG_VERBOSE);

			$data = UrlHelper::fetch(["url" => $url, "max_size" => Config::get(Config::MAX_CACHE_FILE_SIZE)]);

			if ($data)
				return $this->cache->put($local_filename, $data);;

		} else {
			//Debug::log("cache_images: local file exists for $url", Debug::$LOG_VERBOSE);

			return true;
		}

		return false;
	}

	private function cache_article_images($content, $site_url, $owner_uid, $article_id) {
		$status_filename = $article_id . "-" . sha1($site_url) . ".status";

		/* housekeeping might run as a separate user, in this case status/media might not be writable */
		if (!$this->cache->is_writable($status_filename)) {
			Debug::log("status not writable: $status_filename", Debug::$LOG_VERBOSE);
			return false;
		}

		Debug::log("status: $status_filename", Debug::$LOG_VERBOSE);

		if ($this->cache->exists($status_filename))
			$status = json_decode($this->cache->get($status_filename), true);
		else
			$status = ["attempt" => 0];

		$status["attempt"] += 1;

		// only allow several download attempts for article
		if ($status["attempt"] > $this->max_cache_attempts) {
			Debug::log("too many attempts for $site_url", Debug::$LOG_VERBOSE);
			return false;
		}

		if (!$this->cache->put($status_filename, json_encode($status))) {
			user_error("unable to write status file: $status_filename", E_USER_WARNING);
			return false;
		}

		$doc = new DOMDocument();

		$has_images = false;
		$success = false;

		if (@$doc->loadHTML('<?xml encoding="UTF-8">' . $content)) {
			$xpath = new DOMXPath($doc);
			$entries = $xpath->query('(//img[@src])|(//video/source[@src])');

			foreach ($entries as $entry) {

				if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) {

					$has_images = true;

					$src = rewrite_relative_url($site_url, $entry->getAttribute('src'));

					if ($this->cache_url($article_id, $src)) {
						$success = true;
					}
				}
			}
		}

		$esth = $this->pdo->prepare("SELECT content_url FROM ttrss_enclosures WHERE post_id = ? AND
			(content_type LIKE '%image%' OR content_type LIKE '%video%')");

		if ($esth->execute([$article_id])) {
			while ($enc = $esth->fetch()) {

				$has_images = true;
				$url = rewrite_relative_url($site_url, $enc["content_url"]);

				if ($this->cache_url($article_id, $url)) {
					$success = true;
				}
			}
		}

		return $success || !$has_images;
	}

	function api_version() {
		return 2;
	}
}