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