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