2013-08-02 10:04:14 +00:00
|
|
|
<?php
|
2019-08-13 13:40:21 +00:00
|
|
|
class Cache_Starred_Images extends Plugin {
|
2013-08-02 10:04:14 +00:00
|
|
|
|
2021-11-14 17:59:49 +00:00
|
|
|
/** @var PluginHost $host */
|
2013-08-02 10:04:14 +00:00
|
|
|
private $host;
|
2021-11-14 17:59:49 +00:00
|
|
|
|
|
|
|
/** @var DiskCache $cache */
|
2019-08-13 13:40:21 +00:00
|
|
|
private $cache;
|
2021-11-14 17:59:49 +00:00
|
|
|
|
2021-11-18 16:54:42 +00:00
|
|
|
/** @var DiskCache $cache_status */
|
|
|
|
private $cache_status;
|
|
|
|
|
2021-11-14 17:59:49 +00:00
|
|
|
/** @var int $max_cache_attempts (per article) */
|
|
|
|
private $max_cache_attempts = 5;
|
2013-08-02 10:04:14 +00:00
|
|
|
|
|
|
|
function about() {
|
2021-03-01 09:11:42 +00:00
|
|
|
return array(null,
|
2019-08-13 13:40:21 +00:00
|
|
|
"Automatically cache media files in Starred articles",
|
|
|
|
"fox");
|
2015-02-25 18:16:14 +00:00
|
|
|
}
|
|
|
|
|
2013-08-02 10:04:14 +00:00
|
|
|
function init($host) {
|
|
|
|
$this->host = $host;
|
2019-08-13 13:40:21 +00:00
|
|
|
$this->cache = new DiskCache("starred-images");
|
2021-11-18 16:54:42 +00:00
|
|
|
$this->cache_status = new DiskCache("starred-images.status-files");
|
2013-08-02 10:04:14 +00:00
|
|
|
|
2021-02-15 13:11:30 +00:00
|
|
|
if ($this->cache->make_dir())
|
|
|
|
chmod($this->cache->get_dir(), 0777);
|
2013-08-02 10:04:14 +00:00
|
|
|
|
2021-11-18 16:54:42 +00:00
|
|
|
if ($this->cache_status->make_dir())
|
|
|
|
chmod($this->cache_status->get_dir(), 0777);
|
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
if (!$this->cache->exists(".no-auto-expiry"))
|
|
|
|
$this->cache->touch(".no-auto-expiry");
|
2013-08-02 10:04:14 +00:00
|
|
|
|
2021-11-18 16:54:42 +00:00
|
|
|
if (!$this->cache_status->exists(".no-auto-expiry"))
|
|
|
|
$this->cache_status->touch(".no-auto-expiry");
|
|
|
|
|
|
|
|
if ($this->cache->is_writable() && $this->cache_status->is_writable()) {
|
2019-08-13 13:40:21 +00:00
|
|
|
$host->add_hook($host::HOOK_HOUSE_KEEPING, $this);
|
|
|
|
$host->add_hook($host::HOOK_ENCLOSURE_ENTRY, $this);
|
|
|
|
$host->add_hook($host::HOOK_SANITIZE, $this);
|
2013-08-02 10:04:14 +00:00
|
|
|
} else {
|
2021-11-18 16:54:42 +00:00
|
|
|
user_error("Starred cache directory ".$this->cache->get_dir()." (or status cache subdir in status-files/) is not writable.", E_USER_WARNING);
|
2013-08-02 10:04:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-14 17:59:49 +00:00
|
|
|
/** since HOOK_UPDATE_TASK is not available to user plugins, this hook is a next best thing */
|
2019-08-13 13:40:21 +00:00
|
|
|
function hook_house_keeping() {
|
2013-10-05 16:28:26 +00:00
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
Debug::log("caching media of starred articles for user " . $this->host->get_owner_uid() . "...");
|
2013-08-02 10:04:14 +00:00
|
|
|
|
2020-09-22 06:04:33 +00:00
|
|
|
$sth = $this->pdo->prepare("SELECT content, ttrss_entries.title,
|
2021-02-17 13:17:05 +00:00
|
|
|
ttrss_user_entries.owner_uid, link, site_url, ttrss_entries.id, plugin_data
|
2019-08-13 13:40:21 +00:00
|
|
|
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
|
2020-09-22 06:04:33 +00:00
|
|
|
site_url != '' AND
|
2021-02-17 13:17:05 +00:00
|
|
|
ttrss_user_entries.owner_uid = ? AND
|
2019-08-13 13:40:21 +00:00
|
|
|
plugin_data NOT LIKE '%starred_cache_images%'
|
2020-09-22 06:04:33 +00:00
|
|
|
ORDER BY ".Db::sql_random_function()." LIMIT 100");
|
2013-08-02 10:04:14 +00:00
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
if ($sth->execute([$this->host->get_owner_uid()])) {
|
2013-08-02 10:04:14 +00:00
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
$usth = $this->pdo->prepare("UPDATE ttrss_entries SET plugin_data = ? WHERE id = ?");
|
2016-01-06 18:00:53 +00:00
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
while ($line = $sth->fetch()) {
|
2021-11-14 17:59:49 +00:00
|
|
|
Debug::log("processing article " . $line["title"], Debug::LOG_VERBOSE);
|
2019-08-13 13:40:21 +00:00
|
|
|
|
|
|
|
if ($line["site_url"]) {
|
|
|
|
$success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]);
|
|
|
|
|
|
|
|
if ($success) {
|
2021-02-17 13:17:05 +00:00
|
|
|
$plugin_data = "starred_cache_images," . $line["owner_uid"] . ":" . $line["plugin_data"];
|
2019-08-13 13:40:21 +00:00
|
|
|
|
|
|
|
$usth->execute([$plugin_data, $line['id']]);
|
|
|
|
}
|
|
|
|
}
|
2013-08-02 10:04:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
/* actual housekeeping */
|
|
|
|
|
2021-11-18 16:54:42 +00:00
|
|
|
Debug::log("expiring {$this->cache->get_dir()} and {$this->cache_status->get_dir()}...");
|
2019-08-13 13:40:21 +00:00
|
|
|
|
2021-02-17 13:17:05 +00:00
|
|
|
$files = array_merge(
|
2022-01-16 22:44:46 +00:00
|
|
|
glob($this->cache->get_dir() . "/*-*"),
|
|
|
|
glob($this->cache_status->get_dir() . "/*.status")
|
|
|
|
);
|
2013-08-02 10:12:56 +00:00
|
|
|
|
2021-11-18 16:54:42 +00:00
|
|
|
asort($files);
|
|
|
|
|
2013-08-02 10:12:56 +00:00
|
|
|
$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;
|
|
|
|
|
2017-12-03 08:03:09 +00:00
|
|
|
$sth = $this->pdo->prepare("SELECT id FROM ttrss_entries WHERE id = ?");
|
|
|
|
$sth->execute([$article_id]);
|
2013-08-02 10:12:56 +00:00
|
|
|
|
2017-12-03 08:03:09 +00:00
|
|
|
$article_exists = $sth->fetch();
|
2013-08-02 10:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$article_exists) {
|
|
|
|
unlink($file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-14 08:11:49 +00:00
|
|
|
function hook_enclosure_entry($enc, $article_id, $rv) {
|
2019-08-13 13:40:21 +00:00
|
|
|
$local_filename = $article_id . "-" . sha1($enc["content_url"]);
|
|
|
|
|
|
|
|
if ($this->cache->exists($local_filename)) {
|
2021-02-15 13:11:30 +00:00
|
|
|
$enc["content_url"] = $this->cache->get_url($local_filename);
|
2019-08-13 13:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $enc;
|
|
|
|
}
|
|
|
|
|
2013-08-02 10:04:14 +00:00
|
|
|
function hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id) {
|
2021-02-17 13:17:05 +00:00
|
|
|
$xpath = new DOMXPath($doc);
|
2013-08-02 10:04:14 +00:00
|
|
|
|
|
|
|
if ($article_id) {
|
2015-07-07 10:07:58 +00:00
|
|
|
$entries = $xpath->query('(//img[@src])|(//video/source[@src])');
|
2013-08-02 10:04:14 +00:00
|
|
|
|
|
|
|
foreach ($entries as $entry) {
|
|
|
|
if ($entry->hasAttribute('src')) {
|
2021-11-14 17:59:49 +00:00
|
|
|
$src = UrlHelper::rewrite_relative($site_url, $entry->getAttribute('src'));
|
2013-08-02 10:04:14 +00:00
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
$local_filename = $article_id . "-" . sha1($src);
|
2013-08-02 10:04:14 +00:00
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
if ($this->cache->exists($local_filename)) {
|
2021-02-15 13:11:30 +00:00
|
|
|
$entry->setAttribute("src", $this->cache->get_url($local_filename));
|
2019-08-13 13:40:21 +00:00
|
|
|
$entry->removeAttribute("srcset");
|
2013-08-02 10:04:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $doc;
|
|
|
|
}
|
|
|
|
|
2021-11-14 17:59:49 +00:00
|
|
|
private function cache_url(int $article_id, string $url) : bool {
|
2019-08-13 13:40:21 +00:00
|
|
|
$local_filename = $article_id . "-" . sha1($url);
|
2013-08-02 10:04:14 +00:00
|
|
|
|
2019-08-14 09:52:41 +00:00
|
|
|
if (!$this->cache->exists($local_filename)) {
|
2021-11-14 17:59:49 +00:00
|
|
|
Debug::log("cache_images: downloading: $url to $local_filename", Debug::LOG_VERBOSE);
|
2017-12-03 08:03:09 +00:00
|
|
|
|
2021-02-22 19:35:27 +00:00
|
|
|
$data = UrlHelper::fetch(["url" => $url, "max_size" => Config::get(Config::MAX_CACHE_FILE_SIZE)]);
|
2013-08-02 10:04:14 +00:00
|
|
|
|
2019-08-14 09:44:50 +00:00
|
|
|
if ($data)
|
|
|
|
return $this->cache->put($local_filename, $data);;
|
2019-08-13 13:40:21 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
//Debug::log("cache_images: local file exists for $url", Debug::$LOG_VERBOSE);
|
|
|
|
|
|
|
|
return true;
|
2013-08-02 10:04:14 +00:00
|
|
|
}
|
2019-08-13 13:40:21 +00:00
|
|
|
|
|
|
|
return false;
|
2013-08-02 10:04:14 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 17:59:49 +00:00
|
|
|
private function cache_article_images(string $content, string $site_url, int $owner_uid, int $article_id) : bool {
|
2019-08-13 13:40:21 +00:00
|
|
|
$status_filename = $article_id . "-" . sha1($site_url) . ".status";
|
2018-11-30 04:20:13 +00:00
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
/* housekeeping might run as a separate user, in this case status/media might not be writable */
|
2021-11-18 16:54:42 +00:00
|
|
|
if (!$this->cache_status->is_writable($status_filename)) {
|
2021-11-14 17:59:49 +00:00
|
|
|
Debug::log("status not writable: $status_filename", Debug::LOG_VERBOSE);
|
2019-08-13 13:40:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-11-30 04:20:13 +00:00
|
|
|
|
2021-11-14 17:59:49 +00:00
|
|
|
Debug::log("status: $status_filename", Debug::LOG_VERBOSE);
|
2019-08-13 13:40:21 +00:00
|
|
|
|
2021-11-18 16:54:42 +00:00
|
|
|
if ($this->cache_status->exists($status_filename))
|
|
|
|
$status = json_decode($this->cache_status->get($status_filename), true);
|
2021-02-17 13:17:05 +00:00
|
|
|
else
|
2021-02-17 18:54:43 +00:00
|
|
|
$status = ["attempt" => 0];
|
2018-11-30 04:20:13 +00:00
|
|
|
|
2021-02-17 13:17:05 +00:00
|
|
|
$status["attempt"] += 1;
|
2018-11-30 04:20:13 +00:00
|
|
|
|
2021-02-17 13:17:05 +00:00
|
|
|
// only allow several download attempts for article
|
|
|
|
if ($status["attempt"] > $this->max_cache_attempts) {
|
2021-11-14 17:59:49 +00:00
|
|
|
Debug::log("too many attempts for $site_url", Debug::LOG_VERBOSE);
|
2021-02-17 13:17:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-11-30 04:20:13 +00:00
|
|
|
|
2021-11-18 16:54:42 +00:00
|
|
|
if (!$this->cache_status->put($status_filename, json_encode($status))) {
|
2021-02-17 13:17:05 +00:00
|
|
|
user_error("unable to write status file: $status_filename", E_USER_WARNING);
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-30 04:20:13 +00:00
|
|
|
|
2013-08-02 10:04:14 +00:00
|
|
|
$doc = new DOMDocument();
|
|
|
|
|
|
|
|
$has_images = false;
|
2019-08-13 13:40:21 +00:00
|
|
|
$success = false;
|
|
|
|
|
2021-02-17 13:17:05 +00:00
|
|
|
if (@$doc->loadHTML('<?xml encoding="UTF-8">' . $content)) {
|
2019-08-13 13:40:21 +00:00
|
|
|
$xpath = new DOMXPath($doc);
|
|
|
|
$entries = $xpath->query('(//img[@src])|(//video/source[@src])');
|
2013-08-02 10:04:14 +00:00
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
foreach ($entries as $entry) {
|
2015-07-07 10:07:58 +00:00
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) {
|
2017-02-04 08:50:01 +00:00
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
$has_images = true;
|
2013-08-02 10:04:14 +00:00
|
|
|
|
2021-11-14 17:59:49 +00:00
|
|
|
$src = UrlHelper::rewrite_relative($site_url, $entry->getAttribute('src'));
|
2015-07-07 10:07:58 +00:00
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
if ($this->cache_url($article_id, $src)) {
|
|
|
|
$success = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-02 10:04:14 +00:00
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
$esth = $this->pdo->prepare("SELECT content_url FROM ttrss_enclosures WHERE post_id = ? AND
|
|
|
|
(content_type LIKE '%image%' OR content_type LIKE '%video%')");
|
2013-08-02 10:04:14 +00:00
|
|
|
|
2021-02-17 13:17:05 +00:00
|
|
|
if ($esth->execute([$article_id])) {
|
|
|
|
while ($enc = $esth->fetch()) {
|
2013-08-02 10:04:14 +00:00
|
|
|
|
2021-02-17 13:17:05 +00:00
|
|
|
$has_images = true;
|
2021-11-14 17:59:49 +00:00
|
|
|
$url = UrlHelper::rewrite_relative($site_url, $enc["content_url"]);
|
2018-11-30 04:20:13 +00:00
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
if ($this->cache_url($article_id, $url)) {
|
2013-08-02 10:04:14 +00:00
|
|
|
$success = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $success || !$has_images;
|
|
|
|
}
|
|
|
|
|
|
|
|
function api_version() {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|