]> git.mxchange.org Git - friendica-addons.git/blob - superblock/superblock.php
Woodpecker: Update PHP version
[friendica-addons.git] / superblock / superblock.php
1 <?php
2 /**
3  * Name: superblock
4  * Description: block people
5  * Version: 1.0
6  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
7  *
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 function superblock_install()
17 {
18         Hook::register('addon_settings', 'addon/superblock/superblock.php', 'superblock_addon_settings');
19         Hook::register('addon_settings_post', 'addon/superblock/superblock.php', 'superblock_addon_settings_post');
20         Hook::register('conversation_start', 'addon/superblock/superblock.php', 'superblock_conversation_start');
21         Hook::register('item_photo_menu', 'addon/superblock/superblock.php', 'superblock_item_photo_menu');
22         Hook::register('enotify_store', 'addon/superblock/superblock.php', 'superblock_enotify_store');
23 }
24
25 function superblock_addon_settings(App &$a, array &$data)
26 {
27         if (!DI::userSession()->getLocalUserId()) {
28                 return;
29         }
30
31         $blocked = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'blocked', '');
32
33         $t    = Renderer::getMarkupTemplate('settings.tpl', 'addon/superblock/');
34         $html = Renderer::replaceMacros($t, [
35                 '$urls' => ['superblock-words', DI::l10n()->t('Comma separated profile URLs to block'), $blocked],
36         ]);
37
38         $data = [
39                 'addon' => 'superblock',
40                 'title' => DI::l10n()->t('Superblock'),
41                 'html'  => $html,
42         ];
43 }
44
45 function superblock_addon_settings_post(App $a, array &$b)
46 {
47         if (!DI::userSession()->getLocalUserId()) {
48                 return;
49         }
50
51         if (!empty($_POST['superblock-submit'])) {
52                 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'system', 'blocked',trim($_POST['superblock-words']));
53         }
54 }
55
56 function superblock_enotify_store(App $a, array &$b)
57 {
58         if (empty($b['uid'])) {
59                 return;
60         }
61
62         $words = DI::pConfig()->get($b['uid'], 'system', 'blocked');
63         if ($words) {
64                 $arr = explode(',', $words);
65         } else {
66                 return;
67         }
68
69         $found = false;
70         if (count($arr)) {
71                 foreach ($arr as $word) {
72                         if (!strlen(trim($word))) {
73                                 continue;
74                         }
75
76                         if (Strings::compareLink($b['url'], $word)) {
77                                 $found = true;
78                                 break;
79                         }
80                 }
81         }
82
83         if ($found) {
84                 // Empty out the fields
85                 $b = [];
86         }
87 }
88
89
90 function superblock_conversation_start(App $a, array &$b)
91 {
92         if (!DI::userSession()->getLocalUserId()) {
93                 return;
94         }
95
96         $words = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'blocked');
97         if ($words) {
98                 $a->data['superblock'] = explode(',', $words);
99         }
100
101         DI::page()['htmlhead'] .= <<< EOT
102 <script>
103 function superblockBlock(author) {
104         $.get('superblock?block=' +author, function(data) {
105                 location.reload(true);
106         });
107 }
108 </script>
109 EOT;
110
111 }
112
113 function superblock_item_photo_menu(App $a, array &$b)
114 {
115         if (!DI::userSession()->getLocalUserId() || $b['item']['self']) {
116                 return;
117         }
118
119         $blocked = false;
120         $author = $b['item']['author-link'];
121         if (!empty($a->data['superblock'])) {
122                 foreach ($a->data['superblock'] as $bloke) {
123                         if (Strings::compareLink($bloke, $author)) {
124                                 $blocked = true;
125                                 break;
126                         }
127                 }
128         }
129
130         $b['menu'][DI::l10n()->t('Block Completely')] = 'javascript:superblockBlock(\'' . $author . '\'); return false;';
131 }
132
133 /**
134  * This is a statement rather than an actual function definition. The simple
135  * existence of this method is checked to figure out if the addon offers a
136  * module.
137  */
138 function superblock_module() {}
139
140 function superblock_init(App $a)
141 {
142         if (!DI::userSession()->getLocalUserId()) {
143                 return;
144         }
145
146         $words = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'blocked');
147
148         if (array_key_exists('block', $_GET) && $_GET['block']) {
149                 if (strlen($words))
150                         $words .= ',';
151                 $words .= trim($_GET['block']);
152         }
153
154         DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'system', 'blocked', $words);
155         exit();
156 }