]> git.mxchange.org Git - friendica-addons.git/blob - blockem/blockem.php
Merge pull request #164 from tobiasd/20131119
[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 .= '<div class="settings-block">';
53     $s .= '<h3>' . t('"Blockem" Settings') . '</h3>';
54     $s .= '<div id="blockem-wrapper">';
55     $s .= '<label id="blockem-label" for="blockem-words">' . t('Comma separated profile URLS to block') . ' </label>';
56     $s .= '<textarea id="blockem-words" type="text" name="blockem-words" >' . htmlspecialchars($words) . '</textarea>';
57     $s .= '</div><div class="clear"></div>';
58
59     $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="blockem-submit" name="blockem-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';
60
61         return;
62
63 }
64
65 function blockem_addon_settings_post(&$a,&$b) {
66
67         if(! local_user())
68                 return;
69
70         if($_POST['blockem-submit']) {
71                 set_pconfig(local_user(),'blockem','words',trim($_POST['blockem-words']));
72                 info( t('BLOCKEM Settings saved.') . EOL);
73         }
74 }
75
76
77 function blockem_enotify_store(&$a,&$b) {
78
79         $words = get_pconfig($b['uid'],'blockem','words');
80         if($words) {
81                 $arr = explode(',',$words);
82         }
83         else {
84                 return;
85         }
86
87         $found = false;
88         if(count($arr)) {
89                 foreach($arr as $word) {
90                         if(! strlen(trim($word))) {
91                                 continue;
92                         }
93
94                         if(link_compare($b['url'],$word)) {
95                                 $found = true;
96                                 break;
97                         }
98                 }
99         }
100         if($found) {
101                 $b['abort'] = true;
102         }
103 }
104
105 function blockem_prepare_body(&$a,&$b) {
106
107         if(! local_user())
108                 return;
109
110         $words = null;
111         if(local_user()) {
112                 $words = get_pconfig(local_user(),'blockem','words');
113         }
114         if($words) {
115                 $arr = explode(',',$words);
116         }
117         else {
118                 return;
119         }
120
121         $found = false;
122         if(count($arr)) {
123                 foreach($arr as $word) {
124                         if(! strlen(trim($word))) {
125                                 continue;
126                         }
127
128                         if(link_compare($b['item']['author-link'],$word)) {
129                                 $found = true;
130                                 break;
131                         }
132                 }
133         }
134         if($found) {
135                 $rnd = random_string(8);
136                 $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>';  
137         }
138 }
139
140
141 function blockem_display_item(&$a,&$b) {
142         if(strstr($b['output']['body'],'id="blockem-wrap-'))
143                 $b['output']['thumb'] = $a->get_baseurl() . "/images/person-80.jpg";
144 }
145
146
147 function blockem_conversation_start(&$a,&$b) {
148
149         if(! local_user())
150                 return;
151
152         $words = get_pconfig(local_user(),'blockem','words');
153         if($words) {
154                 $a->data['blockem'] = explode(',',$words);
155         }
156         $a->page['htmlhead'] .= <<< EOT
157
158 <script>
159 function blockemBlock(author) {
160         $.get('blockem?block=' +author, function(data) {
161                 location.reload(true);
162         });
163 }
164 function blockemUnblock(author) {
165         $.get('blockem?unblock=' +author, function(data) {
166                 location.reload(true);
167         });
168 }
169 </script>
170
171 EOT;
172
173 }
174
175 function blockem_item_photo_menu(&$a,&$b) {
176
177         if((! local_user()) || ($b['item']['self']))
178                 return;
179
180         $blocked = false;
181         $author = $b['item']['author-link'];
182         if(is_array($a->data['blockem'])) {
183                 foreach($a->data['blockem'] as $bloke) {
184                         if(link_compare($bloke,$author)) {
185                                 $blocked = true;
186                                 break;
187                         }
188                 }
189         }
190         if($blocked)
191                 $b['menu'][ t('Unblock Author')] = 'javascript:blockemUnblock(\'' . $author . '\');';
192         else
193                 $b['menu'][ t('Block Author')] = 'javascript:blockemBlock(\'' . $author . '\');';
194 }
195
196 function blockem_module() {}
197
198
199 function blockem_init(&$a) {
200
201         if(! local_user())
202                 return;
203
204         $words = get_pconfig(local_user(),'blockem','words');
205
206         if(array_key_exists('block',$_GET) && $_GET['block']) {
207                 if(strlen($words))
208                         $words .= ',';
209                 $words .= trim($_GET['block']);
210         }
211         if(array_key_exists('unblock',$_GET) && $_GET['unblock']) {
212                 $arr = explode(',',$words);
213                 $newarr = array();
214
215                 if(count($arr)) {
216                         foreach($arr as $x) {
217                                 if(! link_compare(trim($x),trim($_GET['unblock'])))
218                                         $newarr[] = $x;
219                         }
220                 }
221                 $words = implode(',',$newarr);
222         }
223
224         set_pconfig(local_user(),'blockem','words',$words);
225         info( t('blockem settings updated') . EOL );
226         killme();
227 }