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