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