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\Core;
25 use Friendica\Model\Contact;
26 use Friendica\Network\HTTPException;
27 use Friendica\Object\Search\ContactResult;
28 use Friendica\Object\Search\ResultList;
29 use Friendica\Util\Network;
30 use Friendica\Util\Strings;
33 * Specific class to perform searches for different systems. Currently:
34 * - Probe for contacts
35 * - Search in the local directory
36 * - Search in the global directory
40 const DEFAULT_DIRECTORY = 'https://dir.friendica.social';
42 const TYPE_PEOPLE = 0;
47 * Search a user based on his/her profile address
48 * pattern: @username@domain.tld
50 * @param string $user The user to search for
53 * @throws HTTPException\InternalServerErrorException
54 * @throws \ImagickException
56 public static function getContactsFromProbe($user)
58 $emptyResultList = new ResultList(1, 0, 1);
60 if ((filter_var($user, FILTER_VALIDATE_EMAIL) && Network::isEmailDomainValid($user)) ||
61 (substr(Strings::normaliseLink($user), 0, 7) == "http://")) {
63 $user_data = Contact::getByURL($user);
64 if (empty($user_data)) {
65 return $emptyResultList;
68 if (!in_array($user_data["network"], Protocol::FEDERATED)) {
69 return $emptyResultList;
72 $contactDetails = Contact::getByURLForUser($user_data['url'] ?? '', local_user());
74 $result = new ContactResult(
75 $user_data['name'] ?? '',
76 $user_data['addr'] ?? '',
77 ($contactDetails['addr'] ?? '') ?: ($user_data['url'] ?? ''),
78 $user_data['url'] ?? '',
79 $user_data['photo'] ?? '',
80 $user_data['network'] ?? '',
81 $contactDetails['id'] ?? 0,
82 $user_data['id'] ?? 0,
83 $user_data['tags'] ?? ''
86 return new ResultList(1, 1, 1, [$result]);
88 return $emptyResultList;
93 * Search in the global directory for occurrences of the search string
95 * @see https://github.com/friendica/friendica-directory/blob/stable/docs/Protocol.md#search
97 * @param string $search
98 * @param int $type specific type of searching
102 * @throws HTTPException\InternalServerErrorException
104 public static function getContactsFromGlobalDirectory($search, $type = self::TYPE_ALL, $page = 1)
106 $server = DI::config()->get('system', 'directory', self::DEFAULT_DIRECTORY);
108 $searchUrl = $server . '/search';
111 case self::TYPE_FORUM:
112 $searchUrl .= '/forum';
114 case self::TYPE_PEOPLE:
115 $searchUrl .= '/people';
118 $searchUrl .= '?q=' . urlencode($search);
121 $searchUrl .= '&page=' . $page;
124 $resultJson = DI::httpRequest()->fetch($searchUrl, 0, 'application/json');
126 $results = json_decode($resultJson, true);
128 $resultList = new ResultList(
129 ($results['page'] ?? 0) ?: 1,
130 $results['count'] ?? 0,
131 ($results['itemsperpage'] ?? 0) ?: 30
134 $profiles = $results['profiles'] ?? [];
136 foreach ($profiles as $profile) {
137 $profile_url = $profile['profile_url'] ?? '';
138 $contactDetails = Contact::getByURLForUser($profile_url, local_user());
140 $result = new ContactResult(
141 $profile['name'] ?? '',
142 $profile['addr'] ?? '',
143 ($contactDetails['addr'] ?? '') ?: $profile_url,
145 $profile['photo'] ?? '',
147 $contactDetails['cid'] ?? 0,
149 $profile['tags'] ?? ''
152 $resultList->addResult($result);
159 * Search in the local database for occurrences of the search string
161 * @param string $search
164 * @param int $itemPage
167 * @throws HTTPException\InternalServerErrorException
169 public static function getContactsFromLocalDirectory($search, $type = self::TYPE_ALL, $start = 0, $itemPage = 80)
171 Logger::info('Searching', ['search' => $search, 'type' => $type, 'start' => $start, 'itempage' => $itemPage]);
173 $contacts = Contact::searchByName($search, $type == self::TYPE_FORUM ? 'community' : '');
175 $resultList = new ResultList($start, $itemPage, count($contacts));
177 foreach ($contacts as $contact) {
178 $result = new ContactResult(
185 $contact["cid"] ?? 0,
186 $contact["zid"] ?? 0,
190 $resultList->addResult($result);
193 // Add found profiles from the global directory to the local directory
194 Worker::add(PRIORITY_LOW, 'SearchDirectory', $search);
200 * Searching for contacts for autocompletion
202 * @param string $search Name or part of a name or nick
203 * @param string $mode Search mode (e.g. "community")
204 * @param int $page Page number (starts at 1)
205 * @return array with the search results
206 * @throws HTTPException\InternalServerErrorException
208 public static function searchContact($search, $mode, int $page = 1)
210 Logger::info('Searching', ['search' => $search, 'mode' => $mode, 'page' => $page]);
212 if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
216 // don't search if search term has less than 2 characters
217 if (!$search || mb_strlen($search) < 2) {
221 if (substr($search, 0, 1) === '@') {
222 $search = substr($search, 1);
225 // check if searching in the local global contact table is enabled
226 if (DI::config()->get('system', 'poco_local_search')) {
227 $return = Contact::searchByName($search, $mode);
229 $p = $page > 1 ? 'p=' . $page : '';
230 $curlResult = DI::httpRequest()->get(self::getGlobalDirectory() . '/search/people?' . $p . '&q=' . urlencode($search), ['accept_content' => 'application/json']);
231 if ($curlResult->isSuccess()) {
232 $searchResult = json_decode($curlResult->getBody(), true);
233 if (!empty($searchResult['profiles'])) {
234 $return = $searchResult['profiles'];
239 return $return ?? [];
243 * Returns the global directory name, used in this node
247 public static function getGlobalDirectory()
249 return DI::config()->get('system', 'directory', self::DEFAULT_DIRECTORY);
253 * Return the search path (either fulltext search or tag search)
255 * @param string $search
256 * @return string search path
258 public static function getSearchPath(string $search)
260 if (substr($search, 0, 1) == '#') {
261 return 'search?tag=' . urlencode(substr($search, 1));
263 return 'search?q=' . urlencode($search);