]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub.php
The function is only needed in the same 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\Database\DBA;
8 use Friendica\Core\System;
9 use Friendica\BaseObject;
10 use Friendica\Util\Network;
11 use Friendica\Util\HTTPSignature;
12 use Friendica\Core\Protocol;
13 use Friendica\Model\Conversation;
14 use Friendica\Model\Contact;
15 use Friendica\Model\APContact;
16 use Friendica\Model\Item;
17 use Friendica\Model\Profile;
18 use Friendica\Model\Term;
19 use Friendica\Model\User;
20 use Friendica\Util\DateTimeFormat;
21 use Friendica\Util\Crypto;
22 use Friendica\Content\Text\BBCode;
23 use Friendica\Content\Text\HTML;
24 use Friendica\Util\JsonLD;
25 use Friendica\Util\LDSignature;
26 use Friendica\Core\Config;
27
28 /**
29  * @brief ActivityPub Protocol class
30  * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
31  * https://www.w3.org/TR/activitypub/
32  * https://www.w3.org/TR/activitystreams-core/
33  * https://www.w3.org/TR/activitystreams-vocabulary/
34  *
35  * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
36  * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
37  *
38  * Digest: https://tools.ietf.org/html/rfc5843
39  * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
40  *
41  * Mastodon implementation of supported activities:
42  * https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/activity.rb#L26
43  *
44  * To-do:
45  *
46  * Receiver:
47  * - Update (Image, Video, Article, Note)
48  * - Event
49  * - Undo Announce
50  *
51  * Check what this is meant to do:
52  * - Add
53  * - Block
54  * - Flag
55  * - Remove
56  * - Undo Block
57  * - Undo Accept (Problem: This could invert a contact accept or an event accept)
58  *
59  * Transmitter:
60  * - Event
61  *
62  * Complicated:
63  * - Announce
64  * - Undo Announce
65  *
66  * General:
67  * - Attachments
68  * - nsfw (sensitive)
69  * - Queueing unsucessful deliveries
70  * - Polling the outboxes for missing content?
71  * - Possibly using the LD-JSON parser
72  */
73 class ActivityPub
74 {
75         const PUBLIC_COLLECTION = 'https://www.w3.org/ns/activitystreams#Public';
76         const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
77                 ['vcard' => 'http://www.w3.org/2006/vcard/ns#',
78                 'diaspora' => 'https://diasporafoundation.org/ns/',
79                 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
80                 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag']];
81         const ACCOUNT_TYPES = ['Person', 'Organization', 'Service', 'Group', 'Application'];
82         const CONTENT_TYPES = ['Note', 'Article', 'Video', 'Image'];
83         const ACTIVITY_TYPES = ['Like', 'Dislike', 'Accept', 'Reject', 'TentativeAccept'];
84         /**
85          * @brief Checks if the web request is done for the AP protocol
86          *
87          * @return is it AP?
88          */
89         public static function isRequest()
90         {
91                 return stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json') ||
92                         stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/ld+json');
93         }
94
95         /**
96          * Fetches ActivityPub content from the given url
97          *
98          * @param string $url content url
99          * @return array
100          */
101         public static function fetchContent($url)
102         {
103                 $ret = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']);
104                 if (!$ret['success'] || empty($ret['body'])) {
105                         return false;
106                 }
107
108                 return json_decode($ret['body'], true);
109         }
110
111         /**
112          * Fetches a profile from the given url into an array that is compatible to Probe::uri
113          *
114          * @param string $url profile url
115          * @return array
116          */
117         public static function probeProfile($url)
118         {
119                 $apcontact = APContact::getByURL($url, true);
120                 if (empty($apcontact)) {
121                         return false;
122                 }
123
124                 $profile = ['network' => Protocol::ACTIVITYPUB];
125                 $profile['nick'] = $apcontact['nick'];
126                 $profile['name'] = $apcontact['name'];
127                 $profile['guid'] = $apcontact['uuid'];
128                 $profile['url'] = $apcontact['url'];
129                 $profile['addr'] = $apcontact['addr'];
130                 $profile['alias'] = $apcontact['alias'];
131                 $profile['photo'] = $apcontact['photo'];
132                 // $profile['community']
133                 // $profile['keywords']
134                 // $profile['location']
135                 $profile['about'] = $apcontact['about'];
136                 $profile['batch'] = $apcontact['sharedinbox'];
137                 $profile['notify'] = $apcontact['inbox'];
138                 $profile['poll'] = $apcontact['outbox'];
139                 $profile['pubkey'] = $apcontact['pubkey'];
140                 $profile['baseurl'] = $apcontact['baseurl'];
141
142                 // Remove all "null" fields
143                 foreach ($profile as $field => $content) {
144                         if (is_null($content)) {
145                                 unset($profile[$field]);
146                         }
147                 }
148
149                 return $profile;
150         }
151
152         /**
153          * @brief 
154          *
155          * @param $url
156          * @param integer $uid User ID
157          */
158         public static function fetchOutbox($url, $uid)
159         {
160                 $data = self::fetchContent($url);
161                 if (empty($data)) {
162                         return;
163                 }
164
165                 if (!empty($data['orderedItems'])) {
166                         $items = $data['orderedItems'];
167                 } elseif (!empty($data['first']['orderedItems'])) {
168                         $items = $data['first']['orderedItems'];
169                 } elseif (!empty($data['first'])) {
170                         self::fetchOutbox($data['first'], $uid);
171                         return;
172                 } else {
173                         $items = [];
174                 }
175
176                 foreach ($items as $activity) {
177                         ActivityPub\Receiver::processActivity($activity, '', $uid, true);
178                 }
179         }
180 }