]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub.php
Several fixes
[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                 $ret = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']);
62                 if (!$ret['success'] || empty($ret['body'])) {
63                         return false;
64                 }
65
66                 return json_decode($ret['body'], true);
67         }
68
69         /**
70          * Fetches a profile from the given url into an array that is compatible to Probe::uri
71          *
72          * @param string $url profile url
73          * @return array
74          */
75         public static function probeProfile($url)
76         {
77                 $apcontact = APContact::getByURL($url, true);
78                 if (empty($apcontact)) {
79                         return false;
80                 }
81
82                 $profile = ['network' => Protocol::ACTIVITYPUB];
83                 $profile['nick'] = $apcontact['nick'];
84                 $profile['name'] = $apcontact['name'];
85                 $profile['guid'] = $apcontact['uuid'];
86                 $profile['url'] = $apcontact['url'];
87                 $profile['addr'] = $apcontact['addr'];
88                 $profile['alias'] = $apcontact['alias'];
89                 $profile['photo'] = $apcontact['photo'];
90                 // $profile['community']
91                 // $profile['keywords']
92                 // $profile['location']
93                 $profile['about'] = $apcontact['about'];
94                 $profile['batch'] = $apcontact['sharedinbox'];
95                 $profile['notify'] = $apcontact['inbox'];
96                 $profile['poll'] = $apcontact['outbox'];
97                 $profile['pubkey'] = $apcontact['pubkey'];
98                 $profile['baseurl'] = $apcontact['baseurl'];
99
100                 // Remove all "null" fields
101                 foreach ($profile as $field => $content) {
102                         if (is_null($content)) {
103                                 unset($profile[$field]);
104                         }
105                 }
106
107                 return $profile;
108         }
109
110         /**
111          * Fetches activities from the outbox of a given profile and processes it
112          *
113          * @param string $url
114          * @param integer $uid User ID
115          */
116         public static function fetchOutbox($url, $uid)
117         {
118                 $data = self::fetchContent($url);
119                 if (empty($data)) {
120                         return;
121                 }
122
123                 if (!empty($data['orderedItems'])) {
124                         $items = $data['orderedItems'];
125                 } elseif (!empty($data['first']['orderedItems'])) {
126                         $items = $data['first']['orderedItems'];
127                 } elseif (!empty($data['first'])) {
128                         self::fetchOutbox($data['first'], $uid);
129                         return;
130                 } else {
131                         $items = [];
132                 }
133
134                 foreach ($items as $activity) {
135                         $ldactivity = JsonLD::compact($activity);
136                         ActivityPub\Receiver::processActivity($ldactivity, '', $uid, true);
137                 }
138         }
139 }