]> git.mxchange.org Git - friendica.git/blob - src/Module/ActivityPub/Inbox.php
Use the post language for the language detection / config for quality
[friendica.git] / src / Module / ActivityPub / Inbox.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\Core\Logger;
25 use Friendica\Core\System;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28 use Friendica\Model\User;
29 use Friendica\Module\BaseApi;
30 use Friendica\Module\Special\HTTPException;
31 use Friendica\Protocol\ActivityPub;
32 use Friendica\Util\HTTPSignature;
33 use Friendica\Util\Network;
34 use Psr\Http\Message\ResponseInterface;
35
36 /**
37  * ActivityPub Inbox
38  */
39 class Inbox extends BaseApi
40 {
41         public function run(HTTPException $httpException, array $request = [], bool $scopecheck = true): ResponseInterface
42         {
43                 return parent::run($httpException, $request, false);
44         }
45
46         protected function rawContent(array $request = [])
47         {
48                 $this->checkAllowedScope(self::SCOPE_READ);
49                 $uid  = self::getCurrentUserID();
50                 $page = $request['page'] ?? null;
51
52                 if (empty($page) && empty($request['max_id'])) {
53                         $page = 1;
54                 }
55
56                 if (!empty($this->parameters['nickname'])) {
57                         $owner = User::getOwnerDataByNick($this->parameters['nickname']);
58                         if (empty($owner)) {
59                                 throw new \Friendica\Network\HTTPException\NotFoundException();
60                         }
61                         if ($owner['uid'] != $uid) {
62                                 throw new \Friendica\Network\HTTPException\ForbiddenException();
63                         }
64                         $inbox = ActivityPub\ClientToServer::getInbox($uid, $page, $request['max_id'] ?? null);
65                 } else {
66                         $inbox = ActivityPub\ClientToServer::getPublicInbox($uid, $page, $request['max_id'] ?? null);
67                 }
68
69                 $this->jsonExit($inbox, 'application/activity+json');
70         }
71
72         protected function post(array $request = [])
73         {
74                 $postdata = Network::postdata();
75
76                 if (empty($postdata)) {
77                         throw new \Friendica\Network\HTTPException\BadRequestException();
78                 }
79
80                 if (DI::config()->get('debug', 'ap_inbox_log')) {
81                         if (HTTPSignature::getSigner($postdata, $_SERVER)) {
82                                 $filename = 'signed-activitypub';
83                         } else {
84                                 $filename = 'failed-activitypub';
85                         }
86                         $tempfile = tempnam(System::getTempPath(), $filename);
87                         file_put_contents($tempfile, json_encode(['parameters' => $this->parameters, 'header' => $_SERVER, 'body' => $postdata], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
88                         Logger::notice('Incoming message stored', ['file' => $tempfile]);
89                 }
90
91                 if (!empty($this->parameters['nickname'])) {
92                         $user = DBA::selectFirst('user', ['uid'], ['nickname' => $this->parameters['nickname']]);
93                         if (!DBA::isResult($user)) {
94                                 throw new \Friendica\Network\HTTPException\NotFoundException();
95                         }
96                         $uid = $user['uid'];
97                 } else {
98                         $uid = 0;
99                 }
100
101                 ActivityPub\Receiver::processInbox($postdata, $_SERVER, $uid);
102
103                 throw new \Friendica\Network\HTTPException\AcceptedException();
104         }
105 }