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