]> git.mxchange.org Git - friendica-addons.git/blob - nsfw/nsfw.php
n-s-f-w use title in word search until somebody fixes the tag search
[friendica-addons.git] / nsfw / nsfw.php
1 <?php
2
3
4 /**
5  * Name: NSFW
6  * Description: Collapse posts with inappropriate content
7  * Version: 1.0
8  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
9  * 
10  */
11
12 function nsfw_install() {
13         register_hook('prepare_body', 'addon/nsfw/nsfw.php', 'nsfw_prepare_body', 10);
14         register_hook('plugin_settings', 'addon/nsfw/nsfw.php', 'nsfw_addon_settings');
15         register_hook('plugin_settings_post', 'addon/nsfw/nsfw.php', 'nsfw_addon_settings_post');
16
17 }
18
19
20 function nsfw_uninstall() {
21         unregister_hook('prepare_body', 'addon/nsfw/nsfw.php', 'nsfw_prepare_body');
22         unregister_hook('plugin_settings', 'addon/nsfw/nsfw.php', 'nsfw_addon_settings');
23         unregister_hook('plugin_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(get_pconfig(local_user(),'nsfw','disable')) ? '' : ' checked="checked" ');
73         $words = get_pconfig(local_user(),'nsfw','words');
74         if(! $words)
75                 $words = 'nsfw,';
76
77     $s .= '<div class="settings-block">';
78     $s .= '<h3>' . t('Not Safe For Work (General Purpose Content Filter) settings') . '</h3>';
79     $s .= '<div id="nsfw-wrapper">';
80     $s .= '<p>' . t ('This plugin 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>';
81     $s .= '<label id="nsfw-enable-label" for="nsfw-enable">' . t('Enable Content filter') . ' </label>';
82     $s .= '<input id="nsfw-enable" type="checkbox" name="nsfw-enable" value="1"' . $enable_checked . ' />';
83         $s .= '<div class="clear"></div>';
84     $s .= '<label id="nsfw-label" for="nsfw-words">' . t('Comma separated list of keywords to hide') . ' </label>';
85     $s .= '<input id="nsfw-words" type="text" name="nsfw-words" value="' . $words .'" />';
86     $s .= '</div><div class="clear"></div>';
87
88     $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="nsfw-submit" name="nsfw-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
89         $s .= '<div class="nsfw-desc">' . t('Use /expression/ to provide regular expressions') . '</div></div>';
90
91         return;
92
93 }
94
95 function nsfw_addon_settings_post(&$a,&$b) {
96
97         if(! local_user())
98                 return;
99
100         if($_POST['nsfw-submit']) {
101                 set_pconfig(local_user(),'nsfw','words',trim($_POST['nsfw-words']));
102                 $enable = ((x($_POST,'nsfw-enable')) ? intval($_POST['nsfw-enable']) : 0);
103                 $disable = 1-$enable;
104                 set_pconfig(local_user(),'nsfw','disable', $disable);
105                 info( t('NSFW Settings saved.') . EOL);
106         }
107 }
108
109 function nsfw_prepare_body(&$a,&$b) {
110
111
112         $words = null;
113         if(get_pconfig(local_user(),'nsfw','disable'))
114                 return;
115
116         if(local_user()) {
117                 $words = get_pconfig(local_user(),'nsfw','words');
118         }
119         if($words) {
120                 $arr = explode(',',$words);
121         }
122         else {
123                 $arr = array('nsfw');
124         }
125
126         $found = false;
127         if(count($arr)) {
128
129                 $body = $b['title'] . ' ' . nsfw_extract_photos($b['html']);
130
131                 foreach($arr as $word) {
132                         $word = trim($word);
133                         if(! strlen($word)) {
134                                 continue;
135                         }
136                         if(strpos($word,'/') === 0) {
137                                 if(preg_match($word,$body)) {
138                                         $found = true;
139                                         break;
140                                 }
141                         }
142                         else {
143                                 if(stristr($body,$word)) {
144                                         $found = true;
145                                         break;
146                                 }
147                                 if(stristr($b['item']['tag'], ']' . $word . '[' )) {
148                                         $found = true;
149                                         break;
150                                 }
151                         } 
152                 }
153                 
154         }
155         if($found) {
156                 $rnd = random_string(8);
157                 $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>';  
158         }
159 }