]> git.mxchange.org Git - friendica.git/blob - include/acl_selectors.php
Merge pull request #552 from annando/master
[friendica.git] / include / acl_selectors.php
1 <?php
2 /**
3  * 
4  */
5
6 /**
7  * @package acl_selectors 
8  */
9 function group_select($selname,$selclass,$preselected = false,$size = 4) {
10
11         $a = get_app();
12
13         $o = '';
14
15         $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" >\r\n";
16
17         $r = q("SELECT * FROM `group` WHERE `deleted` = 0 AND `uid` = %d ORDER BY `name` ASC",
18                 intval(local_user())
19         );
20
21
22         $arr = array('group' => $r, 'entry' => $o);
23
24         // e.g. 'network_pre_group_deny', 'profile_pre_group_allow'
25
26         call_hooks($a->module . '_pre_' . $selname, $arr);
27
28         if(count($r)) {
29                 foreach($r as $rr) {
30                         if((is_array($preselected)) && in_array($rr['id'], $preselected))
31                                 $selected = " selected=\"selected\" ";
32                         else
33                                 $selected = '';
34                         $trimmed = mb_substr($rr['name'],0,12);
35
36                         $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}\" >$trimmed</option>\r\n";
37                 }
38         
39         }
40         $o .= "</select>\r\n";
41
42         call_hooks($a->module . '_post_' . $selname, $o);
43
44
45         return $o;
46 }
47
48
49 function contact_selector($selname, $selclass, $preselected = false, $options) {
50
51         $a = get_app();
52
53         $mutual = false;
54         $networks = null;
55         $single = false;
56         $exclude = false;
57         $size = 4;
58
59         if(is_array($options)) {
60                 if(x($options,'size'))
61                         $size = $options['size'];
62
63                 if(x($options,'mutual_friends'))
64                         $mutual = true;
65                 if(x($options,'single'))
66                         $single = true;
67                 if(x($options,'multiple'))
68                         $single = false;
69                 if(x($options,'exclude'))
70                         $exclude = $options['exclude'];
71
72                 if(x($options,'networks')) {
73                         switch($options['networks']) {
74                                 case 'DFRN_ONLY':
75                                         $networks = array('dfrn');
76                                         break;
77                                 case 'PRIVATE':
78                                         if(is_array($a->user) && $a->user['prvnets'])
79                                                 $networks = array('dfrn','mail','dspr');
80                                         else
81                                                 $networks = array('dfrn','face','mail', 'dspr');
82                                         break;
83                                 case 'TWO_WAY':
84                                         if(is_array($a->user) && $a->user['prvnets'])
85                                                 $networks = array('dfrn','mail','dspr');
86                                         else
87                                                 $networks = array('dfrn','face','mail','dspr','stat');
88                                         break;                                  
89                                 default:
90                                         break;
91                         }
92                 }
93         }
94                 
95         $x = array('options' => $options, 'size' => $size, 'single' => $single, 'mutual' => $mutual, 'exclude' => $exclude, 'networks' => $networks);
96
97         call_hooks('contact_select_options', $x);
98
99         $o = '';
100
101         $sql_extra = '';
102
103         if($x['mutual']) {
104                 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
105         }
106
107         if(intval($x['exclude']))
108                 $sql_extra .= sprintf(" AND `id` != %d ", intval($x['exclude']));
109
110         if(is_array($x['networks']) && count($x['networks'])) {
111                 for($y = 0; $y < count($x['networks']) ; $y ++)
112                         $x['networks'][$y] = "'" . dbesc($x['networks'][$y]) . "'";
113                 $str_nets = implode(',',$x['networks']);
114                 $sql_extra .= " AND `network` IN ( $str_nets ) ";
115         }
116         
117         $tabindex = (x($options, 'tabindex') ? "tabindex=\"" . $options["tabindex"] . "\"" : "");
118
119         if($x['single'])
120                 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"" . $x['size'] . "\" $tabindex >\r\n";
121         else 
122                 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
123
124         $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact` 
125                 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
126                 $sql_extra
127                 ORDER BY `name` ASC ",
128                 intval(local_user())
129         );
130
131
132         $arr = array('contact' => $r, 'entry' => $o);
133
134         // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
135
136         call_hooks($a->module . '_pre_' . $selname, $arr);
137
138         if(count($r)) {
139                 foreach($r as $rr) {
140                         if((is_array($preselected)) && in_array($rr['id'], $preselected))
141                                 $selected = " selected=\"selected\" ";
142                         else
143                                 $selected = '';
144
145                         $trimmed = mb_substr($rr['name'],0,20);
146
147                         $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
148                 }
149         
150         }
151
152         $o .= "</select>\r\n";
153
154         call_hooks($a->module . '_post_' . $selname, $o);
155
156         return $o;
157 }
158
159
160
161 function contact_select($selname, $selclass, $preselected = false, $size = 4, $privmail = false, $celeb = false, $privatenet = false, $tabindex = null) {
162
163         $a = get_app();
164
165         $o = '';
166
167         // When used for private messages, we limit correspondence to mutual DFRN/Friendica friends and the selector
168         // to one recipient. By default our selector allows multiple selects amongst all contacts.
169
170         $sql_extra = '';
171
172         if($privmail || $celeb) {
173                 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
174         }
175
176         if($privmail) {
177                 $sql_extra .= " AND `network` IN ( 'dfrn', 'dspr' ) ";
178         }
179         elseif($privatenet) {   
180                 $sql_extra .= " AND `network` IN ( 'dfrn', 'mail', 'face', 'dspr' ) ";
181         }
182
183         $tabindex = ($tabindex > 0 ? "tabindex=\"$tabindex\"" : "");
184
185         if($privmail)
186                 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\" $tabindex >\r\n";
187         else 
188                 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" $tabindex >\r\n";
189
190         $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact` 
191                 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
192                 $sql_extra
193                 ORDER BY `name` ASC ",
194                 intval(local_user())
195         );
196
197
198         $arr = array('contact' => $r, 'entry' => $o);
199
200         // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
201
202         call_hooks($a->module . '_pre_' . $selname, $arr);
203
204         if(count($r)) {
205                 foreach($r as $rr) {
206                         if((is_array($preselected)) && in_array($rr['id'], $preselected))
207                                 $selected = " selected=\"selected\" ";
208                         else
209                                 $selected = '';
210
211                         $trimmed = mb_substr($rr['name'],0,20);
212
213                         $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
214                 }
215         
216         }
217
218         $o .= "</select>\r\n";
219
220         call_hooks($a->module . '_post_' . $selname, $o);
221
222         return $o;
223 }
224
225
226 function fixacl(&$item) {
227         $item = intval(str_replace(array('<','>'),array('',''),$item));
228 }
229
230 function prune_deadguys($arr) {
231
232         if(! $arr)
233                 return $arr;
234         $str = dbesc(implode(',',$arr));
235         $r = q("select id from contact where id in ( " . $str . ") and blocked = 0 and pending = 0 and archive = 0 ");
236         if($r) {
237                 $ret = array();
238                 foreach($r as $rr) 
239                         $ret[] = $rr['id'];
240                 return $ret;
241         }
242         return array();
243 }
244
245
246
247 function populate_acl($user = null,$celeb = false) {
248
249         $allow_cid = $allow_gid = $deny_cid = $deny_gid = false;
250
251         if(is_array($user)) {
252                 $allow_cid = ((strlen($user['allow_cid'])) 
253                         ? explode('><', $user['allow_cid']) : array() );
254                 $allow_gid = ((strlen($user['allow_gid']))
255                         ? explode('><', $user['allow_gid']) : array() );
256                 $deny_cid  = ((strlen($user['deny_cid']))
257                         ? explode('><', $user['deny_cid']) : array() );
258                 $deny_gid  = ((strlen($user['deny_gid']))
259                         ? explode('><', $user['deny_gid']) : array() );
260                 array_walk($allow_cid,'fixacl');
261                 array_walk($allow_gid,'fixacl');
262                 array_walk($deny_cid,'fixacl');
263                 array_walk($deny_gid,'fixacl');
264         }
265
266         $allow_cid = prune_deadguys($allow_cid);
267
268         // We shouldn't need to prune deadguys from the block list. Either way they can't get the message.
269         // Also no point enumerating groups and checking them, that will take place on delivery.
270
271 //      $deny_cid = prune_deadguys($deny_cid);
272
273
274         /*$o = '';
275         $o .= '<div id="acl-wrapper">';
276         $o .= '<div id="acl-permit-outer-wrapper">';
277         $o .= '<div id="acl-permit-text">' . t('Visible To:') . '</div><div id="jot-public">' . t('everybody') . '</div>';
278         $o .= '<div id="acl-permit-text-end"></div>';
279         $o .= '<div id="acl-permit-wrapper">';
280         $o .= '<div id="group_allow_wrapper">';
281         $o .= '<label id="acl-allow-group-label" for="group_allow" >' . t('Groups') . '</label>';
282         $o .= group_select('group_allow','group_allow',$allow_gid);
283         $o .= '</div>';
284         $o .= '<div id="contact_allow_wrapper">';
285         $o .= '<label id="acl-allow-contact-label" for="contact_allow" >' . t('Contacts') . '</label>';
286         $o .= contact_select('contact_allow','contact_allow',$allow_cid,4,false,$celeb,true);
287         $o .= '</div>';
288         $o .= '</div>' . "\r\n";
289         $o .= '<div id="acl-allow-end"></div>' . "\r\n";
290         $o .= '</div>';
291         $o .= '<div id="acl-deny-outer-wrapper">';
292         $o .= '<div id="acl-deny-text">' . t('Except For:') . '</div>';
293         $o .= '<div id="acl-deny-text-end"></div>';
294         $o .= '<div id="acl-deny-wrapper">';
295         $o .= '<div id="group_deny_wrapper" >';
296         $o .= '<label id="acl-deny-group-label" for="group_deny" >' . t('Groups') . '</label>';
297         $o .= group_select('group_deny','group_deny', $deny_gid);
298         $o .= '</div>';
299         $o .= '<div id="contact_deny_wrapper" >';
300         $o .= '<label id="acl-deny-contact-label" for="contact_deny" >' . t('Contacts') . '</label>';
301         $o .= contact_select('contact_deny','contact_deny', $deny_cid,4,false, $celeb,true);
302         $o .= '</div>';
303         $o .= '</div>' . "\r\n";
304         $o .= '<div id="acl-deny-end"></div>' . "\r\n";
305         $o .= '</div>';
306         $o .= '</div>' . "\r\n";
307         $o .= '<div id="acl-wrapper-end"></div>' . "\r\n";*/
308         
309         $tpl = get_markup_template("acl_selector.tpl");
310         $o = replace_macros($tpl, array(
311                 '$showall'=> t("Visible to everybody"),
312                 '$show'          => t("show"),
313                 '$hide'          => t("don't show"),
314                 '$allowcid' => json_encode($allow_cid),
315                 '$allowgid' => json_encode($allow_gid),
316                 '$denycid' => json_encode($deny_cid),
317                 '$denygid' => json_encode($deny_gid),
318         ));
319         
320         
321         return $o;
322
323 }
324