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