4 * @file src/Model/APContact.php
7 namespace Friendica\Model;
9 use Friendica\BaseObject;
10 use Friendica\Content\Text\HTML;
11 use Friendica\Core\Logger;
12 use Friendica\Database\DBA;
13 use Friendica\Protocol\ActivityPub;
14 use Friendica\Util\Network;
15 use Friendica\Util\JsonLD;
16 use Friendica\Util\DateTimeFormat;
17 use Friendica\Util\Strings;
19 require_once 'boot.php';
21 class APContact extends BaseObject
24 * Resolves the profile url from the address by using webfinger
26 * @param string $addr profile address (user@domain.tld)
29 private static function addrToUrl($addr)
31 $addr_parts = explode('@', $addr);
32 if (count($addr_parts) != 2) {
36 $webfinger = 'https://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
38 $curlResult = Network::curl($webfinger, false, $redirects, ['accept_content' => 'application/jrd+json,application/json']);
39 if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
43 $data = json_decode($curlResult->getBody(), true);
45 if (empty($data['links'])) {
49 foreach ($data['links'] as $link) {
50 if (empty($link['href']) || empty($link['rel']) || empty($link['type'])) {
54 if (($link['rel'] == 'self') && ($link['type'] == 'application/activity+json')) {
63 * Fetches a profile from a given url
65 * @param string $url profile url
66 * @param boolean $update true = always update, false = never update, null = update when not found
67 * @return array profile array
69 public static function getByURL($url, $update = null)
76 $apcontact = DBA::selectFirst('apcontact', [], ['url' => $url]);
77 if (DBA::isResult($apcontact)) {
81 $apcontact = DBA::selectFirst('apcontact', [], ['alias' => $url]);
82 if (DBA::isResult($apcontact)) {
86 $apcontact = DBA::selectFirst('apcontact', [], ['addr' => $url]);
87 if (DBA::isResult($apcontact)) {
91 if (!is_null($update)) {
96 if (empty(parse_url($url, PHP_URL_SCHEME))) {
97 $url = self::addrToUrl($url);
103 $data = ActivityPub::fetchContent($url);
108 $compacted = JsonLD::compact($data);
110 if (empty($compacted['@id'])) {
115 $apcontact['url'] = $compacted['@id'];
116 $apcontact['uuid'] = JsonLD::fetchElement($compacted, 'diaspora:guid');
117 $apcontact['type'] = str_replace('as:', '', JsonLD::fetchElement($compacted, '@type'));
118 $apcontact['following'] = JsonLD::fetchElement($compacted, 'as:following', '@id');
119 $apcontact['followers'] = JsonLD::fetchElement($compacted, 'as:followers', '@id');
120 $apcontact['inbox'] = JsonLD::fetchElement($compacted, 'ldp:inbox', '@id');
121 $apcontact['outbox'] = JsonLD::fetchElement($compacted, 'as:outbox', '@id');
123 $apcontact['sharedinbox'] = '';
124 if (!empty($compacted['as:endpoints'])) {
125 $apcontact['sharedinbox'] = JsonLD::fetchElement($compacted['as:endpoints'], 'as:sharedInbox', '@id');
128 $apcontact['nick'] = JsonLD::fetchElement($compacted, 'as:preferredUsername');
129 $apcontact['name'] = JsonLD::fetchElement($compacted, 'as:name');
131 if (empty($apcontact['name'])) {
132 $apcontact['name'] = $apcontact['nick'];
135 $apcontact['about'] = HTML::toBBCode(JsonLD::fetchElement($compacted, 'as:summary'));
137 $apcontact['photo'] = JsonLD::fetchElement($compacted, 'as:icon', '@id');
138 if (is_array($apcontact['photo'])) {
139 $apcontact['photo'] = JsonLD::fetchElement($compacted['as:icon'], 'as:url', '@id');
142 $apcontact['alias'] = JsonLD::fetchElement($compacted, 'as:url', '@id');
143 if (is_array($apcontact['alias'])) {
144 $apcontact['alias'] = JsonLD::fetchElement($compacted['as:url'], 'as:href', '@id');
147 if (empty($apcontact['url']) || empty($apcontact['inbox'])) {
151 $parts = parse_url($apcontact['url']);
152 unset($parts['scheme']);
153 unset($parts['path']);
154 $apcontact['addr'] = $apcontact['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts));
156 $apcontact['pubkey'] = trim(JsonLD::fetchElement($compacted, 'w3id:publicKey', 'w3id:publicKeyPem'));
159 // manuallyApprovesFollowers
162 // @context, tag, attachment, image, nomadicLocations, signature, following, followers, featured, movedTo, liked
164 // Unhandled from Misskey
165 // sharedInbox, isCat
167 // Unhandled from Kroeg
168 // kroeg:blocks, updated
170 // Check if the address is resolvable
171 if (self::addrToUrl($apcontact['addr']) == $apcontact['url']) {
172 $parts = parse_url($apcontact['url']);
173 unset($parts['path']);
174 $apcontact['baseurl'] = Network::unparseURL($parts);
176 $apcontact['addr'] = null;
177 $apcontact['baseurl'] = null;
180 if ($apcontact['url'] == $apcontact['alias']) {
181 $apcontact['alias'] = null;
184 $apcontact['updated'] = DateTimeFormat::utcNow();
186 DBA::update('apcontact', $apcontact, ['url' => $url], true);
188 // Update some data in the contact table with various ways to catch them all
189 $contact_fields = ['name' => $apcontact['name'], 'about' => $apcontact['about']];
190 DBA::update('contact', $contact_fields, ['nurl' => Strings::normaliseLink($url)]);
192 $contacts = DBA::select('contact', ['uid', 'id'], ['nurl' => Strings::normaliseLink($url)]);
193 while ($contact = DBA::fetch($contacts)) {
194 Contact::updateAvatar($apcontact['photo'], $contact['uid'], $contact['id']);
196 DBA::close($contacts);
198 // Update the gcontact table
199 DBA::update('gcontact', $contact_fields, ['nurl' => Strings::normaliseLink($url)]);
201 Logger::log('Updated profile for ' . $url, Logger::DEBUG);