]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub.php
23eb861721279a4a66a454306696d7b0c0342ddc
[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\Network;
8 use Friendica\Core\Protocol;
9 use Friendica\Model\APContact;
10
11 /**
12  * @brief ActivityPub Protocol class
13  * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
14  * https://www.w3.org/TR/activitypub/
15  * https://www.w3.org/TR/activitystreams-core/
16  * https://www.w3.org/TR/activitystreams-vocabulary/
17  *
18  * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
19  * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
20  *
21  * Digest: https://tools.ietf.org/html/rfc5843
22  * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
23  *
24  * Mastodon implementation of supported activities:
25  * https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/activity.rb#L26
26  *
27  * Funkwhale:
28  * http://docs-funkwhale-funkwhale-549-music-federation-documentation.preview.funkwhale.audio/federation/index.html
29  *
30  * To-do:
31  * - Polling the outboxes for missing content?
32  */
33 class ActivityPub
34 {
35         const PUBLIC_COLLECTION = 'https://www.w3.org/ns/activitystreams#Public';
36         const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
37                 ['vcard' => 'http://www.w3.org/2006/vcard/ns#',
38                 'diaspora' => 'https://diasporafoundation.org/ns/',
39                 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
40                 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag']];
41         const ACCOUNT_TYPES = ['Person', 'Organization', 'Service', 'Group', 'Application'];
42         /**
43          * Checks if the web request is done for the AP protocol
44          *
45          * @return is it AP?
46          */
47         public static function isRequest()
48         {
49                 return stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json') ||
50                         stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/ld+json');
51         }
52
53         /**
54          * Fetches ActivityPub content from the given url
55          *
56          * @param string $url content url
57          * @return array
58          */
59         public static function fetchContent($url)
60         {
61                 $curlResult = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']);
62                 if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
63                         return false;
64                 }
65
66                 $content = json_decode($curlResult->getBody(), true);
67
68                 if (empty($content) || !is_array($content)) {
69                         return false;
70                 }
71
72                 return $content;
73         }
74
75         /**
76          * Fetches a profile from the given url into an array that is compatible to Probe::uri
77          *
78          * @param string $url profile url
79          * @return array
80          */
81         public static function probeProfile($url)
82         {
83                 $apcontact = APContact::getByURL($url, true);
84                 if (empty($apcontact)) {
85                         return false;
86                 }
87
88                 $profile = ['network' => Protocol::ACTIVITYPUB];
89                 $profile['nick'] = $apcontact['nick'];
90                 $profile['name'] = $apcontact['name'];
91                 $profile['guid'] = $apcontact['uuid'];
92                 $profile['url'] = $apcontact['url'];
93                 $profile['addr'] = $apcontact['addr'];
94                 $profile['alias'] = $apcontact['alias'];
95                 $profile['photo'] = $apcontact['photo'];
96                 // $profile['community']
97                 // $profile['keywords']
98                 // $profile['location']
99                 $profile['about'] = $apcontact['about'];
100                 $profile['batch'] = $apcontact['sharedinbox'];
101                 $profile['notify'] = $apcontact['inbox'];
102                 $profile['poll'] = $apcontact['outbox'];
103                 $profile['pubkey'] = $apcontact['pubkey'];
104                 $profile['baseurl'] = $apcontact['baseurl'];
105
106                 // Remove all "null" fields
107                 foreach ($profile as $field => $content) {
108                         if (is_null($content)) {
109                                 unset($profile[$field]);
110                         }
111                 }
112
113                 return $profile;
114         }
115
116         /**
117          * Fetches activities from the outbox of a given profile and processes it
118          *
119          * @param string $url
120          * @param integer $uid User ID
121          */
122         public static function fetchOutbox($url, $uid)
123         {
124                 $data = self::fetchContent($url);
125                 if (empty($data)) {
126                         return;
127                 }
128
129                 if (!empty($data['orderedItems'])) {
130                         $items = $data['orderedItems'];
131                 } elseif (!empty($data['first']['orderedItems'])) {
132                         $items = $data['first']['orderedItems'];
133                 } elseif (!empty($data['first'])) {
134                         self::fetchOutbox($data['first'], $uid);
135                         return;
136                 } else {
137                         $items = [];
138                 }
139
140                 foreach ($items as $activity) {
141                         $ldactivity = JsonLD::compact($activity);
142                         ActivityPub\Receiver::processActivity($ldactivity, '', $uid, true);
143                 }
144         }
145 }