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