3 namespace Friendica\Core;
5 use Friendica\BaseObject;
6 use Friendica\Database\DBA;
7 use Friendica\Model\Contact;
8 use Friendica\Network\HTTPException;
9 use Friendica\Network\Probe;
10 use Friendica\Object\Search\ContactResult;
11 use Friendica\Object\Search\ResultList;
12 use Friendica\Protocol\PortableContact;
13 use Friendica\Util\Network;
14 use Friendica\Util\Strings;
17 * Specific class to perform searches for different systems. Currently:
18 * - Probe for contacts
19 * - Search in the local directory
20 * - Search in the global directory
22 class Search extends BaseObject
24 const DEFAULT_DIRECTORY = 'https://dir.friendica.social';
27 * Search a user based on his/her profile address
28 * pattern: @username@domain.tld
30 * @param string $user The user to search for
32 * @return ResultList|null
33 * @throws HTTPException\InternalServerErrorException
34 * @throws \ImagickException
36 public static function getContactsFromProbe($user)
38 if ((filter_var($user, FILTER_VALIDATE_EMAIL) && Network::isEmailDomainValid($user)) ||
39 (substr(Strings::normaliseLink($user), 0, 7) == "http://")) {
41 $user_data = Probe::uri($user);
42 if (empty($user_data)) {
46 if (!(in_array($user_data["network"], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS, Protocol::DIASPORA]))) {
50 $contactDetails = Contact::getDetailsByURL(defaults($user_data, 'url', ''), local_user());
51 $itemUrl = (($contactDetails["addr"] != "") ? $contactDetails["addr"] : defaults($user_data, 'url', ''));
53 $result = new ContactResult(
54 defaults($user_data, 'name', ''),
55 defaults($user_data, 'addr', ''),
57 defaults($user_data, 'url', ''),
58 defaults($user_data, 'photo', ''),
59 defaults($user_data, 'network', ''),
60 defaults($contactDetails, 'cid', 0),
62 defaults($user_data, 'tags', '')
65 return new ResultList(1, 1, 1, [$result]);
73 * Search in the global directory for occurrences of the search string
74 * This is mainly based on the JSON results of https://dir.friendica.social
76 * @param string $search
79 * @return ResultList|null
80 * @throws HTTPException\InternalServerErrorException
82 public static function getContactsFromGlobalDirectory($search, $page = 1)
84 $config = self::getApp()->getConfig();
85 $server = $config->get('system', 'directory', self::DEFAULT_DIRECTORY);
87 $searchUrl = $server . '/search?q=' . urlencode($search);
90 $searchUrl .= '&page=' . $page;
94 $resultJson = Network::fetchUrl($searchUrl, false,$red, 0, 'application/json');
96 $results = json_decode($resultJson, true);
98 $resultList = new ResultList(
99 defaults($results, 'page', 1),
100 defaults($results, 'count', 1),
101 defaults($results, 'itemsperpage', 1)
104 $profiles = defaults($results, 'profiles', []);
106 foreach ($profiles as $profile) {
107 $contactDetails = Contact::getDetailsByURL(defaults($profile, 'profile_url', ''), local_user());
108 $itemUrl = (!empty($contactDetails['addr']) ? $contactDetails['addr'] : defaults($profile, 'profile_url', ''));
110 $result = new ContactResult(
111 defaults($profile, 'name', ''),
112 defaults($profile, 'addr', ''),
114 defaults($profile, 'profile_url', ''),
115 defaults($profile, 'photo', ''),
117 defaults($contactDetails, 'cid', 0),
119 defaults($profile, 'tags', ''));
121 $resultList->addResult($result);
128 * Search in the local database for occurrences of the search string
130 * @param string $search
132 * @param int $itemPage
133 * @param bool $community
135 * @return ResultList|null
136 * @throws HTTPException\InternalServerErrorException
138 public static function getContactsFromLocalDirectory($search, $start = 0, $itemPage = 80, $community = false)
140 $config = self::getApp()->getConfig();
142 $diaspora = $config->get('system', 'diaspora_enabled') ? Protocol::DIASPORA : Protocol::DFRN;
143 $ostatus = !$config->get('system', 'ostatus_disabled') ? Protocol::OSTATUS : Protocol::DFRN;
145 $wildcard = Strings::escapeHtml('%' . $search . '%');
147 $count = DBA::count('gcontact', [
149 AND `network` IN (?, ?, ?, ?)
150 AND ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`))
151 AND (`url` LIKE ? OR `name` LIKE ? OR `location` LIKE ?
152 OR `addr` LIKE ? OR `about` LIKE ? OR `keywords` LIKE ?)
153 AND `community` = ?',
154 Protocol::ACTIVITYPUB, Protocol::DFRN, $ostatus, $diaspora,
155 $wildcard, $wildcard, $wildcard,
156 $wildcard, $wildcard, $wildcard,
164 $data = DBA::select('gcontact', ['nurl'], [
166 AND `network` IN (?, ?, ?, ?)
167 AND ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`))
168 AND (`url` LIKE ? OR `name` LIKE ? OR `location` LIKE ?
169 OR `addr` LIKE ? OR `about` LIKE ? OR `keywords` LIKE ?)
170 AND `community` = ?',
171 Protocol::ACTIVITYPUB, Protocol::DFRN, $ostatus, $diaspora,
172 $wildcard, $wildcard, $wildcard,
173 $wildcard, $wildcard, $wildcard,
176 'group_by' => ['nurl', 'updated'],
177 'limit' => [$start, $itemPage],
178 'order' => ['updated' => 'DESC']
181 if (!DBA::isResult($data)) {
185 $resultList = new ResultList($start, $itemPage, $count);
187 while ($row = DBA::fetch($data)) {
188 if (PortableContact::alternateOStatusUrl($row["nurl"])) {
192 $urlParts = parse_url($row["nurl"]);
194 // Ignore results that look strange.
195 // For historic reasons the gcontact table does contain some garbage.
196 if (!empty($urlParts['query']) || !empty($urlParts['fragment'])) {
200 $contact = Contact::getDetailsByURL($row["nurl"], local_user());
202 if ($contact["name"] == "") {
203 $contact["name"] = end(explode("/", $urlParts["path"]));
206 $result = new ContactResult(
218 $resultList->addResult($result);
223 // Add found profiles from the global directory to the local directory
224 Worker::add(PRIORITY_LOW, 'DiscoverPoCo', "dirsearch", urlencode($search));