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;
27 use Friendica\Network\Probe;
28 use Friendica\Protocol\ActivityNamespace;
29 use Friendica\Protocol\ActivityPub;
30 use Friendica\Util\Crypto;
31 use Friendica\Util\DateTimeFormat;
32 use Friendica\Util\JsonLD;
33 use Friendica\Util\Network;
38 * Fetch webfinger data
40 * @param string $addr Address
41 * @return array webfinger data
43 public static function fetchWebfingerData(string $addr)
45 $addr_parts = explode('@', $addr);
46 if (count($addr_parts) != 2) {
50 $data = ['addr' => $addr];
51 $template = 'https://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
52 $webfinger = Probe::webfinger(str_replace('{uri}', urlencode($addr), $template), 'application/jrd+json');
53 if (empty($webfinger['links'])) {
54 $template = 'http://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
55 $webfinger = Probe::webfinger(str_replace('{uri}', urlencode($addr), $template), 'application/jrd+json');
56 if (empty($webfinger['links'])) {
59 $data['baseurl'] = 'http://' . $addr_parts[1];
61 $data['baseurl'] = 'https://' . $addr_parts[1];
64 foreach ($webfinger['links'] as $link) {
65 if (empty($link['rel'])) {
69 if (!empty($link['template']) && ($link['rel'] == ActivityNamespace::OSTATUSSUB)) {
70 $data['subscribe'] = $link['template'];
73 if (!empty($link['href']) && !empty($link['type']) && ($link['rel'] == 'self') && ($link['type'] == 'application/activity+json')) {
74 $data['url'] = $link['href'];
77 if (!empty($link['href']) && !empty($link['type']) && ($link['rel'] == 'http://webfinger.net/rel/profile-page') && ($link['type'] == 'text/html')) {
78 $data['alias'] = $link['href'];
82 if (!empty($data['url']) && !empty($data['alias']) && ($data['url'] == $data['alias'])) {
83 unset($data['alias']);
90 * Fetches a profile from a given url
92 * @param string $url profile url
93 * @param boolean $update true = always update, false = never update, null = update when not found or outdated
94 * @return array profile array
95 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
96 * @throws \ImagickException
98 public static function getByURL($url, $update = null)
104 $fetched_contact = false;
106 if (empty($update)) {
107 if (is_null($update)) {
108 $ref_update = DateTimeFormat::utc('now - 1 month');
110 $ref_update = DBA::NULL_DATETIME;
113 $apcontact = DBA::selectFirst('apcontact', [], ['url' => $url]);
114 if (!DBA::isResult($apcontact)) {
115 $apcontact = DBA::selectFirst('apcontact', [], ['alias' => $url]);
118 if (!DBA::isResult($apcontact)) {
119 $apcontact = DBA::selectFirst('apcontact', [], ['addr' => $url]);
122 if (DBA::isResult($apcontact) && ($apcontact['updated'] > $ref_update) && !empty($apcontact['pubkey'])) {
126 if (!is_null($update)) {
127 return DBA::isResult($apcontact) ? $apcontact : [];
130 if (DBA::isResult($apcontact)) {
131 $fetched_contact = $apcontact;
137 $webfinger = empty(parse_url($url, PHP_URL_SCHEME));
139 $apcontact = self::fetchWebfingerData($url);
140 if (empty($apcontact['url'])) {
141 return $fetched_contact;
143 $url = $apcontact['url'];
146 $data = ActivityPub::fetchContent($url);
148 return $fetched_contact;
151 $compacted = JsonLD::compact($data);
153 if (empty($compacted['@id'])) {
154 return $fetched_contact;
157 $apcontact['url'] = $compacted['@id'];
158 $apcontact['uuid'] = JsonLD::fetchElement($compacted, 'diaspora:guid', '@value');
159 $apcontact['type'] = str_replace('as:', '', JsonLD::fetchElement($compacted, '@type'));
160 $apcontact['following'] = JsonLD::fetchElement($compacted, 'as:following', '@id');
161 $apcontact['followers'] = JsonLD::fetchElement($compacted, 'as:followers', '@id');
162 $apcontact['inbox'] = JsonLD::fetchElement($compacted, 'ldp:inbox', '@id');
163 self::unarchiveInbox($apcontact['inbox'], false);
165 $apcontact['outbox'] = JsonLD::fetchElement($compacted, 'as:outbox', '@id');
167 $apcontact['sharedinbox'] = '';
168 if (!empty($compacted['as:endpoints'])) {
169 $apcontact['sharedinbox'] = JsonLD::fetchElement($compacted['as:endpoints'], 'as:sharedInbox', '@id');
170 self::unarchiveInbox($apcontact['sharedinbox'], true);
173 $apcontact['nick'] = JsonLD::fetchElement($compacted, 'as:preferredUsername', '@value') ?? '';
174 $apcontact['name'] = JsonLD::fetchElement($compacted, 'as:name', '@value');
176 if (empty($apcontact['name'])) {
177 $apcontact['name'] = $apcontact['nick'];
180 $apcontact['about'] = HTML::toBBCode(JsonLD::fetchElement($compacted, 'as:summary', '@value'));
182 $apcontact['photo'] = JsonLD::fetchElement($compacted, 'as:icon', '@id');
183 if (is_array($apcontact['photo']) || !empty($compacted['as:icon']['as:url']['@id'])) {
184 $apcontact['photo'] = JsonLD::fetchElement($compacted['as:icon'], 'as:url', '@id');
187 if (empty($apcontact['alias'])) {
188 $apcontact['alias'] = JsonLD::fetchElement($compacted, 'as:url', '@id');
189 if (is_array($apcontact['alias'])) {
190 $apcontact['alias'] = JsonLD::fetchElement($compacted['as:url'], 'as:href', '@id');
194 // Quit if none of the basic values are set
195 if (empty($apcontact['url']) || empty($apcontact['inbox']) || empty($apcontact['type'])) {
196 return $fetched_contact;
199 // Quit if this doesn't seem to be an account at all
200 if (!in_array($apcontact['type'], ActivityPub::ACCOUNT_TYPES)) {
201 return $fetched_contact;
204 $parts = parse_url($apcontact['url']);
205 unset($parts['scheme']);
206 unset($parts['path']);
208 if (empty($apcontact['addr'])) {
209 if (!empty($apcontact['nick'])) {
210 $apcontact['addr'] = $apcontact['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts));
212 $apcontact['addr'] = '';
216 $apcontact['pubkey'] = null;
217 if (!empty($compacted['w3id:publicKey'])) {
218 $apcontact['pubkey'] = trim(JsonLD::fetchElement($compacted['w3id:publicKey'], 'w3id:publicKeyPem', '@value'));
219 if (strstr($apcontact['pubkey'], 'RSA ')) {
220 $apcontact['pubkey'] = Crypto::rsaToPem($apcontact['pubkey']);
224 $apcontact['manually-approve'] = (int)JsonLD::fetchElement($compacted, 'as:manuallyApprovesFollowers');
226 if (!empty($compacted['as:generator'])) {
227 $apcontact['baseurl'] = JsonLD::fetchElement($compacted['as:generator'], 'as:url', '@id');
228 $apcontact['generator'] = JsonLD::fetchElement($compacted['as:generator'], 'as:name', '@value');
231 if (!empty($apcontact['following'])) {
232 $data = ActivityPub::fetchContent($apcontact['following']);
234 if (!empty($data['totalItems'])) {
235 $apcontact['following_count'] = $data['totalItems'];
240 if (!empty($apcontact['followers'])) {
241 $data = ActivityPub::fetchContent($apcontact['followers']);
243 if (!empty($data['totalItems'])) {
244 $apcontact['followers_count'] = $data['totalItems'];
249 if (!empty($apcontact['outbox'])) {
250 $data = ActivityPub::fetchContent($apcontact['outbox']);
252 if (!empty($data['totalItems'])) {
253 $apcontact['statuses_count'] = $data['totalItems'];
261 // tag, attachment, image, nomadicLocations, signature, featured, movedTo, liked
263 // Unhandled from Misskey
264 // sharedInbox, isCat
266 // Unhandled from Kroeg
267 // kroeg:blocks, updated
269 // When the photo is too large, try to shorten it by removing parts
270 if (strlen($apcontact['photo']) > 255) {
271 $parts = parse_url($apcontact['photo']);
272 unset($parts['fragment']);
273 $apcontact['photo'] = Network::unparseURL($parts);
275 if (strlen($apcontact['photo']) > 255) {
276 unset($parts['query']);
277 $apcontact['photo'] = Network::unparseURL($parts);
280 if (strlen($apcontact['photo']) > 255) {
281 $apcontact['photo'] = substr($apcontact['photo'], 0, 255);
285 if (!$webfinger && !empty($apcontact['addr'])) {
286 $data = self::fetchWebfingerData($apcontact['addr']);
288 $apcontact['baseurl'] = $data['baseurl'];
290 if (empty($apcontact['alias']) && !empty($data['alias'])) {
291 $apcontact['alias'] = $data['alias'];
293 if (!empty($data['subscribe'])) {
294 $apcontact['subscribe'] = $data['subscribe'];
297 $apcontact['addr'] = null;
301 if (empty($apcontact['baseurl'])) {
302 $apcontact['baseurl'] = null;
305 if (empty($apcontact['subscribe'])) {
306 $apcontact['subscribe'] = null;
309 if (!empty($apcontact['baseurl']) && empty($fetched_contact['gsid'])) {
310 $apcontact['gsid'] = GServer::getID($apcontact['baseurl']);
311 } elseif (!empty($fetched_contact['gsid'])) {
312 $apcontact['gsid'] = $fetched_contact['gsid'];
314 $apcontact['gsid'] = null;
317 if ($apcontact['url'] == $apcontact['alias']) {
318 $apcontact['alias'] = null;
321 $apcontact['updated'] = DateTimeFormat::utcNow();
323 // We delete the old entry when the URL is changed
324 if ($url != $apcontact['url']) {
325 Logger::info('Delete changed profile url', ['old' => $url, 'new' => $apcontact['url']]);
326 DBA::delete('apcontact', ['url' => $url]);
329 if (DBA::exists('apcontact', ['url' => $apcontact['url']])) {
330 DBA::update('apcontact', $apcontact, ['url' => $apcontact['url']]);
332 DBA::replace('apcontact', $apcontact);
335 Logger::info('Updated profile', ['url' => $url]);
343 * @param string $url inbox url
345 private static function unarchiveInbox($url, $shared)
351 $now = DateTimeFormat::utcNow();
353 $fields = ['archive' => false, 'success' => $now, 'shared' => $shared];
355 if (!DBA::exists('inbox-status', ['url' => $url])) {
356 $fields = array_merge($fields, ['url' => $url, 'created' => $now]);
357 DBA::replace('inbox-status', $fields);
359 DBA::update('inbox-status', $fields, ['url' => $url]);