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