summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2020-04-29 11:32:17 +0300
committerAndrew Dolgov <[email protected]>2020-04-29 11:32:17 +0300
commitd4f18144150d2d552631ad302985612f0187a56a (patch)
tree3c12ffcbc2d497358f572e3690066f24cabe3bfc
initial
-rw-r--r--README.md6
-rw-r--r--init.php117
2 files changed, 123 insertions, 0 deletions
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 @@
+<?php
+
+use andreskrey\Readability\Readability;
+use andreskrey\Readability\Configuration;
+
+class Af_Enclosure_Fix_Type extends Plugin {
+
+ /* @var PluginHost $host */
+ private $host;
+
+ function about() {
+ return array(1.0,
+ "Overrides content type for enclosures based on server-provided data",
+ "fox");
+ }
+
+ function save() {
+ $enable_share_anything = checkbox_to_sql_bool($_POST["enable_share_anything"]);
+
+ $this->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 "<header>".__("Override enclosure content type")."</header>";
+ print "<section>";
+
+ $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 "<fieldset>";
+
+ print "<label class='checkbox'><input dojoType='dijit.form.CheckBox' type='checkbox' id='af_enclosure_fix_type_enabled'
+ name='af_enclosure_fix_type_enabled' $checked>&nbsp;".__('Enable for this feed')."</label>";
+
+ print "</fieldset>";
+
+ print "</section>";
+ }
+
+ /** @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;
+ }
+}