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