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