]> git.mxchange.org Git - friendica.git/blob - src/Module/ActivityPub/Objects.php
Merge pull request #13235 from annando/group-circle
[friendica.git] / src / Module / ActivityPub / Objects.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Util\HTTPSignature;
35 use Friendica\Util\Network;
36 use Friendica\Util\Strings;
37
38 /**
39  * ActivityPub Objects
40  */
41 class Objects extends BaseModule
42 {
43         protected function rawContent(array $request = [])
44         {
45                 if (empty($this->parameters['guid'])) {
46                         throw new HTTPException\BadRequestException();
47                 }
48
49                 header('Vary: Accept', false);
50
51                 if (!ActivityPub::isRequest()) {
52                         DI::baseUrl()->redirect(str_replace('objects/', 'display/', DI::args()->getQueryString()));
53                 }
54
55                 $itemuri = DBA::selectFirst('item-uri', ['id'], ['guid' => $this->parameters['guid']]);
56
57                 if (DBA::isResult($itemuri)) {
58                         Logger::info('Provided GUID found.', ['guid' => $this->parameters['guid'], 'uri-id' => $itemuri['id']]);
59                 } else {
60                         // The item URI does not always contain the GUID. This means that we have to search the URL instead
61                         $url = DI::baseUrl() . '/' . DI::args()->getQueryString();
62                         $nurl = Strings::normaliseLink($url);
63                         $ssl_url = str_replace('http://', 'https://', $nurl);
64
65                         $itemuri = DBA::selectFirst('item-uri', ['guid', 'id'], ['uri' => [$url, $nurl, $ssl_url]]);
66                         if (DBA::isResult($itemuri)) {
67                                 Logger::info('URL found.', ['url' => $url, 'guid' => $itemuri['guid'], 'uri-id' => $itemuri['id']]);
68                         } else {
69                                 Logger::info('URL not found.', ['url' => $url]);
70                                 throw new HTTPException\NotFoundException();
71                         }
72                 }
73
74                 $item = Post::selectFirst([], ['uri-id' => $itemuri['id'], 'origin' => true]);
75                 if (!DBA::isResult($item)) {
76                         throw new HTTPException\NotFoundException();
77                 }
78
79                 $validated = in_array($item['private'], [Item::PUBLIC, Item::UNLISTED]);
80
81                 if (!$validated) {
82                         $requester = HTTPSignature::getSigner('', $_SERVER);
83                         if (!empty($requester)) {
84                                 $receivers = Item::enumeratePermissions($item, false);
85                                 $receivers[] = $item['contact-id'];
86
87                                 $validated = in_array(Contact::getIdForURL($requester, $item['uid']), $receivers);
88                                 if (!$validated) {
89                                         $validated = in_array(Contact::getIdForURL($requester), $receivers);
90                                 }
91                         }
92                 }
93
94                 if (!$validated) {
95                         throw new HTTPException\NotFoundException();
96                 }
97
98                 $etag          = md5($this->parameters['guid'] . '-' . $item['changed']);
99                 $last_modified = $item['changed'];
100                 Network::checkEtagModified($etag, $last_modified);
101
102                 if (empty($this->parameters['activity']) && ($item['gravity'] != Item::GRAVITY_ACTIVITY)) {
103                         $activity = ActivityPub\Transmitter::createCachedActivityFromItem($item['id'], false, true);
104                         if (empty($activity['type'])) {
105                                 throw new HTTPException\NotFoundException();
106                         }
107
108                         $activity['type'] = $activity['type'] == 'Update' ? 'Create' : $activity['type'];
109
110                         // Only display "Create" activity objects here, no reshares or anything else
111                         if (empty($activity['object']) || ($activity['type'] != 'Create')) {
112                                 throw new HTTPException\NotFoundException();
113                         }
114
115                         $data = ['@context' => ActivityPub::CONTEXT];
116                         $data = array_merge($data, $activity['object']);
117                 } elseif (empty($this->parameters['activity']) || in_array($this->parameters['activity'],
118                         ['Create', 'Announce', 'Update', 'Like', 'Dislike', 'Accept', 'Reject',
119                         'TentativeAccept', 'Follow', 'Add'])) {
120                         $data = ActivityPub\Transmitter::createCachedActivityFromItem($item['id']);
121                         if (empty($data)) {
122                                 throw new HTTPException\NotFoundException();
123                         }
124                         if (!empty($this->parameters['activity']) && ($this->parameters['activity'] != 'Create')) {
125                                 $data['type'] = $this->parameters['activity'];
126                                 $data['id'] = str_replace('/Create', '/' . $this->parameters['activity'], $data['id']);
127                         }
128                 } else {
129                         throw new HTTPException\NotFoundException();
130                 }
131
132                 // Relaxed CORS header for public items
133                 header('Access-Control-Allow-Origin: *');
134
135                 System::jsonExit($data, 'application/activity+json');
136         }
137 }