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