]> git.mxchange.org Git - friendica-addons.git/blob - blockem/blockem.php
Merge pull request #1240 from tobiasd/20220308-pl
[friendica-addons.git] / blockem / blockem.php
1 <?php
2 /**
3  * Name: blockem
4  * Description: Allows users to hide content by collapsing posts and replies.
5  * Version: 1.0
6  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
7  * Author: Roland Haeder <https://f.haeder.net/roland>
8  */
9
10 use Friendica\App;
11 use Friendica\Core\Hook;
12 use Friendica\Core\Renderer;
13 use Friendica\DI;
14 use Friendica\Util\Strings;
15
16 global $blockem_words;
17
18 function blockem_install()
19 {
20         Hook::register('prepare_body_content_filter', 'addon/blockem/blockem.php', 'blockem_prepare_body_content_filter');
21         Hook::register('display_item'               , 'addon/blockem/blockem.php', 'blockem_display_item');
22         Hook::register('addon_settings'             , 'addon/blockem/blockem.php', 'blockem_addon_settings');
23         Hook::register('addon_settings_post'        , 'addon/blockem/blockem.php', 'blockem_addon_settings_post');
24         Hook::register('conversation_start'         , 'addon/blockem/blockem.php', 'blockem_conversation_start');
25         Hook::register('item_photo_menu'            , 'addon/blockem/blockem.php', 'blockem_item_photo_menu');
26         Hook::register('enotify_store'              , 'addon/blockem/blockem.php', 'blockem_enotify_store');
27 }
28
29 function blockem_addon_settings(App $a, array &$data)
30 {
31         if (!local_user()) {
32                 return;
33         }
34
35         $words   = DI::pConfig()->get(local_user(), 'blockem', 'words', '');
36
37         $t    = Renderer::getMarkupTemplate('settings.tpl', 'addon/blockem/');
38         $html = Renderer::replaceMacros($t, [
39                 '$info'    => DI::l10n()->t("Hides user's content by collapsing posts. Also replaces their avatar with generic image."),
40                 '$words'   => ['blockem-words', DI::l10n()->t('Comma separated profile URLS:'), $words],
41         ]);
42
43         $data = [
44                 'addon' => 'blockem',
45                 'title' => DI::l10n()->t('Blockem'),
46                 'html'  => $html,
47         ];
48 }
49
50 function blockem_addon_settings_post(App $a, array &$b)
51 {
52         if (!local_user()) {
53                 return;
54         }
55
56         if (!empty($_POST['blockem-submit'])) {
57                 DI::pConfig()->set(local_user(), 'blockem', 'words', trim($_POST['blockem-words']));
58         }
59 }
60
61 function blockem_enotify_store(App $a, array &$b)
62 {
63         $words = DI::pConfig()->get($b['uid'], 'blockem', 'words');
64
65         if ($words) {
66                 $arr = explode(',', $words);
67         } else {
68                 return;
69         }
70
71         $found = false;
72
73         if (count($arr)) {
74                 foreach ($arr as $word) {
75                         if (!strlen(trim($word))) {
76                                 continue;
77                         }
78
79                         if (Strings::compareLink($b['url'], $word)) {
80                                 $found = true;
81                                 break;
82                         }
83                 }
84         }
85
86         if ($found) {
87                 // empty out the fields
88                 $b = [];
89         }
90 }
91
92 function blockem_prepare_body_content_filter(App $a, array &$hook_data)
93 {
94         if (!local_user()) {
95                 return;
96         }
97
98         $profiles_string = null;
99
100         if (local_user()) {
101                 $profiles_string = DI::pConfig()->get(local_user(), 'blockem', 'words');
102         }
103
104         if ($profiles_string) {
105                 $profiles_array = explode(',', $profiles_string);
106         } else {
107                 return;
108         }
109
110         $found = false;
111
112         foreach ($profiles_array as $word) {
113                 if (Strings::compareLink($hook_data['item']['author-link'], trim($word))) {
114                         $found = true;
115                         break;
116                 }
117         }
118
119         if ($found) {
120                 $hook_data['filter_reasons'][] = DI::l10n()->t('Filtered user: %s', $hook_data['item']['author-name']);
121         }
122 }
123
124 function blockem_display_item(App $a, array &$b = null)
125 {
126         if (!empty($b['output']['body']) && strstr($b['output']['body'], 'id="blockem-wrap-')) {
127                 $b['output']['thumb'] = DI::baseUrl()->get() . "/images/person-80.jpg";
128         }
129 }
130
131 function blockem_conversation_start(App $a, array &$b)
132 {
133         global $blockem_words;
134
135         if (!local_user()) {
136                 return;
137         }
138
139         $words = DI::pConfig()->get(local_user(), 'blockem', 'words');
140
141         if ($words) {
142                 $blockem_words = explode(',', $words);
143         }
144
145         DI::page()['htmlhead'] .= <<< EOT
146
147 <script>
148 function blockemBlock(author) {
149         $.get('blockem?block=' +author, function(data) {
150                 location.reload(true);
151         });
152 }
153 function blockemUnblock(author) {
154         $.get('blockem?unblock=' +author, function(data) {
155                 location.reload(true);
156         });
157 }
158 </script>
159
160 EOT;
161 }
162
163 function blockem_item_photo_menu(App $a, array &$b)
164 {
165         global $blockem_words;
166
167         if (!local_user() || $b['item']['self']) {
168                 return;
169         }
170
171         $blocked = false;
172         $author = $b['item']['author-link'];
173
174         if (!empty($blockem_words)) {
175                 foreach($blockem_words as $bloke) {
176                         if (Strings::compareLink($bloke,$author)) {
177                                 $blocked = true;
178                                 break;
179                         }
180                 }
181         }
182         if ($blocked) {
183                 $b['menu'][DI::l10n()->t('Unblock Author')] = 'javascript:blockemUnblock(\'' . $author . '\');';
184         } else {
185                 $b['menu'][DI::l10n()->t('Block Author')] = 'javascript:blockemBlock(\'' . $author . '\');';
186         }
187 }
188
189 function blockem_module()
190 {
191 }
192
193 function blockem_init(App $a)
194 {
195         if (!local_user()) {
196                 return;
197         }
198
199         $words = DI::pConfig()->get(local_user(), 'blockem', 'words');
200
201         if (array_key_exists('block', $_GET) && $_GET['block']) {
202                 if (strlen($words)) {
203                         $words .= ',';
204                 }
205
206                 $words .= trim($_GET['block']);
207         }
208
209         if (array_key_exists('unblock', $_GET) && $_GET['unblock']) {
210                 $arr = explode(',',$words);
211                 $newarr = [];
212
213                 if (count($arr)) {
214                         foreach ($arr as $x) {
215                                 if (!Strings::compareLink(trim($x), trim($_GET['unblock']))) {
216                                         $newarr[] = $x;
217                                 }
218                         }
219                 }
220
221                 $words = implode(',', $newarr);
222         }
223
224         DI::pConfig()->set(local_user(), 'blockem', 'words', $words);
225         exit();
226 }