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'));
160 $manually_approve = JsonLD::fetchElement($compacted, 'as:manuallyApprovesFollowers');
165 // @context, tag, attachment, image, nomadicLocations, signature, following, followers, featured, movedTo, liked
167 // Unhandled from Misskey
168 // sharedInbox, isCat
170 // Unhandled from Kroeg
171 // kroeg:blocks, updated
173 // Check if the address is resolvable
174 if (self::addrToUrl($apcontact['addr']) == $apcontact['url']) {
175 $parts = parse_url($apcontact['url']);
176 unset($parts['path']);
177 $apcontact['baseurl'] = Network::unparseURL($parts);
179 $apcontact['addr'] = null;
180 $apcontact['baseurl'] = null;
183 if ($apcontact['url'] == $apcontact['alias']) {
184 $apcontact['alias'] = null;
187 $apcontact['updated'] = DateTimeFormat::utcNow();
189 DBA::update('apcontact', $apcontact, ['url' => $url], true);
191 // Update some data in the contact table with various ways to catch them all
192 $contact_fields = ['name' => $apcontact['name'], 'about' => $apcontact['about']];
194 // Fetch the type and match it with the contact type
195 $contact_types = array_keys(ActivityPub::ACCOUNT_TYPES, $apcontact['type']);
196 if (!empty($contact_types)) {
197 $contact_type = array_pop($contact_types);
198 if (is_int($contact_type)) {
199 $contact_fields['contact-type'] = $contact_type;
201 if ($contact_fields['contact-type'] != Contact::ACCOUNT_TYPE_COMMUNITY) {
202 // Resetting the 'forum' and 'prv' field when it isn't a forum
203 $contact_fields['forum'] = false;
204 $contact_fields['prv'] = false;
206 // Otherwise set the corresponding forum type
207 $contact_fields['forum'] = !$manually_approve;
208 $contact_fields['prv'] = $manually_approve;
213 DBA::update('contact', $contact_fields, ['nurl' => Strings::normaliseLink($url)]);
215 $contacts = DBA::select('contact', ['uid', 'id'], ['nurl' => Strings::normaliseLink($url)]);
216 while ($contact = DBA::fetch($contacts)) {
217 Contact::updateAvatar($apcontact['photo'], $contact['uid'], $contact['id']);
219 DBA::close($contacts);
221 // Update the gcontact table
222 // These two fields don't exist in the gcontact table
223 unset($contact_fields['forum']);
224 unset($contact_fields['prv']);
225 DBA::update('gcontact', $contact_fields, ['nurl' => Strings::normaliseLink($url)]);
227 Logger::log('Updated profile for ' . $url, Logger::DEBUG);