3 namespace Friendica\Core;
5 use Friendica\BaseObject;
6 use Friendica\Database\DBA;
7 use Friendica\Model\Contact;
8 use Friendica\Network\Probe;
9 use Friendica\Network\HTTPException;
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;
16 class Search extends BaseObject
18 const DEFAULT_DIRECTORY = 'https://dir.friendica.social';
21 * Search a user based on his/her profile address
22 * pattern: @username@domain.tld
24 * @param string $user The user to search for
26 * @return ResultList|null
27 * @throws HTTPException\InternalServerErrorException
28 * @throws \ImagickException
30 public static function getContactsFromProbe($user)
32 if ((filter_var($user, FILTER_VALIDATE_EMAIL) && Network::isEmailDomainValid($user)) ||
33 (substr(Strings::normaliseLink($user), 0, 7) == "http://")) {
35 $user_data = Probe::uri($user);
36 if (empty($user_data)) {
40 if (!(in_array($user_data["network"], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS, Protocol::DIASPORA]))) {
44 $contactDetails = Contact::getDetailsByURL(defaults($user_data, 'url', ''), local_user());
45 $itemUrl = (($contactDetails["addr"] != "") ? $contactDetails["addr"] : defaults($user_data, 'url', ''));
47 $result = new ContactResult(
48 defaults($user_data, 'name', ''),
49 defaults($user_data, 'addr', ''),
51 defaults($user_data, 'url', ''),
52 defaults($user_data, 'photo', ''),
53 defaults($user_data, 'network', ''),
54 defaults($contactDetails, 'cid', 0),
56 defaults($user_data, 'tags', '')
59 return new ResultList(1, 1, 1, [$result]);
67 * Search in the global directory for occurrences of the search string
68 * This is mainly based on the JSON results of https://dir.friendica.social
70 * @param string $search
73 * @return ResultList|null
74 * @throws HTTPException\InternalServerErrorException
76 public static function getContactsFromGlobalDirectory($search, $page = 1)
78 $config = self::getApp()->getConfig();
79 $server = $config->get('system', 'directory', self::DEFAULT_DIRECTORY);
81 $searchUrl = $server . '/search?q=' . urlencode($search);
84 $searchUrl .= '&page=' . $page;
88 $resultJson = Network::fetchUrl($searchUrl, false,$red, 0, 'application/json');
90 $results = json_decode($resultJson, true);
92 $resultList = new ResultList(
93 defaults($results, 'page', 1),
94 defaults($results, 'count', 1),
95 defaults($results, 'itemsperpage', 1)
98 $profiles = defaults($results, 'profiles', []);
100 foreach ($profiles as $profile) {
101 $contactDetails = Contact::getDetailsByURL(defaults($profile, 'profile_url', ''), local_user());
102 $itemUrl = (!empty($contactDetails['addr']) ? $contactDetails['addr'] : defaults($profile, 'profile_url', ''));
104 $result = new ContactResult(
105 defaults($profile, 'name', ''),
106 defaults($profile, 'addr', ''),
108 defaults($profile, 'profile_url', ''),
109 defaults($profile, 'photo', ''),
111 defaults($contactDetails, 'cid', 0),
113 defaults($profile, 'tags', ''));
115 $resultList->addResult($result);
122 * Search in the local database for occurrences of the search string
124 * @param string $search
126 * @param int $itemPage
127 * @param bool $community
129 * @return ResultList|null
130 * @throws HTTPException\InternalServerErrorException
132 public static function getContactsFromLocalDirectory($search, $start = 0, $itemPage = 80, $community = false)
134 $config = self::getApp()->getConfig();
136 $diaspora = $config->get('system', 'diaspora_enabled') ? Protocol::DIASPORA : Protocol::DFRN;
137 $ostatus = !$config->get('system', 'ostatus_disabled') ? Protocol::OSTATUS : Protocol::DFRN;
139 $wildcard = Strings::escapeHtml('%' . $search . '%');
141 $count = DBA::count('gcontact', [
143 AND `network` IN (?, ?, ?, ?)
144 AND ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`))
145 AND (`url` LIKE ? OR `name` LIKE ? OR `location` LIKE ?
146 OR `addr` LIKE ? OR `about` LIKE ? OR `keywords` LIKE ?)
147 AND `community` = ?',
148 Protocol::ACTIVITYPUB, Protocol::DFRN, $ostatus, $diaspora,
149 $wildcard, $wildcard, $wildcard,
150 $wildcard, $wildcard, $wildcard,
158 $data = DBA::select('gcontact', ['nurl'], [
160 AND `network` IN (?, ?, ?, ?)
161 AND ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`))
162 AND (`url` LIKE ? OR `name` LIKE ? OR `location` LIKE ?
163 OR `addr` LIKE ? OR `about` LIKE ? OR `keywords` LIKE ?)
164 AND `community` = ?',
165 Protocol::ACTIVITYPUB, Protocol::DFRN, $ostatus, $diaspora,
166 $wildcard, $wildcard, $wildcard,
167 $wildcard, $wildcard, $wildcard,
170 'group_by' => ['nurl', 'updated'],
171 'limit' => [$start, $itemPage],
172 'order' => ['updated' => 'DESC']
175 if (!DBA::isResult($data)) {
179 $resultList = new ResultList($start, $itemPage, $count);
181 while ($row = DBA::fetch($data)) {
182 if (PortableContact::alternateOStatusUrl($row["nurl"])) {
186 $urlParts = parse_url($row["nurl"]);
188 // Ignore results that look strange.
189 // For historic reasons the gcontact table does contain some garbage.
190 if (!empty($urlParts['query']) || !empty($urlParts['fragment'])) {
194 $contact = Contact::getDetailsByURL($row["nurl"], local_user());
196 if ($contact["name"] == "") {
197 $contact["name"] = end(explode("/", $urlParts["path"]));
200 $result = new ContactResult(
212 $resultList->addResult($result);
217 // Add found profiles from the global directory to the local directory
218 Worker::add(PRIORITY_LOW, 'DiscoverPoCo', "dirsearch", urlencode($search));