Fixing method signature
[friendica.git/.git] / src / Core / Search.php
1 <?php
2
3 namespace Friendica\Core;
4
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;
15
16 /**
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
21  */
22 class Search extends BaseObject
23 {
24         const DEFAULT_DIRECTORY = 'https://dir.friendica.social';
25
26         const TYPE_PEOPLE = 0;
27         const TYPE_FORUM  = 1;
28         const TYPE_ALL    = 2;
29
30         /**
31          * Search a user based on his/her profile address
32          * pattern: @username@domain.tld
33          *
34          * @param string $user The user to search for
35          *
36          * @return ResultList|null
37          * @throws HTTPException\InternalServerErrorException
38          * @throws \ImagickException
39          */
40         public static function getContactsFromProbe($user)
41         {
42                 if ((filter_var($user, FILTER_VALIDATE_EMAIL) && Network::isEmailDomainValid($user)) ||
43                     (substr(Strings::normaliseLink($user), 0, 7) == "http://")) {
44
45                         $user_data = Probe::uri($user);
46                         if (empty($user_data)) {
47                                 return null;
48                         }
49
50                         if (!(in_array($user_data["network"], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS, Protocol::DIASPORA]))) {
51                                 return null;
52                         }
53
54                         $contactDetails = Contact::getDetailsByURL(defaults($user_data, 'url', ''), local_user());
55                         $itemUrl        = (($contactDetails["addr"] != "") ? $contactDetails["addr"] : defaults($user_data, 'url', ''));
56
57                         $result = new ContactResult(
58                                 defaults($user_data, 'name', ''),
59                                 defaults($user_data, 'addr', ''),
60                                 $itemUrl,
61                                 defaults($user_data, 'url', ''),
62                                 defaults($user_data, 'photo', ''),
63                                 defaults($user_data, 'network', ''),
64                                 defaults($contactDetails, 'cid', 0),
65                                 0,
66                                 defaults($user_data, 'tags', '')
67                         );
68
69                         return new ResultList(1, 1, 1, [$result]);
70
71                 } else {
72                         return null;
73                 }
74         }
75
76         /**
77          * Search in the global directory for occurrences of the search string
78          *
79          * @see https://github.com/friendica/friendica-directory/blob/master/docs/Protocol.md#search
80          *
81          * @param string $search
82          * @param int    $type specific type of searching
83          * @param int    $page
84          *
85          * @return ResultList|null
86          * @throws HTTPException\InternalServerErrorException
87          */
88         public static function getContactsFromGlobalDirectory($search, $type = self::TYPE_ALL, $page = 1)
89         {
90                 $config = self::getApp()->getConfig();
91                 $server = $config->get('system', 'directory', self::DEFAULT_DIRECTORY);
92
93                 $searchUrl = $server . '/search';
94
95                 switch ($type) {
96                         case self::TYPE_FORUM:
97                                 $searchUrl .= '/forum';
98                                 break;
99                         case self::TYPE_PEOPLE:
100                                 $searchUrl .= '/people';
101                                 break;
102                 }
103                 $searchUrl .= '?q=' . urlencode($search);
104
105                 if ($page > 1) {
106                         $searchUrl .= '&page=' . $page;
107                 }
108
109                 $red        = 0;
110                 $resultJson = Network::fetchUrl($searchUrl, false, $red, 0, 'application/json');
111
112                 $results = json_decode($resultJson, true);
113
114                 $resultList = new ResultList(
115                         defaults($results, 'page', 1),
116                         defaults($results, 'count', 1),
117                         defaults($results, 'itemsperpage', 1)
118                 );
119
120                 $profiles = defaults($results, 'profiles', []);
121
122                 foreach ($profiles as $profile) {
123                         $contactDetails = Contact::getDetailsByURL(defaults($profile, 'profile_url', ''), local_user());
124                         $itemUrl        = (!empty($contactDetails['addr']) ?
125                                 $contactDetails['addr'] :
126                                 defaults($profile, 'profile_url', ''));
127
128                         $result = new ContactResult(
129                                 defaults($profile, 'name', ''),
130                                 defaults($profile, 'addr', ''),
131                                 $itemUrl,
132                                 defaults($profile, 'profile_url', ''),
133                                 defaults($profile, 'photo', ''),
134                                 Protocol::DFRN,
135                                 defaults($contactDetails, 'cid', 0),
136                                 0,
137                                 defaults($profile, 'tags', ''));
138
139                         $resultList->addResult($result);
140                 }
141
142                 return $resultList;
143         }
144
145         /**
146          * Search in the local database for occurrences of the search string
147          *
148          * @param string $search
149          * @param int    $type
150          * @param int    $start
151          * @param int    $itemPage
152          *
153          * @return ResultList|null
154          * @throws HTTPException\InternalServerErrorException
155          */
156         public static function getContactsFromLocalDirectory($search, $type = self::TYPE_ALL, $start = 0, $itemPage = 80)
157         {
158                 $config = self::getApp()->getConfig();
159
160                 $diaspora = $config->get('system', 'diaspora_enabled') ? Protocol::DIASPORA : Protocol::DFRN;
161                 $ostatus  = !$config->get('system', 'ostatus_disabled') ? Protocol::OSTATUS : Protocol::DFRN;
162
163                 $wildcard = Strings::escapeHtml('%' . $search . '%');
164
165                 $count = DBA::count('gcontact', [
166                         'NOT `hide`
167                         AND `network` IN (?, ?, ?, ?)
168                         AND ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`))
169                         AND (`url` LIKE ? OR `name` LIKE ? OR `location` LIKE ? 
170                                 OR `addr` LIKE ? OR `about` LIKE ? OR `keywords` LIKE ?)
171                         AND `community` = ?',
172                         Protocol::ACTIVITYPUB, Protocol::DFRN, $ostatus, $diaspora,
173                         $wildcard, $wildcard, $wildcard,
174                         $wildcard, $wildcard, $wildcard,
175                         ($type === self::TYPE_FORUM),
176                 ]);
177
178                 if (empty($count)) {
179                         return null;
180                 }
181
182                 $data = DBA::select('gcontact', ['nurl'], [
183                         'NOT `hide`
184                         AND `network` IN (?, ?, ?, ?)
185                         AND ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`))
186                         AND (`url` LIKE ? OR `name` LIKE ? OR `location` LIKE ? 
187                                 OR `addr` LIKE ? OR `about` LIKE ? OR `keywords` LIKE ?)
188                         AND `community` = ?',
189                         Protocol::ACTIVITYPUB, Protocol::DFRN, $ostatus, $diaspora,
190                         $wildcard, $wildcard, $wildcard,
191                         $wildcard, $wildcard, $wildcard,
192                         ($type === self::TYPE_FORUM),
193                 ], [
194                         'group_by' => ['nurl', 'updated'],
195                         'limit'    => [$start, $itemPage],
196                         'order'    => ['updated' => 'DESC']
197                 ]);
198
199                 if (!DBA::isResult($data)) {
200                         return null;
201                 }
202
203                 $resultList = new ResultList($start, $itemPage, $count);
204
205                 while ($row = DBA::fetch($data)) {
206                         if (PortableContact::alternateOStatusUrl($row["nurl"])) {
207                                 continue;
208                         }
209
210                         $urlParts = parse_url($row["nurl"]);
211
212                         // Ignore results that look strange.
213                         // For historic reasons the gcontact table does contain some garbage.
214                         if (!empty($urlParts['query']) || !empty($urlParts['fragment'])) {
215                                 continue;
216                         }
217
218                         $contact = Contact::getDetailsByURL($row["nurl"], local_user());
219
220                         if ($contact["name"] == "") {
221                                 $contact["name"] = end(explode("/", $urlParts["path"]));
222                         }
223
224                         $result = new ContactResult(
225                                 $contact["name"],
226                                 $contact["addr"],
227                                 $contact["addr"],
228                                 $contact["url"],
229                                 $contact["photo"],
230                                 $contact["network"],
231                                 $contact["cid"],
232                                 $contact["zid"],
233                                 $contact["keywords"]
234                         );
235
236                         $resultList->addResult($result);
237                 }
238
239                 DBA::close($data);
240
241                 // Add found profiles from the global directory to the local directory
242                 Worker::add(PRIORITY_LOW, 'DiscoverPoCo', "dirsearch", urlencode($search));
243
244                 return $resultList;
245         }
246 }