]> git.mxchange.org Git - friendica-addons.git/blobdiff - blockem/blockem.php
Merge pull request #1294 from annando/no-boot
[friendica-addons.git] / blockem / blockem.php
old mode 100755 (executable)
new mode 100644 (file)
index 223a199..618f21a
 <?php
-
-
 /**
  * Name: blockem
- * Description: block people
+ * Description: Allows users to hide content by collapsing posts and replies.
  * Version: 1.0
  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
- * 
+ * Author: Roland Haeder <https://f.haeder.net/roland>
  */
 
-function blockem_install() {
-       register_hook('prepare_body', 'addon/blockem/blockem.php', 'blockem_prepare_body');
-       register_hook('display_item', 'addon/blockem/blockem.php', 'blockem_display_item');
-       register_hook('plugin_settings', 'addon/blockem/blockem.php', 'blockem_addon_settings');
-       register_hook('plugin_settings_post', 'addon/blockem/blockem.php', 'blockem_addon_settings_post');
-       register_hook('conversation_start', 'addon/blockem/blockem.php', 'blockem_conversation_start');
-       register_hook('item_photo_menu', 'addon/blockem/blockem.php', 'blockem_item_photo_menu');
-       register_hook('enotify_store', 'addon/blockem/blockem.php', 'blockem_enotify_store' );
+use Friendica\App;
+use Friendica\Core\Hook;
+use Friendica\Core\Renderer;
+use Friendica\Core\Session;
+use Friendica\DI;
+use Friendica\Util\Strings;
+
+global $blockem_words;
+
+function blockem_install()
+{
+       Hook::register('prepare_body_content_filter', 'addon/blockem/blockem.php', 'blockem_prepare_body_content_filter');
+       Hook::register('display_item'               , 'addon/blockem/blockem.php', 'blockem_display_item');
+       Hook::register('addon_settings'             , 'addon/blockem/blockem.php', 'blockem_addon_settings');
+       Hook::register('addon_settings_post'        , 'addon/blockem/blockem.php', 'blockem_addon_settings_post');
+       Hook::register('conversation_start'         , 'addon/blockem/blockem.php', 'blockem_conversation_start');
+       Hook::register('item_photo_menu'            , 'addon/blockem/blockem.php', 'blockem_item_photo_menu');
+       Hook::register('enotify_store'              , 'addon/blockem/blockem.php', 'blockem_enotify_store');
 }
 
-
-function blockem_uninstall() {
-       unregister_hook('prepare_body', 'addon/blockem/blockem.php', 'blockem_prepare_body');
-       unregister_hook('display_item', 'addon/blockem/blockem.php', 'blockem_display_item');
-       unregister_hook('plugin_settings', 'addon/blockem/blockem.php', 'blockem_addon_settings');
-       unregister_hook('plugin_settings_post', 'addon/blockem/blockem.php', 'blockem_addon_settings_post');
-       unregister_hook('conversation_start', 'addon/blockem/blockem.php', 'blockem_conversation_start');
-       unregister_hook('item_photo_menu', 'addon/blockem/blockem.php', 'blockem_item_photo_menu');
-       unregister_hook('enotify_store', 'addon/blockem/blockem.php', 'blockem_enotify_store' );
-
-}
-
-
-
-
-
-function blockem_addon_settings(&$a,&$s) {
-
-       if(! local_user())
+function blockem_addon_settings(App $a, array &$data)
+{
+       if (!Session::getLocalUser()) {
                return;
+       }
 
-    /* Add our stylesheet to the page so we can make our settings look nice */
-
-    $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/blockem/blockem.css' . '" media="all" />' . "\r\n";
-
-
-       $words = get_pconfig(local_user(),'blockem','words');
-       if(! $words)
-               $words = '';
-
-    $s .= '<span id="settings_blockem_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_blockem_expanded\'); openClose(\'settings_blockem_inflated\');">';
-    $s .= '<h3>' . t('"Blockem"') . '</h3>';
-    $s .= '</span>';
-    $s .= '<div id="settings_blockem_expanded" class="settings-block" style="display: none;">';
-    $s .= '<span class="fakelink" onclick="openClose(\'settings_blockem_expanded\'); openClose(\'settings_blockem_inflated\');">';
-    $s .= '<h3>' . t('"Blockem"') . '</h3>';
-    $s .= '</span>';
-
-    $s .= '<div id="blockem-wrapper">';
-    $s .= '<label id="blockem-label" for="blockem-words">' . t('Comma separated profile URLS to block') . ' </label>';
-    $s .= '<textarea id="blockem-words" type="text" name="blockem-words" >' . htmlspecialchars($words) . '</textarea>';
-    $s .= '</div><div class="clear"></div>';
-
-    $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="blockem-submit" name="blockem-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';
+       $words   = DI::pConfig()->get(Session::getLocalUser(), 'blockem', 'words', '');
 
-       return;
+       $t    = Renderer::getMarkupTemplate('settings.tpl', 'addon/blockem/');
+       $html = Renderer::replaceMacros($t, [
+               '$info'    => DI::l10n()->t("Hides user's content by collapsing posts. Also replaces their avatar with generic image."),
+               '$words'   => ['blockem-words', DI::l10n()->t('Comma separated profile URLS:'), $words],
+       ]);
 
+       $data = [
+               'addon' => 'blockem',
+               'title' => DI::l10n()->t('Blockem'),
+               'html'  => $html,
+       ];
 }
 
-function blockem_addon_settings_post(&$a,&$b) {
-
-       if(! local_user())
+function blockem_addon_settings_post(App $a, array &$b)
+{
+       if (!Session::getLocalUser()) {
                return;
+       }
 
-       if($_POST['blockem-submit']) {
-               set_pconfig(local_user(),'blockem','words',trim($_POST['blockem-words']));
-               info( t('BLOCKEM Settings saved.') . EOL);
+       if (!empty($_POST['blockem-submit'])) {
+               DI::pConfig()->set(Session::getLocalUser(), 'blockem', 'words', trim($_POST['blockem-words']));
        }
 }
 
+function blockem_enotify_store(App $a, array &$b)
+{
+       $words = DI::pConfig()->get($b['uid'], 'blockem', 'words');
 
-function blockem_enotify_store(&$a,&$b) {
-
-       $words = get_pconfig($b['uid'],'blockem','words');
-       if($words) {
-               $arr = explode(',',$words);
-       }
-       else {
+       if ($words) {
+               $arr = explode(',', $words);
+       } else {
                return;
        }
 
        $found = false;
-       if(count($arr)) {
-               foreach($arr as $word) {
-                       if(! strlen(trim($word))) {
+
+       if (count($arr)) {
+               foreach ($arr as $word) {
+                       if (!strlen(trim($word))) {
                                continue;
                        }
 
-                       if(link_compare($b['url'],$word)) {
+                       if (Strings::compareLink($b['url'], $word)) {
                                $found = true;
                                break;
                        }
                }
        }
-       if($found) {
-               $b['abort'] = true;
+
+       if ($found) {
+               // empty out the fields
+               $b = [];
        }
 }
 
-function blockem_prepare_body(&$a,&$b) {
-
-       if(! local_user())
+function blockem_prepare_body_content_filter(App $a, array &$hook_data)
+{
+       if (!Session::getLocalUser()) {
                return;
-
-       $words = null;
-       if(local_user()) {
-               $words = get_pconfig(local_user(),'blockem','words');
        }
-       if($words) {
-               $arr = explode(',',$words);
+
+       $profiles_string = null;
+
+       if (Session::getLocalUser()) {
+               $profiles_string = DI::pConfig()->get(Session::getLocalUser(), 'blockem', 'words');
        }
-       else {
+
+       if ($profiles_string) {
+               $profiles_array = explode(',', $profiles_string);
+       } else {
                return;
        }
 
        $found = false;
-       if(count($arr)) {
-               foreach($arr as $word) {
-                       if(! strlen(trim($word))) {
-                               continue;
-                       }
 
-                       if(link_compare($b['item']['author-link'],$word)) {
-                               $found = true;
-                               break;
-                       }
+       foreach ($profiles_array as $word) {
+               if (Strings::compareLink($hook_data['item']['author-link'], trim($word))) {
+                       $found = true;
+                       break;
                }
        }
-       if($found) {
-               $rnd = random_string(8);
-               $b['html'] = '<div id="blockem-wrap-' . $rnd . '" class="fakelink" onclick=openClose(\'blockem-' . $rnd . '\'); >' . sprintf( t('Blocked %s - Click to open/close'),$word ) . '</div><div id="blockem-' . $rnd . '" style="display: none; " >' . $b['html'] . '</div>';  
+
+       if ($found) {
+               $hook_data['filter_reasons'][] = DI::l10n()->t('Filtered user: %s', $hook_data['item']['author-name']);
        }
 }
 
-
-function blockem_display_item(&$a,&$b) {
-       if(strstr($b['output']['body'],'id="blockem-wrap-'))
-               $b['output']['thumb'] = $a->get_baseurl() . "/images/person-80.jpg";
+function blockem_display_item(App $a, array &$b = null)
+{
+       if (!empty($b['output']['body']) && strstr($b['output']['body'], 'id="blockem-wrap-')) {
+               $b['output']['thumb'] = DI::baseUrl()->get() . "/images/person-80.jpg";
+       }
 }
 
+function blockem_conversation_start(App $a, array &$b)
+{
+       global $blockem_words;
 
-function blockem_conversation_start(&$a,&$b) {
-
-       if(! local_user())
+       if (!Session::getLocalUser()) {
                return;
+       }
+
+       $words = DI::pConfig()->get(Session::getLocalUser(), 'blockem', 'words');
 
-       $words = get_pconfig(local_user(),'blockem','words');
-       if($words) {
-               $a->data['blockem'] = explode(',',$words);
+       if ($words) {
+               $blockem_words = explode(',', $words);
        }
-       $a->page['htmlhead'] .= <<< EOT
+
+       DI::page()['htmlhead'] .= <<< EOT
 
 <script>
 function blockemBlock(author) {
@@ -175,59 +159,72 @@ function blockemUnblock(author) {
 </script>
 
 EOT;
-
 }
 
-function blockem_item_photo_menu(&$a,&$b) {
+function blockem_item_photo_menu(App $a, array &$b)
+{
+       global $blockem_words;
 
-       if((! local_user()) || ($b['item']['self']))
+       if (!Session::getLocalUser() || $b['item']['self']) {
                return;
+       }
 
        $blocked = false;
        $author = $b['item']['author-link'];
-       if(is_array($a->data['blockem'])) {
-               foreach($a->data['blockem'] as $bloke) {
-                       if(link_compare($bloke,$author)) {
+
+       if (!empty($blockem_words)) {
+               foreach($blockem_words as $bloke) {
+                       if (Strings::compareLink($bloke,$author)) {
                                $blocked = true;
                                break;
                        }
                }
        }
-       if($blocked)
-               $b['menu'][ t('Unblock Author')] = 'javascript:blockemUnblock(\'' . $author . '\');';
-       else
-               $b['menu'][ t('Block Author')] = 'javascript:blockemBlock(\'' . $author . '\');';
+       if ($blocked) {
+               $b['menu'][DI::l10n()->t('Unblock Author')] = 'javascript:blockemUnblock(\'' . $author . '\');';
+       } else {
+               $b['menu'][DI::l10n()->t('Block Author')] = 'javascript:blockemBlock(\'' . $author . '\');';
+       }
 }
 
+/**
+ * This is a statement rather than an actual function definition. The simple
+ * existence of this method is checked to figure out if the addon offers a
+ * module.
+ */
 function blockem_module() {}
 
-
-function blockem_init(&$a) {
-
-       if(! local_user())
+function blockem_init(App $a)
+{
+       if (!Session::getLocalUser()) {
                return;
+       }
 
-       $words = get_pconfig(local_user(),'blockem','words');
+       $words = DI::pConfig()->get(Session::getLocalUser(), 'blockem', 'words');
 
-       if(array_key_exists('block',$_GET) && $_GET['block']) {
-               if(strlen($words))
+       if (array_key_exists('block', $_GET) && $_GET['block']) {
+               if (strlen($words)) {
                        $words .= ',';
+               }
+
                $words .= trim($_GET['block']);
        }
-       if(array_key_exists('unblock',$_GET) && $_GET['unblock']) {
+
+       if (array_key_exists('unblock', $_GET) && $_GET['unblock']) {
                $arr = explode(',',$words);
-               $newarr = array();
+               $newarr = [];
 
-               if(count($arr)) {
-                       foreach($arr as $x) {
-                               if(! link_compare(trim($x),trim($_GET['unblock'])))
+               if (count($arr)) {
+                       foreach ($arr as $x) {
+                               if (!Strings::compareLink(trim($x), trim($_GET['unblock']))) {
                                        $newarr[] = $x;
+                               }
                        }
                }
-               $words = implode(',',$newarr);
+
+               $words = implode(',', $newarr);
        }
 
-       set_pconfig(local_user(),'blockem','words',$words);
-       info( t('blockem settings updated') . EOL );
-       killme();
+       DI::pConfig()->set(Session::getLocalUser(), 'blockem', 'words', $words);
+       exit();
 }