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