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