Merge pull request #1076 from tobiasd/20210201-lngCmessages
[friendica-addons.git/.git] / nsfw / nsfw.php
1 <?php
2
3 /**
4  * Name: NSFW
5  * Description: Collapse posts with inappropriate content
6  * Version: 1.0
7  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
8  *
9  */
10 use Friendica\Core\Hook;
11 use Friendica\DI;
12
13 function nsfw_install()
14 {
15         Hook::register('prepare_body_content_filter', 'addon/nsfw/nsfw.php', 'nsfw_prepare_body_content_filter', 10);
16         Hook::register('addon_settings', 'addon/nsfw/nsfw.php', 'nsfw_addon_settings');
17         Hook::register('addon_settings_post', 'addon/nsfw/nsfw.php', 'nsfw_addon_settings_post');
18 }
19
20 // This function isn't perfect and isn't trying to preserve the html structure - it's just a
21 // quick and dirty filter to pull out embedded photo blobs because 'nsfw' seems to come up
22 // inside them quite often. We don't need anything fancy, just pull out the data blob so we can
23 // check against the rest of the body.
24
25 function nsfw_extract_photos($body)
26 {
27         $new_body = '';
28
29         $img_start = strpos($body, 'src="data:');
30         $img_end = (($img_start !== false) ? strpos(substr($body, $img_start), '>') : false);
31
32         $cnt = 0;
33
34         while ($img_end !== false) {
35                 $img_end += $img_start;
36                 $new_body = $new_body . substr($body, 0, $img_start);
37
38                 $cnt ++;
39                 $body = substr($body, 0, $img_end);
40
41                 $img_start = strpos($body, 'src="data:');
42                 $img_end = (($img_start !== false) ? strpos(substr($body, $img_start), '>') : false);
43         }
44
45         if (!$cnt) {
46                 return $body;
47         }
48         return $new_body;
49 }
50
51 function nsfw_addon_settings(&$a, &$s)
52 {
53         if (!local_user()) {
54                 return;
55         }
56
57         /* Add our stylesheet to the page so we can make our settings look nice */
58
59         DI::page()['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . DI::baseUrl()->get() . '/addon/nsfw/nsfw.css' . '" media="all" />' . "\r\n";
60
61         $enable_checked = (intval(DI::pConfig()->get(local_user(), 'nsfw', 'disable')) ? '' : ' checked="checked" ');
62         $words = DI::pConfig()->get(local_user(), 'nsfw', 'words');
63         if (!$words) {
64                 $words = 'nsfw,';
65         }
66
67         $s .= '<span id="settings_nsfw_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_nsfw_expanded\'); openClose(\'settings_nsfw_inflated\');">';
68         $s .= '<h3>' . DI::l10n()->t('Content Filter (NSFW and more)') . '</h3>';
69         $s .= '</span>';
70         $s .= '<div id="settings_nsfw_expanded" class="settings-block" style="display: none;">';
71         $s .= '<span class="fakelink" onclick="openClose(\'settings_nsfw_expanded\'); openClose(\'settings_nsfw_inflated\');">';
72         $s .= '<h3>' . DI::l10n()->t('Content Filter (NSFW and more)') . '</h3>';
73         $s .= '</span>';
74
75         $s .= '<div id="nsfw-wrapper">';
76         $s .= '<p>' . DI::l10n()->t('This addon searches for specified words/text in posts and collapses them. It can be used to filter content tagged with for instance #NSFW that may be deemed inappropriate at certain times or places, such as being at work. It is also useful for hiding irrelevant or annoying content from direct view.') . '</p>';
77         $s .= '<label id="nsfw-enable-label" for="nsfw-enable">' . DI::l10n()->t('Enable Content filter') . ' </label>';
78         $s .= '<input id="nsfw-enable" type="checkbox" name="nsfw-enable" value="1"' . $enable_checked . ' />';
79         $s .= '<div class="clear"></div>';
80         $s .= '<label id="nsfw-label" for="nsfw-words">' . DI::l10n()->t('Comma separated list of keywords to hide') . ' </label>';
81         $s .= '<textarea id="nsfw-words" type="text" name="nsfw-words">' . $words . '</textarea>';
82         $s .= '</div><div class="clear"></div>';
83
84         $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="nsfw-submit" name="nsfw-submit" class="settings-submit" value="' . DI::l10n()->t('Save Settings') . '" /></div>';
85         $s .= '<div class="nsfw-desc">' . DI::l10n()->t('Use /expression/ to provide regular expressions') . '</div></div>';
86         return;
87 }
88
89 function nsfw_addon_settings_post(&$a, &$b)
90 {
91         if (!local_user()) {
92                 return;
93         }
94
95         if (!empty($_POST['nsfw-submit'])) {
96                 DI::pConfig()->set(local_user(), 'nsfw', 'words', trim($_POST['nsfw-words']));
97                 $enable = (!empty($_POST['nsfw-enable']) ? intval($_POST['nsfw-enable']) : 0);
98                 $disable = 1 - $enable;
99                 DI::pConfig()->set(local_user(), 'nsfw', 'disable', $disable);
100         }
101 }
102
103 function nsfw_prepare_body_content_filter(\Friendica\App $a, &$hook_data)
104 {
105         $words = null;
106         if (DI::pConfig()->get(local_user(), 'nsfw', 'disable')) {
107                 return;
108         }
109
110         if (local_user()) {
111                 $words = DI::pConfig()->get(local_user(), 'nsfw', 'words');
112         }
113
114         if ($words) {
115                 $word_list = explode(',', $words);
116         } else {
117                 $word_list = ['nsfw'];
118         }
119
120         $found = false;
121         if (count($word_list)) {
122                 $body = $hook_data['item']['title'] . "\n" . nsfw_extract_photos($hook_data['item']['body']);
123
124                 foreach ($word_list as $word) {
125                         $word = trim($word);
126                         if (!strlen($word)) {
127                                 continue;
128                         }
129
130                         $tag_search = false;
131                         switch ($word[0]) {
132                                 case '/'; // Regular expression
133                                         $found = preg_match($word, $body);
134                                         break;
135                                 case '#': // Hashtag-only search
136                                         $tag_search = true;
137                                         $found = nsfw_find_word_in_item_tags($hook_data['item']['hashtags'], substr($word, 1));
138                                         break;
139                                 default:
140                                         $found = stripos($body, $word) !== false || nsfw_find_word_in_item_tags($hook_data['item']['tags'], $word);
141                                         break;
142                         }
143
144                         if ($found) {
145                                 break;
146                         }
147                 }
148         }
149
150         if ($found) {
151                 if ($tag_search) {
152                         $hook_data['filter_reasons'][] = DI::l10n()->t('Filtered tag: %s', $word);
153                 } else {
154                         $hook_data['filter_reasons'][] = DI::l10n()->t('Filtered word: %s', $word);
155                 }
156         }
157 }
158
159 function nsfw_find_word_in_item_tags($item_tags, $word)
160 {
161         if (is_array($item_tags)) {
162                 foreach ($item_tags as $tag) {
163                         if (stripos($tag, '>' . $word . '<') !== false) {
164                                 return true;
165                         }
166                 }
167         }
168
169         return false;
170 }