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