]> git.mxchange.org Git - friendica.git/blob - mod/acl.php
rev update
[friendica.git] / mod / acl.php
1 <?php
2 /* ACL selector json backend */
3
4 require_once("include/acl_selectors.php");
5
6 function acl_init(&$a){
7         if(!local_user())
8                 return "";
9
10
11         $start = (x($_REQUEST,'start')?$_REQUEST['start']:0);
12         $count = (x($_REQUEST,'count')?$_REQUEST['count']:100);
13         $search = (x($_REQUEST,'search')?$_REQUEST['search']:"");
14         $type = (x($_REQUEST,'type')?$_REQUEST['type']:"");
15         
16
17         // For use with jquery.autocomplete for private mail completion
18
19         if(x($_REQUEST,'query') && strlen($_REQUEST['query'])) {
20                 if(! $type)
21                         $type = 'm';
22                 $search = $_REQUEST['query'];
23         }
24
25
26         if ($search!=""){
27                 $sql_extra = "AND `name` LIKE '%%".dbesc($search)."%%'";
28                 $sql_extra2 = "AND (`attag` LIKE '%%".dbesc($search)."%%' OR `name` LIKE '%%".dbesc($search)."%%' OR `nick` LIKE '%%".dbesc($search)."%%')";
29         } else {
30                 $sql_extra = $sql_extra2 = "";
31         }
32         
33         // count groups and contacts
34         if ($type=='' || $type=='g'){
35                 $r = q("SELECT COUNT(`id`) AS g FROM `group` WHERE `deleted` = 0 AND `uid` = %d $sql_extra",
36                         intval(local_user())
37                 );
38                 $group_count = (int)$r[0]['g'];
39         } else {
40                 $group_count = 0;
41         }
42         
43         if ($type=='' || $type=='c'){
44                 $r = q("SELECT COUNT(`id`) AS c FROM `contact` 
45                                 WHERE `uid` = %d AND `self` = 0 
46                                 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
47                                 AND `notify` != '' $sql_extra2" ,
48                         intval(local_user())
49                 );
50                 $contact_count = (int)$r[0]['c'];
51         } 
52         elseif ($type == 'm') {
53
54                 // autocomplete for Private Messages
55
56                 $r = q("SELECT COUNT(`id`) AS c FROM `contact` 
57                                 WHERE `uid` = %d AND `self` = 0 
58                                 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 
59                                 AND `network` IN ('%s','%s','%s') $sql_extra2" ,
60                         intval(local_user()),
61                         dbesc(NETWORK_DFRN),
62                         dbesc(NETWORK_ZOT),
63                         dbesc(NETWORK_DIASPORA)
64                 );
65                 $contact_count = (int)$r[0]['c'];
66
67         }
68         elseif ($type == 'a') {
69
70                 // autocomplete for Contacts
71
72                 $r = q("SELECT COUNT(`id`) AS c FROM `contact` 
73                                 WHERE `uid` = %d AND `self` = 0 
74                                 AND `pending` = 0 $sql_extra2" ,
75                         intval(local_user())
76                 );
77                 $contact_count = (int)$r[0]['c'];
78
79         } else {
80                 $contact_count = 0;
81         }
82         
83         $tot = $group_count+$contact_count;
84         
85         $groups = array();
86         $contacts = array();
87         
88         if ($type=='' || $type=='g'){
89                 
90                 $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') as uids
91                                 FROM `group`,`group_member` 
92                                 WHERE `group`.`deleted` = 0 AND `group`.`uid` = %d 
93                                         AND `group_member`.`gid`=`group`.`id`
94                                         $sql_extra
95                                 GROUP BY `group`.`id`
96                                 ORDER BY `group`.`name` 
97                                 LIMIT %d,%d",
98                         intval(local_user()),
99                         intval($start),
100                         intval($count)
101                 );
102
103                 foreach($r as $g){
104 //              logger('acl: group: ' . $g['name'] . ' members: ' . $g['uids']);                
105                         $groups[] = array(
106                                 "type"  => "g",
107                                 "photo" => "images/twopeople.png",
108                                 "name"  => $g['name'],
109                                 "id"    => intval($g['id']),
110                                 "uids"  => array_map("intval", explode(",",$g['uids'])),
111                                 "link"  => ''
112                         );
113                 }
114         }
115         
116         if ($type=='' || $type=='c'){
117         
118                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact` 
119                         WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
120                         $sql_extra2
121                         ORDER BY `name` ASC ",
122                         intval(local_user())
123                 );
124         }
125         elseif($type == 'm') {
126                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact` 
127                         WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
128                         AND `network` IN ('%s','%s','%s')
129                         $sql_extra2
130                         ORDER BY `name` ASC ",
131                         intval(local_user()),
132                         dbesc(NETWORK_DFRN),
133                         dbesc(NETWORK_ZOT),
134                         dbesc(NETWORK_DIASPORA)
135                 );
136         }
137         elseif($type == 'a') {
138                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact` 
139                         WHERE `uid` = %d AND `pending` = 0
140                         $sql_extra2
141                         ORDER BY `name` ASC ",
142                         intval(local_user())
143                 );
144         }
145         else
146                 $r = array();
147
148
149         if($type == 'm' || $type == 'a') {
150                 $x = array();
151                 $x['query'] = $search;
152                 $x['photos'] = array();
153                 $x['links'] = array();
154                 $x['suggestions'] = array();
155                 $x['data'] = array();
156                 if(count($r)) {
157                         foreach($r as $g) {
158                                 $x['photos'][] = $g['micro'];
159                                 $x['links'][] = $g['url'];
160                                 $x['suggestions'][] = $g['name'];
161                                 $x['data'][] = intval($g['id']);
162                         }
163                 }
164                 echo json_encode($x);
165                 killme();
166         }
167
168         if(count($r)) {
169                 foreach($r as $g){
170                         $contacts[] = array(
171                                 "type"  => "c",
172                                 "photo" => $g['micro'],
173                                 "name"  => $g['name'],
174                                 "id"    => intval($g['id']),
175                                 "network" => $g['network'],
176                                 "link" => $g['url'],
177                                 "nick" => ($g['attag']) ? $g['attag'] : $g['nick'],
178                         );
179                 }                       
180         }
181                 
182         $items = array_merge($groups, $contacts);
183         
184         $o = array(
185                 'tot'   => $tot,
186                 'start' => $start,
187                 'count' => $count,
188                 'items' => $items,
189         );
190         
191         echo json_encode($o);
192
193         killme();
194 }
195
196