]> git.mxchange.org Git - friendica-addons.git/blob - nsfw/nsfw.php
332e82245e3e40465e356c47f0d4ce9614233ed1
[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
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     $s .= '<label id="nsfw-label" for="nsfw-words">' . t('Comma separated words to treat as NSFW') . ' </label>';
50     $s .= '<input id="nsfw-words" type="text" name="nsfw-words" value="' . $words .'" />';
51     $s .= '</div><div class="clear"></div>';
52
53     $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="nsfw-submit" name="nsfw-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
54
55         return;
56
57 }
58
59 function nsfw_addon_settings_post(&$a,&$b) {
60
61         if(! local_user())
62                 return;
63
64         if($_POST['nsfw-submit']) {
65                 set_pconfig(local_user(),'nsfw','words',trim($_POST['nsfw-words']));
66                 info( t('NSFW Settings saved.') . EOL);
67         }
68 }
69
70 function nsfw_prepare_body(&$a,&$b) {
71
72         $words = null;
73         if(local_user()) {
74                 $words = get_pconfig(local_user(),'nsfw','words');
75         }
76         if($words) {
77                 $arr = explode(',',$words);
78         }
79         else {
80                 $arr = array('nsfw');
81         }
82
83         $found = false;
84         if(count($arr)) {
85                 foreach($arr as $word) {
86                         if(! strlen(trim($word))) {
87                                 continue;
88                         }
89                         if(strpos($word,'/') === 0) {
90                                 if(preg_match($word,$b['html'])) {
91                                         $found = true;
92                                         break;
93                                 }
94                         }
95                         else {
96                                 if(stristr($b['html'],$word)) {
97                                         $found = true;
98                                         break;
99                                 }
100                                 if(stristr($b['item']['tag'], ']' . $word . '[' )) {
101                                         $found = true;
102                                         break;
103                                 }
104                         } 
105                 }
106         }
107         if($found) {
108                 $rnd = random_string(8);
109                 $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>';  
110         }
111 }