3 * @copyright Copyright (C) 2010-2023, 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\ActivityPub;
24 use Friendica\Core\Logger;
25 use Friendica\Core\System;
26 use Friendica\Database\DBA;
28 use Friendica\Model\User;
29 use Friendica\Module\BaseApi;
30 use Friendica\Protocol\ActivityPub;
31 use Friendica\Util\HTTPSignature;
32 use Friendica\Util\Network;
37 class Inbox extends BaseApi
39 protected function rawContent(array $request = [])
41 self::checkAllowedScope(self::SCOPE_READ);
42 $uid = self::getCurrentUserID();
43 $page = $request['page'] ?? null;
45 if (empty($page) && empty($request['max_id'])) {
49 if (!empty($this->parameters['nickname'])) {
50 $owner = User::getOwnerDataByNick($this->parameters['nickname']);
52 throw new \Friendica\Network\HTTPException\NotFoundException();
54 if ($owner['uid'] != $uid) {
55 throw new \Friendica\Network\HTTPException\ForbiddenException();
57 $outbox = ActivityPub\Transmitter::getInbox($uid, $page, $request['max_id'] ?? null);
59 $outbox = ActivityPub\Transmitter::getPublicInbox($uid, $page, $request['max_id'] ?? null);
62 System::jsonExit($outbox, 'application/activity+json');
65 protected function post(array $request = [])
67 $postdata = Network::postdata();
69 if (empty($postdata)) {
70 throw new \Friendica\Network\HTTPException\BadRequestException();
73 if (DI::config()->get('debug', 'ap_inbox_log')) {
74 if (HTTPSignature::getSigner($postdata, $_SERVER)) {
75 $filename = 'signed-activitypub';
77 $filename = 'failed-activitypub';
79 $tempfile = tempnam(System::getTempPath(), $filename);
80 file_put_contents($tempfile, json_encode(['parameters' => $this->parameters, 'header' => $_SERVER, 'body' => $postdata], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
81 Logger::notice('Incoming message stored', ['file' => $tempfile]);
84 if (!empty($this->parameters['nickname'])) {
85 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $this->parameters['nickname']]);
86 if (!DBA::isResult($user)) {
87 throw new \Friendica\Network\HTTPException\NotFoundException();
94 ActivityPub\Receiver::processInbox($postdata, $_SERVER, $uid);
96 throw new \Friendica\Network\HTTPException\AcceptedException();