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