]> git.mxchange.org Git - friendica.git/blob - src/Model/APContact.php
4593e0377b1de25f3d707231e294f89245a4f3c9
[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\Database\DBA;
11 use Friendica\Protocol\ActivityPub;
12 use Friendica\Util\Network;
13 use Friendica\Util\JsonLD;
14 use Friendica\Util\DateTimeFormat;
15 use Friendica\Content\Text\HTML;
16
17 require_once 'boot.php';
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
103                 if (empty($data) || empty($data['id']) || empty($data['inbox'])) {
104                         return false;
105                 }
106
107                 $apcontact = [];
108                 $apcontact['url'] = $data['id'];
109                 $apcontact['uuid'] = defaults($data, 'diaspora:guid', null);
110                 $apcontact['type'] = defaults($data, 'type', null);
111                 $apcontact['following'] = defaults($data, 'following', null);
112                 $apcontact['followers'] = defaults($data, 'followers', null);
113                 $apcontact['inbox'] = defaults($data, 'inbox', null);
114                 $apcontact['outbox'] = defaults($data, 'outbox', null);
115                 $apcontact['sharedinbox'] = JsonLD::fetchElement($data, 'endpoints', 'sharedInbox');
116                 $apcontact['nick'] = defaults($data, 'preferredUsername', null);
117                 $apcontact['name'] = defaults($data, 'name', $apcontact['nick']);
118                 $apcontact['about'] = HTML::toBBCode(defaults($data, 'summary', ''));
119                 $apcontact['photo'] = JsonLD::fetchElement($data, 'icon', 'url');
120                 $apcontact['alias'] = JsonLD::fetchElement($data, 'url', 'href');
121
122                 $parts = parse_url($apcontact['url']);
123                 unset($parts['scheme']);
124                 unset($parts['path']);
125                 $apcontact['addr'] = $apcontact['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts));
126
127                 $apcontact['pubkey'] = trim(JsonLD::fetchElement($data, 'publicKey', 'publicKeyPem'));
128
129                 // To-Do
130                 // manuallyApprovesFollowers
131
132                 // Unhandled
133                 // @context, tag, attachment, image, nomadicLocations, signature, following, followers, featured, movedTo, liked
134
135                 // Unhandled from Misskey
136                 // sharedInbox, isCat
137
138                 // Unhandled from Kroeg
139                 // kroeg:blocks, updated
140
141                 // Check if the address is resolvable
142                 if (self::addrToUrl($apcontact['addr']) == $apcontact['url']) {
143                         $parts = parse_url($apcontact['url']);
144                         unset($parts['path']);
145                         $apcontact['baseurl'] = Network::unparseURL($parts);
146                 } else {
147                         $apcontact['addr'] = null;
148                         $apcontact['baseurl'] = null;
149                 }
150
151                 if ($apcontact['url'] == $apcontact['alias']) {
152                         $apcontact['alias'] = null;
153                 }
154
155                 $apcontact['updated'] = DateTimeFormat::utcNow();
156
157                 DBA::update('apcontact', $apcontact, ['url' => $url], true);
158
159                 // Update some data in the contact table with various ways to catch them all
160                 $contact_fields = ['name' => $apcontact['name'], 'about' => $apcontact['about']];
161                 DBA::update('contact', $contact_fields, ['nurl' => normalise_link($url)]);
162
163                 $contacts = DBA::select('contact', ['uid', 'id'], ['nurl' => normalise_link($url)]);
164                 while ($contact = DBA::fetch($contacts)) {
165                         Contact::updateAvatar($apcontact['photo'], $contact['uid'], $contact['id']);
166                 }
167                 DBA::close($contacts);
168
169                 // Update the gcontact table
170                 DBA::update('gcontact', $contact_fields, ['nurl' => normalise_link($url)]);
171
172                 logger('Updated profile for ' . $url, LOGGER_DEBUG);
173
174                 return $apcontact;
175         }
176 }