summaryrefslogtreecommitdiff
path: root/plugins/af_comics/init.php
blob: a9a8f3faaa1c280b1c275a6fd0aaca74f044062b (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
118
119
120
121
122
<?php
class Af_Comics extends Plugin {

	/** @var array<object> $filters */
	private $filters = array();

	function about() {
		return array(null,
			"Fixes RSS feeds of assorted comic strips",
			"fox");
	}

	function init($host) {
		$host->add_hook($host::HOOK_FETCH_FEED, $this);
		$host->add_hook($host::HOOK_FEED_BASIC_INFO, $this);
		$host->add_hook($host::HOOK_SUBSCRIBE_FEED, $this);
		$host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
		$host->add_hook($host::HOOK_PREFS_TAB, $this);

		require_once __DIR__ . "/filter_base.php";

		$filters = array_merge(glob(__DIR__ . "/filters.local/*.php"), glob(__DIR__ . "/filters/*.php"));
		$names = [];

		foreach ($filters as $file) {
			$filter_name = preg_replace("/\..*$/", "", basename($file));

			if (array_search($filter_name, $names) === false) {
				if (!class_exists($filter_name)) {
					require_once $file;
				}

				array_push($names, $filter_name);

				$filter = new $filter_name($host);

				if (is_subclass_of($filter, "Af_ComicFilter")) {
					array_push($this->filters, $filter);
					array_push($names, $filter_name);
				}
			}
		}
	}

	function hook_prefs_tab($args) {
		if ($args != "prefFeeds") return;

		$comics = [];

		foreach ($this->filters as $f) {
			foreach ($f->supported() as $comic) {
				array_push($comics, $comic);
			}
		}

		asort($comics);

		?>
		<div dojoType="dijit.layout.AccordionPane"
			title="<i class='material-icons'>photo</i> <?= __('Feeds supported by af_comics') ?>">

			<h3><?= __("The following comics are currently supported:") ?></h3>

			<ul class='panel panel-scrollable list list-unstyled'>
				<?php foreach ($comics as $comic) { ?>
					<li><?= htmlspecialchars($comic) ?></li>
				<?php } ?>
			</ul>

			<?= format_notice("To subscribe to GoComics use the comic's regular web page as the feed URL (e.g. for the <em>Garfield</em> comic use <code>http://www.gocomics.com/garfield</code>).") ?>
			<?= format_notice('Drop any updated filters into <code>filters.local</code> in plugin directory.') ?>
		</div>
		<?php
	}

	function hook_article_filter($article) {
		foreach ($this->filters as $f) {
			if ($f->process($article))
				break;
		}

		return $article;
	}

	function hook_fetch_feed($feed_data, $fetch_url, $owner_uid, $feed, $last_article_timestamp, $auth_login, $auth_pass) {
		foreach ($this->filters as $f) {
			$res = $f->on_fetch($fetch_url);

			if ($res)
				return $res;
		}

		return $feed_data;
	}

	function hook_subscribe_feed($contents, $url, $auth_login, $auth_pass) {
		foreach ($this->filters as $f) {
			$res = $f->on_subscribe($url);

			if ($res)
				return $res;
		}

		return $contents;
	}

	function hook_feed_basic_info($basic_info, $fetch_url, $owner_uid, $feed, $auth_login, $auth_pass) {
		foreach ($this->filters as $f) {
			$res = $f->on_basic_info($fetch_url);

			if ($res)
				return $res;
		}

		return $basic_info;
	}

	function api_version() {
		return 2;
	}

}