2013-08-02 10:04:14 +00:00
|
|
|
<?php
|
2015-02-25 18:16:14 +00:00
|
|
|
class Cache_Starred_Images extends Plugin implements IHandler {
|
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;
|
|
|
|
private $cache_dir;
|
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,
|
2015-07-07 10:07:58 +00:00
|
|
|
"Automatically cache Starred articles' images and HTML5 video files",
|
2013-08-02 10:04:14 +00:00
|
|
|
"fox",
|
|
|
|
true);
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:57:36 +00:00
|
|
|
/**
|
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
|
|
|
*/
|
2015-02-25 18:16:14 +00:00
|
|
|
function csrf_ignore($method) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:57:36 +00:00
|
|
|
/**
|
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
|
|
|
*/
|
2015-02-25 18:16:14 +00:00
|
|
|
function before($method) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function after() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-02 10:04:14 +00:00
|
|
|
function init($host) {
|
|
|
|
$this->host = $host;
|
|
|
|
|
|
|
|
$this->cache_dir = CACHE_DIR . "/starred-images/";
|
|
|
|
|
|
|
|
if (!is_dir($this->cache_dir)) {
|
|
|
|
mkdir($this->cache_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_dir($this->cache_dir)) {
|
2013-09-06 05:21:17 +00:00
|
|
|
|
|
|
|
if (!is_writable($this->cache_dir))
|
|
|
|
chmod($this->cache_dir, 0777);
|
2013-08-02 10:04:14 +00:00
|
|
|
|
|
|
|
if (is_writable($this->cache_dir)) {
|
|
|
|
$host->add_hook($host::HOOK_UPDATE_TASK, $this);
|
2013-08-02 10:12:56 +00:00
|
|
|
$host->add_hook($host::HOOK_HOUSE_KEEPING, $this);
|
2013-08-02 10:04:14 +00:00
|
|
|
$host->add_hook($host::HOOK_SANITIZE, $this);
|
2015-02-25 18:16:14 +00:00
|
|
|
$host->add_handler("public", "cache_starred_images_getimage", $this);
|
|
|
|
|
2013-08-02 10:04:14 +00:00
|
|
|
} else {
|
|
|
|
user_error("Starred cache directory is not writable.", E_USER_WARNING);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
user_error("Unable to create starred cache directory.", E_USER_WARNING);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 18:16:14 +00:00
|
|
|
function cache_starred_images_getimage() {
|
2013-10-05 16:28:26 +00:00
|
|
|
ob_end_clean();
|
|
|
|
|
2013-08-02 10:04:14 +00:00
|
|
|
$hash = basename($_REQUEST["hash"]);
|
|
|
|
|
|
|
|
if ($hash) {
|
|
|
|
|
2016-01-06 18:00:53 +00:00
|
|
|
$filename = $this->cache_dir . "/" . basename($hash);
|
2013-08-02 10:04:14 +00:00
|
|
|
|
|
|
|
if (file_exists($filename)) {
|
2016-01-06 18:00:53 +00:00
|
|
|
header("Content-Disposition: attachment; filename=\"$hash\"");
|
|
|
|
|
2017-10-08 14:10:05 +00:00
|
|
|
send_local_file($filename);
|
2013-08-02 10:04:14 +00:00
|
|
|
} else {
|
|
|
|
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
|
|
|
|
echo "File not found.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:57:36 +00:00
|
|
|
/**
|
|
|
|
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
|
|
|
|
*/
|
2013-08-02 10:12:56 +00:00
|
|
|
function hook_house_keeping() {
|
2018-11-30 04:20:13 +00:00
|
|
|
$files = glob($this->cache_dir . "/*.{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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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'));
|
|
|
|
|
2015-07-07 10:07:58 +00:00
|
|
|
$extension = $entry->tagName == 'source' ? '.mp4' : '.png';
|
|
|
|
$local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension;
|
2013-08-02 10:04:14 +00:00
|
|
|
|
|
|
|
if (file_exists($local_filename)) {
|
|
|
|
$entry->setAttribute("src", get_self_url_prefix() .
|
2015-02-25 18:16:14 +00:00
|
|
|
"/public.php?op=cache_starred_images_getimage&method=image&hash=" .
|
2015-07-07 10:07:58 +00:00
|
|
|
$article_id . "-" . sha1($src) . $extension);
|
2013-08-02 10:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $doc;
|
|
|
|
}
|
|
|
|
|
|
|
|
function hook_update_task() {
|
2017-12-03 08:03:09 +00:00
|
|
|
$res = $this->pdo->query("SELECT content, ttrss_user_entries.owner_uid, link, site_url, ttrss_entries.id, plugin_data
|
2013-08-02 10:04:14 +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
|
2015-07-07 10:07:58 +00:00
|
|
|
(UPPER(content) LIKE '%<IMG%' OR UPPER(content) LIKE '%<VIDEO%') AND
|
2013-08-02 10:04:14 +00:00
|
|
|
site_url != '' AND
|
|
|
|
plugin_data NOT LIKE '%starred_cache_images%'
|
|
|
|
ORDER BY ".sql_random_function()." LIMIT 100");
|
|
|
|
|
2017-12-03 08:03:09 +00:00
|
|
|
$usth = $this->pdo->prepare("UPDATE ttrss_entries SET plugin_data = ? WHERE id = ?");
|
|
|
|
|
|
|
|
while ($line = $res->fetch()) {
|
2013-08-02 10:04:14 +00:00
|
|
|
if ($line["site_url"]) {
|
|
|
|
$success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]);
|
|
|
|
|
|
|
|
if ($success) {
|
2017-12-03 08:03:09 +00:00
|
|
|
$plugin_data = "starred_cache_images,${line['owner_uid']}:" . $line["plugin_data"];
|
2013-08-02 10:04:14 +00:00
|
|
|
|
2017-12-03 08:03:09 +00:00
|
|
|
$usth->execute([$plugin_data, $line['id']]);
|
2013-08-02 10:04:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:57:36 +00:00
|
|
|
/**
|
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
|
|
|
*/
|
2013-08-02 10:04:14 +00:00
|
|
|
function cache_article_images($content, $site_url, $owner_uid, $article_id) {
|
2018-11-30 04:20:13 +00:00
|
|
|
$status_filename = $this->cache_dir . $article_id . "-" . sha1($site_url) . ".status";
|
|
|
|
|
2018-11-30 05:34:29 +00:00
|
|
|
Debug::log("status: $status_filename", Debug::$LOG_EXTENDED);
|
2018-11-30 04:20:13 +00:00
|
|
|
|
|
|
|
if (file_exists($status_filename))
|
|
|
|
$status = json_decode(file_get_contents($status_filename), true);
|
|
|
|
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);
|
2018-11-30 04:20:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file_put_contents($status_filename, json_encode($status))) {
|
|
|
|
user_error("unable to write status file: $status_filename", E_USER_WARNING);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-02 10:04:14 +00:00
|
|
|
$doc = new DOMDocument();
|
2019-03-21 18:08:02 +00:00
|
|
|
$doc->loadHTML('<?xml encoding="UTF-8">' . $content);
|
2013-08-02 10:04:14 +00:00
|
|
|
$xpath = new DOMXPath($doc);
|
|
|
|
|
2015-07-07 10:07:58 +00:00
|
|
|
$entries = $xpath->query('(//img[@src])|(//video/source[@src])');
|
2013-08-02 10:04:14 +00:00
|
|
|
|
|
|
|
$success = false;
|
|
|
|
$has_images = false;
|
|
|
|
|
|
|
|
foreach ($entries as $entry) {
|
2015-07-07 10:07:58 +00:00
|
|
|
|
2017-02-04 08:50:01 +00:00
|
|
|
if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) {
|
|
|
|
|
2013-08-02 10:04:14 +00:00
|
|
|
$has_images = true;
|
|
|
|
$src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
|
|
|
|
|
2015-07-07 10:07:58 +00:00
|
|
|
$extension = $entry->tagName == 'source' ? '.mp4' : '.png';
|
|
|
|
|
|
|
|
$local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension;
|
2013-08-02 10:04:14 +00:00
|
|
|
|
2018-11-30 05:34:29 +00:00
|
|
|
Debug::log("cache_images: downloading: $src to $local_filename", Debug::$LOG_VERBOSE);
|
2013-08-02 10:04:14 +00:00
|
|
|
|
|
|
|
if (!file_exists($local_filename)) {
|
2018-05-20 08:08:33 +00:00
|
|
|
$file_content = fetch_file_contents(["url" => $src, "max_size" => MAX_CACHE_FILE_SIZE]);
|
2013-08-02 10:04:14 +00:00
|
|
|
|
2018-11-30 04:20:13 +00:00
|
|
|
if ($file_content) {
|
|
|
|
if (strlen($file_content) > MIN_CACHE_FILE_SIZE) {
|
|
|
|
file_put_contents($local_filename, $file_content);
|
|
|
|
}
|
|
|
|
|
2013-08-02 10:04:14 +00:00
|
|
|
$success = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$success = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $success || !$has_images;
|
|
|
|
}
|
|
|
|
|
|
|
|
function api_version() {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|