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