3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Model;
24 use Friendica\Content\Text\HTML;
25 use Friendica\Core\Logger;
26 use Friendica\Database\DBA;
28 use Friendica\Network\Probe;
29 use Friendica\Protocol\ActivityNamespace;
30 use Friendica\Protocol\ActivityPub;
31 use Friendica\Util\Crypto;
32 use Friendica\Util\Network;
33 use Friendica\Util\JsonLD;
34 use Friendica\Util\DateTimeFormat;
35 use Friendica\Util\Strings;
40 * Fetch webfinger data
42 * @param string $addr Address
43 * @return array webfinger data
45 public static function fetchWebfingerData(string $addr)
47 $addr_parts = explode('@', $addr);
48 if (count($addr_parts) != 2) {
52 $data = ['addr' => $addr];
53 $template = 'https://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
54 $webfinger = Probe::webfinger(str_replace('{uri}', urlencode($addr), $template), 'application/jrd+json');
55 if (empty($webfinger['links'])) {
56 $template = 'http://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
57 $webfinger = Probe::webfinger(str_replace('{uri}', urlencode($addr), $template), 'application/jrd+json');
58 if (empty($webfinger['links'])) {
61 $data['baseurl'] = 'http://' . $addr_parts[1];
63 $data['baseurl'] = 'https://' . $addr_parts[1];
66 foreach ($webfinger['links'] as $link) {
67 if (empty($link['rel'])) {
71 if (!empty($link['template']) && ($link['rel'] == ActivityNamespace::OSTATUSSUB)) {
72 $data['subscribe'] = $link['template'];
75 if (!empty($link['href']) && !empty($link['type']) && ($link['rel'] == 'self') && ($link['type'] == 'application/activity+json')) {
76 $data['url'] = $link['href'];
79 if (!empty($link['href']) && !empty($link['type']) && ($link['rel'] == 'http://webfinger.net/rel/profile-page') && ($link['type'] == 'text/html')) {
80 $data['alias'] = $link['href'];
84 if (!empty($data['url']) && !empty($data['alias']) && ($data['url'] == $data['alias'])) {
85 unset($data['alias']);
92 * Fetches a profile from a given url
94 * @param string $url profile url
95 * @param boolean $update true = always update, false = never update, null = update when not found or outdated
96 * @return array profile array
97 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
98 * @throws \ImagickException
100 public static function getByURL($url, $update = null)
106 $fetched_contact = false;
108 if (empty($update)) {
109 if (is_null($update)) {
110 $ref_update = DateTimeFormat::utc('now - 1 month');
112 $ref_update = DBA::NULL_DATETIME;
115 $apcontact = DBA::selectFirst('apcontact', [], ['url' => $url]);
116 if (!DBA::isResult($apcontact)) {
117 $apcontact = DBA::selectFirst('apcontact', [], ['alias' => $url]);
120 if (!DBA::isResult($apcontact)) {
121 $apcontact = DBA::selectFirst('apcontact', [], ['addr' => $url]);
124 if (DBA::isResult($apcontact) && ($apcontact['updated'] > $ref_update) && !empty($apcontact['pubkey'])) {
128 if (!is_null($update)) {
129 return DBA::isResult($apcontact) ? $apcontact : [];
132 if (DBA::isResult($apcontact)) {
133 $fetched_contact = $apcontact;
139 $webfinger = empty(parse_url($url, PHP_URL_SCHEME));
141 $apcontact = self::fetchWebfingerData($url);
142 if (empty($apcontact['url'])) {
143 return $fetched_contact;
145 $url = $apcontact['url'];
148 $data = ActivityPub::fetchContent($url);
150 return $fetched_contact;
153 $compacted = JsonLD::compact($data);
155 if (empty($compacted['@id'])) {
156 return $fetched_contact;
159 $apcontact['url'] = $compacted['@id'];
160 $apcontact['uuid'] = JsonLD::fetchElement($compacted, 'diaspora:guid', '@value');
161 $apcontact['type'] = str_replace('as:', '', JsonLD::fetchElement($compacted, '@type'));
162 $apcontact['following'] = JsonLD::fetchElement($compacted, 'as:following', '@id');
163 $apcontact['followers'] = JsonLD::fetchElement($compacted, 'as:followers', '@id');
164 $apcontact['inbox'] = JsonLD::fetchElement($compacted, 'ldp:inbox', '@id');
165 self::unarchiveInbox($apcontact['inbox'], false);
167 $apcontact['outbox'] = JsonLD::fetchElement($compacted, 'as:outbox', '@id');
169 $apcontact['sharedinbox'] = '';
170 if (!empty($compacted['as:endpoints'])) {
171 $apcontact['sharedinbox'] = JsonLD::fetchElement($compacted['as:endpoints'], 'as:sharedInbox', '@id');
172 self::unarchiveInbox($apcontact['sharedinbox'], true);
175 $apcontact['nick'] = JsonLD::fetchElement($compacted, 'as:preferredUsername', '@value') ?? '';
176 $apcontact['name'] = JsonLD::fetchElement($compacted, 'as:name', '@value');
178 if (empty($apcontact['name'])) {
179 $apcontact['name'] = $apcontact['nick'];
182 $apcontact['about'] = HTML::toBBCode(JsonLD::fetchElement($compacted, 'as:summary', '@value'));
184 $apcontact['photo'] = JsonLD::fetchElement($compacted, 'as:icon', '@id');
185 if (is_array($apcontact['photo']) || !empty($compacted['as:icon']['as:url']['@id'])) {
186 $apcontact['photo'] = JsonLD::fetchElement($compacted['as:icon'], 'as:url', '@id');
189 if (empty($apcontact['alias'])) {
190 $apcontact['alias'] = JsonLD::fetchElement($compacted, 'as:url', '@id');
191 if (is_array($apcontact['alias'])) {
192 $apcontact['alias'] = JsonLD::fetchElement($compacted['as:url'], 'as:href', '@id');
196 // Quit if none of the basic values are set
197 if (empty($apcontact['url']) || empty($apcontact['inbox']) || empty($apcontact['type'])) {
198 return $fetched_contact;
201 // Quit if this doesn't seem to be an account at all
202 if (!in_array($apcontact['type'], ActivityPub::ACCOUNT_TYPES)) {
203 return $fetched_contact;
206 $parts = parse_url($apcontact['url']);
207 unset($parts['scheme']);
208 unset($parts['path']);
210 if (empty($apcontact['addr'])) {
211 if (!empty($apcontact['nick'])) {
212 $apcontact['addr'] = $apcontact['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts));
214 $apcontact['addr'] = '';
218 $apcontact['pubkey'] = null;
219 if (!empty($compacted['w3id:publicKey'])) {
220 $apcontact['pubkey'] = trim(JsonLD::fetchElement($compacted['w3id:publicKey'], 'w3id:publicKeyPem', '@value'));
221 if (strstr($apcontact['pubkey'], 'RSA ')) {
222 $apcontact['pubkey'] = Crypto::rsaToPem($apcontact['pubkey']);
226 $apcontact['manually-approve'] = (int)JsonLD::fetchElement($compacted, 'as:manuallyApprovesFollowers');
228 if (!empty($compacted['as:generator'])) {
229 $apcontact['baseurl'] = JsonLD::fetchElement($compacted['as:generator'], 'as:url', '@id');
230 $apcontact['generator'] = JsonLD::fetchElement($compacted['as:generator'], 'as:name', '@value');
233 if (!empty($apcontact['following'])) {
234 $data = ActivityPub::fetchContent($apcontact['following']);
236 if (!empty($data['totalItems'])) {
237 $apcontact['following_count'] = $data['totalItems'];
242 if (!empty($apcontact['followers'])) {
243 $data = ActivityPub::fetchContent($apcontact['followers']);
245 if (!empty($data['totalItems'])) {
246 $apcontact['followers_count'] = $data['totalItems'];
251 if (!empty($apcontact['outbox'])) {
252 $data = ActivityPub::fetchContent($apcontact['outbox']);
254 if (!empty($data['totalItems'])) {
255 $apcontact['statuses_count'] = $data['totalItems'];
263 // tag, attachment, image, nomadicLocations, signature, featured, movedTo, liked
265 // Unhandled from Misskey
266 // sharedInbox, isCat
268 // Unhandled from Kroeg
269 // kroeg:blocks, updated
271 // When the photo is too large, try to shorten it by removing parts
272 if (strlen($apcontact['photo']) > 255) {
273 $parts = parse_url($apcontact['photo']);
274 unset($parts['fragment']);
275 $apcontact['photo'] = Network::unparseURL($parts);
277 if (strlen($apcontact['photo']) > 255) {
278 unset($parts['query']);
279 $apcontact['photo'] = Network::unparseURL($parts);
282 if (strlen($apcontact['photo']) > 255) {
283 $apcontact['photo'] = substr($apcontact['photo'], 0, 255);
287 if (!$webfinger && !empty($apcontact['addr'])) {
288 $data = self::fetchWebfingerData($apcontact['addr']);
290 $apcontact['baseurl'] = $data['baseurl'];
292 if (empty($apcontact['alias']) && !empty($data['alias'])) {
293 $apcontact['alias'] = $data['alias'];
295 if (!empty($data['subscribe'])) {
296 $apcontact['subscribe'] = $data['subscribe'];
301 if (empty($apcontact['baseurl'])) {
302 $apcontact['baseurl'] = null;
305 if (!empty($apcontact['baseurl']) && empty($fetched_contact['gsid'])) {
306 $apcontact['gsid'] = GServer::getID($apcontact['baseurl']);
307 } elseif (!empty($fetched_contact['gsid'])) {
308 $apcontact['gsid'] = $fetched_contact['gsid'];
310 $apcontact['gsid'] = null;
313 if ($apcontact['url'] == $apcontact['alias']) {
314 $apcontact['alias'] = null;
317 $apcontact['updated'] = DateTimeFormat::utcNow();
319 DBA::update('apcontact', $apcontact, ['url' => $url], true);
321 // We delete the old entry when the URL is changed
322 if (($url != $apcontact['url']) && DBA::exists('apcontact', ['url' => $url]) && DBA::exists('apcontact', ['url' => $apcontact['url']])) {
323 DBA::delete('apcontact', ['url' => $url]);
326 Logger::info('Updated profile', ['url' => $url]);
334 * @param string $url inbox url
336 private static function unarchiveInbox($url, $shared)
342 $now = DateTimeFormat::utcNow();
344 $fields = ['archive' => false, 'success' => $now, 'shared' => $shared];
346 if (!DBA::exists('inbox-status', ['url' => $url])) {
347 $fields = array_merge($fields, ['url' => $url, 'created' => $now]);
348 DBA::insert('inbox-status', $fields);
350 DBA::update('inbox-status', $fields, ['url' => $url]);