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