Update function names
[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\PConfig;
11
12 function nsfw_install() {
13         Addon::registerHook('prepare_body', 'addon/nsfw/nsfw.php', 'nsfw_prepare_body', 10);
14         Addon::registerHook('addon_settings', 'addon/nsfw/nsfw.php', 'nsfw_addon_settings');
15         Addon::registerHook('addon_settings_post', 'addon/nsfw/nsfw.php', 'nsfw_addon_settings_post');
16
17 }
18
19
20 function nsfw_uninstall() {
21         Addon::unregisterHook('prepare_body', 'addon/nsfw/nsfw.php', 'nsfw_prepare_body');
22         Addon::unregisterHook('addon_settings', 'addon/nsfw/nsfw.php', 'nsfw_addon_settings');
23         Addon::unregisterHook('addon_settings_post', 'addon/nsfw/nsfw.php', 'nsfw_addon_settings_post');
24
25 }
26
27 // This function isn't perfect and isn't trying to preserve the html structure - it's just a
28 // quick and dirty filter to pull out embedded photo blobs because 'nsfw' seems to come up
29 // inside them quite often. We don't need anything fancy, just pull out the data blob so we can
30 // check against the rest of the body.
31
32 function nsfw_extract_photos($body) {
33
34         $new_body = '';
35
36         $img_start = strpos($body,'src="data:');
37         $img_end = (($img_start !== false) ? strpos(substr($body,$img_start),'>') : false);
38
39         $cnt = 0;
40
41         while($img_end !== false) {
42                 $img_end += $img_start;
43                 $new_body = $new_body . substr($body,0,$img_start);
44
45                 $cnt ++;
46                 $body = substr($body,0,$img_end);
47
48                 $img_start = strpos($body,'src="data:');
49                 $img_end = (($img_start !== false) ? strpos(substr($body,$img_start),'>') : false);
50
51         }
52
53         if(! $cnt)
54                 return $body;
55
56         return $new_body;
57 }
58
59
60
61
62 function nsfw_addon_settings(&$a,&$s) {
63
64
65         if(! local_user())
66                 return;
67
68     /* Add our stylesheet to the page so we can make our settings look nice */
69
70     $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/nsfw/nsfw.css' . '" media="all" />' . "\r\n";
71
72         $enable_checked = (intval(PConfig::get(local_user(),'nsfw','disable')) ? '' : ' checked="checked" ');
73         $words = PConfig::get(local_user(),'nsfw','words');
74         if(! $words)
75                 $words = 'nsfw,';
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>' . t('Not Safe For Work (General Purpose Content Filter)') . '</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>' . t('Not Safe For Work (General Purpose Content Filter)') . '</h3>';
83     $s .= '</span>';
84
85     $s .= '<div id="nsfw-wrapper">';
86     $s .= '<p>' . t ('This addon looks in posts for the words/text you specify below, and collapses any content containing those keywords so it is not displayed at inappropriate times, such as sexual innuendo that may be improper in a work setting. It is polite and recommended to tag any content containing nudity with #NSFW.  This filter can also match any other word/text you specify, and can thereby be used as a general purpose content filter.') . '</p>';
87     $s .= '<label id="nsfw-enable-label" for="nsfw-enable">' . 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">' . 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="' . t('Save Settings') . '" /></div>';
95         $s .= '<div class="nsfw-desc">' . t('Use /expression/ to provide regular expressions') . '</div></div>';
96
97         return;
98
99 }
100
101 function nsfw_addon_settings_post(&$a,&$b) {
102
103         if(! local_user())
104                 return;
105
106         if($_POST['nsfw-submit']) {
107                 PConfig::set(local_user(),'nsfw','words',trim($_POST['nsfw-words']));
108                 $enable = ((x($_POST,'nsfw-enable')) ? intval($_POST['nsfw-enable']) : 0);
109                 $disable = 1-$enable;
110                 PConfig::set(local_user(),'nsfw','disable', $disable);
111                 info( t('NSFW Settings saved.') . EOL);
112         }
113 }
114
115 function nsfw_prepare_body(&$a,&$b) {
116
117
118         $words = null;
119         if(PConfig::get(local_user(),'nsfw','disable'))
120                 return;
121
122         if(local_user()) {
123                 $words = PConfig::get(local_user(),'nsfw','words');
124         }
125         if($words) {
126                 $arr = explode(',',$words);
127         }
128         else {
129                 $arr = ['nsfw'];
130         }
131
132         $found = false;
133         if(count($arr)) {
134
135                 $body = $b['item']['title'] . "\n" . nsfw_extract_photos($b['html']);
136
137                 foreach($arr as $word) {
138                         $word = trim($word);
139                         if(! strlen($word)) {
140                                 continue;
141                         }
142                         if(strpos($word,'/') === 0) {
143                                 if(preg_match($word,$body)) {
144                                         $found = true;
145                                         break;
146                                 }
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         if($found) {
165                 $rnd = random_string(8);
166                 $b['html'] = '<div id="nsfw-wrap-' . $rnd . '" class="fakelink" onclick=openClose(\'nsfw-' . $rnd . '\'); >' . sprintf( t('%s - Click to open/close'),$word ) . '</div><div id="nsfw-' . $rnd . '" style="display: none; " >' . $b['html'] . '</div>';
167         }
168 }