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