]> git.mxchange.org Git - friendica.git/blob - src/Module/Debug/ActivityPubConversion.php
Issue 9743: Added translatable texts
[friendica.git] / src / Module / Debug / ActivityPubConversion.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\Content\Text;
26 use Friendica\Core\Logger;
27 use Friendica\Core\Renderer;
28 use Friendica\DI;
29 use Friendica\Model\Item;
30 use Friendica\Model\Tag;
31 use Friendica\Protocol\ActivityPub;
32 use Friendica\Util\JsonLD;
33 use Friendica\Util\XML;
34
35 class ActivityPubConversion extends BaseModule
36 {
37         public static function content(array $parameters = [])
38         {
39                 function visible_whitespace($s)
40                 {
41                         return '<pre>' . htmlspecialchars($s) . '</pre>';
42                 }
43
44                 $results = [];
45                 if (!empty($_REQUEST['source'])) {
46                         try {
47                                 $source = json_decode($_REQUEST['source'], true);
48                                 $trust_source = true;
49                                 $uid = local_user();
50                                 $push = false;
51
52                                 if (!$source) {
53                                         throw new \Exception('Failed to decode source JSON');
54                                 }
55
56                                 $formatted = json_encode($source, JSON_PRETTY_PRINT);
57                                 $results[] = [
58                                         'title'   => DI::l10n()->t('Formatted'),
59                                         'content' => visible_whitespace(trim(var_export($formatted, true), "'")),
60                                 ];
61                                 $results[] = [
62                                         'title'   => DI::l10n()->t('Source'),
63                                         'content' => visible_whitespace(var_export($source, true))
64                                 ];
65                                 $activity = JsonLD::compact($source);
66                                 if (!$activity) {
67                                         throw new \Exception('Failed to compact JSON');
68                                 }
69                                 $results[] = [
70                                         'title'   => DI::l10n()->t('Activity'),
71                                         'content' => visible_whitespace(var_export($activity, true))
72                                 ];
73
74                                 $type = JsonLD::fetchElement($activity, '@type');
75
76                                 if (!$type) {
77                                         throw new \Exception('Empty type');
78                                 }
79
80                                 if (!JsonLD::fetchElement($activity, 'as:object', '@id')) {
81                                         throw new \Exception('Empty object');
82                                 }
83
84                                 if (!JsonLD::fetchElement($activity, 'as:actor', '@id')) {
85                                         throw new \Exception('Empty actor');
86                                 }
87
88                                 // Don't trust the source if "actor" differs from "attributedTo". The content could be forged.
89                                 if ($trust_source && ($type == 'as:Create') && is_array($activity['as:object'])) {
90                                         $actor = JsonLD::fetchElement($activity, 'as:actor', '@id');
91                                         $attributed_to = JsonLD::fetchElement($activity['as:object'], 'as:attributedTo', '@id');
92                                         $trust_source = ($actor == $attributed_to);
93                                         if (!$trust_source) {
94                                                 throw new \Exception('Not trusting actor: ' . $actor . '. It differs from attributedTo: ' . $attributed_to);
95                                         }
96                                 }
97
98                                 // $trust_source is called by reference and is set to true if the content was retrieved successfully
99                                 $object_data = ActivityPub\Receiver::prepareObjectData($activity, $uid, $push, $trust_source);
100                                 if (empty($object_data)) {
101                                         throw new \Exception('No object data found');
102                                 }
103
104                                 if (!$trust_source) {
105                                         throw new \Exception('No trust for activity type "' . $type . '", so we quit now.');
106                                 }
107
108                                 if (!empty($body) && empty($object_data['raw'])) {
109                                         $object_data['raw'] = $body;
110                                 }
111
112                                 // Internal flag for thread completion. See Processor.php
113                                 if (!empty($activity['thread-completion'])) {
114                                         $object_data['thread-completion'] = $activity['thread-completion'];
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);
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 }