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