X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=nsfw%2Fnsfw.php;h=08e19195ce95a8ea4088d33eaf81e8048650b8b3;hb=e504db1f5085dd89c3950087df4f4e29ea8cbb43;hp=71880499613d79bc9ed4415f937c740d8a7947a6;hpb=b5741aacf6f7d40d265585636841185e9d6bb769;p=friendica-addons.git diff --git a/nsfw/nsfw.php b/nsfw/nsfw.php old mode 100644 new mode 100755 index 71880499..08e19195 --- a/nsfw/nsfw.php +++ b/nsfw/nsfw.php @@ -10,7 +10,7 @@ */ function nsfw_install() { - register_hook('prepare_body', 'addon/nsfw/nsfw.php', 'nsfw_prepare_body'); + register_hook('prepare_body', 'addon/nsfw/nsfw.php', 'nsfw_prepare_body', 10); register_hook('plugin_settings', 'addon/nsfw/nsfw.php', 'nsfw_addon_settings'); register_hook('plugin_settings_post', 'addon/nsfw/nsfw.php', 'nsfw_addon_settings_post'); @@ -24,6 +24,37 @@ function nsfw_uninstall() { } +// This function isn't perfect and isn't trying to preserve the html structure - it's just a +// quick and dirty filter to pull out embedded photo blobs because 'nsfw' seems to come up +// inside them quite often. We don't need anything fancy, just pull out the data blob so we can +// check against the rest of the body. + +function nsfw_extract_photos($body) { + + $new_body = ''; + + $img_start = strpos($body,'src="data:'); + $img_end = (($img_start !== false) ? strpos(substr($body,$img_start),'>') : false); + + $cnt = 0; + + while($img_end !== false) { + $img_end += $img_start; + $new_body = $new_body . substr($body,0,$img_start); + + $cnt ++; + $body = substr($body,0,$img_end); + + $img_start = strpos($body,'src="data:'); + $img_end = (($img_start !== false) ? strpos(substr($body,$img_start),'>') : false); + + } + + if(! $cnt) + return $body; + + return $new_body; +} @@ -38,19 +69,24 @@ function nsfw_addon_settings(&$a,&$s) { $a->page['htmlhead'] .= '' . "\r\n"; - + $enable_checked = (intval(get_pconfig(local_user(),'nsfw','disable')) ? '' : ' checked="checked" '); $words = get_pconfig(local_user(),'nsfw','words'); if(! $words) $words = 'nsfw,'; $s .= '
'; - $s .= '

' . t('"Not Safe For Work" Settings') . '

'; + $s .= '

' . t('Not Safe For Work (General Purpose Content Filter) settings') . '

'; $s .= '
'; - $s .= ''; + $s .= '

' . 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.') . '

'; + $s .= ''; + $s .= ''; + $s .= '
'; + $s .= ''; $s .= ''; $s .= '
'; - $s .= '
'; + $s .= '
'; + $s .= '
' . t('Use /expression/ to provide regular expressions') . '
'; return; @@ -63,13 +99,20 @@ function nsfw_addon_settings_post(&$a,&$b) { if($_POST['nsfw-submit']) { set_pconfig(local_user(),'nsfw','words',trim($_POST['nsfw-words'])); + $enable = ((x($_POST,'nsfw-enable')) ? intval($_POST['nsfw-enable']) : 0); + $disable = 1-$enable; + set_pconfig(local_user(),'nsfw','disable', $disable); info( t('NSFW Settings saved.') . EOL); } } function nsfw_prepare_body(&$a,&$b) { + $words = null; + if(get_pconfig(local_user(),'nsfw','disable')) + return; + if(local_user()) { $words = get_pconfig(local_user(),'nsfw','words'); } @@ -82,20 +125,32 @@ function nsfw_prepare_body(&$a,&$b) { $found = false; if(count($arr)) { + + $body = $b['item']['title'] . "\n" . nsfw_extract_photos($b['html']); + foreach($arr as $word) { - if(! strlen(trim($word))) { + $word = trim($word); + if(! strlen($word)) { continue; } - - if(stristr($b['html'],$word)) { - $found = true; - break; + if(strpos($word,'/') === 0) { + if(preg_match($word,$body)) { + $found = true; + break; + } } - if(stristr($b['item']['tag'], ']' . $word . '[' )) { - $found = true; - break; + else { + if(stristr($body,$word)) { + $found = true; + break; + } + if(stristr($b['item']['tag'], ']' . $word . '[' )) { + $found = true; + break; + } } } + } if($found) { $rnd = random_string(8);