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';
26 const TYPE_PEOPLE = 0;
31 * Search a user based on his/her profile address
32 * pattern: @username@domain.tld
34 * @param string $user The user to search for
37 * @throws HTTPException\InternalServerErrorException
38 * @throws \ImagickException
40 public static function getContactsFromProbe($user)
42 $emptyResultList = new ResultList(1, 0, 1);
44 if ((filter_var($user, FILTER_VALIDATE_EMAIL) && Network::isEmailDomainValid($user)) ||
45 (substr(Strings::normaliseLink($user), 0, 7) == "http://")) {
47 /// @todo Possibly use "getIdForURL" instead?
48 $user_data = Probe::uri($user);
49 if (empty($user_data)) {
50 return $emptyResultList;
53 if (!in_array($user_data["network"], Protocol::FEDERATED)) {
54 return $emptyResultList;
57 // Ensure that we do have a contact entry
58 Contact::getIdForURL(defaults($user_data, 'url', ''));
60 $contactDetails = Contact::getDetailsByURL(defaults($user_data, 'url', ''), local_user());
61 $itemUrl = defaults($contactDetails, 'addr', defaults($user_data, 'url', ''));
63 $result = new ContactResult(
64 defaults($user_data, 'name', ''),
65 defaults($user_data, 'addr', ''),
67 defaults($user_data, 'url', ''),
68 defaults($user_data, 'photo', ''),
69 defaults($user_data, 'network', ''),
70 defaults($contactDetails, 'id', 0),
72 defaults($user_data, 'tags', '')
75 return new ResultList(1, 1, 1, [$result]);
77 return $emptyResultList;
82 * Search in the global directory for occurrences of the search string
84 * @see https://github.com/friendica/friendica-directory/blob/master/docs/Protocol.md#search
86 * @param string $search
87 * @param int $type specific type of searching
91 * @throws HTTPException\InternalServerErrorException
93 public static function getContactsFromGlobalDirectory($search, $type = self::TYPE_ALL, $page = 1)
95 $config = self::getApp()->getConfig();
96 $server = $config->get('system', 'directory', self::DEFAULT_DIRECTORY);
98 $searchUrl = $server . '/search';
101 case self::TYPE_FORUM:
102 $searchUrl .= '/forum';
104 case self::TYPE_PEOPLE:
105 $searchUrl .= '/people';
108 $searchUrl .= '?q=' . urlencode($search);
111 $searchUrl .= '&page=' . $page;
114 $resultJson = Network::fetchUrl($searchUrl, false, 0, 'application/json');
116 $results = json_decode($resultJson, true);
118 $resultList = new ResultList(
119 defaults($results, 'page', 1),
120 defaults($results, 'count', 0),
121 defaults($results, 'itemsperpage', 30)
124 $profiles = defaults($results, 'profiles', []);
126 foreach ($profiles as $profile) {
127 $contactDetails = Contact::getDetailsByURL(defaults($profile, 'profile_url', ''), local_user());
128 $itemUrl = defaults($contactDetails, 'addr', defaults($profile, 'profile_url', ''));
130 $result = new ContactResult(
131 defaults($profile, 'name', ''),
132 defaults($profile, 'addr', ''),
134 defaults($profile, 'profile_url', ''),
135 defaults($profile, 'photo', ''),
137 defaults($contactDetails, 'cid', 0),
139 defaults($profile, 'tags', ''));
141 $resultList->addResult($result);
148 * Search in the local database for occurrences of the search string
150 * @param string $search
153 * @param int $itemPage
156 * @throws HTTPException\InternalServerErrorException
158 public static function getContactsFromLocalDirectory($search, $type = self::TYPE_ALL, $start = 0, $itemPage = 80)
160 $config = self::getApp()->getConfig();
162 $diaspora = $config->get('system', 'diaspora_enabled') ? Protocol::DIASPORA : Protocol::DFRN;
163 $ostatus = !$config->get('system', 'ostatus_disabled') ? Protocol::OSTATUS : Protocol::DFRN;
165 $wildcard = Strings::escapeHtml('%' . $search . '%');
167 $count = DBA::count('gcontact', [
169 AND `network` IN (?, ?, ?, ?)
170 AND ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`))
171 AND (`url` LIKE ? OR `name` LIKE ? OR `location` LIKE ?
172 OR `addr` LIKE ? OR `about` LIKE ? OR `keywords` LIKE ?)
173 AND `community` = ?',
174 Protocol::ACTIVITYPUB, Protocol::DFRN, $ostatus, $diaspora,
175 $wildcard, $wildcard, $wildcard,
176 $wildcard, $wildcard, $wildcard,
177 ($type === self::TYPE_FORUM),
180 $resultList = new ResultList($start, $itemPage, $count);
186 $data = DBA::select('gcontact', ['nurl'], [
188 AND `network` IN (?, ?, ?, ?)
189 AND ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`))
190 AND (`url` LIKE ? OR `name` LIKE ? OR `location` LIKE ?
191 OR `addr` LIKE ? OR `about` LIKE ? OR `keywords` LIKE ?)
192 AND `community` = ?',
193 Protocol::ACTIVITYPUB, Protocol::DFRN, $ostatus, $diaspora,
194 $wildcard, $wildcard, $wildcard,
195 $wildcard, $wildcard, $wildcard,
196 ($type === self::TYPE_FORUM),
198 'group_by' => ['nurl', 'updated'],
199 'limit' => [$start, $itemPage],
200 'order' => ['updated' => 'DESC']
203 if (!DBA::isResult($data)) {
207 while ($row = DBA::fetch($data)) {
208 if (PortableContact::alternateOStatusUrl($row["nurl"])) {
212 $urlParts = parse_url($row["nurl"]);
214 // Ignore results that look strange.
215 // For historic reasons the gcontact table does contain some garbage.
216 if (!empty($urlParts['query']) || !empty($urlParts['fragment'])) {
220 $contact = Contact::getDetailsByURL($row["nurl"], local_user());
222 if ($contact["name"] == "") {
223 $contact["name"] = end(explode("/", $urlParts["path"]));
226 $result = new ContactResult(
238 $resultList->addResult($result);
243 // Add found profiles from the global directory to the local directory
244 Worker::add(PRIORITY_LOW, 'DiscoverPoCo', "dirsearch", urlencode($search));