summaryrefslogtreecommitdiff
path: root/plugins/nsfw/init.php
blob: a349ceac0ba12dcaef8ff034d5acf1445b1ba722 (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
123
124
<?php
class NSFW extends Plugin {

	/** @var PluginHost $host */
	private $host;

	function about() {
		return array(null,
			"Hide article content based on tags",
			"fox",
			false);
	}

	function init($host) {
		$this->host = $host;

		$host->add_hook(PluginHost::HOOK_RENDER_ARTICLE, $this);
		$host->add_hook(PluginHost::HOOK_RENDER_ARTICLE_CDM, $this);
		$host->add_hook(PluginHost::HOOK_RENDER_ARTICLE_API, $this);
		$host->add_hook(PluginHost::HOOK_ARTICLE_IMAGE, $this);
		$host->add_hook(PluginHost::HOOK_PREFS_TAB, $this);

	}

	function hook_article_image($enclosures, $content, $site_url, $article) {
		$tags = explode(",", $this->host->get($this, "tags"));
		$article_tags = $article["tags"];

		if (count(array_intersect($tags, $article_tags)) > 0) {
			return [Config::get_self_url() . "/plugins/nsfw/nsfw.png", "", "nsfw"];
		} else {
			return ["", "", $content];
		}
	}

	/**
	 * @param array<string, mixed> $article
	 * @return array<string,mixed>
	 * @throws PDOException
	 */
	private function rewrite_contents(array $article) : array {
		$tags = explode(",", $this->host->get($this, "tags"));
		$article_tags = $article["tags"];

		if (count(array_intersect($tags, $article_tags)) > 0) {
			$article["content"] = "<details class='nsfw'><summary>" . __("Not safe for work (click to toggle)") . "</summary>" . $article["content"] . "</details>";
		}

		return $article;
	}

	function get_css() {
		return
			'details.nsfw {
				cursor : pointer;
				user-select : none;
			}';
	}

	function hook_render_article_api($row) {
		$article = isset($row['headline']) ? $row['headline'] : $row['article'];
		return $this->rewrite_contents($article);
	}

	function hook_render_article($article) {
		return $this->rewrite_contents($article);
	}

	function hook_render_article_cdm($article) {
		return $this->rewrite_contents($article);
	}

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

		$tags = $this->host->get($this, "tags");

		?>
		<div dojoType="dijit.layout.AccordionPane"
			title="<i class='material-icons'>extension</i> <?= __("NSFW Plugin") ?>">
			<form dojoType="dijit.form.Form">

				<?= \Controls\pluginhandler_tags($this, "save") ?>

				<script type="dojo/method" event="onSubmit" args="evt">
					evt.preventDefault();
					if (this.validate()) {
						Notify.progress('Saving data...', true);
						xhr.post("backend.php", this.getValues(), (reply) => {
							Notify.info(reply);
						})
					}
				</script>

				<header><?= __("Tags to consider NSFW (comma-separated):") ?></header>

				<fieldset>
					<textarea dojoType='dijit.form.SimpleTextarea' rows='4'
							style='width: 500px; font-size : 12px;'
							name='tags'><?= $tags ?></textarea>
				</fieldset>

				<hr/>

				<?= \Controls\submit_tag(__("Save")) ?>
			</form>
		</div>
		<?php
	}

	function save() : void {
		$tags = implode(", ",
			FeedItem_Common::normalize_categories(explode(",", $_POST["tags"] ?? "")));

		$this->host->set($this, "tags", $tags);

		echo __("Configuration saved.");
	}

	function api_version() {
		return 2;
	}
}