]> git.mxchange.org Git - friendica.git/blob - src/Model/APContact.php
2b5a5c4e7b15015636170933b764d82e31071b75
[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          */
27         private static function addrToUrl($addr)
28         {
29                 $addr_parts = explode('@', $addr);
30                 if (count($addr_parts) != 2) {
31                         return false;
32                 }
33
34                 $webfinger = 'https://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
35
36                 $curlResult = Network::curl($webfinger, false, $redirects, ['accept_content' => 'application/jrd+json,application/json']);
37                 if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
38                         return false;
39                 }
40
41                 $data = json_decode($curlResult->getBody(), true);
42
43                 if (empty($data['links'])) {
44                         return false;
45                 }
46
47                 foreach ($data['links'] as $link) {
48                         if (empty($link['href']) || empty($link['rel']) || empty($link['type'])) {
49                                 continue;
50                         }
51
52                         if (($link['rel'] == 'self') && ($link['type'] == 'application/activity+json')) {
53                                 return $link['href'];
54                         }
55                 }
56
57                 return false;
58         }
59
60         /**
61          * Fetches a profile from a given url
62          *
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
66          */
67         public static function getByURL($url, $update = null)
68         {
69                 if (empty($url)) {
70                         return false;
71                 }
72
73                 if (empty($update)) {
74                         if (is_null($update)) {
75                                 $ref_update = DateTimeFormat::utc('now - 1 month');
76                         } else {
77                                 $ref_update = DBA::NULL_DATETIME;
78                         }
79
80                         $apcontact = DBA::selectFirst('apcontact', [], ['url' => $url]);
81                         if (!DBA::isResult($apcontact)) {
82                                 $apcontact = DBA::selectFirst('apcontact', [], ['alias' => $url]);
83                         }
84
85                         if (!DBA::isResult($apcontact)) {
86                                 $apcontact = DBA::selectFirst('apcontact', [], ['addr' => $url]);
87                         }
88
89                         if (DBA::isResult($apcontact) && ($apcontact['updated'] > $ref_update)) {
90                                 return $apcontact;
91                         }
92
93                         if (!is_null($update)) {
94                                 return false;
95                         }
96                 }
97
98                 if (empty(parse_url($url, PHP_URL_SCHEME))) {
99                         $url = self::addrToUrl($url);
100                         if (empty($url)) {
101                                 return false;
102                         }
103                 }
104
105                 $data = ActivityPub::fetchContent($url);
106                 if (empty($data)) {
107                         return false;
108                 }
109
110                 $compacted = JsonLD::compact($data);
111
112                 if (empty($compacted['@id'])) {
113                         return false;
114                 }
115
116                 $apcontact = [];
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');
124
125                 $apcontact['sharedinbox'] = '';
126                 if (!empty($compacted['as:endpoints'])) {
127                         $apcontact['sharedinbox'] = JsonLD::fetchElement($compacted['as:endpoints'], 'as:sharedInbox', '@id');
128                 }
129
130                 $apcontact['nick'] = JsonLD::fetchElement($compacted, 'as:preferredUsername');
131                 $apcontact['name'] = JsonLD::fetchElement($compacted, 'as:name');
132
133                 if (empty($apcontact['name'])) {
134                         $apcontact['name'] = $apcontact['nick'];
135                 }
136
137                 $apcontact['about'] = HTML::toBBCode(JsonLD::fetchElement($compacted, 'as:summary'));
138
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');
142                 }
143
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');
147                 }
148
149                 if (empty($apcontact['url']) || empty($apcontact['inbox'])) {
150                         return false;
151                 }
152
153                 $parts = parse_url($apcontact['url']);
154                 unset($parts['scheme']);
155                 unset($parts['path']);
156                 $apcontact['addr'] = $apcontact['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts));
157
158                 $apcontact['pubkey'] = trim(JsonLD::fetchElement($compacted, 'w3id:publicKey', 'w3id:publicKeyPem'));
159
160                 $manually_approve = JsonLD::fetchElement($compacted, 'as:manuallyApprovesFollowers');
161
162                 // To-Do
163
164                 // Unhandled
165                 // @context, tag, attachment, image, nomadicLocations, signature, following, followers, featured, movedTo, liked
166
167                 // Unhandled from Misskey
168                 // sharedInbox, isCat
169
170                 // Unhandled from Kroeg
171                 // kroeg:blocks, updated
172
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);
178                 } else {
179                         $apcontact['addr'] = null;
180                         $apcontact['baseurl'] = null;
181                 }
182
183                 if ($apcontact['url'] == $apcontact['alias']) {
184                         $apcontact['alias'] = null;
185                 }
186
187                 $apcontact['updated'] = DateTimeFormat::utcNow();
188
189                 DBA::update('apcontact', $apcontact, ['url' => $url], true);
190
191                 // Update some data in the contact table with various ways to catch them all
192                 $contact_fields = ['name' => $apcontact['name'], 'about' => $apcontact['about']];
193
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;
200
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;
205                                 } else {
206                                         // Otherwise set the corresponding forum type
207                                         $contact_fields['forum'] = !$manually_approve;
208                                         $contact_fields['prv'] = $manually_approve;
209                                 }
210                         }
211                 }
212
213                 DBA::update('contact', $contact_fields, ['nurl' => Strings::normaliseLink($url)]);
214
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']);
218                 }
219                 DBA::close($contacts);
220
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)]);
226
227                 Logger::log('Updated profile for ' . $url, Logger::DEBUG);
228
229                 return $apcontact;
230         }
231 }