]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub.php
Preparations for a relocation message / fix for notice
[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  * Missing parts from DFRN:
34  * - Public Forum
35  * - Private Forum
36  * - Relocation
37  */
38 class ActivityPub
39 {
40         const PUBLIC_COLLECTION = 'https://www.w3.org/ns/activitystreams#Public';
41         const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
42                 ['vcard' => 'http://www.w3.org/2006/vcard/ns#',
43                 'dfrn' => 'http://purl.org/macgirvin/dfrn/1.0/',
44                 'diaspora' => 'https://diasporafoundation.org/ns/',
45                 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
46                 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag']];
47         const ACCOUNT_TYPES = ['Person', 'Organization', 'Service', 'Group', 'Application'];
48         /**
49          * Checks if the web request is done for the AP protocol
50          *
51          * @return is it AP?
52          */
53         public static function isRequest()
54         {
55                 return stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json') ||
56                         stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/ld+json');
57         }
58
59         /**
60          * Fetches ActivityPub content from the given url
61          *
62          * @param string $url content url
63          * @return array
64          */
65         public static function fetchContent($url)
66         {
67                 $curlResult = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']);
68                 if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
69                         return false;
70                 }
71
72                 $content = json_decode($curlResult->getBody(), true);
73
74                 if (empty($content) || !is_array($content)) {
75                         return false;
76                 }
77
78                 return $content;
79         }
80
81         /**
82          * Fetches a profile from the given url into an array that is compatible to Probe::uri
83          *
84          * @param string $url profile url
85          * @return array
86          */
87         public static function probeProfile($url)
88         {
89                 $apcontact = APContact::getByURL($url, true);
90                 if (empty($apcontact)) {
91                         return false;
92                 }
93
94                 $profile = ['network' => Protocol::ACTIVITYPUB];
95                 $profile['nick'] = $apcontact['nick'];
96                 $profile['name'] = $apcontact['name'];
97                 $profile['guid'] = $apcontact['uuid'];
98                 $profile['url'] = $apcontact['url'];
99                 $profile['addr'] = $apcontact['addr'];
100                 $profile['alias'] = $apcontact['alias'];
101                 $profile['photo'] = $apcontact['photo'];
102                 // $profile['community']
103                 // $profile['keywords']
104                 // $profile['location']
105                 $profile['about'] = $apcontact['about'];
106                 $profile['batch'] = $apcontact['sharedinbox'];
107                 $profile['notify'] = $apcontact['inbox'];
108                 $profile['poll'] = $apcontact['outbox'];
109                 $profile['pubkey'] = $apcontact['pubkey'];
110                 $profile['baseurl'] = $apcontact['baseurl'];
111
112                 // Remove all "null" fields
113                 foreach ($profile as $field => $content) {
114                         if (is_null($content)) {
115                                 unset($profile[$field]);
116                         }
117                 }
118
119                 return $profile;
120         }
121
122         /**
123          * Fetches activities from the outbox of a given profile and processes it
124          *
125          * @param string $url
126          * @param integer $uid User ID
127          */
128         public static function fetchOutbox($url, $uid)
129         {
130                 $data = self::fetchContent($url);
131                 if (empty($data)) {
132                         return;
133                 }
134
135                 if (!empty($data['orderedItems'])) {
136                         $items = $data['orderedItems'];
137                 } elseif (!empty($data['first']['orderedItems'])) {
138                         $items = $data['first']['orderedItems'];
139                 } elseif (!empty($data['first'])) {
140                         self::fetchOutbox($data['first'], $uid);
141                         return;
142                 } else {
143                         $items = [];
144                 }
145
146                 foreach ($items as $activity) {
147                         $ldactivity = JsonLD::compact($activity);
148                         ActivityPub\Receiver::processActivity($ldactivity, '', $uid, true);
149                 }
150         }
151 }