]> git.mxchange.org Git - friendica.git/blob - src/Module/Debug/ActivityPubConversion.php
Merge pull request #12026 from annando/no-boot-src-module-2
[friendica.git] / src / Module / Debug / ActivityPubConversion.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Debug;
23
24 use Friendica\BaseModule;
25 use Friendica\Core\Renderer;
26 use Friendica\Core\Session;
27 use Friendica\DI;
28 use Friendica\Protocol\ActivityPub;
29 use Friendica\Util\JsonLD;
30
31 class ActivityPubConversion extends BaseModule
32 {
33         protected function content(array $request = []): string
34         {
35                 function visible_whitespace($s)
36                 {
37                         return '<pre>' . htmlspecialchars($s) . '</pre>';
38                 }
39
40                 $results = [];
41                 if (!empty($_REQUEST['source'])) {
42                         try {
43                                 $source = json_decode($_REQUEST['source'], true);
44                                 $trust_source = true;
45                                 $uid = Session::getLocalUser();
46                                 $push = false;
47
48                                 if (!$source) {
49                                         throw new \Exception('Failed to decode source JSON');
50                                 }
51
52                                 $formatted = json_encode($source, JSON_PRETTY_PRINT);
53                                 $results[] = [
54                                         'title'   => DI::l10n()->t('Formatted'),
55                                         'content' => visible_whitespace(trim(var_export($formatted, true), "'")),
56                                 ];
57                                 $results[] = [
58                                         'title'   => DI::l10n()->t('Source'),
59                                         'content' => visible_whitespace(var_export($source, true))
60                                 ];
61                                 $activity = JsonLD::compact($source);
62                                 if (!$activity) {
63                                         throw new \Exception('Failed to compact JSON');
64                                 }
65                                 $results[] = [
66                                         'title'   => DI::l10n()->t('Activity'),
67                                         'content' => visible_whitespace(var_export($activity, true))
68                                 ];
69
70                                 $type = JsonLD::fetchElement($activity, '@type');
71
72                                 if (!$type) {
73                                         throw new \Exception('Empty type');
74                                 }
75
76                                 if (!JsonLD::fetchElement($activity, 'as:object', '@id')) {
77                                         throw new \Exception('Empty object');
78                                 }
79
80                                 if (!JsonLD::fetchElement($activity, 'as:actor', '@id')) {
81                                         throw new \Exception('Empty actor');
82                                 }
83
84                                 // Don't trust the source if "actor" differs from "attributedTo". The content could be forged.
85                                 if ($trust_source && ($type == 'as:Create') && is_array($activity['as:object'])) {
86                                         $actor = JsonLD::fetchElement($activity, 'as:actor', '@id');
87                                         $attributed_to = JsonLD::fetchElement($activity['as:object'], 'as:attributedTo', '@id');
88                                         $trust_source = ($actor == $attributed_to);
89                                         if (!$trust_source) {
90                                                 throw new \Exception('Not trusting actor: ' . $actor . '. It differs from attributedTo: ' . $attributed_to);
91                                         }
92                                 }
93
94                                 // $trust_source is called by reference and is set to true if the content was retrieved successfully
95                                 $object_data = ActivityPub\Receiver::prepareObjectData($activity, $uid, $push, $trust_source);
96                                 if (empty($object_data)) {
97                                         throw new \Exception('No object data found');
98                                 }
99
100                                 if (!$trust_source) {
101                                         throw new \Exception('No trust for activity type "' . $type . '", so we quit now.');
102                                 }
103
104                                 if (!empty($body) && empty($object_data['raw'])) {
105                                         $object_data['raw'] = $body;
106                                 }
107
108                                 // Internal flag for thread completion. See Processor.php
109                                 if (!empty($activity['thread-completion'])) {
110                                         $object_data['thread-completion'] = $activity['thread-completion'];
111                                 }
112
113                                 if (!empty($activity['completion-mode'])) {
114                                         $object_data['completion-mode'] = $activity['completion-mode'];
115                                 }
116
117                                 $results[] = [
118                                         'title'   => DI::l10n()->t('Object data'),
119                                         'content' => visible_whitespace(var_export($object_data, true))
120                                 ];
121
122                                 $item = ActivityPub\Processor::createItem($object_data, true);
123
124                                 $results[] = [
125                                         'title'   => DI::l10n()->t('Result Item'),
126                                         'content' => visible_whitespace(var_export($item, true))
127                                 ];
128                         } catch (\Throwable $e) {
129                                 $results[] = [
130                                         'title'   => DI::l10n()->t('Error'),
131                                         'content' => $e->getMessage(),
132                                 ];
133                         }
134                 }
135
136                 $tpl = Renderer::getMarkupTemplate('debug/activitypubconversion.tpl');
137                 $o = Renderer::replaceMacros($tpl, [
138                         '$title'   => DI::l10n()->t('ActivityPub Conversion'),
139                         '$source'  => ['source', DI::l10n()->t('Source activity'), $_REQUEST['source'] ?? '', ''],
140                         '$results' => $results,
141                         '$submit' => DI::l10n()->t('Submit'),
142                 ]);
143
144                 return $o;
145         }
146 }