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
|
|
|
|
2017-12-03 08:03:09 +00:00
|
|
|
/* @var PluginHost $host */
|
2013-08-02 10:04:14 +00:00
|
|
|
private $host;
|
2019-08-13 13:40:21 +00:00
|
|
|
/* @var DiskCache $cache */
|
|
|
|
private $cache;
|
2018-11-30 04:20:13 +00:00
|
|
|
private $max_cache_attempts = 5; // per-article
|
2013-08-02 10:04:14 +00:00
|
|
|
|
|
|
|
function about() {
|
|
|
|
return array(1.0,
|
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");
|
2013-08-02 10:04:14 +00:00
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
if ($this->cache->makeDir())
|
|
|
|
chmod($this->cache->getDir(), 0777);
|
2013-08-02 10:04:14 +00:00
|
|
|
|
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
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
if ($this->cache->isWritable()) {
|
|
|
|
$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 {
|
2019-08-13 13:40:21 +00:00
|
|
|
user_error("Starred cache directory ".$this->cache->getDir()." is not writable.", E_USER_WARNING);
|
2013-08-02 10:04:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
/**
|
|
|
|
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
|
|
|
|
*/
|
|
|
|
function hook_house_keeping() {
|
|
|
|
/* since HOOK_UPDATE_TASK is not available to user plugins, this hook is a next best thing */
|
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
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
$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 ".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()) {
|
|
|
|
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']]);
|
|
|
|
}
|
|
|
|
}
|
2013-08-02 10:04:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
/* actual housekeeping */
|
|
|
|
|
|
|
|
Debug::log("expiring " . $this->cache->getDir() . "...");
|
|
|
|
|
|
|
|
$files = glob($this->cache->getDir() . "/*.{png,mp4,status}", GLOB_BRACE);
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
function hook_enclosure_entry($enc, $article_id) {
|
|
|
|
$local_filename = $article_id . "-" . sha1($enc["content_url"]);
|
|
|
|
|
|
|
|
if ($this->cache->exists($local_filename)) {
|
|
|
|
$enc["content_url"] = DiskCache::getUrl("starred-images/" . $local_filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $enc;
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:57:36 +00:00
|
|
|
/**
|
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
|
|
|
*/
|
2013-08-02 10:04:14 +00:00
|
|
|
function hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id) {
|
|
|
|
$xpath = new DOMXpath($doc);
|
|
|
|
|
|
|
|
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')) {
|
|
|
|
$src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
|
|
|
|
|
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)) {
|
|
|
|
$entry->setAttribute("src", DiskCache::getUrl("starred-images/" . $local_filename));
|
|
|
|
$entry->removeAttribute("srcset");
|
2013-08-02 10:04:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $doc;
|
|
|
|
}
|
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
private function cache_url($article_id, $url) {
|
|
|
|
$local_filename = $article_id . "-" . sha1($url);
|
2013-08-02 10:04:14 +00:00
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
if (!$this->cache->getSize($local_filename) >= 0) {
|
|
|
|
Debug::log("cache_images: downloading: $url to $local_filename", Debug::$LOG_VERBOSE);
|
2017-12-03 08:03:09 +00:00
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
$data = fetch_file_contents(["url" => $url, "max_size" => MAX_CACHE_FILE_SIZE]);
|
2013-08-02 10:04:14 +00:00
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
if ($data) {
|
|
|
|
if (strlen($data) > MIN_CACHE_FILE_SIZE) {
|
|
|
|
$this->cache->put($local_filename, $data);
|
2013-08-02 10:04:14 +00:00
|
|
|
}
|
2019-08-13 13:40:21 +00:00
|
|
|
|
|
|
|
return true;
|
2013-08-02 10:04:14 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2017-04-26 17:57:36 +00:00
|
|
|
/**
|
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
|
|
|
*/
|
2019-08-13 13:40:21 +00:00
|
|
|
private function cache_article_images($content, $site_url, $owner_uid, $article_id) {
|
|
|
|
$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 */
|
|
|
|
if (!$this->cache->isWritable($status_filename)) {
|
|
|
|
Debug::log("status not writable: $status_filename", Debug::$LOG_VERBOSE);
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-30 04:20:13 +00:00
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
Debug::log("status: $status_filename", Debug::$LOG_VERBOSE);
|
|
|
|
|
|
|
|
if ($this->cache->exists($status_filename))
|
|
|
|
$status = json_decode($this->cache->get($status_filename), true);
|
2018-11-30 04:20:13 +00:00
|
|
|
else
|
|
|
|
$status = [];
|
|
|
|
|
|
|
|
$status["attempt"] += 1;
|
|
|
|
|
|
|
|
// only allow several download attempts for article
|
|
|
|
if ($status["attempt"] > $this->max_cache_attempts) {
|
2018-11-30 05:34:29 +00:00
|
|
|
Debug::log("too many attempts for $site_url", Debug::$LOG_VERBOSE);
|
2019-08-13 13:40:21 +00:00
|
|
|
return false;
|
2018-11-30 04:20:13 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
if (!$this->cache->put($status_filename, json_encode($status))) {
|
2018-11-30 04:20:13 +00:00
|
|
|
user_error("unable to write status file: $status_filename", E_USER_WARNING);
|
2019-08-13 13:40:21 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
if ($doc->loadHTML('<?xml encoding="UTF-8">' . $content)) {
|
|
|
|
$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
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
$src = rewrite_relative_url($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
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
if ($esth->execute([$article_id])) {
|
|
|
|
while ($enc = $esth->fetch()) {
|
2013-08-02 10:04:14 +00:00
|
|
|
|
2019-08-13 13:40:21 +00:00
|
|
|
$has_images = true;
|
|
|
|
$url = rewrite_relative_url($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;
|
|
|
|
}
|
|
|
|
}
|