]> git.mxchange.org Git - friendica.git/blob - src/Model/APContact.php
Preparations for a relocation message / fix for notice
[friendica.git] / src / Model / APContact.php
1 <?php
2
3 /**
4  * @file src/Model/APContact.php
5  */
6
7 namespace Friendica\Model;
8
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;
17
18 require_once 'boot.php';
19
20 class APContact extends BaseObject
21 {
22         /**
23          * Resolves the profile url from the address by using webfinger
24          *
25          * @param string $addr profile address (user@domain.tld)
26          * @return string url
27          */
28         private static function addrToUrl($addr)
29         {
30                 $addr_parts = explode('@', $addr);
31                 if (count($addr_parts) != 2) {
32                         return false;
33                 }
34
35                 $webfinger = 'https://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
36
37                 $curlResult = Network::curl($webfinger, false, $redirects, ['accept_content' => 'application/jrd+json,application/json']);
38                 if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
39                         return false;
40                 }
41
42                 $data = json_decode($curlResult->getBody(), true);
43
44                 if (empty($data['links'])) {
45                         return false;
46                 }
47
48                 foreach ($data['links'] as $link) {
49                         if (empty($link['href']) || empty($link['rel']) || empty($link['type'])) {
50                                 continue;
51                         }
52
53                         if (($link['rel'] == 'self') && ($link['type'] == 'application/activity+json')) {
54                                 return $link['href'];
55                         }
56                 }
57
58                 return false;
59         }
60
61         /**
62          * Fetches a profile from a given url
63          *
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
67          */
68         public static function getByURL($url, $update = null)
69         {
70                 if (empty($url)) {
71                         return false;
72                 }
73
74                 if (empty($update)) {
75                         $apcontact = DBA::selectFirst('apcontact', [], ['url' => $url]);
76                         if (DBA::isResult($apcontact)) {
77                                 return $apcontact;
78                         }
79
80                         $apcontact = DBA::selectFirst('apcontact', [], ['alias' => $url]);
81                         if (DBA::isResult($apcontact)) {
82                                 return $apcontact;
83                         }
84
85                         $apcontact = DBA::selectFirst('apcontact', [], ['addr' => $url]);
86                         if (DBA::isResult($apcontact)) {
87                                 return $apcontact;
88                         }
89
90                         if (!is_null($update)) {
91                                 return false;
92                         }
93                 }
94
95                 if (empty(parse_url($url, PHP_URL_SCHEME))) {
96                         $url = self::addrToUrl($url);
97                         if (empty($url)) {
98                                 return false;
99                         }
100                 }
101
102                 $data = ActivityPub::fetchContent($url);
103                 if (empty($data)) {
104                         return false;
105                 }
106
107                 $compacted = JsonLD::compact($data);
108
109                 if (empty($compacted['@id'])) {
110                         return false;
111                 }
112
113                 $apcontact = [];
114                 $apcontact['url'] = $compacted['@id'];
115                 $apcontact['uuid'] = JsonLD::fetchElement($compacted, 'diaspora:guid');
116                 $apcontact['type'] = str_replace('as:', '', JsonLD::fetchElement($compacted, '@type'));
117                 $apcontact['following'] = JsonLD::fetchElement($compacted, 'as:following', '@id');
118                 $apcontact['followers'] = JsonLD::fetchElement($compacted, 'as:followers', '@id');
119                 $apcontact['inbox'] = JsonLD::fetchElement($compacted, 'ldp:inbox', '@id');
120                 $apcontact['outbox'] = JsonLD::fetchElement($compacted, 'as:outbox', '@id');
121
122                 $apcontact['sharedinbox'] = '';
123                 if (!empty($compacted['as:endpoints'])) {
124                         $apcontact['sharedinbox'] = JsonLD::fetchElement($compacted['as:endpoints'], 'as:sharedInbox', '@id');
125                 }
126
127                 $apcontact['nick'] = JsonLD::fetchElement($compacted, 'as:preferredUsername');
128                 $apcontact['name'] = JsonLD::fetchElement($compacted, 'as:name');
129
130                 if (empty($apcontact['name'])) {
131                         $apcontact['name'] = $apcontact['nick'];
132                 }
133
134                 $apcontact['about'] = HTML::toBBCode(JsonLD::fetchElement($compacted, 'as:summary'));
135
136                 $apcontact['photo'] = JsonLD::fetchElement($compacted, 'as:icon', '@id');
137                 if (is_array($apcontact['photo'])) {
138                         $apcontact['photo'] = JsonLD::fetchElement($compacted['as:icon'], 'as:url', '@id');
139                 }
140
141                 $apcontact['alias'] = JsonLD::fetchElement($compacted, 'as:url', '@id');
142                 if (is_array($apcontact['alias'])) {
143                         $apcontact['alias'] = JsonLD::fetchElement($compacted['as:url'], 'as:href', '@id');
144                 }
145
146                 if (empty($apcontact['url']) || empty($apcontact['inbox'])) {
147                         return false;
148                 }
149
150                 $parts = parse_url($apcontact['url']);
151                 unset($parts['scheme']);
152                 unset($parts['path']);
153                 $apcontact['addr'] = $apcontact['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts));
154
155                 $apcontact['pubkey'] = trim(JsonLD::fetchElement($compacted, 'w3id:publicKey', 'w3id:publicKeyPem'));
156
157                 // To-Do
158                 // manuallyApprovesFollowers
159
160                 // Unhandled
161                 // @context, tag, attachment, image, nomadicLocations, signature, following, followers, featured, movedTo, liked
162
163                 // Unhandled from Misskey
164                 // sharedInbox, isCat
165
166                 // Unhandled from Kroeg
167                 // kroeg:blocks, updated
168
169                 // Check if the address is resolvable
170                 if (self::addrToUrl($apcontact['addr']) == $apcontact['url']) {
171                         $parts = parse_url($apcontact['url']);
172                         unset($parts['path']);
173                         $apcontact['baseurl'] = Network::unparseURL($parts);
174                 } else {
175                         $apcontact['addr'] = null;
176                         $apcontact['baseurl'] = null;
177                 }
178
179                 if ($apcontact['url'] == $apcontact['alias']) {
180                         $apcontact['alias'] = null;
181                 }
182
183                 $apcontact['updated'] = DateTimeFormat::utcNow();
184
185                 DBA::update('apcontact', $apcontact, ['url' => $url], true);
186
187                 // Update some data in the contact table with various ways to catch them all
188                 $contact_fields = ['name' => $apcontact['name'], 'about' => $apcontact['about']];
189                 DBA::update('contact', $contact_fields, ['nurl' => normalise_link($url)]);
190
191                 $contacts = DBA::select('contact', ['uid', 'id'], ['nurl' => normalise_link($url)]);
192                 while ($contact = DBA::fetch($contacts)) {
193                         Contact::updateAvatar($apcontact['photo'], $contact['uid'], $contact['id']);
194                 }
195                 DBA::close($contacts);
196
197                 // Update the gcontact table
198                 DBA::update('gcontact', $contact_fields, ['nurl' => normalise_link($url)]);
199
200                 Logger::log('Updated profile for ' . $url, Logger::DEBUG);
201
202                 return $apcontact;
203         }
204 }