3 * @copyright Copyright (C) 2020, Friendica
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\System;
26 use Friendica\Database\DBA;
28 use Friendica\Model\Contact;
29 use Friendica\Model\Item;
30 use Friendica\Network\HTTPException;
31 use Friendica\Protocol\ActivityPub;
32 use Friendica\Util\HTTPSignature;
33 use Friendica\Util\Network;
38 class Objects extends BaseModule
40 public static function rawContent(array $parameters = [])
42 if (empty($parameters['guid'])) {
43 throw new HTTPException\BadRequestException();
46 if (!ActivityPub::isRequest()) {
47 DI::baseUrl()->redirect(str_replace('objects/', 'display/', DI::args()->getQueryString()));
50 $item = Item::selectFirst(['id', 'uid', 'origin', 'author-link', 'changed', 'private', 'psid'],
51 ['guid' => $parameters['guid']], ['order' => ['origin' => true]]);
54 $requester = HTTPSignature::getSigner('', $_SERVER);
55 if (!empty($requester) && $item['origin']) {
56 $requester_id = Contact::getIdForURL($requester, $item['uid']);
57 if (!empty($requester_id)) {
58 $permissionSets = DI::permissionSet()->selectByContactId($requester_id, $item['uid']);
59 if (!empty($permissionSets)) {
60 $psid = array_merge($permissionSets->column('id'),
61 [DI::permissionSet()->getIdFromACL($item['uid'], '', '', '', '')]);
62 $validated = in_array($item['psid'], $psid);
67 if (!$validated && !in_array($item['private'], [Item::PUBLIC, Item::UNLISTED])) {
71 // Valid items are original post or posted from this node (including in the case of a forum)
72 if (!DBA::isResult($item) || !$item['origin'] && (parse_url($item['author-link'], PHP_URL_HOST) != parse_url(DI::baseUrl()->get(), PHP_URL_HOST))) {
73 throw new HTTPException\NotFoundException();
76 $etag = md5($parameters['guid'] . '-' . $item['changed']);
77 $last_modified = $item['changed'];
78 Network::checkEtagModified($etag, $last_modified);
80 if (empty($parameters['activity'])) {
81 $activity = ActivityPub\Transmitter::createActivityFromItem($item['id'], true);
82 $activity['type'] = $activity['type'] == 'Update' ? 'Create' : $activity['type'];
84 // Only display "Create" activity objects here, no reshares or anything else
85 if (empty($activity['object']) || ($activity['type'] != 'Create')) {
86 throw new HTTPException\NotFoundException();
89 $data = ['@context' => ActivityPub::CONTEXT];
90 $data = array_merge($data, $activity['object']);
91 } elseif (in_array($parameters['activity'], ['Create', 'Announce', 'Update',
92 'Like', 'Dislike', 'Accept', 'Reject', 'TentativeAccept', 'Follow', 'Add'])) {
93 $data = ActivityPub\Transmitter::createActivityFromItem($item['id']);
95 throw new HTTPException\NotFoundException();
97 if ($parameters['activity'] != 'Create') {
98 $data['type'] = $parameters['activity'];
99 $data['id'] = str_replace('/Create', '/' . $parameters['activity'], $data['id']);
102 throw new HTTPException\NotFoundException();
105 // Relaxed CORS header for public items
106 header('Access-Control-Allow-Origin: *');
107 System::jsonExit($data, 'application/activity+json');