3 namespace Friendica\Model;
5 use Friendica\BaseObject;
6 use Friendica\Core\Protocol;
7 use Friendica\Core\Worker;
8 use Friendica\Database\DBA;
9 use Friendica\Network\Probe;
10 use Friendica\Object\Search\Result;
11 use Friendica\Object\Search\ResultList;
12 use Friendica\Protocol\PortableContact;
13 use Friendica\Util\Network;
14 use Friendica\Util\Strings;
19 class Search extends BaseObject
21 const DEFAULT_DIRECTORY = 'https://dir.friendica.social';
24 * Returns the list of user defined tags (e.g. #Friendica)
30 public static function getUserTags()
32 $termsStmt = DBA::p("SELECT DISTINCT(`term`) FROM `search`");
36 while ($term = DBA::fetch($termsStmt)) {
37 $tags[] = trim($term['term'], '#');
44 * Search a user based on his/her profile address
45 * pattern: @username@domain.tld
47 * @param string $user The user to search for
49 * @return ResultList|null
50 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
51 * @throws \ImagickException
53 public static function searchUser($user)
55 if ((filter_var($user, FILTER_VALIDATE_EMAIL) && Network::isEmailDomainValid($user)) ||
56 (substr(Strings::normaliseLink($user), 0, 7) == "http://")) {
58 $user_data = Probe::uri($user);
59 if (empty($user_data)) {
63 if (!(in_array($user_data["network"], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS, Protocol::DIASPORA]))) {
67 $contactDetails = Contact::getDetailsByURL(defaults($user_data, 'url', ''), local_user());
68 $itemurl = (($contactDetails["addr"] != "") ? $contactDetails["addr"] : defaults($user_data, 'url', ''));
71 defaults($user_data, 'name', ''),
72 defaults($user_data, 'addr', ''),
74 defaults($user_data, 'url', ''),
75 defaults($user_data, 'photo', ''),
76 defaults($user_data, 'network', ''),
77 defaults($contactDetails, 'cid', 0),
79 defaults($user_data, 'tags', '')
82 return new ResultList(1, 1, 1, [$result]);
90 * Search in the global directory for occurrences of the search string
91 * This is mainly based on the JSON results of https://dir.friendica.social
93 * @param string $search
96 * @return ResultList|null
97 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
99 public static function searchDirectory($search, $page = 1)
101 $config = self::getApp()->getConfig();
102 $server = $config->get('system', 'directory', self::DEFAULT_DIRECTORY);
104 $searchUrl = $server . '/search?q=' . urlencode($search);
107 $searchUrl .= '&page=' . $page;
111 $resultJson = Network::fetchUrl($searchUrl, false,$red, 0, 'application/json');
113 $results = json_decode($resultJson, true);
115 $resultList = new ResultList(
116 defaults($results, 'page', 1),
117 defaults($results, 'count', 1),
118 defaults($results, 'itemsperpage', 1)
121 $profiles = defaults($results, 'profiles', []);
123 foreach ($profiles as $profile) {
124 $contactDetails = Contact::getDetailsByURL(defaults($profile, 'profile_url', ''), local_user());
125 $itemurl = (!empty($contactDetails['addr']) ? $contactDetails['addr'] : defaults($profile, 'profile_url', ''));
127 $result = new Result(
128 defaults($profile, 'name', ''),
129 defaults($profile, 'addr', ''),
131 defaults($profile, 'profile_url', ''),
132 defaults($profile, 'photo', ''),
134 defaults($contactDetails, 'cid', 0),
136 defaults($profile, 'tags', ''));
138 $resultList->addResult($result);
145 * Search in the local database for occurrences of the search string
147 * @param string $search
149 * @param int $itemPage
150 * @param bool $community
152 * @return ResultList|null
153 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
155 public static function searchLocal($search, $start = 0, $itemPage = 80, $community = false)
157 $config = self::getApp()->getConfig();
159 $diaspora = $config->get('system', 'diaspora_enabled') ? Protocol::DIASPORA : Protocol::DFRN;
160 $ostatus = !$config->get('system', 'ostatus_disabled') ? Protocol::OSTATUS : Protocol::DFRN;
162 $wildcard = Strings::escapeHtml('%' . $search . '%');
164 $count = DBA::count('gcontact', [
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,
181 $data = DBA::select('gcontact', ['nurl'], [
183 AND `network` IN (?, ?, ?, ?)
184 AND ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`))
185 AND (`url` LIKE ? OR `name` LIKE ? OR `location` LIKE ?
186 OR `addr` LIKE ? OR `about` LIKE ? OR `keywords` LIKE ?)
187 AND `community` = ?',
188 Protocol::ACTIVITYPUB, Protocol::DFRN, $ostatus, $diaspora,
189 $wildcard, $wildcard, $wildcard,
190 $wildcard, $wildcard, $wildcard,
193 'group_by' => ['nurl', 'updated'],
194 'limit' => [$start, $itemPage],
195 'order' => ['updated' => 'DESC']
198 if (!DBA::isResult($data)) {
202 $resultList = new ResultList($start, $itemPage, $count);
204 while ($row = DBA::fetch($data)) {
205 if (PortableContact::alternateOStatusUrl($row["nurl"])) {
209 $urlparts = parse_url($row["nurl"]);
211 // Ignore results that look strange.
212 // For historic reasons the gcontact table does contain some garbage.
213 if (!empty($urlparts['query']) || !empty($urlparts['fragment'])) {
217 $contact = Contact::getDetailsByURL($row["nurl"], local_user());
219 if ($contact["name"] == "") {
220 $contact["name"] = end(explode("/", $urlparts["path"]));
223 $result = new Result(
235 $resultList->addResult($result);
240 // Add found profiles from the global directory to the local directory
241 Worker::add(PRIORITY_LOW, 'DiscoverPoCo', "dirsearch", urlencode($search));