]> git.mxchange.org Git - friendica.git/blob - src/Module/ActivityPub/Inbox.php
First implementation of ActivityPub C2S
[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\Protocol\ActivityPub;
31 use Friendica\Util\HTTPSignature;
32 use Friendica\Util\Network;
33
34 /**
35  * ActivityPub Inbox
36  */
37 class Inbox extends BaseApi
38 {
39         protected function rawContent(array $request = [])
40         {
41                 self::checkAllowedScope(self::SCOPE_READ);
42                 $uid  = self::getCurrentUserID();
43                 $page = $request['page'] ?? null;
44
45                 if (empty($page) && empty($request['max_id'])) {
46                         $page = 1;
47                 }
48
49                 if (!empty($this->parameters['nickname'])) {
50                         $owner = User::getOwnerDataByNick($this->parameters['nickname']);
51                         if (empty($owner)) {
52                                 throw new \Friendica\Network\HTTPException\NotFoundException();
53                         }
54                         if ($owner['uid'] != $uid) {
55                                 throw new \Friendica\Network\HTTPException\ForbiddenException();
56                         }
57                         $outbox = ActivityPub\Transmitter::getInbox($uid, $page, $request['max_id'] ?? null);
58                 } else {
59                         $outbox = ActivityPub\Transmitter::getPublicInbox($uid, $page, $request['max_id'] ?? null);
60                 }
61
62                 System::jsonExit($outbox, 'application/activity+json');
63         }
64
65         protected function post(array $request = [])
66         {
67                 $postdata = Network::postdata();
68
69                 if (empty($postdata)) {
70                         throw new \Friendica\Network\HTTPException\BadRequestException();
71                 }
72
73                 if (DI::config()->get('debug', 'ap_inbox_log')) {
74                         if (HTTPSignature::getSigner($postdata, $_SERVER)) {
75                                 $filename = 'signed-activitypub';
76                         } else {
77                                 $filename = 'failed-activitypub';
78                         }
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]);
82                 }
83
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();
88                         }
89                         $uid = $user['uid'];
90                 } else {
91                         $uid = 0;
92                 }
93
94                 ActivityPub\Receiver::processInbox($postdata, $_SERVER, $uid);
95
96                 throw new \Friendica\Network\HTTPException\AcceptedException();
97         }
98 }