4 * @file src/Model/APContact.php
7 namespace Friendica\Model;
9 use Friendica\BaseObject;
10 use Friendica\Database\DBA;
11 use Friendica\Protocol\ActivityPub;
12 use Friendica\Util\Network;
13 use Friendica\Util\JsonLD;
14 use Friendica\Util\DateTimeFormat;
15 use Friendica\Content\Text\HTML;
17 require_once 'boot.php';
19 class APContact extends BaseObject
22 * Resolves the profile url from the address by using webfinger
24 * @param string $addr profile address (user@domain.tld)
27 private static function addrToUrl($addr)
29 $addr_parts = explode('@', $addr);
30 if (count($addr_parts) != 2) {
34 $webfinger = 'https://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
36 $curlResult = Network::curl($webfinger, false, $redirects, ['accept_content' => 'application/jrd+json,application/json']);
37 if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
41 $data = json_decode($curlResult->getBody(), true);
43 if (empty($data['links'])) {
47 foreach ($data['links'] as $link) {
48 if (empty($link['href']) || empty($link['rel']) || empty($link['type'])) {
52 if (($link['rel'] == 'self') && ($link['type'] == 'application/activity+json')) {
61 * Fetches a profile from a given url
63 * @param string $url profile url
64 * @param boolean $update true = always update, false = never update, null = update when not found
65 * @return array profile array
67 public static function getByURL($url, $update = null)
74 $apcontact = DBA::selectFirst('apcontact', [], ['url' => $url]);
75 if (DBA::isResult($apcontact)) {
79 $apcontact = DBA::selectFirst('apcontact', [], ['alias' => $url]);
80 if (DBA::isResult($apcontact)) {
84 $apcontact = DBA::selectFirst('apcontact', [], ['addr' => $url]);
85 if (DBA::isResult($apcontact)) {
89 if (!is_null($update)) {
94 if (empty(parse_url($url, PHP_URL_SCHEME))) {
95 $url = self::addrToUrl($url);
101 $data = ActivityPub::fetchContent($url);
106 $compacted = JsonLD::compact($data);
109 $apcontact['url'] = $compacted['@id'];
110 $apcontact['uuid'] = JsonLD::fetchElement($compacted, 'diaspora:guid');
111 $apcontact['type'] = str_replace('as:', '', JsonLD::fetchElement($compacted, '@type'));
112 $apcontact['following'] = JsonLD::fetchElement($compacted, 'as:following', '@id');
113 $apcontact['followers'] = JsonLD::fetchElement($compacted, 'as:followers', '@id');
114 $apcontact['inbox'] = JsonLD::fetchElement($compacted, 'ldp:inbox', '@id');
115 $apcontact['outbox'] = JsonLD::fetchElement($compacted, 'as:outbox', '@id');
117 $apcontact['sharedinbox'] = '';
118 if (!empty($compacted['as:endpoints'])) {
119 $apcontact['sharedinbox'] = JsonLD::fetchElement($compacted['as:endpoints'], 'as:sharedInbox', '@id');
122 $apcontact['nick'] = JsonLD::fetchElement($compacted, 'as:preferredUsername');
123 $apcontact['name'] = JsonLD::fetchElement($compacted, 'as:name');
125 if (empty($apcontact['name'])) {
126 $apcontact['name'] = $apcontact['nick'];
129 $apcontact['about'] = HTML::toBBCode(JsonLD::fetchElement($compacted, 'as:summary'));
131 $apcontact['photo'] = JsonLD::fetchElement($compacted, 'as:icon', '@id');
132 if (is_array($apcontact['photo'])) {
133 $apcontact['photo'] = JsonLD::fetchElement($compacted['as:icon'], 'as:url', '@id');
136 $apcontact['alias'] = JsonLD::fetchElement($compacted, 'as:url', '@id');
137 if (is_array($apcontact['alias'])) {
138 $apcontact['alias'] = JsonLD::fetchElement($compacted['as:url'], 'as:href', '@id');
141 if (empty($apcontact['url']) || empty($apcontact['inbox'])) {
145 $parts = parse_url($apcontact['url']);
146 unset($parts['scheme']);
147 unset($parts['path']);
148 $apcontact['addr'] = $apcontact['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts));
150 $apcontact['pubkey'] = trim(JsonLD::fetchElement($compacted, 'w3id:publicKey', 'w3id:publicKeyPem'));
153 // manuallyApprovesFollowers
156 // @context, tag, attachment, image, nomadicLocations, signature, following, followers, featured, movedTo, liked
158 // Unhandled from Misskey
159 // sharedInbox, isCat
161 // Unhandled from Kroeg
162 // kroeg:blocks, updated
164 // Check if the address is resolvable
165 if (self::addrToUrl($apcontact['addr']) == $apcontact['url']) {
166 $parts = parse_url($apcontact['url']);
167 unset($parts['path']);
168 $apcontact['baseurl'] = Network::unparseURL($parts);
170 $apcontact['addr'] = null;
171 $apcontact['baseurl'] = null;
174 if ($apcontact['url'] == $apcontact['alias']) {
175 $apcontact['alias'] = null;
178 $apcontact['updated'] = DateTimeFormat::utcNow();
180 DBA::update('apcontact', $apcontact, ['url' => $url], true);
182 // Update some data in the contact table with various ways to catch them all
183 $contact_fields = ['name' => $apcontact['name'], 'about' => $apcontact['about']];
184 DBA::update('contact', $contact_fields, ['nurl' => normalise_link($url)]);
186 $contacts = DBA::select('contact', ['uid', 'id'], ['nurl' => normalise_link($url)]);
187 while ($contact = DBA::fetch($contacts)) {
188 Contact::updateAvatar($apcontact['photo'], $contact['uid'], $contact['id']);
190 DBA::close($contacts);
192 // Update the gcontact table
193 DBA::update('gcontact', $contact_fields, ['nurl' => normalise_link($url)]);
195 logger('Updated profile for ' . $url, LOGGER_DEBUG);