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