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