Update copyright
[friendica.git/.git] / src / Module / Debug / Babel.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\Content\PageInfo;
26 use Friendica\Content\Text;
27 use Friendica\Core\Renderer;
28 use Friendica\DI;
29 use Friendica\Model\Item;
30 use Friendica\Protocol\Activity;
31 use Friendica\Util\XML;
32
33 /**
34  * Translates input text into different formats (HTML, BBCode, Markdown)
35  */
36 class Babel extends BaseModule
37 {
38         public static function content(array $parameters = [])
39         {
40                 function visible_whitespace($s)
41                 {
42                         return '<pre>' . htmlspecialchars($s) . '</pre>';
43                 }
44
45                 $results = [];
46                 if (!empty($_REQUEST['text'])) {
47                         switch (($_REQUEST['type'] ?? '') ?: 'bbcode') {
48                                 case 'bbcode':
49                                         $bbcode = $_REQUEST['text'];
50                                         $results[] = [
51                                                 'title'   => DI::l10n()->t('Source input'),
52                                                 'content' => visible_whitespace($bbcode)
53                                         ];
54
55                                         $plain = Text\BBCode::toPlaintext($bbcode, false);
56                                         $results[] = [
57                                                 'title'   => DI::l10n()->t('BBCode::toPlaintext'),
58                                                 'content' => visible_whitespace($plain)
59                                         ];
60
61                                         $html = Text\BBCode::convert($bbcode);
62                                         $results[] = [
63                                                 'title'   => DI::l10n()->t('BBCode::convert (raw HTML)'),
64                                                 'content' => visible_whitespace($html)
65                                         ];
66
67                                         $results[] = [
68                                                 'title'   => DI::l10n()->t('BBCode::convert (hex)'),
69                                                 'content' => visible_whitespace(bin2hex($html)),
70                                         ];
71
72                                         $results[] = [
73                                                 'title'   => DI::l10n()->t('BBCode::convert'),
74                                                 'content' => $html
75                                         ];
76
77                                         $bbcode2 = Text\HTML::toBBCode($html);
78                                         $results[] = [
79                                                 'title'   => DI::l10n()->t('BBCode::convert => HTML::toBBCode'),
80                                                 'content' => visible_whitespace($bbcode2)
81                                         ];
82
83                                         $markdown = Text\BBCode::toMarkdown($bbcode);
84                                         $results[] = [
85                                                 'title'   => DI::l10n()->t('BBCode::toMarkdown'),
86                                                 'content' => visible_whitespace($markdown)
87                                         ];
88
89                                         $html2 = Text\Markdown::convert($markdown);
90                                         $results[] = [
91                                                 'title'   => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert (raw HTML)'),
92                                                 'content' => visible_whitespace($html2)
93                                         ];
94                                         $results[] = [
95                                                 'title'   => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert'),
96                                                 'content' => $html2
97                                         ];
98
99                                         $bbcode3 = Text\Markdown::toBBCode($markdown);
100                                         $results[] = [
101                                                 'title'   => DI::l10n()->t('BBCode::toMarkdown => Markdown::toBBCode'),
102                                                 'content' => visible_whitespace($bbcode3)
103                                         ];
104
105                                         $bbcode4 = Text\HTML::toBBCode($html2);
106                                         $results[] = [
107                                                 'title'   => DI::l10n()->t('BBCode::toMarkdown =>  Markdown::convert => HTML::toBBCode'),
108                                                 'content' => visible_whitespace($bbcode4)
109                                         ];
110
111                                         $tags = Text\BBCode::getTags($bbcode);
112
113                                         $body = Item::setHashtags($bbcode);
114                                         $results[] = [
115                                                 'title'   => DI::l10n()->t('Item Body'),
116                                                 'content' => visible_whitespace($body)
117                                         ];
118                                         $results[] = [
119                                                 'title'   => DI::l10n()->t('Item Tags'),
120                                                 'content' => visible_whitespace(var_export($tags, true)),
121                                         ];
122
123                                         $body2 = PageInfo::searchAndAppendToBody($bbcode, true);
124                                         $results[] = [
125                                                 'title'   => DI::l10n()->t('PageInfo::appendToBody'),
126                                                 'content' => visible_whitespace($body2)
127                                         ];
128                                         $html3 = Text\BBCode::convert($body2);
129                                         $results[] = [
130                                                 'title'   => DI::l10n()->t('PageInfo::appendToBody => BBCode::convert (raw HTML)'),
131                                                 'content' => visible_whitespace($html3)
132                                         ];
133                                         $results[] = [
134                                                 'title'   => DI::l10n()->t('PageInfo::appendToBody => BBCode::convert'),
135                                                 'content' => $html3
136                                         ];
137                                         break;
138                                 case 'diaspora':
139                                         $diaspora = trim($_REQUEST['text']);
140                                         $results[] = [
141                                                 'title'   => DI::l10n()->t('Source input (Diaspora format)'),
142                                                 'content' => visible_whitespace($diaspora),
143                                         ];
144
145                                         $markdown = XML::unescape($diaspora);
146                                 case 'markdown':
147                                         $markdown = $markdown ?? trim($_REQUEST['text']);
148
149                                         $results[] = [
150                                                 'title'   => DI::l10n()->t('Source input (Markdown)'),
151                                                 'content' => visible_whitespace($markdown),
152                                         ];
153
154                                         $html = Text\Markdown::convert($markdown);
155                                         $results[] = [
156                                                 'title'   => DI::l10n()->t('Markdown::convert (raw HTML)'),
157                                                 'content' => visible_whitespace($html),
158                                         ];
159
160                                         $results[] = [
161                                                 'title'   => DI::l10n()->t('Markdown::convert'),
162                                                 'content' => $html
163                                         ];
164
165                                         $bbcode = Text\Markdown::toBBCode($markdown);
166                                         $results[] = [
167                                                 'title'   => DI::l10n()->t('Markdown::toBBCode'),
168                                                 'content' => visible_whitespace($bbcode),
169                                         ];
170                                         break;
171                                 case 'html' :
172                                         $html = trim($_REQUEST['text']);
173                                         $results[] = [
174                                                 'title'   => DI::l10n()->t('Raw HTML input'),
175                                                 'content' => visible_whitespace($html),
176                                         ];
177
178                                         $results[] = [
179                                                 'title'   => DI::l10n()->t('HTML Input'),
180                                                 'content' => $html
181                                         ];
182
183                                         $purified = Text\HTML::purify($html);
184
185                                         $results[] = [
186                                                 'title'   => DI::l10n()->t('HTML Purified (raw)'),
187                                                 'content' => visible_whitespace($purified),
188                                         ];
189
190                                         $results[] = [
191                                                 'title'   => DI::l10n()->t('HTML Purified (hex)'),
192                                                 'content' => visible_whitespace(bin2hex($purified)),
193                                         ];
194
195                                         $results[] = [
196                                                 'title'   => DI::l10n()->t('HTML Purified'),
197                                                 'content' => $purified,
198                                         ];
199
200                                         $bbcode = Text\HTML::toBBCode($html);
201                                         $results[] = [
202                                                 'title'   => DI::l10n()->t('HTML::toBBCode'),
203                                                 'content' => visible_whitespace($bbcode)
204                                         ];
205
206                                         $html2 = Text\BBCode::convert($bbcode);
207                                         $results[] = [
208                                                 'title'   => DI::l10n()->t('HTML::toBBCode => BBCode::convert'),
209                                                 'content' => $html2
210                                         ];
211
212                                         $results[] = [
213                                                 'title'   => DI::l10n()->t('HTML::toBBCode => BBCode::convert (raw HTML)'),
214                                                 'content' => htmlspecialchars($html2)
215                                         ];
216
217                                         $bbcode2plain = Text\BBCode::toPlaintext($bbcode);
218                                         $results[] = [
219                                                 'title'   => DI::l10n()->t('HTML::toBBCode => BBCode::toPlaintext'),
220                                                 'content' => visible_whitespace($bbcode2plain),
221                                         ];
222
223                                         $markdown = Text\HTML::toMarkdown($html);
224                                         $results[] = [
225                                                 'title'   => DI::l10n()->t('HTML::toMarkdown'),
226                                                 'content' => visible_whitespace($markdown)
227                                         ];
228
229                                         $text = Text\HTML::toPlaintext($html, 0);
230                                         $results[] = [
231                                                 'title'   => DI::l10n()->t('HTML::toPlaintext'),
232                                                 'content' => visible_whitespace($text),
233                                         ];
234
235                                         $text = Text\HTML::toPlaintext($html, 0, true);
236                                         $results[] = [
237                                                 'title'   => DI::l10n()->t('HTML::toPlaintext (compact)'),
238                                                 'content' => visible_whitespace($text),
239                                         ];
240                                         break;
241                                 case 'twitter':
242                                         $json = trim($_REQUEST['text']);
243
244                                         if (file_exists('addon/twitter/twitter.php')) {
245                                                 require_once 'addon/twitter/twitter.php';
246
247                                                 if (parse_url($json) !== false) {
248                                                         preg_match('#^https?://(?:mobile\.|www\.)?twitter.com/[^/]+/status/(\d+).*#', $json, $matches);
249                                                         $status = twitter_statuses_show($matches[1]);
250                                                 } else {
251                                                         $status = json_decode($json);
252                                                 }
253
254                                                 $results[] = [
255                                                         'title'   => DI::l10n()->t('Decoded post'),
256                                                         'content' => visible_whitespace(var_export($status, true)),
257                                                 ];
258
259                                                 $postarray = [];
260                                                 $postarray['object-type'] = Activity\ObjectType::NOTE;
261
262                                                 if (!empty($status->full_text)) {
263                                                         $postarray['body'] = $status->full_text;
264                                                 } else {
265                                                         $postarray['body'] = $status->text;
266                                                 }
267
268                                                 // When the post contains links then use the correct object type
269                                                 if (count($status->entities->urls) > 0) {
270                                                         $postarray['object-type'] = Activity\ObjectType::BOOKMARK;
271                                                 }
272
273                                                 $picture = \twitter_media_entities($status, $postarray);
274
275                                                 $results[] = [
276                                                         'title'   => DI::l10n()->t('Post array before expand entities'),
277                                                         'content' => visible_whitespace(var_export($postarray, true)),
278                                                 ];
279
280                                                 $converted = \twitter_expand_entities($postarray['body'], $status, $picture);
281
282                                                 $results[] = [
283                                                         'title'   => DI::l10n()->t('Post converted'),
284                                                         'content' => visible_whitespace(var_export($converted, true)),
285                                                 ];
286
287                                                 $results[] = [
288                                                         'title'   => DI::l10n()->t('Converted body'),
289                                                         'content' => visible_whitespace($converted['body']),
290                                                 ];
291                                         } else {
292                                                 $results[] = [
293                                                         'title'   => DI::l10n()->t('Error'),
294                                                         'content' => DI::l10n()->t('Twitter addon is absent from the addon/ folder.'),
295                                                 ];
296                                         }
297
298                                         break;
299                         }
300                 }
301
302                 $tpl = Renderer::getMarkupTemplate('babel.tpl');
303                 $o = Renderer::replaceMacros($tpl, [
304                         '$title'         => DI::l10n()->t('Babel Diagnostic'),
305                         '$text'          => ['text', DI::l10n()->t('Source text'), $_REQUEST['text'] ?? '', ''],
306                         '$type_bbcode'   => ['type', DI::l10n()->t('BBCode'), 'bbcode', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'bbcode'],
307                         '$type_diaspora' => ['type', DI::l10n()->t('Diaspora'), 'diaspora', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'diaspora'],
308                         '$type_markdown' => ['type', DI::l10n()->t('Markdown'), 'markdown', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'markdown'],
309                         '$type_html'     => ['type', DI::l10n()->t('HTML'), 'html', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'html'],
310                         '$flag_twitter'  => file_exists('addon/twitter/twitter.php'),
311                         '$type_twitter'  => ['type', DI::l10n()->t('Twitter Source / Tweet URL (requires API key)'), 'twitter', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'twitter'],
312                         '$results'       => $results,
313                         '$submit'        => DI::l10n()->t('Submit'),
314                 ]);
315
316                 return $o;
317         }
318 }