]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub.php
Merge pull request #7139 from annando/ap-mail
[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\JsonLD;
8 use Friendica\Util\Network;
9 use Friendica\Core\Protocol;
10 use Friendica\Model\APContact;
11 use Friendica\Util\HTTPSignature;
12
13 /**
14  * @brief ActivityPub Protocol class
15  * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
16  * https://www.w3.org/TR/activitypub/
17  * https://www.w3.org/TR/activitystreams-core/
18  * https://www.w3.org/TR/activitystreams-vocabulary/
19  *
20  * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
21  * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
22  *
23  * Digest: https://tools.ietf.org/html/rfc5843
24  * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
25  *
26  * Mastodon implementation of supported activities:
27  * https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/activity.rb#L26
28  *
29  * Funkwhale:
30  * http://docs-funkwhale-funkwhale-549-music-federation-documentation.preview.funkwhale.audio/federation/index.html
31  *
32  * To-do:
33  * - Polling the outboxes for missing content?
34  *
35  * Missing parts from DFRN:
36  * - Public Forum
37  * - Private Forum
38  * - Relocation
39  */
40 class ActivityPub
41 {
42         const PUBLIC_COLLECTION = 'https://www.w3.org/ns/activitystreams#Public';
43         const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
44                 ['vcard' => 'http://www.w3.org/2006/vcard/ns#',
45                 'dfrn' => 'http://purl.org/macgirvin/dfrn/1.0/',
46                 'diaspora' => 'https://diasporafoundation.org/ns/',
47                 'litepub' => 'http://litepub.social/ns#',
48                 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
49                 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag',
50                 'directMessage' => 'litepub:directMessage']];
51         const ACCOUNT_TYPES = ['Person', 'Organization', 'Service', 'Group', 'Application'];
52         /**
53          * Checks if the web request is done for the AP protocol
54          *
55          * @return bool is it AP?
56          */
57         public static function isRequest()
58         {
59                 return stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json') ||
60                         stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/ld+json');
61         }
62
63         /**
64          * Fetches ActivityPub content from the given url
65          *
66          * @param string  $url content url
67          * @param integer $uid User ID for the signature
68          * @return array
69          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
70          */
71         public static function fetchContent($url, $uid = 0)
72         {
73                 if (!empty($uid)) {
74                         return HTTPSignature::fetch($url, $uid);
75                 }
76
77                 $curlResult = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']);
78                 if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
79                         return false;
80                 }
81
82                 $content = json_decode($curlResult->getBody(), true);
83
84                 if (empty($content) || !is_array($content)) {
85                         return false;
86                 }
87
88                 return $content;
89         }
90
91         /**
92          * Fetches a profile from the given url into an array that is compatible to Probe::uri
93          *
94          * @param string  $url    profile url
95          * @param boolean $update Update the profile
96          * @return array
97          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
98          * @throws \ImagickException
99          */
100         public static function probeProfile($url, $update = true)
101         {
102                 $apcontact = APContact::getByURL($url, $update);
103                 if (empty($apcontact)) {
104                         return false;
105                 }
106
107                 $profile = ['network' => Protocol::ACTIVITYPUB];
108                 $profile['nick'] = $apcontact['nick'];
109                 $profile['name'] = $apcontact['name'];
110                 $profile['guid'] = $apcontact['uuid'];
111                 $profile['url'] = $apcontact['url'];
112                 $profile['addr'] = $apcontact['addr'];
113                 $profile['alias'] = $apcontact['alias'];
114                 $profile['photo'] = $apcontact['photo'];
115                 // $profile['community']
116                 // $profile['keywords']
117                 // $profile['location']
118                 $profile['about'] = $apcontact['about'];
119                 $profile['batch'] = $apcontact['sharedinbox'];
120                 $profile['notify'] = $apcontact['inbox'];
121                 $profile['poll'] = $apcontact['outbox'];
122                 $profile['pubkey'] = $apcontact['pubkey'];
123                 $profile['baseurl'] = $apcontact['baseurl'];
124
125                 // Remove all "null" fields
126                 foreach ($profile as $field => $content) {
127                         if (is_null($content)) {
128                                 unset($profile[$field]);
129                         }
130                 }
131
132                 return $profile;
133         }
134
135         /**
136          * Fetches activities from the outbox of a given profile and processes it
137          *
138          * @param string  $url
139          * @param integer $uid User ID
140          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
141          */
142         public static function fetchOutbox($url, $uid)
143         {
144                 $data = self::fetchContent($url);
145                 if (empty($data)) {
146                         return;
147                 }
148
149                 if (!empty($data['orderedItems'])) {
150                         $items = $data['orderedItems'];
151                 } elseif (!empty($data['first']['orderedItems'])) {
152                         $items = $data['first']['orderedItems'];
153                 } elseif (!empty($data['first'])) {
154                         self::fetchOutbox($data['first'], $uid);
155                         return;
156                 } else {
157                         $items = [];
158                 }
159
160                 foreach ($items as $activity) {
161                         $ldactivity = JsonLD::compact($activity);
162                         ActivityPub\Receiver::processActivity($ldactivity, '', $uid, true);
163                 }
164         }
165 }