use Friendica\App;
use Friendica\Core\Hook;
use Friendica\Core\Renderer;
-use Friendica\Core\Session;
use Friendica\DI;
function nsfw_install()
return $new_body;
}
-function nsfw_addon_settings(App &$a, array &$data)
+function nsfw_addon_settings(array &$data)
{
- if (!Session::getLocalUser()) {
+ if (!DI::userSession()->getLocalUserId()) {
return;
}
- $enabled = !DI::pConfig()->get(Session::getLocalUser(), 'nsfw', 'disable');
- $words = DI::pConfig()->get(Session::getLocalUser(), 'nsfw', 'words', 'nsfw,');
+ $enabled = !DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'nsfw', 'disable');
+ $words = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'nsfw', 'words', 'nsfw,');
$t = Renderer::getMarkupTemplate('settings.tpl', 'addon/nsfw/');
$html = Renderer::replaceMacros($t, [
'$info' => DI::l10n()->t('This addon searches for specified words/text in posts and collapses them. It can be used to filter content tagged with for instance #NSFW that may be deemed inappropriate at certain times or places, such as being at work. It is also useful for hiding irrelevant or annoying content from direct view.'),
'$enabled' => ['nsfw-enable', DI::l10n()->t('Enable Content filter'), $enabled],
- '$words' => ['nsfw-words', DI::l10n()->t('Comma separated list of keywords to hide'), $words, DI::l10n()->t('Use /expression/ to provide regular expressions')],
+ '$words' => ['nsfw-words', DI::l10n()->t('Comma separated list of keywords to hide'), $words, DI::l10n()->t('Use /expression/ to provide regular expressions, #tag to specfically match hashtags (case-insensitive), or regular words (case-sensitive)')],
]);
$data = [
];
}
-function nsfw_addon_settings_post(App $a, array &$b)
+function nsfw_addon_settings_post(array &$b)
{
- if (!Session::getLocalUser()) {
+ if (!DI::userSession()->getLocalUserId()) {
return;
}
if (!empty($_POST['nsfw-submit'])) {
- DI::pConfig()->set(Session::getLocalUser(), 'nsfw', 'words', trim($_POST['nsfw-words']));
- $enable = (!empty($_POST['nsfw-enable']) ? intval($_POST['nsfw-enable']) : 0);
+ $enable = !empty($_POST['nsfw-enable']) ? intval($_POST['nsfw-enable']) : 0;
$disable = 1 - $enable;
- DI::pConfig()->set(Session::getLocalUser(), 'nsfw', 'disable', $disable);
+
+ $words = trim($_POST['nsfw-words']);
+ $word_list = explode(',', $words);
+ foreach ($word_list as $word) {
+ $word = trim($word);
+ if (!$words || strpos($word, '/') !== 0) {
+ continue;
+ }
+
+ if (@preg_match($word, '') === false) {
+ DI::sysmsg()->addNotice(DI::l10n()->t('Regular expression "%s" fails to compile', $word));
+ };
+ }
+
+ DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'nsfw', 'words', $words);
+ DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'nsfw', 'disable', $disable);
}
}
-function nsfw_prepare_body_content_filter(App $a, &$hook_data)
+function nsfw_prepare_body_content_filter(&$hook_data)
{
$words = null;
- if (DI::pConfig()->get(Session::getLocalUser(), 'nsfw', 'disable')) {
+ if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'nsfw', 'disable')) {
return;
}
- if (Session::getLocalUser()) {
- $words = DI::pConfig()->get(Session::getLocalUser(), 'nsfw', 'words');
+ if (DI::userSession()->getLocalUserId()) {
+ $words = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'nsfw', 'words');
}
if ($words) {
$tag_search = false;
switch ($word[0]) {
case '/'; // Regular expression
- $found = preg_match($word, $body);
+ $found = @preg_match($word, $body);
break;
case '#': // Hashtag-only search
$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($hook_data['item']['tags'], $word);
+ $found = strpos($body, $word) !== false || nsfw_find_word_in_item_tags($hook_data['item']['tags'], $word);
break;
}