]> git.mxchange.org Git - friendica-addons.git/blob - nsfw/nsfw.php
a5b3ede184db868c48d8de72714f44a79c9cda59
[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');
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
28
29
30
31 function nsfw_addon_settings(&$a,&$s) {
32
33
34         if(! local_user())
35                 return;
36
37     /* Add our stylesheet to the page so we can make our settings look nice */
38
39     $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/nsfw/nsfw.css' . '" media="all" />' . "\r\n";
40
41         $enable_checked = (intval(get_pconfig(local_user(),'nsfw','disable')) ? '' : ' checked="checked" ');
42         $words = get_pconfig(local_user(),'nsfw','words');
43         if(! $words)
44                 $words = 'nsfw,';
45
46     $s .= '<div class="settings-block">';
47     $s .= '<h3>' . t('"Not Safe For Work" Settings') . '</h3>';
48     $s .= '<div id="nsfw-wrapper">';
49         
50     $s .= '<label id="nsfw-enable-label" for="nsfw-enable">' . t('Enable NSFW filter') . ' </label>';
51     $s .= '<input id="nsfw-enable" type="checkbox" name="nsfw-enable" value="1"' . $enable_checked . ' />';
52         $s .= '<div class="clear"></div>';
53     $s .= '<label id="nsfw-label" for="nsfw-words">' . t('Comma separated words to treat as NSFW') . ' </label>';
54     $s .= '<input id="nsfw-words" type="text" name="nsfw-words" value="' . $words .'" />';
55     $s .= '</div><div class="clear"></div>';
56
57     $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="nsfw-submit" name="nsfw-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
58         $s .= '<div class="nsfw-desc">' . t('Use /expression/ to provide regular expressions') . '</div></div>';
59
60         return;
61
62 }
63
64 function nsfw_addon_settings_post(&$a,&$b) {
65
66         if(! local_user())
67                 return;
68
69         if($_POST['nsfw-submit']) {
70                 set_pconfig(local_user(),'nsfw','words',trim($_POST['nsfw-words']));
71                 $enable = ((x($_POST,'nsfw-enable')) ? intval($_POST['nsfw-enable']) : 0);
72                 $disable = 1-$enable;
73                 set_pconfig(local_user(),'nsfw','disable', $disable);
74                 info( t('NSFW Settings saved.') . EOL);
75         }
76 }
77
78 function nsfw_prepare_body(&$a,&$b) {
79
80         $words = null;
81         if(get_pconfig(local_user(),'nsfw','disable'))
82                 return;
83
84         if(local_user()) {
85                 $words = get_pconfig(local_user(),'nsfw','words');
86         }
87         if($words) {
88                 $arr = explode(',',$words);
89         }
90         else {
91                 $arr = array('nsfw');
92         }
93
94         $found = false;
95         if(count($arr)) {
96                 foreach($arr as $word) {
97                         if(! strlen(trim($word))) {
98                                 continue;
99                         }
100                         if(strpos($word,'/') === 0) {
101                                 if(preg_match($word,$b['html'])) {
102                                         $found = true;
103                                         break;
104                                 }
105                         }
106                         else {
107                                 if(stristr($b['html'],$word)) {
108                                         $found = true;
109                                         break;
110                                 }
111                                 if(stristr($b['item']['tag'], ']' . $word . '[' )) {
112                                         $found = true;
113                                         break;
114                                 }
115                         } 
116                 }
117         }
118         if($found) {
119                 $rnd = random_string(8);
120                 $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>';  
121         }
122 }