]> git.mxchange.org Git - friendica.git/blob - src/Model/APContact.php
Merge pull request #6725 from nupplaphil/6691-rendertime-fix
[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\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;
18
19 class APContact extends BaseObject
20 {
21         /**
22          * Resolves the profile url from the address by using webfinger
23          *
24          * @param string $addr profile address (user@domain.tld)
25          * @return string url
26          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
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 or outdated
66          * @return array profile array
67          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
68          * @throws \ImagickException
69          */
70         public static function getByURL($url, $update = null)
71         {
72                 if (empty($url)) {
73                         return false;
74                 }
75
76                 if (empty($update)) {
77                         if (is_null($update)) {
78                                 $ref_update = DateTimeFormat::utc('now - 1 month');
79                         } else {
80                                 $ref_update = DBA::NULL_DATETIME;
81                         }
82
83                         $apcontact = DBA::selectFirst('apcontact', [], ['url' => $url]);
84                         if (!DBA::isResult($apcontact)) {
85                                 $apcontact = DBA::selectFirst('apcontact', [], ['alias' => $url]);
86                         }
87
88                         if (!DBA::isResult($apcontact)) {
89                                 $apcontact = DBA::selectFirst('apcontact', [], ['addr' => $url]);
90                         }
91
92                         if (DBA::isResult($apcontact) && ($apcontact['updated'] > $ref_update)) {
93                                 return $apcontact;
94                         }
95
96                         if (!is_null($update)) {
97                                 return false;
98                         }
99                 }
100
101                 if (empty(parse_url($url, PHP_URL_SCHEME))) {
102                         $url = self::addrToUrl($url);
103                         if (empty($url)) {
104                                 return false;
105                         }
106                 }
107
108                 $data = ActivityPub::fetchContent($url);
109                 if (empty($data)) {
110                         return false;
111                 }
112
113                 $compacted = JsonLD::compact($data);
114
115                 if (empty($compacted['@id'])) {
116                         return false;
117                 }
118
119                 $apcontact = [];
120                 $apcontact['url'] = $compacted['@id'];
121                 $apcontact['uuid'] = JsonLD::fetchElement($compacted, 'diaspora:guid');
122                 $apcontact['type'] = str_replace('as:', '', JsonLD::fetchElement($compacted, '@type'));
123                 $apcontact['following'] = JsonLD::fetchElement($compacted, 'as:following', '@id');
124                 $apcontact['followers'] = JsonLD::fetchElement($compacted, 'as:followers', '@id');
125                 $apcontact['inbox'] = JsonLD::fetchElement($compacted, 'ldp:inbox', '@id');
126                 $apcontact['outbox'] = JsonLD::fetchElement($compacted, 'as:outbox', '@id');
127
128                 $apcontact['sharedinbox'] = '';
129                 if (!empty($compacted['as:endpoints'])) {
130                         $apcontact['sharedinbox'] = JsonLD::fetchElement($compacted['as:endpoints'], 'as:sharedInbox', '@id');
131                 }
132
133                 $apcontact['nick'] = JsonLD::fetchElement($compacted, 'as:preferredUsername');
134                 $apcontact['name'] = JsonLD::fetchElement($compacted, 'as:name');
135
136                 if (empty($apcontact['name'])) {
137                         $apcontact['name'] = $apcontact['nick'];
138                 }
139
140                 $apcontact['about'] = HTML::toBBCode(JsonLD::fetchElement($compacted, 'as:summary'));
141
142                 $apcontact['photo'] = JsonLD::fetchElement($compacted, 'as:icon', '@id');
143                 if (is_array($apcontact['photo']) || !empty($compacted['as:icon']['as:url']['@id'])) {
144                         $apcontact['photo'] = JsonLD::fetchElement($compacted['as:icon'], 'as:url', '@id');
145                 }
146
147                 $apcontact['alias'] = JsonLD::fetchElement($compacted, 'as:url', '@id');
148                 if (is_array($apcontact['alias'])) {
149                         $apcontact['alias'] = JsonLD::fetchElement($compacted['as:url'], 'as:href', '@id');
150                 }
151
152                 if (empty($apcontact['url']) || empty($apcontact['inbox'])) {
153                         return false;
154                 }
155
156                 $parts = parse_url($apcontact['url']);
157                 unset($parts['scheme']);
158                 unset($parts['path']);
159                 $apcontact['addr'] = $apcontact['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts));
160
161                 $apcontact['pubkey'] = trim(JsonLD::fetchElement($compacted, 'w3id:publicKey', 'w3id:publicKeyPem'));
162
163                 $apcontact['manually-approve'] = (int)JsonLD::fetchElement($compacted, 'as:manuallyApprovesFollowers');
164
165                 // To-Do
166
167                 // Unhandled
168                 // @context, tag, attachment, image, nomadicLocations, signature, following, followers, featured, movedTo, liked
169
170                 // Unhandled from Misskey
171                 // sharedInbox, isCat
172
173                 // Unhandled from Kroeg
174                 // kroeg:blocks, updated
175
176                 // Check if the address is resolvable
177                 if (self::addrToUrl($apcontact['addr']) == $apcontact['url']) {
178                         $parts = parse_url($apcontact['url']);
179                         unset($parts['path']);
180                         $apcontact['baseurl'] = Network::unparseURL($parts);
181                 } else {
182                         $apcontact['addr'] = null;
183                         $apcontact['baseurl'] = null;
184                 }
185
186                 if ($apcontact['url'] == $apcontact['alias']) {
187                         $apcontact['alias'] = null;
188                 }
189
190                 $apcontact['updated'] = DateTimeFormat::utcNow();
191
192                 DBA::update('apcontact', $apcontact, ['url' => $url], true);
193
194                 // Update some data in the contact table with various ways to catch them all
195                 $contact_fields = ['name' => $apcontact['name'], 'about' => $apcontact['about'], 'alias' => $apcontact['alias']];
196
197                 // Fetch the type and match it with the contact type
198                 $contact_types = array_keys(ActivityPub::ACCOUNT_TYPES, $apcontact['type']);
199                 if (!empty($contact_types)) {
200                         $contact_type = array_pop($contact_types);
201                         if (is_int($contact_type)) {
202                                 $contact_fields['contact-type'] = $contact_type;
203
204                                 if ($contact_fields['contact-type'] != User::ACCOUNT_TYPE_COMMUNITY) {
205                                         // Resetting the 'forum' and 'prv' field when it isn't a forum
206                                         $contact_fields['forum'] = false;
207                                         $contact_fields['prv'] = false;
208                                 } else {
209                                         // Otherwise set the corresponding forum type
210                                         $contact_fields['forum'] = !$apcontact['manually-approve'];
211                                         $contact_fields['prv'] = $apcontact['manually-approve'];
212                                 }
213                         }
214                 }
215
216                 DBA::update('contact', $contact_fields, ['nurl' => Strings::normaliseLink($url)]);
217
218                 $contacts = DBA::select('contact', ['uid', 'id'], ['nurl' => Strings::normaliseLink($url)]);
219                 while ($contact = DBA::fetch($contacts)) {
220                         Contact::updateAvatar($apcontact['photo'], $contact['uid'], $contact['id']);
221                 }
222                 DBA::close($contacts);
223
224                 // Update the gcontact table
225                 // These two fields don't exist in the gcontact table
226                 unset($contact_fields['forum']);
227                 unset($contact_fields['prv']);
228                 DBA::update('gcontact', $contact_fields, ['nurl' => Strings::normaliseLink($url)]);
229
230                 Logger::log('Updated profile for ' . $url, Logger::DEBUG);
231
232                 return $apcontact;
233         }
234 }