]> git.mxchange.org Git - friendica.git/blob - mod/acl.php
dd5dd90281d0140782d45195dead24039635b8ae
[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 use Friendica\Model\Item;
12
13 require_once 'include/dba.php';
14 require_once 'mod/proxy.php';
15
16 function acl_content(App $a)
17 {
18         if (!local_user()) {
19                 return '';
20         }
21
22         $start   = defaults($_REQUEST, 'start'       , 0);
23         $count   = defaults($_REQUEST, 'count'       , 100);
24         $search  = defaults($_REQUEST, 'search'      , '');
25         $type    = defaults($_REQUEST, 'type'        , '');
26         $conv_id = defaults($_REQUEST, 'conversation', null);
27
28         // For use with jquery.textcomplete for private mail completion
29         if (!empty($_REQUEST['query'])) {
30                 if (!$type) {
31                         $type = 'm';
32                 }
33                 $search = $_REQUEST['query'];
34         }
35
36         logger("Searching for ".$search." - type ".$type." conversation ".$conv_id, LOGGER_DEBUG);
37
38         if ($search != '') {
39                 $sql_extra = "AND `name` LIKE '%%" . dbesc($search) . "%%'";
40                 $sql_extra2 = "AND (`attag` LIKE '%%" . dbesc($search) . "%%' OR `name` LIKE '%%" . dbesc($search) . "%%' OR `nick` LIKE '%%" . dbesc($search) . "%%')";
41         } else {
42                 /// @TODO Avoid these needless else blocks by putting variable-initialization atop of if()
43                 $sql_extra = $sql_extra2 = '';
44         }
45
46         // count groups and contacts
47         $group_count = 0;
48         if ($type == '' || $type == 'g') {
49                 $r = q("SELECT COUNT(*) AS g FROM `group` WHERE `deleted` = 0 AND `uid` = %d $sql_extra",
50                         intval(local_user())
51                 );
52                 $group_count = (int) $r[0]['g'];
53         }
54
55         $sql_extra2 .= ' ' . Widget::unavailableNetworks();
56
57         $contact_count = 0;
58         if ($type == '' || $type == 'c') {
59                 // autocomplete for editor mentions
60                 $r = q("SELECT COUNT(*) AS c FROM `contact`
61                                 WHERE `uid` = %d AND NOT `self`
62                                 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
63                                 AND `success_update` >= `failure_update`
64                                 AND `notify` != '' $sql_extra2",
65                         intval(local_user())
66                 );
67                 $contact_count = (int) $r[0]['c'];
68         } elseif ($type == 'f') {
69                 // autocomplete for editor mentions of forums
70                 $r = q("SELECT COUNT(*) AS c FROM `contact`
71                                 WHERE `uid` = %d AND NOT `self`
72                                 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
73                                 AND (`forum` OR `prv`)
74                                 AND `success_update` >= `failure_update`
75                                 AND `notify` != '' $sql_extra2",
76                         intval(local_user())
77                 );
78                 $contact_count = (int) $r[0]['c'];
79         } elseif ($type == 'm') {
80                 // autocomplete for Private Messages
81                 $r = q("SELECT COUNT(*) AS c FROM `contact`
82                                 WHERE `uid` = %d AND NOT `self`
83                                 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
84                                 AND `success_update` >= `failure_update`
85                                 AND `network` IN ('%s', '%s') $sql_extra2",
86                         intval(local_user()),
87                         dbesc(NETWORK_DFRN),
88                         dbesc(NETWORK_DIASPORA)
89                 );
90                 $contact_count = (int) $r[0]['c'];
91         } elseif ($type == 'a') {
92                 // autocomplete for Contacts
93                 $r = q("SELECT COUNT(*) AS c FROM `contact`
94                                 WHERE `uid` = %d AND NOT `self`
95                                 AND NOT `pending` $sql_extra2",
96                         intval(local_user())
97                 );
98                 $contact_count = (int) $r[0]['c'];
99         }
100
101         $tot = $group_count + $contact_count;
102
103         $groups = [];
104         $contacts = [];
105
106         if ($type == '' || $type == 'g') {
107                 /// @todo We should cache this query.
108                 // This can be done when we can delete cache entries via wildcard
109                 $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') AS uids
110                                 FROM `group`
111                                 INNER JOIN `group_member` ON `group_member`.`gid`=`group`.`id`
112                                 WHERE NOT `group`.`deleted` AND `group`.`uid` = %d
113                                         $sql_extra
114                                 GROUP BY `group`.`name`, `group`.`id`
115                                 ORDER BY `group`.`name`
116                                 LIMIT %d,%d",
117                         intval(local_user()),
118                         intval($start),
119                         intval($count)
120                 );
121
122                 foreach ($r as $g) {
123                         $groups[] = [
124                                 'type'  => 'g',
125                                 'photo' => 'images/twopeople.png',
126                                 'name'  => htmlentities($g['name']),
127                                 'id'    => intval($g['id']),
128                                 'uids'  => array_map('intval', explode(',', $g['uids'])),
129                                 'link'  => '',
130                                 'forum' => '0'
131                         ];
132                 }
133                 if ((count($groups) > 0) && ($search == '')) {
134                         $groups[] = ['separator' => true];
135                 }
136         }
137
138         $r = [];
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'    => defaults($g, 'addr', $g['url']),
198                                 'network' => $g['network'],
199                                 'link'    => $g['url'],
200                                 'forum'   => !empty($g['community']) ? 1 : 0,
201                         ];
202                 }
203                 $o = [
204                         'start' => $start,
205                         'count' => $count,
206                         'items' => $contacts,
207                 ];
208                 echo json_encode($o);
209                 exit;
210         }
211
212         if (DBM::is_result($r)) {
213                 $forums = [];
214                 foreach ($r as $g) {
215                         $entry = [
216                                 'type'    => 'c',
217                                 'photo'   => proxy_url($g['micro'], false, PROXY_SIZE_MICRO),
218                                 'name'    => htmlentities($g['name']),
219                                 'id'      => intval($g['id']),
220                                 'network' => $g['network'],
221                                 'link'    => $g['url'],
222                                 'nick'    => htmlentities(defaults($g, 'attag', $g['nick'])),
223                                 'addr'    => htmlentities(defaults($g, 'addr', $g['url'])),
224                                 'forum'   => !empty($g['forum']) || !empty($g['prv']) ? 1 : 0,
225                         ];
226                         if ($entry['forum']) {
227                                 $forums[] = $entry;
228                         } else {
229                                 $contacts[] = $entry;
230                         }
231                 }
232                 if (count($forums) > 0) {
233                         if ($search == '') {
234                                 $forums[] = ['separator' => true];
235                         }
236                         $contacts = array_merge($forums, $contacts);
237                 }
238         }
239
240         $items = array_merge($groups, $contacts);
241
242         if ($conv_id) {
243                 // In multi threaded posts the conv_id is not the parent of the whole thread
244                 $parent_item = Item::selectFirst(['parent'], ['id' => $conv_id]);
245                 if (DBM::is_result($parent_item)) {
246                         $conv_id = $parent_item['parent'];
247                 }
248
249                 /*
250                  * if $conv_id is set, get unknown contacts in thread
251                  * but first get known contacts url to filter them out
252                  */
253                 $known_contacts = array_map(function ($i) {
254                         return $i['link'];
255                 }, $contacts);
256
257                 $unknown_contacts = [];
258
259                 $condition = ["`parent` = ?", $conv_id];
260                 $params = ['order' => ['author-name' => true]];
261                 $authors = Item::selectForUser(local_user(), ['author-link'], $condition, $params);
262                 $item_authors = [];
263                 while ($author = Item::fetch($authors)) {
264                         $item_authors[$author['author-link']] = $author['author-link'];
265                 }
266                 dba::close($authors);
267
268                 foreach ($item_authors as $author) {
269                         if (in_array($author, $known_contacts)) {
270                                 continue;
271                         }
272
273                         $contact = Contact::getDetailsByURL($author);
274
275                         if (count($contact) > 0) {
276                                 $unknown_contacts[] = [
277                                         'type'    => 'c',
278                                         'photo'   => proxy_url($contact['micro'], false, PROXY_SIZE_MICRO),
279                                         'name'    => htmlentities($contact['name']),
280                                         'id'      => intval($contact['cid']),
281                                         'network' => $contact['network'],
282                                         'link'    => $contact['url'],
283                                         'nick'    => htmlentities(defaults($contact, 'nick', $contact['addr'])),
284                                         'addr'    => htmlentities(defaults($contact, 'addr', $contact['url'])),
285                                         'forum'   => $contact['forum']
286                                 ];
287                         }
288                 }
289
290                 $items = array_merge($items, $unknown_contacts);
291                 $tot += count($unknown_contacts);
292         }
293
294         $results = [
295                 'tot'      => $tot,
296                 'start'    => $start,
297                 'count'    => $count,
298                 'groups'   => $groups,
299                 'contacts' => $contacts,
300                 'items'    => $items,
301                 'type'     => $type,
302                 'search'   => $search,
303         ];
304
305         Addon::callHooks('acl_lookup_end', $results);
306
307         $o = [
308                 'tot'   => $results['tot'],
309                 'start' => $results['start'],
310                 'count' => $results['count'],
311                 'items' => $results['items'],
312         ];
313
314         echo json_encode($o);
315         exit;
316 }