From d4f18144150d2d552631ad302985612f0187a56a Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Wed, 29 Apr 2020 11:32:17 +0300 Subject: initial --- README.md | 6 ++++ init.php | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 README.md create mode 100644 init.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..286e2f9 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +Overrides content type for enclosures based on data returned by HTTP server +for enclosure URLs. Helps if enclosures are not detected properly as images, etc. + +Enable for specific feeds in the feed editor. + +Git clone to ``(tt-rss-root)/plugins.local/af_enclosure_fix_type``. diff --git a/init.php b/init.php new file mode 100644 index 0000000..2534897 --- /dev/null +++ b/init.php @@ -0,0 +1,117 @@ +host->set($this, "enable_share_anything", $enable_share_anything); + + echo __("Data saved."); + } + + function init($host) + { + $this->host = $host; + + if (version_compare(PHP_VERSION, '5.6.0', '<')) { + return; + } + + $host->add_hook($host::HOOK_ENCLOSURE_IMPORTED, $this); + $host->add_hook($host::HOOK_PREFS_EDIT_FEED, $this); + $host->add_hook($host::HOOK_PREFS_SAVE_FEED, $this); + + $host->add_filter_action($this, "action_fix_enclosure_type", __("Fix media enclosures")); + } + + /** @noinspection PhpUnused */ + function hook_prefs_edit_feed($feed_id) { + print "
".__("Override enclosure content type")."
"; + print "
"; + + $enabled_feeds = $this->host->get($this, "enabled_feeds"); + if (!is_array($enabled_feeds)) $enabled_feeds = array(); + + $key = array_search($feed_id, $enabled_feeds); + $checked = $key !== FALSE ? "checked" : ""; + + print "
"; + + print ""; + + print "
"; + + print "
"; + } + + /** @noinspection PhpUnused */ + function hook_prefs_save_feed($feed_id) { + $enabled_feeds = $this->filter_unknown_feeds($this->host->get($this, "enabled_feeds")); + if (!is_array($enabled_feeds)) $enabled_feeds = array(); + + $enable = checkbox_to_sql_bool($_POST["af_enclosure_fix_type_enabled"]); + $key = array_search($feed_id, $enabled_feeds); + + if ($enable) { + if ($key === FALSE) { + array_push($enabled_feeds, $feed_id); + } + } else { + if ($key !== FALSE) { + unset($enabled_feeds[$key]); + } + } + + $this->host->set($this, "enabled_feeds", $enabled_feeds); + } + + /** @noinspection PhpUnused */ + function hook_enclosure_imported($enc, $feed) { + $enabled_feeds = $this->host->get($this, "enabled_feeds"); + + if (!is_array($enabled_feeds) || array_search($feed, $enabled_feeds) === FALSE) + return $enc; + + $headers = @get_headers($enc->link, 1); + + if (is_array($headers) && isset($headers["Content-Type"])) { + $enc->type = $headers["Content-Type"]; + } + + return $enc; + } + + private function filter_unknown_feeds($enabled_feeds) { + $tmp = array(); + + foreach ($enabled_feeds as $feed) { + + $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE id = ? AND owner_uid = ?"); + $sth->execute([$feed, $_SESSION['uid']]); + + if ($row = $sth->fetch()) { + array_push($tmp, $feed); + } + } + + return $tmp; + } + + function api_version() { + return 2; + } +} -- cgit v1.2.3