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 .= '<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>';
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>';
95 function nsfw_addon_settings_post(&$a,&$b) {
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);
109 function nsfw_prepare_body(&$a,&$b) {
113 if(get_pconfig(local_user(),'nsfw','disable'))
117 $words = get_pconfig(local_user(),'nsfw','words');
120 $arr = explode(',',$words);
123 $arr = array('nsfw');
129 $body = nsfw_extract_photos($b['html']);
131 foreach($arr as $word) {
133 if(! strlen($word)) {
136 if(strpos($word,'/') === 0) {
137 if(preg_match($word,$body)) {
143 if(stristr($body,$word)) {
147 if(stristr($b['item']['tag'], ']' . $word . '[' )) {
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>';