4 * @file src/Model/APContact.php
7 namespace Friendica\Model;
9 use Friendica\BaseObject;
10 use Friendica\Core\Logger;
11 use Friendica\Database\DBA;
12 use Friendica\Protocol\ActivityPub;
13 use Friendica\Util\Network;
14 use Friendica\Util\JsonLD;
15 use Friendica\Util\DateTimeFormat;
16 use Friendica\Content\Text\HTML;
18 require_once 'boot.php';
20 class APContact extends BaseObject
23 * Resolves the profile url from the address by using webfinger
25 * @param string $addr profile address (user@domain.tld)
28 private static function addrToUrl($addr)
30 $addr_parts = explode('@', $addr);
31 if (count($addr_parts) != 2) {
35 $webfinger = 'https://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
37 $curlResult = Network::curl($webfinger, false, $redirects, ['accept_content' => 'application/jrd+json,application/json']);
38 if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
42 $data = json_decode($curlResult->getBody(), true);
44 if (empty($data['links'])) {
48 foreach ($data['links'] as $link) {
49 if (empty($link['href']) || empty($link['rel']) || empty($link['type'])) {
53 if (($link['rel'] == 'self') && ($link['type'] == 'application/activity+json')) {
62 * Fetches a profile from a given url
64 * @param string $url profile url
65 * @param boolean $update true = always update, false = never update, null = update when not found
66 * @return array profile array
68 public static function getByURL($url, $update = null)
75 $apcontact = DBA::selectFirst('apcontact', [], ['url' => $url]);
76 if (DBA::isResult($apcontact)) {
80 $apcontact = DBA::selectFirst('apcontact', [], ['alias' => $url]);
81 if (DBA::isResult($apcontact)) {
85 $apcontact = DBA::selectFirst('apcontact', [], ['addr' => $url]);
86 if (DBA::isResult($apcontact)) {
90 if (!is_null($update)) {
95 if (empty(parse_url($url, PHP_URL_SCHEME))) {
96 $url = self::addrToUrl($url);
102 $data = ActivityPub::fetchContent($url);
107 $compacted = JsonLD::compact($data);
110 $apcontact['url'] = $compacted['@id'];
111 $apcontact['uuid'] = JsonLD::fetchElement($compacted, 'diaspora:guid');
112 $apcontact['type'] = str_replace('as:', '', JsonLD::fetchElement($compacted, '@type'));
113 $apcontact['following'] = JsonLD::fetchElement($compacted, 'as:following', '@id');
114 $apcontact['followers'] = JsonLD::fetchElement($compacted, 'as:followers', '@id');
115 $apcontact['inbox'] = JsonLD::fetchElement($compacted, 'ldp:inbox', '@id');
116 $apcontact['outbox'] = JsonLD::fetchElement($compacted, 'as:outbox', '@id');
118 $apcontact['sharedinbox'] = '';
119 if (!empty($compacted['as:endpoints'])) {
120 $apcontact['sharedinbox'] = JsonLD::fetchElement($compacted['as:endpoints'], 'as:sharedInbox', '@id');
123 $apcontact['nick'] = JsonLD::fetchElement($compacted, 'as:preferredUsername');
124 $apcontact['name'] = JsonLD::fetchElement($compacted, 'as:name');
126 if (empty($apcontact['name'])) {
127 $apcontact['name'] = $apcontact['nick'];
130 $apcontact['about'] = HTML::toBBCode(JsonLD::fetchElement($compacted, 'as:summary'));
132 $apcontact['photo'] = JsonLD::fetchElement($compacted, 'as:icon', '@id');
133 if (is_array($apcontact['photo'])) {
134 $apcontact['photo'] = JsonLD::fetchElement($compacted['as:icon'], 'as:url', '@id');
137 $apcontact['alias'] = JsonLD::fetchElement($compacted, 'as:url', '@id');
138 if (is_array($apcontact['alias'])) {
139 $apcontact['alias'] = JsonLD::fetchElement($compacted['as:url'], 'as:href', '@id');
142 if (empty($apcontact['url']) || empty($apcontact['inbox'])) {
146 $parts = parse_url($apcontact['url']);
147 unset($parts['scheme']);
148 unset($parts['path']);
149 $apcontact['addr'] = $apcontact['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts));
151 $apcontact['pubkey'] = trim(JsonLD::fetchElement($compacted, 'w3id:publicKey', 'w3id:publicKeyPem'));
154 // manuallyApprovesFollowers
157 // @context, tag, attachment, image, nomadicLocations, signature, following, followers, featured, movedTo, liked
159 // Unhandled from Misskey
160 // sharedInbox, isCat
162 // Unhandled from Kroeg
163 // kroeg:blocks, updated
165 // Check if the address is resolvable
166 if (self::addrToUrl($apcontact['addr']) == $apcontact['url']) {
167 $parts = parse_url($apcontact['url']);
168 unset($parts['path']);
169 $apcontact['baseurl'] = Network::unparseURL($parts);
171 $apcontact['addr'] = null;
172 $apcontact['baseurl'] = null;
175 if ($apcontact['url'] == $apcontact['alias']) {
176 $apcontact['alias'] = null;
179 $apcontact['updated'] = DateTimeFormat::utcNow();
181 DBA::update('apcontact', $apcontact, ['url' => $url], true);
183 // Update some data in the contact table with various ways to catch them all
184 $contact_fields = ['name' => $apcontact['name'], 'about' => $apcontact['about']];
185 DBA::update('contact', $contact_fields, ['nurl' => normalise_link($url)]);
187 $contacts = DBA::select('contact', ['uid', 'id'], ['nurl' => normalise_link($url)]);
188 while ($contact = DBA::fetch($contacts)) {
189 Contact::updateAvatar($apcontact['photo'], $contact['uid'], $contact['id']);
191 DBA::close($contacts);
193 // Update the gcontact table
194 DBA::update('gcontact', $contact_fields, ['nurl' => normalise_link($url)]);
196 Logger::log('Updated profile for ' . $url, Logger::DEBUG);