3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module\Search;
24 use Friendica\BaseModule;
25 use Friendica\Content\Widget;
26 use Friendica\Core\Hook;
27 use Friendica\Core\Logger;
28 use Friendica\Core\Protocol;
29 use Friendica\Core\Search;
30 use Friendica\Database\DBA;
32 use Friendica\Model\Contact;
33 use Friendica\Model\Post;
34 use Friendica\Network\HTTPException;
35 use Friendica\Util\Strings;
38 * ACL selector json backend
40 * @package Friendica\Module\Search
42 class Acl extends BaseModule
44 const TYPE_GLOBAL_CONTACT = 'x';
45 const TYPE_MENTION_CONTACT = 'c';
46 const TYPE_MENTION_GROUP = 'g';
47 const TYPE_MENTION_CONTACT_GROUP = '';
48 const TYPE_MENTION_FORUM = 'f';
49 const TYPE_PRIVATE_MESSAGE = 'm';
50 const TYPE_ANY_CONTACT = 'a';
52 public static function rawContent(array $parameters = [])
55 throw new HTTPException\UnauthorizedException(DI::l10n()->t('You must be logged in to use this module.'));
58 $type = $_REQUEST['type'] ?? self::TYPE_MENTION_CONTACT_GROUP;
59 if ($type === self::TYPE_GLOBAL_CONTACT) {
60 $o = self::globalContactSearch();
62 $o = self::regularContactSearch($type);
69 private static function globalContactSearch()
71 // autocomplete for global contact search (e.g. navbar search)
72 $search = Strings::escapeTags(trim($_REQUEST['search']));
73 $mode = $_REQUEST['smode'];
74 $page = $_REQUEST['page'] ?? 1;
76 $result = Search::searchContact($search, $mode, $page);
79 foreach ($result as $contact) {
81 'photo' => Contact::getMicro($contact, true),
82 'name' => htmlspecialchars($contact['name']),
83 'nick' => $contact['addr'] ?: $contact['url'],
84 'network' => $contact['network'],
85 'link' => $contact['url'],
86 'forum' => $contact['contact-type'] == Contact::TYPE_COMMUNITY,
91 'start' => ($page - 1) * 20,
99 private static function regularContactSearch(string $type)
101 $start = $_REQUEST['start'] ?? 0;
102 $count = $_REQUEST['count'] ?? 100;
103 $search = $_REQUEST['search'] ?? '';
104 $conv_id = $_REQUEST['conversation'] ?? null;
106 // For use with jquery.textcomplete for private mail completion
107 if (!empty($_REQUEST['query'])) {
109 $type = self::TYPE_PRIVATE_MESSAGE;
111 $search = $_REQUEST['query'];
114 Logger::info('ACL {action} - {subaction}', ['module' => 'acl', 'action' => 'content', 'subaction' => 'search', 'search' => $search, 'type' => $type, 'conversation' => $conv_id]);
117 $condition = ["`uid` = ? AND NOT `deleted` AND NOT `pending` AND NOT `archive`", local_user()];
118 $condition_group = ["`uid` = ? AND NOT `deleted`", local_user()];
121 $sql_extra = "AND `name` LIKE '%%" . DBA::escape($search) . "%%'";
122 $condition = DBA::mergeConditions($condition, ["(`attag` LIKE ? OR `name` LIKE ? OR `nick` LIKE ?)",
123 '%' . $search . '%', '%' . $search . '%', '%' . $search . '%']);
124 $condition_group = DBA::mergeConditions($condition_group, ["`name` LIKE ?", '%' . $search . '%']);
127 // count groups and contacts
129 if ($type == self::TYPE_MENTION_CONTACT_GROUP || $type == self::TYPE_MENTION_GROUP) {
130 $group_count = DBA::count('group', $condition_group);
133 $networks = Widget::unavailableNetworks();
134 $condition = DBA::mergeConditions($condition, array_merge(["NOT `network` IN (" . substr(str_repeat("?, ", count($networks)), 0, -2) . ")"], $networks));
137 case self::TYPE_MENTION_CONTACT_GROUP:
138 $condition = DBA::mergeConditions($condition,
139 ["NOT `self` AND NOT `blocked` AND `notify` != ? AND NOT `network` IN (?, ?)", '', Protocol::OSTATUS, Protocol::STATUSNET
143 case self::TYPE_MENTION_CONTACT:
144 $condition = DBA::mergeConditions($condition,
145 ["NOT `self` AND NOT `blocked` AND `notify` != ? AND `network` != ?", '', Protocol::STATUSNET
149 case self::TYPE_MENTION_FORUM:
150 $condition = DBA::mergeConditions($condition,
151 ["NOT `self` AND NOT `blocked` AND `notify` != ? AND `contact-type` = ?", '', Contact::TYPE_COMMUNITY
155 case self::TYPE_PRIVATE_MESSAGE:
156 $condition = DBA::mergeConditions($condition,
157 ["NOT `self` AND NOT `blocked` AND `notify` != ? AND `network` IN (?, ?, ?)", '', Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA
162 $contact_count = DBA::count('contact', $condition);
164 $tot = $group_count + $contact_count;
169 if ($type == self::TYPE_MENTION_CONTACT_GROUP || $type == self::TYPE_MENTION_GROUP) {
170 /// @todo We should cache this query.
171 // This can be done when we can delete cache entries via wildcard
172 $r = DBA::toArray(DBA::p("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') AS uids
174 INNER JOIN `group_member` ON `group_member`.`gid`=`group`.`id`
175 WHERE NOT `group`.`deleted` AND `group`.`uid` = ?
177 GROUP BY `group`.`name`, `group`.`id`
178 ORDER BY `group`.`name`
188 'photo' => 'images/twopeople.png',
189 'name' => htmlspecialchars($g['name']),
190 'id' => intval($g['id']),
191 'uids' => array_map('intval', explode(',', $g['uids'])),
196 if ((count($groups) > 0) && ($search == '')) {
197 $groups[] = ['separator' => true];
202 if ($type != self::TYPE_MENTION_GROUP) {
203 $r = Contact::selectToArray([], $condition, ['order' => ['name']]);
206 if (DBA::isResult($r)) {
211 'photo' => Contact::getMicro($g),
212 'name' => htmlspecialchars($g['name']),
213 'id' => intval($g['id']),
214 'network' => $g['network'],
216 'nick' => htmlentities(($g['attag'] ?? '') ?: $g['nick']),
217 'addr' => htmlentities(($g['addr'] ?? '') ?: $g['url']),
218 'forum' => $g['contact-type'] == Contact::TYPE_COMMUNITY,
220 if ($entry['forum']) {
223 $contacts[] = $entry;
226 if (count($forums) > 0) {
228 $forums[] = ['separator' => true];
230 $contacts = array_merge($forums, $contacts);
234 $items = array_merge($groups, $contacts);
237 // In multi threaded posts the conv_id is not the parent of the whole thread
238 $parent_item = Post::selectFirst(['parent'], ['id' => $conv_id]);
239 if (DBA::isResult($parent_item)) {
240 $conv_id = $parent_item['parent'];
244 * if $conv_id is set, get unknown contacts in thread
245 * but first get known contacts url to filter them out
247 $known_contacts = array_map(function ($i) {
251 $unknown_contacts = [];
253 $condition = ["`parent` = ?", $conv_id];
254 $params = ['order' => ['author-name' => true]];
255 $authors = Post::selectForUser(local_user(), ['author-link'], $condition, $params);
257 while ($author = Post::fetch($authors)) {
258 $item_authors[$author['author-link']] = $author['author-link'];
260 DBA::close($authors);
262 foreach ($item_authors as $author) {
263 if (in_array($author, $known_contacts)) {
267 $contact = Contact::getByURL($author, false, ['micro', 'name', 'id', 'network', 'nick', 'addr', 'url', 'forum', 'avatar']);
269 if (count($contact) > 0) {
270 $unknown_contacts[] = [
272 'photo' => Contact::getMicro($contact),
273 'name' => htmlspecialchars($contact['name']),
274 'id' => intval($contact['id']),
275 'network' => $contact['network'],
276 'link' => $contact['url'],
277 'nick' => htmlentities(($contact['nick'] ?? '') ?: $contact['addr']),
278 'addr' => htmlentities(($contact['addr'] ?? '') ?: $contact['url']),
279 'forum' => $contact['forum']
284 $items = array_merge($items, $unknown_contacts);
285 $tot += count($unknown_contacts);
293 'contacts' => $contacts,
299 Hook::callAll('acl_lookup_end', $results);
302 'tot' => $results['tot'],
303 'start' => $results['start'],
304 'count' => $results['count'],
305 'items' => $results['items'],