3 * @copyright Copyright (C) 2010-2022, 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\HTTPClient\Client\HttpClientAccept;
27 use Friendica\Network\HTTPException;
28 use Friendica\Object\Search\ContactResult;
29 use Friendica\Object\Search\ResultList;
30 use Friendica\Util\Network;
31 use Friendica\Util\Strings;
34 * Specific class to perform searches for different systems. Currently:
35 * - Probe for contacts
36 * - Search in the local directory
37 * - Search in the global directory
41 const DEFAULT_DIRECTORY = 'https://dir.friendica.social';
43 const TYPE_PEOPLE = 0;
48 * Search a user based on his/her profile address
49 * pattern: @username@domain.tld
51 * @param string $user The user to search for
54 * @throws HTTPException\InternalServerErrorException
55 * @throws \ImagickException
57 public static function getContactsFromProbe(string $user): ResultList
59 $emptyResultList = new ResultList(1, 0, 1);
61 if ((filter_var($user, FILTER_VALIDATE_EMAIL) && Network::isEmailDomainValid($user)) ||
62 (substr(Strings::normaliseLink($user), 0, 7) == 'http://')) {
64 $user_data = Contact::getByURL($user);
65 if (empty($user_data)) {
66 return $emptyResultList;
69 if (!in_array($user_data['network'], Protocol::FEDERATED)) {
70 return $emptyResultList;
73 $contactDetails = Contact::getByURLForUser($user_data['url'] ?? '', Session::getLocalUser());
75 $result = new ContactResult(
76 $user_data['name'] ?? '',
77 $user_data['addr'] ?? '',
78 ($contactDetails['addr'] ?? '') ?: ($user_data['url'] ?? ''),
79 $user_data['url'] ?? '',
80 $user_data['photo'] ?? '',
81 $user_data['network'] ?? '',
82 $contactDetails['id'] ?? 0,
83 $user_data['id'] ?? 0,
84 $user_data['tags'] ?? ''
87 return new ResultList(1, 1, 1, [$result]);
89 return $emptyResultList;
94 * Search in the global directory for occurrences of the search string
96 * @see https://github.com/friendica/friendica-directory/blob/stable/docs/Protocol.md#search
98 * @param string $search
99 * @param int $type specific type of searching
103 * @throws HTTPException\InternalServerErrorException
105 public static function getContactsFromGlobalDirectory(string $search, int $type = self::TYPE_ALL, int $page = 1): ResultList
107 $server = self::getGlobalDirectory();
109 $searchUrl = $server . '/search';
112 case self::TYPE_FORUM:
113 $searchUrl .= '/forum';
115 case self::TYPE_PEOPLE:
116 $searchUrl .= '/people';
119 $searchUrl .= '?q=' . urlencode($search);
122 $searchUrl .= '&page=' . $page;
125 $resultJson = DI::httpClient()->fetch($searchUrl, HttpClientAccept::JSON);
127 $results = json_decode($resultJson, true);
129 $resultList = new ResultList(
130 ($results['page'] ?? 0) ?: 1,
131 $results['count'] ?? 0,
132 ($results['itemsperpage'] ?? 0) ?: 30
135 $profiles = $results['profiles'] ?? [];
137 foreach ($profiles as $profile) {
138 $profile_url = $profile['profile_url'] ?? '';
139 $contactDetails = Contact::getByURLForUser($profile_url, Session::getLocalUser());
141 $result = new ContactResult(
142 $profile['name'] ?? '',
143 $profile['addr'] ?? '',
144 ($contactDetails['addr'] ?? '') ?: $profile_url,
146 $profile['photo'] ?? '',
148 $contactDetails['cid'] ?? 0,
150 $profile['tags'] ?? ''
153 $resultList->addResult($result);
160 * Search in the local database for occurrences of the search string
162 * @param string $search
165 * @param int $itemPage
168 * @throws HTTPException\InternalServerErrorException
170 public static function getContactsFromLocalDirectory(string $search, int $type = self::TYPE_ALL, int $start = 0, int $itemPage = 80): ResultList
172 Logger::info('Searching', ['search' => $search, 'type' => $type, 'start' => $start, 'itempage' => $itemPage]);
174 $contacts = Contact::searchByName($search, $type == self::TYPE_FORUM ? 'community' : '');
176 $resultList = new ResultList($start, $itemPage, count($contacts));
178 foreach ($contacts as $contact) {
179 $result = new ContactResult(
186 $contact['cid'] ?? 0,
187 $contact['zid'] ?? 0,
191 $resultList->addResult($result);
194 // Add found profiles from the global directory to the local directory
195 Worker::add(Worker::PRIORITY_LOW, 'SearchDirectory', $search);
201 * Searching for contacts for autocompletion
203 * @param string $search Name or part of a name or nick
204 * @param string $mode Search mode (e.g. "community")
205 * @param int $page Page number (starts at 1)
207 * @return array with the search results or empty if error or nothing found
208 * @throws HTTPException\InternalServerErrorException
210 public static function searchContact(string $search, string $mode, int $page = 1): array
212 Logger::info('Searching', ['search' => $search, 'mode' => $mode, 'page' => $page]);
214 if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
218 // don't search if search term has less than 2 characters
219 if (!$search || mb_strlen($search) < 2) {
223 if (substr($search, 0, 1) === '@') {
224 $search = substr($search, 1);
227 // check if searching in the local global contact table is enabled
228 if (DI::config()->get('system', 'poco_local_search')) {
229 $return = Contact::searchByName($search, $mode);
231 $p = $page > 1 ? 'p=' . $page : '';
232 $curlResult = DI::httpClient()->get(self::getGlobalDirectory() . '/search/people?' . $p . '&q=' . urlencode($search), HttpClientAccept::JSON);
233 if ($curlResult->isSuccess()) {
234 $searchResult = json_decode($curlResult->getBody(), true);
235 if (!empty($searchResult['profiles'])) {
236 $return = $searchResult['profiles'];
241 return $return ?? [];
245 * Returns the global directory name, used in this node
249 public static function getGlobalDirectory(): string
251 return DI::config()->get('system', 'directory', self::DEFAULT_DIRECTORY);
255 * Return the search path (either fulltext search or tag search)
257 * @param string $search
259 * @return string search path
261 public static function getSearchPath(string $search): string
263 if (substr($search, 0, 1) == '#') {
264 return 'search?tag=' . urlencode(substr($search, 1));
266 return 'search?q=' . urlencode($search);