Skip to content

Commit

Permalink
Allow multibyte chars for telegram footer
Browse files Browse the repository at this point in the history
  • Loading branch information
D-MBLD committed Mar 21, 2023
1 parent d3036a0 commit f406cf1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion controller/acp_controller.php
Expand Up @@ -106,7 +106,7 @@ public function display_options($testcall = false)
{
foreach ($configparams as $name)
{
$this->config->set($name, $this->request->variable($name, ''));
$this->config->set($name, $this->request->variable($name, '', true));
}

// Add option settings change action to the admin log
Expand Down
27 changes: 19 additions & 8 deletions core/commands.php
Expand Up @@ -494,7 +494,11 @@ private function errorInvalidForum()
return [$text, [$this->language->lang('EBT_OK') => 'allForums']];
}

public function format_text($text, $entities) {
/** Format the text by adding bbCodes according to the formatting information,
* which telegram sends as so called entities.
*/
public function format_text($text, $entities)
{
/* Split the text, at every point where a formatting starts or ends into an array.
* Therefore we collect at first all splitpoints, and remove duplicates.
*/
Expand All @@ -507,18 +511,23 @@ public function format_text($text, $entities) {
$split_points = array_unique($split_points);
rsort($split_points);
$chunks = array();
foreach($split_points as $point) {
foreach ($split_points as $point)
{
$chunks[$point] = mb_substr($text, $point);
$text = mb_substr($text, 0, $point);
}
}
ksort($chunks);
//Sort by end of formatting, such that in case of overlapping formats, the opening tag
//for the format, that gets closed last is placed at first.
usort($entities, function($a, $b) {return (($a->offset + $a->length) < ($b->offset + $b->length)) ? -1 : 1;});
usort($entities, function($a, $b)
{
return (($a->offset + $a->length) < ($b->offset + $b->length)) ? -1 : 1;
});
foreach ($entities as $entity)
{
$bbcode = $this->get_bbcode($entity->type);
if (!$bbcode) {
if (!$bbcode)
{
continue;
}
$chunks[$entity->offset] = $bbcode . $chunks[$entity->offset];
Expand All @@ -527,7 +536,8 @@ public function format_text($text, $entities) {
{
$entity = $entities[$i];
$bbcode = $this->get_bbcode($entity->type, false);
if (!$bbcode) {
if (!$bbcode)
{
continue;
}
$bbcode_start = $this->get_bbcode($entity->type);
Expand All @@ -548,8 +558,9 @@ public function format_text($text, $entities) {
return $text;
}

private function get_bbcode($format_type, $start = true) {
switch($format_type)
private function get_bbcode($format_type, $start = true)
{
switch ($format_type)
{
case 'bold': return $start ? '[b]' : '[/b]';
case 'italic': return $start ? '[i]' : '[/i]';
Expand Down

0 comments on commit f406cf1

Please sign in to comment.