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