From 32f888714509dad880b0771bc80c9262fdc48eb3 Mon Sep 17 00:00:00 2001 From: giuliof Date: Sat, 25 Nov 2017 16:58:29 +0100 Subject: [PATCH] text formatting in message listing (not yet fully working) --- main.php | 79 +++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 69 insertions(+), 10 deletions(-) diff --git a/main.php b/main.php index 3eb38d5..63a8aa9 100644 --- a/main.php +++ b/main.php @@ -43,6 +43,7 @@ $mc->addServer('localhost', MEMCACHED_PORT) * |_ forward_from § User * |_ forward_from_chat § Chat * |_ text § string (message_text) + * |_ entities: [{"offset":...,"length":...,"type":"italic"}] * |_ photo, location, video * * **Callback Query** @@ -195,7 +196,7 @@ if (isset ($row->callback_query)) { "inline_keyboard" => $container); $reply = json_encode($resp); - $text="$row[Text]\n_$row[Author]_"; // NOME CANALE ".API_CHANNEL_ID."?"; + $text="$row[Text]\n$row[Author]"; // NOME CANALE ".API_CHANNEL_ID."?"; //~ $query = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID). //~ "&message_id=".$inlineID. @@ -203,7 +204,7 @@ if (isset ($row->callback_query)) { //~ "&parse_mode=".urlencode("Markdown"). //~ "&reply_markup=".$reply; //~ $answer = file_get_contents($query); - $answer = editMessageText($chatID, $inlineID, $text, "Markdown", false, $reply); + $answer = editMessageText($chatID, $inlineID, $text, "HTML", false, $reply); $answer = json_decode($answer); $mc->set($chatID.MC_LIST_NUMBER, $offset) @@ -306,7 +307,7 @@ if (isset ($row->callback_query)) { //~ "&message_id=".$inlineID . //~ "&text=".urlencode($text). //~ "&reply_markup=".$reply; - editMessageText($chatID, $inlineID, $text, "Markdown", false, $reply); + editMessageText($chatID, $inlineID, $text, "HTML", false, $reply); //~ file_get_contents($url); @@ -420,7 +421,7 @@ if (isset ($row->callback_query)) { //~ "&text=".urlencode($text)."&reply_markup=".$reply; //~ file_get_contents($url); - editMessageText($chatID, $inlineID, $text, "Markdown", false, $reply); + editMessageText($chatID, $inlineID, $text, "HTML", false, $reply); $mc->replace($chatID.MC_STATUS, STATE_WAIT_TIME) or die(); break; @@ -438,7 +439,7 @@ if (isset ($row->callback_query)) { //~ "&message_id=".$inlineID . //~ "&text=".urlencode($text)."&reply_markup=".$reply; //~ file_get_contents($url); - editMessageText($chatID, $inlineID, $text, "Markdown", false, $reply); + editMessageText($chatID, $inlineID, $text, "HTML", false, $reply); $mc->set($chatID.MC_INLINE_ID, $inlineID) or $mc->replace($chatID.MC_INLINE_ID, $inlineID); @@ -471,8 +472,58 @@ if (isset ($row->callback_query)) { $msg = $mc->get($chatID.MC_FORWARD_MSG); // BEGIN TextEscaper - $text = $msg->message->text; - $text = str_replace(array("_","*"), array("\_","\*"), $text); + // + + // 1- replace all * and _ with \* and \_ + //~ $text = $msg->message->text; + //~ $text = str_replace(array("_","*"), array("\_","\*"), $text); + + // 2- insert text formatting + if (isset($msg->message->entities)) + { + $elements = array(); + $string_pointer = 0; + // split text for each entity + foreach ($msg->message->entities as $value) { + switch($value->type) { + case "italic": + $parse_char = 'i'; + break; + case "bold": + $parse_char = 'b'; + break; + case "code": + $parse_char = 'code'; + break; + default: + continue 2; // exit from switch and continue foreach + } + + if (DEBUG) { + info("Offset: $value->offset\nLength: $value->length"); + } + + $no_format_string = mb_substr($msg->message->text, $string_pointer, $value->offset-$string_pointer); + if (DEBUG) info("Stringa no format: $no_format_string"); + $elements[] = htmlspecialchars($no_format_string); + + $format_string = mb_substr($msg->message->text, $value->offset, $value->length); + if (DEBUG) info("Stringa format: $format_string"); + $elements[] = "<$parse_char>".htmlspecialchars($format_string).""; + + $string_pointer = $value->offset+$value->length; + } + if ($string_pointer != mb_strlen($msg->message->text)) { + $no_format_string = mb_substr($msg->message->text, $string_pointer); + $elements[] = htmlspecialchars($no_format_string); + } + $text = implode($elements); + } + else + $text = htmlspecialchars($msg->message->text); + + + // END TextEscaper @@ -489,7 +540,7 @@ if (isset ($row->callback_query)) { $value .= $currentTab['day'].' '; $value .= $callback_data.":00:00'"; $value .= ","; - if (isset($msg->message->text)) + if (isset($text)) $value .= "'".$sql->escape_string($text)."'"; else $value .= "''"; @@ -624,13 +675,13 @@ elseif (isset($row->message)) { $reply = json_encode($resp); if ($row['Text'] != ''){ - $text="$row[Text]\n_$row[Author]_"; // NOME CANALE ".API_CHANNEL_ID."?"; + $text="$row[Text]\n$row[Author]"; // NOME CANALE ".API_CHANNEL_ID."?"; } else $text="Non testo puro"; - $answer = sendMessage($chatID, $text, "Markdown", false, false, null, $reply); + $answer = sendMessage($chatID, $text, "HTML", false, false, null, $reply); $answer = json_decode($answer); // Should forward the message if is not pure text //forwardMessage($chatID, $row['ChatID'], $row['MessageID']); @@ -710,6 +761,14 @@ elseif (isset($row->message)) { $inlineID = $answer->result->message_id; if (DEBUG) info("Callback message id: $inlineID"); + // DEBUGGING strlen + $mi_pigli_per_il_culo = mb_convert_encoding($row->message->text, 'UTF-16', 'UTF-8'); + if (DEBUG) if(isset($row->message->text)) {info("String length: ".mb_strlen($row->message->text, "UTF-8")); + info("String encoding: ".mb_detect_encoding($row->message->text)); + info("String length: ".mb_strlen($mi_pigli_per_il_culo, "UTF-16")); + info("String encoding: ".mb_detect_encoding($mi_pigli_per_il_culo)); + } + $mc->replace($chatID.MC_STATUS, STATE_MSG_ANSWER); $mc->set($chatID.MC_INLINE_ID, $inlineID) or $mc->replace($chatID.MC_INLINE_ID, $inlineID); }