6 * Description: Collapse posts with inappropriate content
8 * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
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');
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');
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.
32 function nsfw_extract_photos($body) {
36 $img_start = strpos($body,'src="data:');
37 $img_end = (($img_start !== false) ? strpos(substr($body,$img_start),'>') : false);
41 while($img_end !== false) {
42 $img_end += $img_start;
43 $new_body = $new_body . substr($body,0,$img_start);
46 $body = substr($body,0,$img_end);
48 $img_start = strpos($body,'src="data:');
49 $img_end = (($img_start !== false) ? strpos(substr($body,$img_start),'>') : false);
62 function nsfw_addon_settings(&$a,&$s) {
68 /* Add our stylesheet to the page so we can make our settings look nice */
70 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/nsfw/nsfw.css' . '" media="all" />' . "\r\n";
72 $enable_checked = (intval(get_pconfig(local_user(),'nsfw','disable')) ? '' : ' checked="checked" ');
73 $words = get_pconfig(local_user(),'nsfw','words');
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>';
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>';
85 $s .= '<div id="nsfw-wrapper">';
86 $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>';
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 .= '<input id="nsfw-words" type="text" name="nsfw-words" value="' . $words .'" />';
92 $s .= '</div><div class="clear"></div>';
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>';
101 function nsfw_addon_settings_post(&$a,&$b) {
106 if($_POST['nsfw-submit']) {
107 set_pconfig(local_user(),'nsfw','words',trim($_POST['nsfw-words']));
108 $enable = ((x($_POST,'nsfw-enable')) ? intval($_POST['nsfw-enable']) : 0);
109 $disable = 1-$enable;
110 set_pconfig(local_user(),'nsfw','disable', $disable);
111 info( t('NSFW Settings saved.') . EOL);
115 function nsfw_prepare_body(&$a,&$b) {
119 if(get_pconfig(local_user(),'nsfw','disable'))
123 $words = get_pconfig(local_user(),'nsfw','words');
126 $arr = explode(',',$words);
129 $arr = array('nsfw');
135 $body = $b['item']['title'] . "\n" . nsfw_extract_photos($b['html']);
137 foreach($arr as $word) {
139 if(! strlen($word)) {
142 if(strpos($word,'/') === 0) {
143 if(preg_match($word,$body)) {
149 if(stristr($body,$word)) {
153 if(is_array($b['item']['tags']) && count($b['item']['tags'])) {
154 foreach($b['item']['tags'] as $t) {
155 if(stristr($t, '>' . $word . '<' )) {
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>';