3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module;
24 use Friendica\BaseModule;
25 use Friendica\Core\Logger;
26 use Friendica\Core\System;
27 use Friendica\Database\DBA;
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;
41 class Objects extends BaseModule
43 public static function rawContent(array $parameters = [])
45 if (empty($parameters['guid'])) {
46 throw new HTTPException\BadRequestException();
49 if (!ActivityPub::isRequest()) {
50 DI::baseUrl()->redirect(str_replace('objects/', 'display/', DI::args()->getQueryString()));
53 $itemuri = DBA::selectFirst('item-uri', ['id'], ['guid' => $parameters['guid']]);
55 if (DBA::isResult($itemuri)) {
56 Logger::info('Provided GUID found.', ['guid' => $parameters['guid'], 'uri-id' => $itemuri['id']]);
58 // The item URI does not always contain the GUID. This means that we have to search the URL instead
59 $url = DI::baseUrl()->get() . '/' . DI::args()->getQueryString();
60 $nurl = Strings::normaliseLink($url);
61 $ssl_url = str_replace('http://', 'https://', $nurl);
63 $itemuri = DBA::selectFirst('item-uri', ['guid', 'id'], ['uri' => [$url, $nurl, $ssl_url]]);
64 if (DBA::isResult($itemuri)) {
65 Logger::info('URL found.', ['url' => $url, 'guid' => $itemuri['guid'], 'uri-id' => $itemuri['id']]);
67 Logger::info('URL not found.', ['url' => $url]);
68 throw new HTTPException\NotFoundException();
72 $item = Post::selectFirst(['id', 'uid', 'origin', 'author-link', 'changed', 'private', 'psid', 'gravity', 'deleted', 'parent-uri-id'],
73 ['uri-id' => $itemuri['id']], ['order' => ['origin' => true]]);
75 if (!DBA::isResult($item)) {
76 throw new HTTPException\NotFoundException();
79 $validated = in_array($item['private'], [Item::PUBLIC, Item::UNLISTED]);
82 $requester = HTTPSignature::getSigner('', $_SERVER);
83 if (!empty($requester) && $item['origin']) {
84 $requester_id = Contact::getIdForURL($requester, $item['uid']);
85 if (!empty($requester_id)) {
86 $permissionSets = DI::permissionSet()->selectByContactId($requester_id, $item['uid']);
87 if (!empty($permissionSets)) {
88 $psid = array_merge($permissionSets->column('id'),
89 [DI::permissionSet()->getIdFromACL($item['uid'], '', '', '', '')]);
90 $validated = in_array($item['psid'], $psid);
97 // Valid items are original post or posted from this node (including in the case of a forum)
98 $validated = ($item['origin'] || (parse_url($item['author-link'], PHP_URL_HOST) == parse_url(DI::baseUrl()->get(), PHP_URL_HOST)));
100 if (!$validated && $item['deleted']) {
101 $validated = Post::exists(['origin' => true, 'uri-id' => $item['parent-uri-id']]);
106 throw new HTTPException\NotFoundException();
109 $etag = md5($parameters['guid'] . '-' . $item['changed']);
110 $last_modified = $item['changed'];
111 Network::checkEtagModified($etag, $last_modified);
113 if (empty($parameters['activity']) && ($item['gravity'] != GRAVITY_ACTIVITY)) {
114 $activity = ActivityPub\Transmitter::createActivityFromItem($item['id'], true);
115 if (empty($activity['type'])) {
116 throw new HTTPException\NotFoundException();
119 $activity['type'] = $activity['type'] == 'Update' ? 'Create' : $activity['type'];
121 // Only display "Create" activity objects here, no reshares or anything else
122 if (empty($activity['object']) || ($activity['type'] != 'Create')) {
123 throw new HTTPException\NotFoundException();
126 $data = ['@context' => ActivityPub::CONTEXT];
127 $data = array_merge($data, $activity['object']);
128 } elseif (empty($parameters['activity']) || in_array($parameters['activity'],
129 ['Create', 'Announce', 'Update', 'Like', 'Dislike', 'Accept', 'Reject',
130 'TentativeAccept', 'Follow', 'Add'])) {
131 $data = ActivityPub\Transmitter::createActivityFromItem($item['id']);
133 throw new HTTPException\NotFoundException();
135 if (!empty($parameters['activity']) && ($parameters['activity'] != 'Create')) {
136 $data['type'] = $parameters['activity'];
137 $data['id'] = str_replace('/Create', '/' . $parameters['activity'], $data['id']);
140 throw new HTTPException\NotFoundException();
143 // Relaxed CORS header for public items
144 header('Access-Control-Allow-Origin: *');
145 System::jsonExit($data, 'application/activity+json');