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