]> git.mxchange.org Git - friendica.git/blob - src/Module/ActivityPub/Objects.php
Merge pull request #11019 from foss-/develop
[friendica.git] / src / Module / ActivityPub / Objects.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\ActivityPub;
23
24 use Friendica\BaseModule;
25 use Friendica\Core\Logger;
26 use Friendica\Core\System;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model\Contact;
30 use Friendica\Model\Item;
31 use Friendica\Model\Post;
32 use Friendica\Network\HTTPException;
33 use Friendica\Protocol\ActivityPub;
34 use Friendica\Security\PermissionSet\Repository\PermissionSet;
35 use Friendica\Util\HTTPSignature;
36 use Friendica\Util\Network;
37 use Friendica\Util\Strings;
38
39 /**
40  * ActivityPub Objects
41  */
42 class Objects extends BaseModule
43 {
44         public function rawContent()
45         {
46                 if (empty($this->parameters['guid'])) {
47                         throw new HTTPException\BadRequestException();
48                 }
49
50                 if (!ActivityPub::isRequest()) {
51                         DI::baseUrl()->redirect(str_replace('objects/', 'display/', DI::args()->getQueryString()));
52                 }
53
54                 $itemuri = DBA::selectFirst('item-uri', ['id'], ['guid' => $this->parameters['guid']]);
55
56                 if (DBA::isResult($itemuri)) {
57                         Logger::info('Provided GUID found.', ['guid' => $this->parameters['guid'], 'uri-id' => $itemuri['id']]);
58                 } else {
59                         // The item URI does not always contain the GUID. This means that we have to search the URL instead
60                         $url = DI::baseUrl()->get() . '/' . DI::args()->getQueryString();
61                         $nurl = Strings::normaliseLink($url);
62                         $ssl_url = str_replace('http://', 'https://', $nurl);
63
64                         $itemuri = DBA::selectFirst('item-uri', ['guid', 'id'], ['uri' => [$url, $nurl, $ssl_url]]);
65                         if (DBA::isResult($itemuri)) {
66                                 Logger::info('URL found.', ['url' => $url, 'guid' => $itemuri['guid'], 'uri-id' => $itemuri['id']]);
67                         } else {
68                                 Logger::info('URL not found.', ['url' => $url]);
69                                 throw new HTTPException\NotFoundException();
70                         }
71                 }
72
73                 $item = Post::selectFirst(['id', 'uid', 'origin', 'author-link', 'changed', 'private', 'psid', 'gravity', 'deleted', 'parent-uri-id'],
74                         ['uri-id' => $itemuri['id']], ['order' => ['origin' => true]]);
75
76                 if (!DBA::isResult($item)) {
77                         throw new HTTPException\NotFoundException();
78                 }
79
80                 $validated = in_array($item['private'], [Item::PUBLIC, Item::UNLISTED]);
81
82                 if (!$validated) {
83                         $requester = HTTPSignature::getSigner('', $_SERVER);
84                         if (!empty($requester) && $item['origin']) {
85                                 $requester_id = Contact::getIdForURL($requester, $item['uid']);
86                                 if (!empty($requester_id)) {
87                                         $permissionSets = DI::permissionSet()->selectByContactId($requester_id, $item['uid']);
88                                         $psids = array_merge($permissionSets->column('id'), [PermissionSet::PUBLIC]);
89                                         $validated = in_array($item['psid'], $psids);
90                                 }
91                         }
92                 }
93
94                 if ($validated) {
95                         // Valid items are original post or posted from this node (including in the case of a forum)
96                         $validated = ($item['origin'] || (parse_url($item['author-link'], PHP_URL_HOST) == parse_url(DI::baseUrl()->get(), PHP_URL_HOST)));
97
98                         if (!$validated && $item['deleted']) {
99                                 $validated = Post::exists(['origin' => true, 'uri-id' => $item['parent-uri-id']]);
100                         }
101                 }
102
103                 if (!$validated) {
104                         throw new HTTPException\NotFoundException();
105                 }
106
107                 $etag          = md5($this->parameters['guid'] . '-' . $item['changed']);
108                 $last_modified = $item['changed'];
109                 Network::checkEtagModified($etag, $last_modified);
110
111                 if (empty($this->parameters['activity']) && ($item['gravity'] != GRAVITY_ACTIVITY)) {
112                         $activity = ActivityPub\Transmitter::createActivityFromItem($item['id'], true);
113                         if (empty($activity['type'])) {
114                                 throw new HTTPException\NotFoundException();
115                         }
116
117                         $activity['type'] = $activity['type'] == 'Update' ? 'Create' : $activity['type'];
118
119                         // Only display "Create" activity objects here, no reshares or anything else
120                         if (empty($activity['object']) || ($activity['type'] != 'Create')) {
121                                 throw new HTTPException\NotFoundException();
122                         }
123
124                         $data = ['@context' => ActivityPub::CONTEXT];
125                         $data = array_merge($data, $activity['object']);
126                 } elseif (empty($this->parameters['activity']) || in_array($this->parameters['activity'],
127                         ['Create', 'Announce', 'Update', 'Like', 'Dislike', 'Accept', 'Reject',
128                         'TentativeAccept', 'Follow', 'Add'])) {
129                         $data = ActivityPub\Transmitter::createActivityFromItem($item['id']);
130                         if (empty($data)) {
131                                 throw new HTTPException\NotFoundException();
132                         }
133                         if (!empty($this->parameters['activity']) && ($this->parameters['activity'] != 'Create')) {
134                                 $data['type'] = $this->parameters['activity'];
135                                 $data['id'] = str_replace('/Create', '/' . $this->parameters['activity'], $data['id']);
136                         }
137                 } else {
138                         throw new HTTPException\NotFoundException();
139                 }
140
141                 // Relaxed CORS header for public items
142                 header('Access-Control-Allow-Origin: *');
143                 System::jsonExit($data, 'application/activity+json');
144         }
145 }