]> git.mxchange.org Git - friendica-addons.git/blobdiff - langfilter/langfilter.php
One false positive, many bots added
[friendica-addons.git] / langfilter / langfilter.php
index 3c033211f214d5f11842b5ea0f2b7325b55d56a1..950219698a58a722706e33bc29e19b4f2f37d2a9 100644 (file)
@@ -8,9 +8,11 @@
  */
 
 use Friendica\App;
-use Friendica\Core\Addon;
+use Friendica\Content\Text\BBCode;
+use Friendica\Core\Hook;
 use Friendica\Core\L10n;
 use Friendica\Core\PConfig;
+use Friendica\Core\Renderer;
 
 /* Define the hooks we want to use
  * that is, we have settings, we need to save the settings and we want
@@ -19,17 +21,17 @@ use Friendica\Core\PConfig;
 
 function langfilter_install()
 {
-       Addon::registerHook('content_filter', 'addon/langfilter/langfilter.php', 'langfilter_content_filter', 10);
-       Addon::registerHook('addon_settings', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings');
-       Addon::registerHook('addon_settings_post', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings_post');
+       Hook::register('prepare_body_content_filter', 'addon/langfilter/langfilter.php', 'langfilter_prepare_body_content_filter', 10);
+       Hook::register('addon_settings', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings');
+       Hook::register('addon_settings_post', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings_post');
 }
 
 function langfilter_uninstall()
 {
-       Addon::unregisterHook('content_filter', 'addon/langfilter/langfilter.php', 'langfilter_content_filter');
-       Addon::unregisterHook('prepare_body', 'addon/langfilter/langfilter.php', 'langfilter_prepare_body');
-       Addon::unregisterHook('addon_settings', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings');
-       Addon::unregisterHook('addon_settings_post', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings_post');
+       Hook::unregister('prepare_body_content_filter', 'addon/langfilter/langfilter.php', 'langfilter_prepare_body_content_filter');
+       Hook::unregister('prepare_body', 'addon/langfilter/langfilter.php', 'langfilter_prepare_body');
+       Hook::unregister('addon_settings', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings');
+       Hook::unregister('addon_settings_post', 'addon/langfilter/langfilter.php', 'langfilter_addon_settings_post');
 }
 
 /* The settings
@@ -49,12 +51,8 @@ function langfilter_addon_settings(App $a, &$s)
        $minconfidence  = PConfig::get(local_user(), 'langfilter', 'minconfidence') * 100;
        $minlength      = PConfig::get(local_user(), 'langfilter', 'minlength');
 
-       if (!$languages) {
-               $languages = 'en,de,fr,it,es';
-       }
-
-       $t = get_markup_template("settings.tpl", "addon/langfilter/");
-       $s .= replace_macros($t, [
+       $t = Renderer::getMarkupTemplate("settings.tpl", "addon/langfilter/");
+       $s .= Renderer::replaceMacros($t, [
                '$title'         => L10n::t("Language Filter"),
                '$intro'         => L10n::t('This addon tries to identify the language posts are writen in. If it does not match any language specifed below, posts will be hidden by collapsing them.'),
                '$enabled'       => ['langfilter_enable', L10n::t('Use the language filter'), $enable_checked, ''],
@@ -79,9 +77,9 @@ function langfilter_addon_settings_post(App $a, &$b)
                return;
        }
 
-       if ($_POST['langfilter-settings-submit']) {
+       if (!empty($_POST['langfilter-settings-submit'])) {
                PConfig::set(local_user(), 'langfilter', 'languages', trim($_POST['langfilter_languages']));
-               $enable = ((x($_POST, 'langfilter_enable')) ? intval($_POST['langfilter_enable']) : 0);
+               $enable = (!empty($_POST['langfilter_enable']) ? intval($_POST['langfilter_enable']) : 0);
                $disable = 1 - $enable;
                PConfig::set(local_user(), 'langfilter', 'disable', $disable);
                $minconfidence = 0 + $_POST['langfilter_minconfidence'];
@@ -97,7 +95,7 @@ function langfilter_addon_settings_post(App $a, &$b)
                $minlength = 0 + $_POST['langfilter_minlength'];
                if (!$minlength) {
                        $minlength = 32;
-               } elseif ($minlengt8h < 0) {
+               } elseif ($minlength < 0) {
                        $minlength = 32;
                }
                PConfig::set(local_user(), 'langfilter', 'minlength', $minlength);
@@ -115,7 +113,7 @@ function langfilter_addon_settings_post(App $a, &$b)
  *     expand it again.
  */
 
-function langfilter_content_filter(App $a, &$hook_data)
+function langfilter_prepare_body_content_filter(App $a, &$hook_data)
 {
        $logged_user = local_user();
        if (!$logged_user) {
@@ -124,7 +122,7 @@ function langfilter_content_filter(App $a, &$hook_data)
 
        // Never filter own messages
        // TODO: find a better way to extract this
-       $logged_user_profile = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
+       $logged_user_profile = $a->getBaseURL() . '/profile/' . $a->user['nickname'];
        if ($logged_user_profile == $hook_data['item']['author-link']) {
                return;
        }
@@ -134,12 +132,15 @@ function langfilter_content_filter(App $a, &$hook_data)
                return;
        }
 
+       $naked_body = BBCode::toPlaintext($hook_data['item']['body'], false);
+
        // Don't filter if body lenght is below minimum
-       $minlen = PConfig::get(local_user(), 'langfilter', 'minlength');
+       $minlen = PConfig::get(local_user(), 'langfilter', 'minlength', 32);
        if (!$minlen) {
                $minlen = 32;
        }
-       if (strlen($hook_data['item']['body']) < $minlen) {
+
+       if (strlen($naked_body) < $minlen) {
                return;
        }
 
@@ -153,27 +154,44 @@ function langfilter_content_filter(App $a, &$hook_data)
        $read_languages_array = explode(',', $read_languages_string);
 
        // Extract the language of the post
-       $opts = $hook_data['item']['postopts'];
-       if (!$opts) {
-               // no options associated to post
-               return;
-       }
+       if (!empty($hook_data['item']['language'])) {
+               $languages = json_decode($hook_data['item']['language'], true);
+               if (!is_array($languages)) {
+                       return;
+               }
 
-       if (!preg_match('/\blang=([^;]*);([^:]*)/', $opts, $matches)) {
-               // no lang options associated to post
-               return;
-       }
+               foreach ($languages as $iso2 => $confidence) {
+                       break;
+               }
 
-       $lang = $matches[1];
-       $confidence = $matches[2];
+               if (empty($iso2)) {
+                       return;
+               }
+
+               $lang = Text_LanguageDetect_ISO639::code2ToName($iso2);
+       } else {
+               $opts = $hook_data['item']['postopts'];
+               if (!$opts) {
+                       // no options associated to post
+                       return;
+               }
+
+               if (!preg_match('/\blang=([^;]*);([^:]*)/', $opts, $matches)) {
+                       // no lang options associated to post
+                       return;
+               }
+
+               $lang = $matches[1];
+               $confidence = $matches[2];
+
+               $iso2 = Text_LanguageDetect_ISO639::nameToCode2($lang);
+       }
 
        // Do not filter if language detection confidence is too low
        if ($minconfidence && $confidence < $minconfidence) {
                return;
        }
 
-       $iso2 = Text_LanguageDetect_ISO639::nameToCode2($lang);
-
        if (!$iso2) {
                return;
        }