]> git.mxchange.org Git - friendica.git/blob - src/Module/Debug/Babel.php
Issue 9743: Added translatable texts
[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\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                                         $config = \HTMLPurifier_Config::createDefault();
184                                         $HTMLPurifier = new \HTMLPurifier($config);
185                                         $purified = $HTMLPurifier->purify($html);
186
187                                         $results[] = [
188                                                 'title'   => DI::l10n()->t('HTML Purified (raw)'),
189                                                 'content' => visible_whitespace($purified),
190                                         ];
191
192                                         $results[] = [
193                                                 'title'   => DI::l10n()->t('HTML Purified (hex)'),
194                                                 'content' => visible_whitespace(bin2hex($purified)),
195                                         ];
196
197                                         $results[] = [
198                                                 'title'   => DI::l10n()->t('HTML Purified'),
199                                                 'content' => $purified,
200                                         ];
201
202                                         $bbcode = Text\HTML::toBBCode($html);
203                                         $results[] = [
204                                                 'title'   => DI::l10n()->t('HTML::toBBCode'),
205                                                 'content' => visible_whitespace($bbcode)
206                                         ];
207
208                                         $html2 = Text\BBCode::convert($bbcode);
209                                         $results[] = [
210                                                 'title'   => DI::l10n()->t('HTML::toBBCode => BBCode::convert'),
211                                                 'content' => $html2
212                                         ];
213
214                                         $results[] = [
215                                                 'title'   => DI::l10n()->t('HTML::toBBCode => BBCode::convert (raw HTML)'),
216                                                 'content' => htmlspecialchars($html2)
217                                         ];
218
219                                         $bbcode2plain = Text\BBCode::toPlaintext($bbcode);
220                                         $results[] = [
221                                                 'title'   => DI::l10n()->t('HTML::toBBCode => BBCode::toPlaintext'),
222                                                 'content' => visible_whitespace($bbcode2plain),
223                                         ];
224
225                                         $markdown = Text\HTML::toMarkdown($html);
226                                         $results[] = [
227                                                 'title'   => DI::l10n()->t('HTML::toMarkdown'),
228                                                 'content' => visible_whitespace($markdown)
229                                         ];
230
231                                         $text = Text\HTML::toPlaintext($html, 0);
232                                         $results[] = [
233                                                 'title'   => DI::l10n()->t('HTML::toPlaintext'),
234                                                 'content' => visible_whitespace($text),
235                                         ];
236
237                                         $text = Text\HTML::toPlaintext($html, 0, true);
238                                         $results[] = [
239                                                 'title'   => DI::l10n()->t('HTML::toPlaintext (compact)'),
240                                                 'content' => visible_whitespace($text),
241                                         ];
242                                         break;
243                                 case 'twitter':
244                                         $json = trim($_REQUEST['text']);
245
246                                         if (file_exists('addon/twitter/twitter.php')) {
247                                                 require_once 'addon/twitter/twitter.php';
248
249                                                 if (parse_url($json) !== false) {
250                                                         preg_match('#^https?://(?:mobile\.|www\.)?twitter.com/[^/]+/status/(\d+).*#', $json, $matches);
251                                                         $status = twitter_statuses_show($matches[1]);
252                                                 } else {
253                                                         $status = json_decode($json);
254                                                 }
255
256                                                 $results[] = [
257                                                         'title'   => DI::l10n()->t('Decoded post'),
258                                                         'content' => visible_whitespace(var_export($status, true)),
259                                                 ];
260
261                                                 $postarray = [];
262                                                 $postarray['object-type'] = Activity\ObjectType::NOTE;
263
264                                                 if (!empty($status->full_text)) {
265                                                         $postarray['body'] = $status->full_text;
266                                                 } else {
267                                                         $postarray['body'] = $status->text;
268                                                 }
269
270                                                 // When the post contains links then use the correct object type
271                                                 if (count($status->entities->urls) > 0) {
272                                                         $postarray['object-type'] = Activity\ObjectType::BOOKMARK;
273                                                 }
274
275                                                 $picture = \twitter_media_entities($status, $postarray);
276
277                                                 $results[] = [
278                                                         'title'   => DI::l10n()->t('Post array before expand entities'),
279                                                         'content' => visible_whitespace(var_export($postarray, true)),
280                                                 ];
281
282                                                 $converted = \twitter_expand_entities($postarray['body'], $status, $picture);
283
284                                                 $results[] = [
285                                                         'title'   => DI::l10n()->t('Post converted'),
286                                                         'content' => visible_whitespace(var_export($converted, true)),
287                                                 ];
288
289                                                 $results[] = [
290                                                         'title'   => DI::l10n()->t('Converted body'),
291                                                         'content' => visible_whitespace($converted['body']),
292                                                 ];
293                                         } else {
294                                                 $results[] = [
295                                                         'title'   => DI::l10n()->t('Error'),
296                                                         'content' => DI::l10n()->t('Twitter addon is absent from the addon/ folder.'),
297                                                 ];
298                                         }
299
300                                         break;
301                         }
302                 }
303
304                 $tpl = Renderer::getMarkupTemplate('babel.tpl');
305                 $o = Renderer::replaceMacros($tpl, [
306                         '$title'         => DI::l10n()->t('Babel Diagnostic'),
307                         '$text'          => ['text', DI::l10n()->t('Source text'), $_REQUEST['text'] ?? '', ''],
308                         '$type_bbcode'   => ['type', DI::l10n()->t('BBCode'), 'bbcode', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'bbcode'],
309                         '$type_diaspora' => ['type', DI::l10n()->t('Diaspora'), 'diaspora', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'diaspora'],
310                         '$type_markdown' => ['type', DI::l10n()->t('Markdown'), 'markdown', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'markdown'],
311                         '$type_html'     => ['type', DI::l10n()->t('HTML'), 'html', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'html'],
312                         '$flag_twitter'  => file_exists('addon/twitter/twitter.php'),
313                         '$type_twitter'  => ['type', DI::l10n()->t('Twitter Source / Tweet URL (requires API key)'), 'twitter', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'twitter'],
314                         '$results'       => $results,
315                         '$submit'        => DI::l10n()->t('Submit'),
316                 ]);
317
318                 return $o;
319         }
320 }