blockem addon ES translation updated THX Senex Petrovic
[friendica-addons.git/.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\DI;
13 use Friendica\Util\Strings;
14
15 function blockem_install()
16 {
17         Hook::register('prepare_body_content_filter', 'addon/blockem/blockem.php', 'blockem_prepare_body_content_filter');
18         Hook::register('display_item'               , 'addon/blockem/blockem.php', 'blockem_display_item');
19         Hook::register('addon_settings'             , 'addon/blockem/blockem.php', 'blockem_addon_settings');
20         Hook::register('addon_settings_post'        , 'addon/blockem/blockem.php', 'blockem_addon_settings_post');
21         Hook::register('conversation_start'         , 'addon/blockem/blockem.php', 'blockem_conversation_start');
22         Hook::register('item_photo_menu'            , 'addon/blockem/blockem.php', 'blockem_item_photo_menu');
23         Hook::register('enotify_store'              , 'addon/blockem/blockem.php', 'blockem_enotify_store');
24 }
25
26 function blockem_addon_settings (App $a, &$s)
27 {
28         if (!local_user()) {
29                 return;
30         }
31
32         /* Add our stylesheet to the page so we can make our settings look nice */
33         DI::page()['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . DI::baseUrl()->get() . '/addon/blockem/blockem.css' . '" media="all" />' . "\r\n";
34
35         $words = DI::pConfig()->get(local_user(), 'blockem', 'words');
36
37         if (!$words) {
38                 $words = '';
39         }
40
41         $s .= '<span id="settings_blockem_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_blockem_expanded\'); openClose(\'settings_blockem_inflated\');">';
42         $s .= '<h3>' . DI::l10n()->t('Blockem') . '</h3>';
43         $s .= '</span>';
44         $s .= '<div id="settings_blockem_expanded" class="settings-block" style="display: none;">';
45         $s .= '<span class="fakelink" onclick="openClose(\'settings_blockem_expanded\'); openClose(\'settings_blockem_inflated\');">';
46         $s .= '<h3>' . DI::l10n()->t('Blockem') . '</h3>';
47         $s .= '</span>';
48
49         $s .= '<div id="blockem-wrapper">';
50         $s .= '<div id="blockem-desc">'. DI::l10n()->t("Hides user's content by collapsing posts. Also replaces their avatar with generic image.") . ' </div>';
51         $s .= '<label id="blockem-label" for="blockem-words">' . DI::l10n()->t('Comma separated profile URLS:') . ' </label>';
52         $s .= '<textarea id="blockem-words" type="text" name="blockem-words" >' . htmlspecialchars($words) . '</textarea>';
53         $s .= '</div><div class="clear"></div>';
54
55         $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="blockem-submit" name="blockem-submit" class="settings-submit" value="' . DI::l10n()->t('Save Settings') . '" /></div></div>';
56
57         return;
58
59 }
60
61 function blockem_addon_settings_post(App $a, array &$b)
62 {
63         if (!local_user()) {
64                 return;
65         }
66
67         if (!empty($_POST['blockem-submit'])) {
68                 DI::pConfig()->set(local_user(), 'blockem', 'words', trim($_POST['blockem-words']));
69         }
70 }
71
72 function blockem_enotify_store(App $a, array &$b)
73 {
74         $words = DI::pConfig()->get($b['uid'], 'blockem', 'words');
75
76         if ($words) {
77                 $arr = explode(',', $words);
78         } else {
79                 return;
80         }
81
82         $found = false;
83
84         if (count($arr)) {
85                 foreach ($arr as $word) {
86                         if (!strlen(trim($word))) {
87                                 continue;
88                         }
89
90                         if (Strings::compareLink($b['url'], $word)) {
91                                 $found = true;
92                                 break;
93                         }
94                 }
95         }
96
97         if ($found) {
98                 // empty out the fields
99                 $b = [];
100         }
101 }
102
103 function blockem_prepare_body_content_filter(App $a, array &$hook_data)
104 {
105         if (!local_user()) {
106                 return;
107         }
108
109         $profiles_string = null;
110
111         if (local_user()) {
112                 $profiles_string = DI::pConfig()->get(local_user(), 'blockem', 'words');
113         }
114
115         if ($profiles_string) {
116                 $profiles_array = explode(',', $profiles_string);
117         } else {
118                 return;
119         }
120
121         $found = false;
122
123         foreach ($profiles_array as $word) {
124                 if (Strings::compareLink($hook_data['item']['author-link'], trim($word))) {
125                         $found = true;
126                         break;
127                 }
128         }
129
130         if ($found) {
131                 $hook_data['filter_reasons'][] = DI::l10n()->t('Filtered user: %s', $hook_data['item']['author-name']);
132         }
133 }
134
135 function blockem_display_item(App $a, array &$b = null)
136 {
137         if (!empty($b['output']['body']) && strstr($b['output']['body'], 'id="blockem-wrap-')) {
138                 $b['output']['thumb'] = DI::baseUrl()->get() . "/images/person-80.jpg";
139         }
140 }
141
142 function blockem_conversation_start(App $a, array &$b)
143 {
144         if (!local_user()) {
145                 return;
146         }
147
148         $words = DI::pConfig()->get(local_user(), 'blockem', 'words');
149
150         if ($words) {
151                 $a->data['blockem'] = explode(',', $words);
152         }
153
154         DI::page()['htmlhead'] .= <<< EOT
155
156 <script>
157 function blockemBlock(author) {
158         $.get('blockem?block=' +author, function(data) {
159                 location.reload(true);
160         });
161 }
162 function blockemUnblock(author) {
163         $.get('blockem?unblock=' +author, function(data) {
164                 location.reload(true);
165         });
166 }
167 </script>
168
169 EOT;
170 }
171
172 function blockem_item_photo_menu(App $a, array &$b)
173 {
174         if (!local_user() || $b['item']['self']) {
175                 return;
176         }
177
178         $blocked = false;
179         $author = $b['item']['author-link'];
180
181         if (!empty($a->data['blockem'])) {
182                 foreach($a->data['blockem'] as $bloke) {
183                         if (Strings::compareLink($bloke,$author)) {
184                                 $blocked = true;
185                                 break;
186                         }
187                 }
188         }
189         if ($blocked) {
190                 $b['menu'][DI::l10n()->t('Unblock Author')] = 'javascript:blockemUnblock(\'' . $author . '\');';
191         } else {
192                 $b['menu'][DI::l10n()->t('Block Author')] = 'javascript:blockemBlock(\'' . $author . '\');';
193         }
194 }
195
196 function blockem_module()
197 {
198 }
199
200 function blockem_init(App $a)
201 {
202         if (!local_user()) {
203                 return;
204         }
205
206         $words = DI::pConfig()->get(local_user(), 'blockem', 'words');
207
208         if (array_key_exists('block', $_GET) && $_GET['block']) {
209                 if (strlen($words)) {
210                         $words .= ',';
211                 }
212
213                 $words .= trim($_GET['block']);
214         }
215
216         if (array_key_exists('unblock', $_GET) && $_GET['unblock']) {
217                 $arr = explode(',',$words);
218                 $newarr = [];
219
220                 if (count($arr)) {
221                         foreach ($arr as $x) {
222                                 if (!Strings::compareLink(trim($x), trim($_GET['unblock']))) {
223                                         $newarr[] = $x;
224                                 }
225                         }
226                 }
227
228                 $words = implode(',', $newarr);
229         }
230
231         DI::pConfig()->set(local_user(), 'blockem', 'words', $words);
232         exit();
233 }