3 * @copyright Copyright (C) 2020, Friendica
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\Protocol;
28 use Friendica\Core\Renderer;
30 use Friendica\Model\Conversation;
31 use Friendica\Model\Item;
32 use Friendica\Protocol\Activity;
33 use Friendica\Model\Tag;
34 use Friendica\Util\XML;
37 * Translates input text into different formats (HTML, BBCode, Markdown)
39 class Babel extends BaseModule
41 public static function content(array $parameters = [])
43 function visible_whitespace($s)
45 return '<pre>' . htmlspecialchars($s) . '</pre>';
49 if (!empty($_REQUEST['text'])) {
50 switch (($_REQUEST['type'] ?? '') ?: 'bbcode') {
52 $bbcode = $_REQUEST['text'];
54 'title' => DI::l10n()->t('Source input'),
55 'content' => visible_whitespace($bbcode)
58 $plain = Text\BBCode::toPlaintext($bbcode, false);
60 'title' => DI::l10n()->t('BBCode::toPlaintext'),
61 'content' => visible_whitespace($plain)
64 $html = Text\BBCode::convert($bbcode);
66 'title' => DI::l10n()->t('BBCode::convert (raw HTML)'),
67 'content' => visible_whitespace($html)
71 'title' => DI::l10n()->t('BBCode::convert (hex)'),
72 'content' => visible_whitespace(bin2hex($html)),
76 'title' => DI::l10n()->t('BBCode::convert'),
80 $bbcode2 = Text\HTML::toBBCode($html);
82 'title' => DI::l10n()->t('BBCode::convert => HTML::toBBCode'),
83 'content' => visible_whitespace($bbcode2)
86 $markdown = Text\BBCode::toMarkdown($bbcode);
88 'title' => DI::l10n()->t('BBCode::toMarkdown'),
89 'content' => visible_whitespace($markdown)
92 $html2 = Text\Markdown::convert($markdown);
94 'title' => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert (raw HTML)'),
95 'content' => visible_whitespace($html2)
98 'title' => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert'),
102 $bbcode3 = Text\Markdown::toBBCode($markdown);
104 'title' => DI::l10n()->t('BBCode::toMarkdown => Markdown::toBBCode'),
105 'content' => visible_whitespace($bbcode3)
108 $bbcode4 = Text\HTML::toBBCode($html2);
110 'title' => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert => HTML::toBBCode'),
111 'content' => visible_whitespace($bbcode4)
114 $tags = Text\BBCode::getTags($bbcode);
116 $body = Item::setHashtags($bbcode);
118 'title' => DI::l10n()->t('Item Body'),
119 'content' => visible_whitespace($body)
122 'title' => DI::l10n()->t('Item Tags'),
123 'content' => visible_whitespace(var_export($tags, true)),
126 $body2 = PageInfo::searchAndAppendToBody($bbcode, true);
128 'title' => DI::l10n()->t('PageInfo::appendToBody'),
129 'content' => visible_whitespace($body2)
131 $html3 = Text\BBCode::convert($body2);
133 'title' => DI::l10n()->t('PageInfo::appendToBody => BBCode::convert (raw HTML)'),
134 'content' => visible_whitespace($html3)
137 'title' => DI::l10n()->t('PageInfo::appendToBody => BBCode::convert'),
142 $diaspora = trim($_REQUEST['text']);
144 'title' => DI::l10n()->t('Source input (Diaspora format)'),
145 'content' => visible_whitespace($diaspora),
148 $markdown = XML::unescape($diaspora);
150 $markdown = $markdown ?? trim($_REQUEST['text']);
153 'title' => DI::l10n()->t('Source input (Markdown)'),
154 'content' => visible_whitespace($markdown),
157 $html = Text\Markdown::convert($markdown);
159 'title' => DI::l10n()->t('Markdown::convert (raw HTML)'),
160 'content' => visible_whitespace($html),
164 'title' => DI::l10n()->t('Markdown::convert'),
168 $bbcode = Text\Markdown::toBBCode($markdown);
170 'title' => DI::l10n()->t('Markdown::toBBCode'),
171 'content' => visible_whitespace($bbcode),
175 $html = trim($_REQUEST['text']);
177 'title' => DI::l10n()->t('Raw HTML input'),
178 'content' => visible_whitespace($html),
182 'title' => DI::l10n()->t('HTML Input'),
186 $config = \HTMLPurifier_Config::createDefault();
187 $HTMLPurifier = new \HTMLPurifier($config);
188 $purified = $HTMLPurifier->purify($html);
191 'title' => DI::l10n()->t('HTML Purified (raw)'),
192 'content' => visible_whitespace($purified),
196 'title' => DI::l10n()->t('HTML Purified (hex)'),
197 'content' => visible_whitespace(bin2hex($purified)),
201 'title' => DI::l10n()->t('HTML Purified'),
202 'content' => $purified,
205 $bbcode = Text\HTML::toBBCode($html);
207 'title' => DI::l10n()->t('HTML::toBBCode'),
208 'content' => visible_whitespace($bbcode)
211 $html2 = Text\BBCode::convert($bbcode);
213 'title' => DI::l10n()->t('HTML::toBBCode => BBCode::convert'),
218 'title' => DI::l10n()->t('HTML::toBBCode => BBCode::convert (raw HTML)'),
219 'content' => htmlspecialchars($html2)
222 $bbcode2plain = Text\BBCode::toPlaintext($bbcode);
224 'title' => DI::l10n()->t('HTML::toBBCode => BBCode::toPlaintext'),
225 'content' => visible_whitespace($bbcode2plain),
228 $markdown = Text\HTML::toMarkdown($html);
230 'title' => DI::l10n()->t('HTML::toMarkdown'),
231 'content' => visible_whitespace($markdown)
234 $text = Text\HTML::toPlaintext($html, 0);
236 'title' => DI::l10n()->t('HTML::toPlaintext'),
237 'content' => visible_whitespace($text),
240 $text = Text\HTML::toPlaintext($html, 0, true);
242 'title' => DI::l10n()->t('HTML::toPlaintext (compact)'),
243 'content' => visible_whitespace($text),
247 $json = trim($_REQUEST['text']);
249 $status = json_decode($json);
252 'title' => DI::l10n()->t('Decoded post'),
253 'content' => visible_whitespace(var_export($status, true)),
257 $postarray['object-type'] = Activity\ObjectType::NOTE;
259 if (!empty($status->full_text)) {
260 $postarray['body'] = $status->full_text;
262 $postarray['body'] = $status->text;
265 // When the post contains links then use the correct object type
266 if (count($status->entities->urls) > 0) {
267 $postarray['object-type'] = Activity\ObjectType::BOOKMARK;
270 if (file_exists('addon/twitter/twitter.php')) {
271 require_once 'addon/twitter/twitter.php';
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()->t('Error'),
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 '$text' => ['text', DI::l10n()->t('Source text'), $_REQUEST['text'] ?? '', ''],
305 '$type_bbcode' => ['type', DI::l10n()->t('BBCode'), 'bbcode', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'bbcode'],
306 '$type_diaspora' => ['type', DI::l10n()->t('Diaspora'), 'diaspora', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'diaspora'],
307 '$type_markdown' => ['type', DI::l10n()->t('Markdown'), 'markdown', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'markdown'],
308 '$type_html' => ['type', DI::l10n()->t('HTML'), 'html', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'html'],
309 '$flag_twitter' => file_exists('addon/twitter/twitter.php'),
310 '$type_twitter' => ['type', DI::l10n()->t('Twitter Source'), 'twitter', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'twitter'],
311 '$results' => $results