]> git.mxchange.org Git - friendica.git/blob - src/Module/Objects.php
Merge pull request #9345 from annando/issue-9344
[friendica.git] / src / Module / Objects.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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;
23
24 use Friendica\BaseModule;
25 use Friendica\Core\System;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
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;
34
35 /**
36  * ActivityPub Objects
37  */
38 class Objects extends BaseModule
39 {
40         public static function rawContent(array $parameters = [])
41         {
42                 if (empty($parameters['guid'])) {
43                         throw new HTTPException\BadRequestException();
44                 }
45
46                 if (!ActivityPub::isRequest()) {
47                         DI::baseUrl()->redirect(str_replace('objects/', 'display/', DI::args()->getQueryString()));
48                 }
49
50                 $item = Item::selectFirst(['id', 'uid', 'origin', 'author-link', 'changed', 'private', 'psid', 'gravity'],
51                         ['guid' => $parameters['guid']], ['order' => ['origin' => true]]);
52
53                 if (!DBA::isResult($item)) {
54                         throw new HTTPException\NotFoundException();
55                 }
56
57                 $validated = in_array($item['private'], [Item::PUBLIC, Item::UNLISTED]);
58
59                 if (!$validated) {
60                         $requester = HTTPSignature::getSigner('', $_SERVER);
61                         if (!empty($requester) && $item['origin']) {
62                                 $requester_id = Contact::getIdForURL($requester, $item['uid']);
63                                 if (!empty($requester_id)) {
64                                         $permissionSets = DI::permissionSet()->selectByContactId($requester_id, $item['uid']);
65                                         if (!empty($permissionSets)) {
66                                                 $psid = array_merge($permissionSets->column('id'),
67                                                         [DI::permissionSet()->getIdFromACL($item['uid'], '', '', '', '')]);
68                                                 $validated = in_array($item['psid'], $psid);
69                                         }
70                                 }
71                         }
72                 }
73
74                 // Valid items are original post or posted from this node (including in the case of a forum)
75                 if (!$validated || !$item['origin'] && (parse_url($item['author-link'], PHP_URL_HOST) != parse_url(DI::baseUrl()->get(), PHP_URL_HOST))) {
76                         throw new HTTPException\NotFoundException();
77                 }
78
79                 $etag          = md5($parameters['guid'] . '-' . $item['changed']);
80                 $last_modified = $item['changed'];
81                 Network::checkEtagModified($etag, $last_modified);
82
83                 if (empty($parameters['activity']) && ($item['gravity'] != GRAVITY_ACTIVITY)) {
84                         $activity = ActivityPub\Transmitter::createActivityFromItem($item['id'], true);
85                         $activity['type'] = $activity['type'] == 'Update' ? 'Create' : $activity['type'];
86
87                         // Only display "Create" activity objects here, no reshares or anything else
88                         if (empty($activity['object']) || ($activity['type'] != 'Create')) {
89                                 throw new HTTPException\NotFoundException();
90                         }
91
92                         $data = ['@context' => ActivityPub::CONTEXT];
93                         $data = array_merge($data, $activity['object']);
94                 } elseif (empty($parameters['activity']) || in_array($parameters['activity'],
95                         ['Create', 'Announce', 'Update', 'Like', 'Dislike', 'Accept', 'Reject',
96                         'TentativeAccept', 'Follow', 'Add'])) {
97                         $data = ActivityPub\Transmitter::createActivityFromItem($item['id']);
98                         if (empty($data)) {
99                                 throw new HTTPException\NotFoundException();
100                         }
101                         if (!empty($parameters['activity']) && ($parameters['activity'] != 'Create')) {
102                                 $data['type'] = $parameters['activity'];
103                                 $data['id'] = str_replace('/Create', '/' . $parameters['activity'], $data['id']);
104                         }
105                 } else {
106                         throw new HTTPException\NotFoundException();
107                 }
108
109                 // Relaxed CORS header for public items
110                 header('Access-Control-Allow-Origin: *');
111                 System::jsonExit($data, 'application/activity+json');
112         }
113 }