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 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 or outdated
65 * @return array profile array
67 public static function getByURL($url, $update = null)
74 if (is_null($update)) {
75 $ref_update = DateTimeFormat::utc('now - 1 month');
77 $ref_update = DBA::NULL_DATETIME;
80 $apcontact = DBA::selectFirst('apcontact', [], ['url' => $url]);
81 if (!DBA::isResult($apcontact)) {
82 $apcontact = DBA::selectFirst('apcontact', [], ['alias' => $url]);
85 if (!DBA::isResult($apcontact)) {
86 $apcontact = DBA::selectFirst('apcontact', [], ['addr' => $url]);
89 if (DBA::isResult($apcontact) && ($apcontact['updated'] > $ref_update)) {
93 if (!is_null($update)) {
98 if (empty(parse_url($url, PHP_URL_SCHEME))) {
99 $url = self::addrToUrl($url);
105 $data = ActivityPub::fetchContent($url);
110 $compacted = JsonLD::compact($data);
112 if (empty($compacted['@id'])) {
117 $apcontact['url'] = $compacted['@id'];
118 $apcontact['uuid'] = JsonLD::fetchElement($compacted, 'diaspora:guid');
119 $apcontact['type'] = str_replace('as:', '', JsonLD::fetchElement($compacted, '@type'));
120 $apcontact['following'] = JsonLD::fetchElement($compacted, 'as:following', '@id');
121 $apcontact['followers'] = JsonLD::fetchElement($compacted, 'as:followers', '@id');
122 $apcontact['inbox'] = JsonLD::fetchElement($compacted, 'ldp:inbox', '@id');
123 $apcontact['outbox'] = JsonLD::fetchElement($compacted, 'as:outbox', '@id');
125 $apcontact['sharedinbox'] = '';
126 if (!empty($compacted['as:endpoints'])) {
127 $apcontact['sharedinbox'] = JsonLD::fetchElement($compacted['as:endpoints'], 'as:sharedInbox', '@id');
130 $apcontact['nick'] = JsonLD::fetchElement($compacted, 'as:preferredUsername');
131 $apcontact['name'] = JsonLD::fetchElement($compacted, 'as:name');
133 if (empty($apcontact['name'])) {
134 $apcontact['name'] = $apcontact['nick'];
137 $apcontact['about'] = HTML::toBBCode(JsonLD::fetchElement($compacted, 'as:summary'));
139 $apcontact['photo'] = JsonLD::fetchElement($compacted, 'as:icon', '@id');
140 if (is_array($apcontact['photo'])) {
141 $apcontact['photo'] = JsonLD::fetchElement($compacted['as:icon'], 'as:url', '@id');
144 $apcontact['alias'] = JsonLD::fetchElement($compacted, 'as:url', '@id');
145 if (is_array($apcontact['alias'])) {
146 $apcontact['alias'] = JsonLD::fetchElement($compacted['as:url'], 'as:href', '@id');
149 if (empty($apcontact['url']) || empty($apcontact['inbox'])) {
153 $parts = parse_url($apcontact['url']);
154 unset($parts['scheme']);
155 unset($parts['path']);
156 $apcontact['addr'] = $apcontact['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts));
158 $apcontact['pubkey'] = trim(JsonLD::fetchElement($compacted, 'w3id:publicKey', 'w3id:publicKeyPem'));
161 // manuallyApprovesFollowers
164 // @context, tag, attachment, image, nomadicLocations, signature, following, followers, featured, movedTo, liked
166 // Unhandled from Misskey
167 // sharedInbox, isCat
169 // Unhandled from Kroeg
170 // kroeg:blocks, updated
172 // Check if the address is resolvable
173 if (self::addrToUrl($apcontact['addr']) == $apcontact['url']) {
174 $parts = parse_url($apcontact['url']);
175 unset($parts['path']);
176 $apcontact['baseurl'] = Network::unparseURL($parts);
178 $apcontact['addr'] = null;
179 $apcontact['baseurl'] = null;
182 if ($apcontact['url'] == $apcontact['alias']) {
183 $apcontact['alias'] = null;
186 $apcontact['updated'] = DateTimeFormat::utcNow();
188 DBA::update('apcontact', $apcontact, ['url' => $url], true);
190 // Update some data in the contact table with various ways to catch them all
191 $contact_fields = ['name' => $apcontact['name'], 'about' => $apcontact['about']];
193 // Fetch the type and match it with the contact type
194 $contact_types = array_keys(ActivityPub::ACCOUNT_TYPES, $apcontact['type']);
195 if (!empty($contact_types)) {
196 $contact_type = array_pop($contact_types);
197 if (is_int($contact_type)) {
198 $contact_fields['contact-type'] = $contact_type;
202 DBA::update('contact', $contact_fields, ['nurl' => Strings::normaliseLink($url)]);
204 $contacts = DBA::select('contact', ['uid', 'id'], ['nurl' => Strings::normaliseLink($url)]);
205 while ($contact = DBA::fetch($contacts)) {
206 Contact::updateAvatar($apcontact['photo'], $contact['uid'], $contact['id']);
208 DBA::close($contacts);
210 // Update the gcontact table
211 DBA::update('gcontact', $contact_fields, ['nurl' => Strings::normaliseLink($url)]);
213 Logger::log('Updated profile for ' . $url, Logger::DEBUG);