]> git.mxchange.org Git - friendica.git/blob - mod/acl.php
Remove references to include/acl_selectors
[friendica.git] / mod / acl.php
1 <?php
2
3 /* ACL selector json backend */
4
5 use Friendica\App;
6 use Friendica\Content\Widget;
7 use Friendica\Core\Acl;
8 use Friendica\Core\Addon;
9 use Friendica\Database\DBM;
10 use Friendica\Model\Contact;
11
12 require_once 'include/dba.php';
13 require_once 'mod/proxy.php';
14
15 function acl_content(App $a)
16 {
17         if (!local_user()) {
18                 return '';
19         }
20
21         $start = defaults($_REQUEST, 'start', 0);
22         $count = defaults($_REQUEST, 'count', 100);
23         $search = defaults($_REQUEST, 'search', '');
24         $type = defaults($_REQUEST, 'type', '');
25         $conv_id = defaults($_REQUEST, 'conversation', null);
26
27         // For use with jquery.textcomplete for private mail completion
28         if (x($_REQUEST, 'query')) {
29                 if (!$type) {
30                         $type = 'm';
31                 }
32                 $search = $_REQUEST['query'];
33         }
34
35         logger('Searching for ' . $search . ' - type ' . $type, LOGGER_DEBUG);
36
37         if ($search != '') {
38                 $sql_extra = "AND `name` LIKE '%%" . dbesc($search) . "%%'";
39                 $sql_extra2 = "AND (`attag` LIKE '%%" . dbesc($search) . "%%' OR `name` LIKE '%%" . dbesc($search) . "%%' OR `nick` LIKE '%%" . dbesc($search) . "%%')";
40         } else {
41                 /// @TODO Avoid these needless else blocks by putting variable-initialization atop of if()
42                 $sql_extra = $sql_extra2 = '';
43         }
44
45         // count groups and contacts
46         if ($type == '' || $type == 'g') {
47                 $r = q("SELECT COUNT(*) AS g FROM `group` WHERE `deleted` = 0 AND `uid` = %d $sql_extra",
48                         intval(local_user())
49                 );
50                 $group_count = (int) $r[0]['g'];
51         } else {
52                 $group_count = 0;
53         }
54
55         $sql_extra2 .= ' ' . Widget::unavailableNetworks();
56
57         if ($type == '' || $type == 'c') {
58                 // autocomplete for editor mentions
59                 $r = q("SELECT COUNT(*) AS c FROM `contact`
60                                 WHERE `uid` = %d AND NOT `self`
61                                 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
62                                 AND `success_update` >= `failure_update`
63                                 AND `notify` != '' $sql_extra2",
64                         intval(local_user())
65                 );
66                 $contact_count = (int) $r[0]['c'];
67         } elseif ($type == 'f') {
68                 // autocomplete for editor mentions of forums
69                 $r = q("SELECT COUNT(*) AS c FROM `contact`
70                                 WHERE `uid` = %d AND NOT `self`
71                                 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
72                                 AND (`forum` OR `prv`)
73                                 AND `success_update` >= `failure_update`
74                                 AND `notify` != '' $sql_extra2",
75                         intval(local_user())
76                 );
77                 $contact_count = (int) $r[0]['c'];
78         } elseif ($type == 'm') {
79                 // autocomplete for Private Messages
80                 $r = q("SELECT COUNT(*) AS c FROM `contact`
81                                 WHERE `uid` = %d AND NOT `self`
82                                 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
83                                 AND `success_update` >= `failure_update`
84                                 AND `network` IN ('%s', '%s') $sql_extra2",
85                         intval(local_user()),
86                         dbesc(NETWORK_DFRN),
87                         dbesc(NETWORK_DIASPORA)
88                 );
89                 $contact_count = (int) $r[0]['c'];
90         } elseif ($type == 'a') {
91                 // autocomplete for Contacts
92                 $r = q("SELECT COUNT(*) AS c FROM `contact`
93                                 WHERE `uid` = %d AND NOT `self`
94                                 AND NOT `pending` $sql_extra2",
95                         intval(local_user())
96                 );
97                 $contact_count = (int) $r[0]['c'];
98         } else {
99                 $contact_count = 0;
100         }
101
102         $tot = $group_count + $contact_count;
103
104         $groups = [];
105         $contacts = [];
106
107         if ($type == '' || $type == 'g') {
108                 /// @todo We should cache this query.
109                 // This can be done when we can delete cache entries via wildcard
110                 $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') AS uids
111                                 FROM `group`
112                                 INNER JOIN `group_member` ON `group_member`.`gid`=`group`.`id`
113                                 WHERE NOT `group`.`deleted` AND `group`.`uid` = %d
114                                         $sql_extra
115                                 GROUP BY `group`.`name`, `group`.`id`
116                                 ORDER BY `group`.`name`
117                                 LIMIT %d,%d",
118                         intval(local_user()),
119                         intval($start),
120                         intval($count)
121                 );
122
123                 foreach ($r as $g) {
124                         $groups[] = [
125                                 'type' => 'g',
126                                 'photo' => 'images/twopeople.png',
127                                 'name' => htmlentities($g['name']),
128                                 'id' => intval($g['id']),
129                                 'uids' => array_map('intval', explode(',', $g['uids'])),
130                                 'link' => '',
131                                 'forum' => '0'
132                         ];
133                 }
134                 if ((count($groups) > 0) && ($search == '')) {
135                         $groups[] = ['separator' => true];
136                 }
137         }
138
139         if ($type == '') {
140                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv`, (`prv` OR `forum`) AS `frm` FROM `contact`
141                         WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
142                         AND `success_update` >= `failure_update` AND NOT (`network` IN ('%s', '%s'))
143                         $sql_extra2
144                         ORDER BY `name` ASC ",
145                         intval(local_user()),
146                         dbesc(NETWORK_OSTATUS),
147                         dbesc(NETWORK_STATUSNET)
148                 );
149         } elseif ($type == 'c') {
150                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv` FROM `contact`
151                         WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
152                         AND `success_update` >= `failure_update` AND NOT (`network` IN ('%s'))
153                         $sql_extra2
154                         ORDER BY `name` ASC ",
155                         intval(local_user()),
156                         dbesc(NETWORK_STATUSNET)
157                 );
158         } elseif ($type == 'f') {
159                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv` FROM `contact`
160                         WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
161                         AND `success_update` >= `failure_update` AND NOT (`network` IN ('%s'))
162                         AND (`forum` OR `prv`)
163                         $sql_extra2
164                         ORDER BY `name` ASC ",
165                         intval(local_user()),
166                         dbesc(NETWORK_STATUSNET)
167                 );
168         } elseif ($type == 'm') {
169                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr` FROM `contact`
170                         WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive`
171                         AND `success_update` >= `failure_update` AND `network` IN ('%s', '%s')
172                         $sql_extra2
173                         ORDER BY `name` ASC ",
174                         intval(local_user()),
175                         dbesc(NETWORK_DFRN),
176                         dbesc(NETWORK_DIASPORA)
177                 );
178         } elseif ($type == 'a') {
179                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv` FROM `contact`
180                         WHERE `uid` = %d AND `pending` = 0 AND `success_update` >= `failure_update`
181                         $sql_extra2
182                         ORDER BY `name` ASC ",
183                         intval(local_user())
184                 );
185         } elseif ($type == 'x') {
186                 // autocomplete for global contact search (e.g. navbar search)
187                 $search = notags(trim($_REQUEST['search']));
188                 $mode = $_REQUEST['smode'];
189
190                 $r = Acl::contactAutocomplete($search, $mode);
191
192                 $contacts = [];
193                 foreach ($r as $g) {
194                         $contacts[] = [
195                                 'photo'   => proxy_url($g['photo'], false, PROXY_SIZE_MICRO),
196                                 'name'    => $g['name'],
197                                 'nick'    => (x($g['addr']) ? $g['addr'] : $g['url']),
198                                 'network' => $g['network'],
199                                 'link'    => $g['url'],
200                                 'forum'   => (x($g['community']) ? 1 : 0),
201                         ];
202                 }
203                 $o = [
204                         'start' => $start,
205                         'count' => $count,
206                         'items' => $contacts,
207                 ];
208                 echo json_encode($o);
209                 killme();
210         } else {
211                 $r = [];
212         }
213
214         if (DBM::is_result($r)) {
215                 $forums = [];
216                 foreach ($r as $g) {
217                         $entry = [
218                                 'type'    => 'c',
219                                 'photo'   => proxy_url($g['micro'], false, PROXY_SIZE_MICRO),
220                                 'name'    => htmlentities($g['name']),
221                                 'id'      => intval($g['id']),
222                                 'network' => $g['network'],
223                                 'link'    => $g['url'],
224                                 'nick'    => htmlentities(($g['attag']) ? $g['attag'] : $g['nick']),
225                                 'addr'    => htmlentities(($g['addr']) ? $g['addr'] : $g['url']),
226                                 'forum'   => ((x($g, 'forum') || x($g, 'prv')) ? 1 : 0),
227                         ];
228                         if ($entry['forum']) {
229                                 $forums[] = $entry;
230                         } else {
231                                 $contacts[] = $entry;
232                         }
233                 }
234                 if (count($forums) > 0) {
235                         if ($search == '') {
236                                 $forums[] = ['separator' => true];
237                         }
238                         $contacts = array_merge($forums, $contacts);
239                 }
240         }
241
242         $items = array_merge($groups, $contacts);
243
244         if ($conv_id) {
245                 /*
246                  * if $conv_id is set, get unknown contacts in thread
247                  * but first get known contacts url to filter them out
248                  */
249                 $known_contacts = array_map(function ($i) {
250                         return dbesc($i['link']);
251                 }, $contacts);
252
253                 $unknown_contacts = [];
254                 $r = q("SELECT `author-link`
255                                 FROM `item` WHERE `parent` = %d
256                                         AND (`author-name` LIKE '%%%s%%' OR `author-link` LIKE '%%%s%%')
257                                         AND `author-link` NOT IN ('%s')
258                                 GROUP BY `author-link`, `author-avatar`, `author-name`
259                                 ORDER BY `author-name` ASC
260                                 ",
261                         intval($conv_id),
262                         dbesc($search),
263                         dbesc($search),
264                         implode("', '", $known_contacts)
265                 );
266                 if (DBM::is_result($r)) {
267                         foreach ($r as $row) {
268                                 $contact = Contact::getDetailsByURL($row['author-link']);
269
270                                 if (count($contact) > 0) {
271                                         $unknown_contacts[] = [
272                                                 'type' => 'c',
273                                                 'photo' => proxy_url($contact['micro'], false, PROXY_SIZE_MICRO),
274                                                 'name' => htmlentities($contact['name']),
275                                                 'id' => intval($contact['cid']),
276                                                 'network' => $contact['network'],
277                                                 'link' => $contact['url'],
278                                                 'nick' => htmlentities($contact['nick'] ?: $contact['addr']),
279                                                 'addr' => htmlentities(($contact['addr']) ? $contact['addr'] : $contact['url']),
280                                                 'forum' => $contact['forum']
281                                         ];
282                                 }
283                         }
284                 }
285
286                 $items = array_merge($items, $unknown_contacts);
287                 $tot += count($unknown_contacts);
288         }
289
290         $results = [
291                 'tot'      => $tot,
292                 'start'    => $start,
293                 'count'    => $count,
294                 'groups'   => $groups,
295                 'contacts' => $contacts,
296                 'items'    => $items,
297                 'type'     => $type,
298                 'search'   => $search,
299         ];
300
301         Addon::callHooks('acl_lookup_end', $results);
302
303         $o = [
304                 'tot' => $results['tot'],
305                 'start' => $results['start'],
306                 'count' => $results['count'],
307                 'items' => $results['items'],
308         ];
309
310         echo json_encode($o);
311
312         killme();
313 }