]> git.mxchange.org Git - friendica.git/blob - src/Model/APContact.php
c7cf54b7209e2d60188bd5f352abcce2dc19c5a4
[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                 $apcontact = [];
110                 $apcontact['url'] = $compacted['@id'];
111                 $apcontact['uuid'] = JsonLD::fetchElement($compacted, 'diaspora:guid');
112                 $apcontact['type'] = str_replace('as:', '', JsonLD::fetchElement($compacted, '@type'));
113                 $apcontact['following'] = JsonLD::fetchElement($compacted, 'as:following', '@id');
114                 $apcontact['followers'] = JsonLD::fetchElement($compacted, 'as:followers', '@id');
115                 $apcontact['inbox'] = JsonLD::fetchElement($compacted, 'ldp:inbox', '@id');
116                 $apcontact['outbox'] = JsonLD::fetchElement($compacted, 'as:outbox', '@id');
117
118                 $apcontact['sharedinbox'] = '';
119                 if (!empty($compacted['as:endpoints'])) {
120                         $apcontact['sharedinbox'] = JsonLD::fetchElement($compacted['as:endpoints'], 'as:sharedInbox', '@id');
121                 }
122
123                 $apcontact['nick'] = JsonLD::fetchElement($compacted, 'as:preferredUsername');
124                 $apcontact['name'] = JsonLD::fetchElement($compacted, 'as:name');
125
126                 if (empty($apcontact['name'])) {
127                         $apcontact['name'] = $apcontact['nick'];
128                 }
129
130                 $apcontact['about'] = HTML::toBBCode(JsonLD::fetchElement($compacted, 'as:summary'));
131
132                 $apcontact['photo'] = JsonLD::fetchElement($compacted, 'as:icon', '@id');
133                 if (is_array($apcontact['photo'])) {
134                         $apcontact['photo'] = JsonLD::fetchElement($compacted['as:icon'], 'as:url', '@id');
135                 }
136
137                 $apcontact['alias'] = JsonLD::fetchElement($compacted, 'as:url', '@id');
138                 if (is_array($apcontact['alias'])) {
139                         $apcontact['alias'] = JsonLD::fetchElement($compacted['as:url'], 'as:href', '@id');
140                 }
141
142                 if (empty($apcontact['url']) || empty($apcontact['inbox'])) {
143                         return false;
144                 }
145
146                 $parts = parse_url($apcontact['url']);
147                 unset($parts['scheme']);
148                 unset($parts['path']);
149                 $apcontact['addr'] = $apcontact['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts));
150
151                 $apcontact['pubkey'] = trim(JsonLD::fetchElement($compacted, 'w3id:publicKey', 'w3id:publicKeyPem'));
152
153                 // To-Do
154                 // manuallyApprovesFollowers
155
156                 // Unhandled
157                 // @context, tag, attachment, image, nomadicLocations, signature, following, followers, featured, movedTo, liked
158
159                 // Unhandled from Misskey
160                 // sharedInbox, isCat
161
162                 // Unhandled from Kroeg
163                 // kroeg:blocks, updated
164
165                 // Check if the address is resolvable
166                 if (self::addrToUrl($apcontact['addr']) == $apcontact['url']) {
167                         $parts = parse_url($apcontact['url']);
168                         unset($parts['path']);
169                         $apcontact['baseurl'] = Network::unparseURL($parts);
170                 } else {
171                         $apcontact['addr'] = null;
172                         $apcontact['baseurl'] = null;
173                 }
174
175                 if ($apcontact['url'] == $apcontact['alias']) {
176                         $apcontact['alias'] = null;
177                 }
178
179                 $apcontact['updated'] = DateTimeFormat::utcNow();
180
181                 DBA::update('apcontact', $apcontact, ['url' => $url], true);
182
183                 // Update some data in the contact table with various ways to catch them all
184                 $contact_fields = ['name' => $apcontact['name'], 'about' => $apcontact['about']];
185                 DBA::update('contact', $contact_fields, ['nurl' => normalise_link($url)]);
186
187                 $contacts = DBA::select('contact', ['uid', 'id'], ['nurl' => normalise_link($url)]);
188                 while ($contact = DBA::fetch($contacts)) {
189                         Contact::updateAvatar($apcontact['photo'], $contact['uid'], $contact['id']);
190                 }
191                 DBA::close($contacts);
192
193                 // Update the gcontact table
194                 DBA::update('gcontact', $contact_fields, ['nurl' => normalise_link($url)]);
195
196                 Logger::log('Updated profile for ' . $url, Logger::DEBUG);
197
198                 return $apcontact;
199         }
200 }