Update nsfw.php
[friendica-addons.git] / nsfw / nsfw.php
1 <?php
2 /**
3  * Name: NSFW
4  * Description: Collapse posts with inappropriate content
5  * Version: 1.0
6  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
7  *
8  */
9 use Friendica\Core\Addon;
10 use Friendica\Core\L10n;
11 use Friendica\Core\PConfig;
12
13 function nsfw_install()
14 {
15         Addon::registerHook('prepare_body', 'addon/nsfw/nsfw.php', 'nsfw_prepare_body', 10);
16         Addon::registerHook('addon_settings', 'addon/nsfw/nsfw.php', 'nsfw_addon_settings');
17         Addon::registerHook('addon_settings_post', 'addon/nsfw/nsfw.php', 'nsfw_addon_settings_post');
18 }
19
20
21 function nsfw_uninstall()
22 {
23         Addon::unregisterHook('prepare_body', 'addon/nsfw/nsfw.php', 'nsfw_prepare_body');
24         Addon::unregisterHook('addon_settings', 'addon/nsfw/nsfw.php', 'nsfw_addon_settings');
25         Addon::unregisterHook('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
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(&$a, &$b)
114 {
115         // Don't do the check when there is a content warning
116         if (!empty($b['item']['content-warning'])) {
117                 return;
118         }
119
120         $words = null;
121         if (PConfig::get(local_user(), 'nsfw', 'disable')) {
122                 return;
123         }
124
125         if (local_user()) {
126                 $words = PConfig::get(local_user(), 'nsfw', 'words');
127         }
128         if ($words) {
129                 $arr = explode(',', $words);
130         } else {
131                 $arr = ['nsfw'];
132         }
133
134         $found = false;
135         if (count($arr)) {
136                 $body = $b['item']['title'] . "\n" . nsfw_extract_photos($b['html']);
137
138                 foreach ($arr as $word) {
139                         $word = trim($word);
140                         if (!strlen($word)) {
141                                 continue;
142                         }
143                         if (strpos($word,'/') === 0) {
144                                 if (preg_match($word, $body)) {
145                                         $found = true;
146                                         break;
147                                 }
148                         } else {
149                                 if (stristr($body, $word)) {
150                                         $found = true;
151                                         break;
152                                 }
153                                 if (is_array($b['item']['tags']) && count($b['item']['tags'])) {
154                                         foreach ($b['item']['tags'] as $t) {
155                                                 if (stristr($t, '>' . $word . '<')) {
156                                                         $found = true;
157                                                         break;
158                                                 }
159                                         }
160                                 }
161                         }
162                 }
163         }
164
165         if ($found) {
166                 $rnd = random_string(8);
167                 $b['html'] = '<div id="nsfw-wrap-' . $rnd . '" class="fakelink" onclick=openClose(\'nsfw-' . $rnd . '\'); >' . L10n::t('%s - Click to open/close', $word) . '</div><div id="nsfw-' . $rnd . '" style="display: none; " >' . $b['html'] . '</div>';
168         }
169 }