]> git.mxchange.org Git - friendica-addons.git/blobdiff - nsfw/nsfw.php
Improved logging message
[friendica-addons.git] / nsfw / nsfw.php
index da45df3efe81858079c743d48b0466ef423cd704..4a54d899e201c2eb9d77477a2def9679fef945e8 100644 (file)
@@ -11,7 +11,6 @@
 use Friendica\App;
 use Friendica\Core\Hook;
 use Friendica\Core\Renderer;
-use Friendica\Core\Session;
 use Friendica\DI;
 
 function nsfw_install()
@@ -52,20 +51,20 @@ function nsfw_extract_photos($body)
        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 = [
@@ -75,29 +74,43 @@ function nsfw_addon_settings(App &$a, array &$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) {
@@ -119,14 +132,14 @@ function nsfw_prepare_body_content_filter(App $a, &$hook_data)
                        $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;
                        }