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\Module\Special\HTTPException;
31 use Friendica\Protocol\ActivityPub;
32 use Friendica\Util\HTTPSignature;
33 use Friendica\Util\Network;
34 use Psr\Http\Message\ResponseInterface;
39 class Inbox extends BaseApi
41 public function run(HTTPException $httpException, array $request = [], bool $scopecheck = true): ResponseInterface
43 return parent::run($httpException, $request, false);
46 protected function rawContent(array $request = [])
48 self::checkAllowedScope(self::SCOPE_READ);
49 $uid = self::getCurrentUserID();
50 $page = $request['page'] ?? null;
52 if (empty($page) && empty($request['max_id'])) {
56 if (!empty($this->parameters['nickname'])) {
57 $owner = User::getOwnerDataByNick($this->parameters['nickname']);
59 throw new \Friendica\Network\HTTPException\NotFoundException();
61 if ($owner['uid'] != $uid) {
62 throw new \Friendica\Network\HTTPException\ForbiddenException();
64 $inbox = ActivityPub\ClientToServer::getInbox($uid, $page, $request['max_id'] ?? null);
66 $inbox = ActivityPub\ClientToServer::getPublicInbox($uid, $page, $request['max_id'] ?? null);
69 System::jsonExit($inbox, 'application/activity+json');
72 protected function post(array $request = [])
74 $postdata = Network::postdata();
76 if (empty($postdata)) {
77 throw new \Friendica\Network\HTTPException\BadRequestException();
80 if (DI::config()->get('debug', 'ap_inbox_log')) {
81 if (HTTPSignature::getSigner($postdata, $_SERVER)) {
82 $filename = 'signed-activitypub';
84 $filename = 'failed-activitypub';
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]);
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();
101 ActivityPub\Receiver::processInbox($postdata, $_SERVER, $uid);
103 throw new \Friendica\Network\HTTPException\AcceptedException();