X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=nsfw%2Fnsfw.php;h=9f81b29d8333557f669a26dc735a9a23c711616b;hb=4e208f8f12424608a83682b2c1ae39011f840b9a;hp=081071510b483c130f5b650798c12385fcf1f3ee;hpb=a406fbc9e45f8405749d72a3ac63b1b3a4d296fb;p=friendica-addons.git diff --git a/nsfw/nsfw.php b/nsfw/nsfw.php index 08107151..9f81b29d 100644 --- a/nsfw/nsfw.php +++ b/nsfw/nsfw.php @@ -7,22 +7,14 @@ * Author: Mike Macgirvin * */ -use Friendica\Core\Addon; -use Friendica\Core\L10n; -use Friendica\Core\PConfig; +use Friendica\Core\Hook; +use Friendica\DI; function nsfw_install() { - Addon::registerHook('prepare_body', 'addon/nsfw/nsfw.php', 'nsfw_prepare_body', 10); - Addon::registerHook('addon_settings', 'addon/nsfw/nsfw.php', 'nsfw_addon_settings'); - Addon::registerHook('addon_settings_post', 'addon/nsfw/nsfw.php', 'nsfw_addon_settings_post'); -} - -function nsfw_uninstall() -{ - Addon::unregisterHook('prepare_body', 'addon/nsfw/nsfw.php', 'nsfw_prepare_body'); - Addon::unregisterHook('addon_settings', 'addon/nsfw/nsfw.php', 'nsfw_addon_settings'); - Addon::unregisterHook('addon_settings_post', 'addon/nsfw/nsfw.php', 'nsfw_addon_settings_post'); + Hook::register('prepare_body_content_filter', 'addon/nsfw/nsfw.php', 'nsfw_prepare_body_content_filter', 10); + Hook::register('addon_settings', 'addon/nsfw/nsfw.php', 'nsfw_addon_settings'); + Hook::register('addon_settings_post', 'addon/nsfw/nsfw.php', 'nsfw_addon_settings_post'); } // This function isn't perfect and isn't trying to preserve the html structure - it's just a @@ -64,33 +56,33 @@ function nsfw_addon_settings(&$a, &$s) /* Add our stylesheet to the page so we can make our settings look nice */ - $a->page['htmlhead'] .= '' . "\r\n"; + DI::page()['htmlhead'] .= '' . "\r\n"; - $enable_checked = (intval(PConfig::get(local_user(), 'nsfw', 'disable')) ? '' : ' checked="checked" '); - $words = PConfig::get(local_user(), 'nsfw', 'words'); + $enable_checked = (intval(DI::pConfig()->get(local_user(), 'nsfw', 'disable')) ? '' : ' checked="checked" '); + $words = DI::pConfig()->get(local_user(), 'nsfw', 'words'); if (!$words) { $words = 'nsfw,'; } $s .= ''; - $s .= '

' . L10n::t('Content Filter (NSFW and more)') . '

'; + $s .= '

' . DI::l10n()->t('Content Filter (NSFW and more)') . '

'; $s .= '
'; $s .= ''; + $s .= '
'; + $s .= '
' . DI::l10n()->t('Use /expression/ to provide regular expressions') . '
'; return; } @@ -100,29 +92,23 @@ function nsfw_addon_settings_post(&$a, &$b) return; } - if ($_POST['nsfw-submit']) { - PConfig::set(local_user(), 'nsfw', 'words', trim($_POST['nsfw-words'])); - $enable = (x($_POST, 'nsfw-enable') ? intval($_POST['nsfw-enable']) : 0); + if (!empty($_POST['nsfw-submit'])) { + DI::pConfig()->set(local_user(), 'nsfw', 'words', trim($_POST['nsfw-words'])); + $enable = (!empty($_POST['nsfw-enable']) ? intval($_POST['nsfw-enable']) : 0); $disable = 1 - $enable; - PConfig::set(local_user(), 'nsfw', 'disable', $disable); - info(L10n::t('NSFW Settings saved.') . EOL); + DI::pConfig()->set(local_user(), 'nsfw', 'disable', $disable); } } -function nsfw_prepare_body(Friendica\App $a, &$b) +function nsfw_prepare_body_content_filter(\Friendica\App $a, &$hook_data) { - // Don't do the check when there is a content warning - if (!empty($b['item']['content-warning'])) { - return; - } - $words = null; - if (PConfig::get(local_user(), 'nsfw', 'disable')) { + if (DI::pConfig()->get(local_user(), 'nsfw', 'disable')) { return; } if (local_user()) { - $words = PConfig::get(local_user(), 'nsfw', 'words'); + $words = DI::pConfig()->get(local_user(), 'nsfw', 'words'); } if ($words) { @@ -133,7 +119,7 @@ function nsfw_prepare_body(Friendica\App $a, &$b) $found = false; if (count($word_list)) { - $body = $b['item']['title'] . "\n" . nsfw_extract_photos($b['html']); + $body = $hook_data['item']['title'] . "\n" . nsfw_extract_photos($hook_data['item']['body']); foreach ($word_list as $word) { $word = trim($word); @@ -141,15 +127,17 @@ function nsfw_prepare_body(Friendica\App $a, &$b) continue; } + $tag_search = false; switch ($word[0]) { case '/'; // Regular expression $found = preg_match($word, $body); break; case '#': // Hashtag-only search - $found = nsfw_find_word_in_item_tags($b['item']['hashtags'], substr($word, 1)); + $tag_search = true; + $found = nsfw_find_word_in_item_tags($hook_data['item']['hashtags'], substr($word, 1)); break; default: - $found = stripos($body, $word) !== false || nsfw_find_word_in_item_tags($b['item']['tags'], $word); + $found = stripos($body, $word) !== false || nsfw_find_word_in_item_tags($hook_data['item']['tags'], $word); break; } @@ -160,10 +148,11 @@ function nsfw_prepare_body(Friendica\App $a, &$b) } if ($found) { - $rnd = random_string(8); - $b['html'] = ''; + if ($tag_search) { + $hook_data['filter_reasons'][] = DI::l10n()->t('Filtered tag: %s', $word); + } else { + $hook_data['filter_reasons'][] = DI::l10n()->t('Filtered word: %s', $word); + } } }