summaryrefslogtreecommitdiff
path: root/init.php
blob: 2534897a8a98d894a662293fbb199494e3bba9fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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;
	}
}