]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub.php
Merge pull request #10707 from annando/issue-10706
[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 ]];
75         const ACCOUNT_TYPES = ['Person', 'Organization', 'Service', 'Group', 'Application', 'Tombstone'];
76         /**
77          * Checks if the web request is done for the AP protocol
78          *
79          * @return bool is it AP?
80          */
81         public static function isRequest()
82         {
83                 return stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/activity+json') ||
84                         stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/json') ||
85                         stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/ld+json');
86         }
87
88         /**
89          * Fetches ActivityPub content from the given url
90          *
91          * @param string  $url content url
92          * @param integer $uid User ID for the signature
93          * @return array
94          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
95          */
96         public static function fetchContent(string $url, int $uid = 0)
97         {
98                 return HTTPSignature::fetch($url, $uid);
99         }
100
101         private static function getAccountType($apcontact)
102         {
103                 $accounttype = -1;
104
105                 switch($apcontact['type']) {
106                         case 'Person':
107                                 $accounttype = User::ACCOUNT_TYPE_PERSON;
108                                 break;
109                         case 'Organization':
110                                 $accounttype = User::ACCOUNT_TYPE_ORGANISATION;
111                                 break;
112                         case 'Service':
113                                 $accounttype = User::ACCOUNT_TYPE_NEWS;
114                                 break;
115                         case 'Group':
116                                 $accounttype = User::ACCOUNT_TYPE_COMMUNITY;
117                                 break;
118                         case 'Application':
119                                 $accounttype = User::ACCOUNT_TYPE_RELAY;
120                                 break;
121                         case 'Tombstone':
122                                 $accounttype = User::ACCOUNT_TYPE_DELETED;
123                                 break;
124                 }
125
126                 return $accounttype;
127         }
128
129         /**
130          * Fetches a profile from the given url into an array that is compatible to Probe::uri
131          *
132          * @param string  $url    profile url
133          * @param boolean $update Update the profile
134          * @return array
135          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
136          * @throws \ImagickException
137          */
138         public static function probeProfile($url, $update = true)
139         {
140                 $apcontact = APContact::getByURL($url, $update);
141                 if (empty($apcontact)) {
142                         return [];
143                 }
144
145                 $profile = ['network' => Protocol::ACTIVITYPUB];
146                 $profile['nick'] = $apcontact['nick'];
147                 $profile['name'] = $apcontact['name'];
148                 $profile['guid'] = $apcontact['uuid'];
149                 $profile['url'] = $apcontact['url'];
150                 $profile['addr'] = $apcontact['addr'];
151                 $profile['alias'] = $apcontact['alias'];
152                 $profile['following'] = $apcontact['following'];
153                 $profile['followers'] = $apcontact['followers'];
154                 $profile['inbox'] = $apcontact['inbox'];
155                 $profile['outbox'] = $apcontact['outbox'];
156                 $profile['sharedinbox'] = $apcontact['sharedinbox'];
157                 $profile['photo'] = $apcontact['photo'];
158                 $profile['header'] = $apcontact['header'];
159                 $profile['account-type'] = self::getAccountType($apcontact);
160                 $profile['community'] = ($profile['account-type'] == User::ACCOUNT_TYPE_COMMUNITY);
161                 // $profile['keywords']
162                 // $profile['location']
163                 $profile['about'] = $apcontact['about'];
164                 $profile['xmpp'] = $apcontact['xmpp'];
165                 $profile['matrix'] = $apcontact['matrix'];
166                 $profile['batch'] = $apcontact['sharedinbox'];
167                 $profile['notify'] = $apcontact['inbox'];
168                 $profile['poll'] = $apcontact['outbox'];
169                 $profile['pubkey'] = $apcontact['pubkey'];
170                 $profile['subscribe'] = $apcontact['subscribe'];
171                 $profile['manually-approve'] = $apcontact['manually-approve'];
172                 $profile['baseurl'] = $apcontact['baseurl'];
173                 $profile['gsid'] = $apcontact['gsid'];
174
175                 if (!is_null($apcontact['discoverable'])) {
176                         $profile['hide'] = !$apcontact['discoverable'];
177                 }
178
179                 // Remove all "null" fields
180                 foreach ($profile as $field => $content) {
181                         if (is_null($content)) {
182                                 unset($profile[$field]);
183                         }
184                 }
185
186                 return $profile;
187         }
188
189         /**
190          * Fetches activities from the outbox of a given profile and processes it
191          *
192          * @param string  $url
193          * @param integer $uid User ID
194          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
195          */
196         public static function fetchOutbox($url, $uid)
197         {
198                 $data = self::fetchContent($url, $uid);
199                 if (empty($data)) {
200                         return;
201                 }
202
203                 if (!empty($data['orderedItems'])) {
204                         $items = $data['orderedItems'];
205                 } elseif (!empty($data['first']['orderedItems'])) {
206                         $items = $data['first']['orderedItems'];
207                 } elseif (!empty($data['first'])) {
208                         self::fetchOutbox($data['first'], $uid);
209                         return;
210                 } else {
211                         $items = [];
212                 }
213
214                 foreach ($items as $activity) {
215                         $ldactivity = JsonLD::compact($activity);
216                         ActivityPub\Receiver::processActivity($ldactivity, '', $uid, true);
217                 }
218         }
219
220         /**
221          * Fetch items from AP endpoints
222          *
223          * @param string $url  Address of the endpoint
224          * @param integer $uid Optional user id
225          * @return array Endpoint items
226          */
227         public static function fetchItems(string $url, int $uid = 0)
228         {
229                 $data = self::fetchContent($url, $uid);
230                 if (empty($data)) {
231                         return [];
232                 }
233
234                 if (!empty($data['orderedItems'])) {
235                         $items = $data['orderedItems'];
236                 } elseif (!empty($data['first']['orderedItems'])) {
237                         $items = $data['first']['orderedItems'];
238                 } elseif (!empty($data['first']) && is_string($data['first']) && ($data['first'] != $url)) {
239                         return self::fetchItems($data['first'], $uid);
240                 } else {
241                         return [];
242                 }
243
244                 if (!empty($data['next']) && is_string($data['next'])) {
245                         $items = array_merge($items, self::fetchItems($data['next'], $uid));
246                 }
247
248                 return $items;
249         }
250
251         /**
252          * Checks if the given contact url does support ActivityPub
253          *
254          * @param string  $url    profile url
255          * @param boolean $update true = always update, false = never update, null = update when not found or outdated
256          * @return boolean
257          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
258          * @throws \ImagickException
259          */
260         public static function isSupportedByContactUrl($url, $update = null)
261         {
262                 return !empty(APContact::getByURL($url, $update));
263         }
264 }