3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Debug;
24 use Friendica\BaseModule;
25 use Friendica\Content\PageInfo;
26 use Friendica\Content\Text;
27 use Friendica\Core\Renderer;
29 use Friendica\Model\Item;
30 use Friendica\Protocol\Activity;
31 use Friendica\Util\XML;
34 * Translates input text into different formats (HTML, BBCode, Markdown)
36 class Babel extends BaseModule
38 protected function content(array $request = []): string
40 function visible_whitespace($s)
42 return '<pre>' . htmlspecialchars($s) . '</pre>';
46 if (!empty($_REQUEST['text'])) {
47 switch (($_REQUEST['type'] ?? '') ?: 'bbcode') {
49 $bbcode = $_REQUEST['text'];
51 'title' => DI::l10n()->t('Source input'),
52 'content' => visible_whitespace($bbcode)
55 $plain = Text\BBCode::toPlaintext($bbcode, false);
57 'title' => DI::l10n()->t('BBCode::toPlaintext'),
58 'content' => visible_whitespace($plain)
61 $html = Text\BBCode::convert($bbcode);
63 'title' => DI::l10n()->t('BBCode::convert (raw HTML)'),
64 'content' => visible_whitespace($html)
68 'title' => DI::l10n()->t('BBCode::convert (hex)'),
69 'content' => visible_whitespace(bin2hex($html)),
73 'title' => DI::l10n()->t('BBCode::convert'),
77 $bbcode2 = Text\HTML::toBBCode($html);
79 'title' => DI::l10n()->t('BBCode::convert => HTML::toBBCode'),
80 'content' => visible_whitespace($bbcode2)
83 $markdown = Text\BBCode::toMarkdown($bbcode);
85 'title' => DI::l10n()->t('BBCode::toMarkdown'),
86 'content' => visible_whitespace($markdown)
89 $html2 = Text\Markdown::convert($markdown);
91 'title' => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert (raw HTML)'),
92 'content' => visible_whitespace($html2)
95 'title' => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert'),
99 $bbcode3 = Text\Markdown::toBBCode($markdown);
101 'title' => DI::l10n()->t('BBCode::toMarkdown => Markdown::toBBCode'),
102 'content' => visible_whitespace($bbcode3)
105 $bbcode4 = Text\HTML::toBBCode($html2);
107 'title' => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert => HTML::toBBCode'),
108 'content' => visible_whitespace($bbcode4)
111 $tags = Text\BBCode::getTags($bbcode);
113 $body = Item::setHashtags($bbcode);
115 'title' => DI::l10n()->t('Item Body'),
116 'content' => visible_whitespace($body)
119 'title' => DI::l10n()->t('Item Tags'),
120 'content' => visible_whitespace(var_export($tags, true)),
123 $body2 = PageInfo::searchAndAppendToBody($bbcode, true);
125 'title' => DI::l10n()->t('PageInfo::appendToBody'),
126 'content' => visible_whitespace($body2)
128 $html3 = Text\BBCode::convert($body2);
130 'title' => DI::l10n()->t('PageInfo::appendToBody => BBCode::convert (raw HTML)'),
131 'content' => visible_whitespace($html3)
134 'title' => DI::l10n()->t('PageInfo::appendToBody => BBCode::convert'),
139 $diaspora = trim($_REQUEST['text']);
141 'title' => DI::l10n()->t('Source input (Diaspora format)'),
142 'content' => visible_whitespace($diaspora),
145 $markdown = XML::unescape($diaspora);
147 $markdown = $markdown ?? trim($_REQUEST['text']);
150 'title' => DI::l10n()->t('Source input (Markdown)'),
151 'content' => visible_whitespace($markdown),
154 $html = Text\Markdown::convert($markdown);
156 'title' => DI::l10n()->t('Markdown::convert (raw HTML)'),
157 'content' => visible_whitespace($html),
161 'title' => DI::l10n()->t('Markdown::convert'),
165 $bbcode = Text\Markdown::toBBCode($markdown);
167 'title' => DI::l10n()->t('Markdown::toBBCode'),
168 'content' => visible_whitespace($bbcode),
172 $html = trim($_REQUEST['text']);
174 'title' => DI::l10n()->t('Raw HTML input'),
175 'content' => visible_whitespace($html),
179 'title' => DI::l10n()->t('HTML Input'),
183 $purified = Text\HTML::purify($html);
186 'title' => DI::l10n()->t('HTML Purified (raw)'),
187 'content' => visible_whitespace($purified),
191 'title' => DI::l10n()->t('HTML Purified (hex)'),
192 'content' => visible_whitespace(bin2hex($purified)),
196 'title' => DI::l10n()->t('HTML Purified'),
197 'content' => $purified,
200 $bbcode = Text\HTML::toBBCode($html);
202 'title' => DI::l10n()->t('HTML::toBBCode'),
203 'content' => visible_whitespace($bbcode)
206 $html2 = Text\BBCode::convert($bbcode);
208 'title' => DI::l10n()->t('HTML::toBBCode => BBCode::convert'),
213 'title' => DI::l10n()->t('HTML::toBBCode => BBCode::convert (raw HTML)'),
214 'content' => htmlspecialchars($html2)
217 $bbcode2plain = Text\BBCode::toPlaintext($bbcode);
219 'title' => DI::l10n()->t('HTML::toBBCode => BBCode::toPlaintext'),
220 'content' => visible_whitespace($bbcode2plain),
223 $markdown = Text\HTML::toMarkdown($html);
225 'title' => DI::l10n()->t('HTML::toMarkdown'),
226 'content' => visible_whitespace($markdown)
229 $text = Text\HTML::toPlaintext($html, 0);
231 'title' => DI::l10n()->t('HTML::toPlaintext'),
232 'content' => visible_whitespace($text),
235 $text = Text\HTML::toPlaintext($html, 0, true);
237 'title' => DI::l10n()->t('HTML::toPlaintext (compact)'),
238 'content' => visible_whitespace($text),
242 $json = trim($_REQUEST['text']);
244 if (file_exists('addon/twitter/twitter.php')) {
245 require_once 'addon/twitter/twitter.php';
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]);
251 $status = json_decode($json);
255 'title' => DI::l10n()->t('Decoded post'),
256 'content' => visible_whitespace(var_export($status, true)),
260 $postarray['object-type'] = Activity\ObjectType::NOTE;
262 if (!empty($status->full_text)) {
263 $postarray['body'] = $status->full_text;
265 $postarray['body'] = $status->text;
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;
273 $picture = \twitter_media_entities($status, $postarray);
276 'title' => DI::l10n()->t('Post array before expand entities'),
277 'content' => visible_whitespace(var_export($postarray, true)),
280 $converted = \twitter_expand_entities($postarray['body'], $status, $picture);
283 'title' => DI::l10n()->t('Post converted'),
284 'content' => visible_whitespace(var_export($converted, true)),
288 'title' => DI::l10n()->t('Converted body'),
289 'content' => visible_whitespace($converted['body']),
293 'title' => DI::l10n()->tt('Error', 'Errors', 1),
294 'content' => DI::l10n()->t('Twitter addon is absent from the addon/ folder.'),
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'),