]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub.php
85a387229f344d59ae372885630d51e64e316dd9
[friendica.git] / src / Protocol / ActivityPub.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Protocol;
23
24 use Friendica\Core\Protocol;
25 use Friendica\Model\APContact;
26 use Friendica\Model\User;
27 use Friendica\Util\HTTPSignature;
28 use Friendica\Util\JsonLD;
29
30 /**
31  * ActivityPub Protocol class
32  *
33  * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
34  * https://www.w3.org/TR/activitypub/
35  * https://www.w3.org/TR/activitystreams-core/
36  * https://www.w3.org/TR/activitystreams-vocabulary/
37  *
38  * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
39  * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
40  *
41  * Digest: https://tools.ietf.org/html/rfc5843
42  * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
43  *
44  * Mastodon implementation of supported activities:
45  * https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/activity.rb#L26
46  *
47  * Funkwhale:
48  * http://docs-funkwhale-funkwhale-549-music-federation-documentation.preview.funkwhale.audio/federation/index.html
49  *
50  * To-do:
51  * - Polling the outboxes for missing content?
52  *
53  * Missing parts from DFRN:
54  * - Public Forum
55  * - Private Forum
56  * - Relocation
57  */
58 class ActivityPub
59 {
60         const PUBLIC_COLLECTION = 'https://www.w3.org/ns/activitystreams#Public';
61         const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
62                 ['vcard' => 'http://www.w3.org/2006/vcard/ns#',
63                 'dfrn' => 'http://purl.org/macgirvin/dfrn/1.0/',
64                 'diaspora' => 'https://diasporafoundation.org/ns/',
65                 'litepub' => 'http://litepub.social/ns#',
66                 'toot' => 'http://joinmastodon.org/ns#',
67                 'schema' => 'http://schema.org#',
68                 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
69                 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag',
70                 'directMessage' => 'litepub:directMessage',
71                 'discoverable' => 'toot:discoverable',
72                 'PropertyValue' => 'schema:PropertyValue',
73                 'value' => 'schema:value']];
74         const ACCOUNT_TYPES = ['Person', 'Organization', 'Service', 'Group', 'Application', 'Tombstone'];
75         /**
76          * Checks if the web request is done for the AP protocol
77          *
78          * @return bool is it AP?
79          */
80         public static function isRequest()
81         {
82                 return stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/activity+json') ||
83                         stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/json') ||
84                         stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/ld+json');
85         }
86
87         /**
88          * Fetches ActivityPub content from the given url
89          *
90          * @param string  $url content url
91          * @param integer $uid User ID for the signature
92          * @return array
93          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
94          */
95         public static function fetchContent(string $url, int $uid = 0)
96         {
97                 return HTTPSignature::fetch($url, $uid);
98         }
99
100         private static function getAccountType($apcontact)
101         {
102                 $accounttype = -1;
103
104                 switch($apcontact['type']) {
105                         case 'Person':
106                                 $accounttype = User::ACCOUNT_TYPE_PERSON;
107                                 break;
108                         case 'Organization':
109                                 $accounttype = User::ACCOUNT_TYPE_ORGANISATION;
110                                 break;
111                         case 'Service':
112                                 $accounttype = User::ACCOUNT_TYPE_NEWS;
113                                 break;
114                         case 'Group':
115                                 $accounttype = User::ACCOUNT_TYPE_COMMUNITY;
116                                 break;
117                         case 'Application':
118                                 $accounttype = User::ACCOUNT_TYPE_RELAY;
119                                 break;
120                         case 'Tombstone':
121                                 $accounttype = User::ACCOUNT_TYPE_DELETED;
122                                 break;
123                 }
124
125                 return $accounttype;
126         }
127
128         /**
129          * Fetches a profile from the given url into an array that is compatible to Probe::uri
130          *
131          * @param string  $url    profile url
132          * @param boolean $update Update the profile
133          * @return array
134          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
135          * @throws \ImagickException
136          */
137         public static function probeProfile($url, $update = true)
138         {
139                 $apcontact = APContact::getByURL($url, $update);
140                 if (empty($apcontact)) {
141                         return [];
142                 }
143
144                 $profile = ['network' => Protocol::ACTIVITYPUB];
145                 $profile['nick'] = $apcontact['nick'];
146                 $profile['name'] = $apcontact['name'];
147                 $profile['guid'] = $apcontact['uuid'];
148                 $profile['url'] = $apcontact['url'];
149                 $profile['addr'] = $apcontact['addr'];
150                 $profile['alias'] = $apcontact['alias'];
151                 $profile['following'] = $apcontact['following'];
152                 $profile['followers'] = $apcontact['followers'];
153                 $profile['inbox'] = $apcontact['inbox'];
154                 $profile['outbox'] = $apcontact['outbox'];
155                 $profile['sharedinbox'] = $apcontact['sharedinbox'];
156                 $profile['photo'] = $apcontact['photo'];
157                 $profile['header'] = $apcontact['header'];
158                 $profile['account-type'] = self::getAccountType($apcontact);
159                 $profile['community'] = ($profile['account-type'] == User::ACCOUNT_TYPE_COMMUNITY);
160                 // $profile['keywords']
161                 // $profile['location']
162                 $profile['about'] = $apcontact['about'];
163                 $profile['xmpp'] = $apcontact['xmpp'];
164                 $profile['matrix'] = $apcontact['matrix'];
165                 $profile['batch'] = $apcontact['sharedinbox'];
166                 $profile['notify'] = $apcontact['inbox'];
167                 $profile['poll'] = $apcontact['outbox'];
168                 $profile['pubkey'] = $apcontact['pubkey'];
169                 $profile['subscribe'] = $apcontact['subscribe'];
170                 $profile['manually-approve'] = $apcontact['manually-approve'];
171                 $profile['baseurl'] = $apcontact['baseurl'];
172                 $profile['gsid'] = $apcontact['gsid'];
173
174                 if (!is_null($apcontact['discoverable'])) {
175                         $profile['hide'] = !$apcontact['discoverable'];
176                 }
177
178                 // Remove all "null" fields
179                 foreach ($profile as $field => $content) {
180                         if (is_null($content)) {
181                                 unset($profile[$field]);
182                         }
183                 }
184
185                 return $profile;
186         }
187
188         /**
189          * Fetches activities from the outbox of a given profile and processes it
190          *
191          * @param string  $url
192          * @param integer $uid User ID
193          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
194          */
195         public static function fetchOutbox($url, $uid)
196         {
197                 $data = self::fetchContent($url, $uid);
198                 if (empty($data)) {
199                         return;
200                 }
201
202                 if (!empty($data['orderedItems'])) {
203                         $items = $data['orderedItems'];
204                 } elseif (!empty($data['first']['orderedItems'])) {
205                         $items = $data['first']['orderedItems'];
206                 } elseif (!empty($data['first'])) {
207                         self::fetchOutbox($data['first'], $uid);
208                         return;
209                 } else {
210                         $items = [];
211                 }
212
213                 foreach ($items as $activity) {
214                         $ldactivity = JsonLD::compact($activity);
215                         ActivityPub\Receiver::processActivity($ldactivity, '', $uid, true);
216                 }
217         }
218
219         /**
220          * Fetch items from AP endpoints
221          *
222          * @param string $url  Address of the endpoint
223          * @param integer $uid Optional user id
224          * @return array Endpoint items
225          */
226         public static function fetchItems(string $url, int $uid = 0)
227         {
228                 $data = self::fetchContent($url, $uid);
229                 if (empty($data)) {
230                         return [];
231                 }
232
233                 if (!empty($data['orderedItems'])) {
234                         $items = $data['orderedItems'];
235                 } elseif (!empty($data['first']['orderedItems'])) {
236                         $items = $data['first']['orderedItems'];
237                 } elseif (!empty($data['first']) && is_string($data['first']) && ($data['first'] != $url)) {
238                         return self::fetchItems($data['first'], $uid);
239                 } else {
240                         return [];
241                 }
242
243                 if (!empty($data['next']) && is_string($data['next'])) {
244                         $items = array_merge($items, self::fetchItems($data['next'], $uid));
245                 }
246
247                 return $items;
248         }
249
250         /**
251          * Checks if the given contact url does support ActivityPub
252          *
253          * @param string  $url    profile url
254          * @param boolean $update true = always update, false = never update, null = update when not found or outdated
255          * @return boolean
256          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
257          * @throws \ImagickException
258          */
259         public static function isSupportedByContactUrl($url, $update = null)
260         {
261                 return !empty(APContact::getByURL($url, $update));
262         }
263 }