From 376be6ae86cb1a74522162ad703562299534272f Mon Sep 17 00:00:00 2001 From: Nick Phillips Date: Wed, 20 Jul 2016 16:08:54 +1200 Subject: [PATCH] MDL-55274 mod_chat: regain beep and / command in mod_chat. format_text cannot be called on the whole input text, but must be called on user-supplied text that will be output into chats. This means it cannot simply be called in one place, but must instead be called on the relevant parts of text once commands have been matched. --- mod/chat/lib.php | 56 +++++++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/mod/chat/lib.php b/mod/chat/lib.php index 5fde3dc7797..e36a70acc82 100644 --- a/mod/chat/lib.php +++ b/mod/chat/lib.php @@ -746,22 +746,25 @@ function chat_format_message_manually($message, $courseid, $sender, $currentuser } // It's not a system event. - $text = trim($message->message); + $rawtext = trim($message->message); - // Parse the text to clean and filter it. + // Options for format_text, when we get to it... + // format_text call will parse the text to clean and filter it. + // It cannot be called here as HTML-isation interferes with special case + // recognition, but *must* be called on any user-sourced text to be inserted + // into $outmain. $options = new stdClass(); $options->para = false; $options->blanktarget = true; - $text = format_text($text, FORMAT_MOODLE, $options, $courseid); // And now check for special cases. $patternto = '#^\s*To\s([^:]+):(.*)#'; $special = false; - if (substr($text, 0, 5) == 'beep ') { + if (substr($rawtext, 0, 5) == 'beep ') { // It's a beep! $special = true; - $beepwho = trim(substr($text, 5)); + $beepwho = trim(substr($rawtext, 5)); if ($beepwho == 'all') { // Everyone. $outinfobasic = get_string('messagebeepseveryone', 'chat', fullname($sender)); @@ -779,29 +782,31 @@ function chat_format_message_manually($message, $courseid, $sender, $currentuser } else { // Something is not caught? return false; } - } else if (substr($text, 0, 1) == '/') { // It's a user command. + } else if (substr($rawtext, 0, 1) == '/') { // It's a user command. $special = true; $pattern = '#(^\/)(\w+).*#'; - preg_match($pattern, $text, $matches); + preg_match($pattern, $rawtext, $matches); $command = isset($matches[2]) ? $matches[2] : false; // Support some IRC commands. switch ($command) { case 'me': $outinfo = $message->strtime; - $outmain = '*** '.$sender->firstname.' '.substr($text, 4).''; + $text = '*** '.$sender->firstname.' '.substr($rawtext, 4).''; + $outmain = format_text($text, FORMAT_MOODLE, $options, $courseid); break; default: // Error, we set special back to false to use the classic message output. $special = false; break; } - } else if (preg_match($patternto, $text)) { + } else if (preg_match($patternto, $rawtext)) { $special = true; $matches = array(); - preg_match($patternto, $text, $matches); + preg_match($patternto, $rawtext, $matches); if (isset($matches[1]) && isset($matches[2])) { + $text = format_text($matches[2], FORMAT_MOODLE, $options, $courseid); $outinfo = $message->strtime; - $outmain = $sender->firstname.' '.get_string('saidto', 'chat').' '.$matches[1].': '.$matches[2]; + $outmain = $sender->firstname.' '.get_string('saidto', 'chat').' '.$matches[1].': '.$text; } else { // Error, we set special back to false to use the classic message output. $special = false; @@ -809,6 +814,7 @@ function chat_format_message_manually($message, $courseid, $sender, $currentuser } if (!$special) { + $text = format_text($rawtext, FORMAT_MOODLE, $options, $courseid); $outinfo = $message->strtime.' '.$sender->firstname; $outmain = $text; } @@ -920,13 +926,16 @@ function chat_format_message_theme ($message, $chatuser, $currentuser, $grouping } // It's not a system event. - $text = trim($message->message); + $rawtext = trim($message->message); - // Parse the text to clean and filter it. + // Options for format_text, when we get to it... + // format_text call will parse the text to clean and filter it. + // It cannot be called here as HTML-isation interferes with special case + // recognition, but *must* be called on any user-sourced text to be inserted + // into $outmain. $options = new stdClass(); $options->para = false; $options->blanktarget = true; - $text = format_text($text, FORMAT_MOODLE, $options, $courseid); // And now check for special cases. $special = false; @@ -936,11 +945,11 @@ function chat_format_message_theme ($message, $chatuser, $currentuser, $grouping $outmain = ''; $patternto = '#^\s*To\s([^:]+):(.*)#'; - if (substr($text, 0, 5) == 'beep ') { + if (substr($rawtext, 0, 5) == 'beep ') { $special = true; // It's a beep! $result->type = 'beep'; - $beepwho = trim(substr($text, 5)); + $beepwho = trim(substr($rawtext, 5)); if ($beepwho == 'all') { // Everyone. $outmain = get_string('messagebeepseveryone', 'chat', fullname($sender)); @@ -959,29 +968,31 @@ function chat_format_message_theme ($message, $chatuser, $currentuser, $grouping $outmain = get_string('messageyoubeep', 'chat', $beepwho); } } - } else if (substr($text, 0, 1) == '/') { // It's a user command. + } else if (substr($rawtext, 0, 1) == '/') { // It's a user command. $special = true; $result->type = 'command'; $pattern = '#(^\/)(\w+).*#'; - preg_match($pattern, $text, $matches); + preg_match($pattern, $rawtext, $matches); $command = isset($matches[2]) ? $matches[2] : false; // Support some IRC commands. switch ($command) { case 'me': - $outmain = '*** '.$sender->firstname.' '.substr($text, 4).''; + $text = '*** '.$sender->firstname.' '.substr($rawtext, 4).''; + $outmain = format_text($text, FORMAT_MOODLE, $options, $courseid); break; default: // Error, we set special back to false to use the classic message output. $special = false; break; } - } else if (preg_match($patternto, $text)) { + } else if (preg_match($patternto, $rawtext)) { $special = true; $result->type = 'dialogue'; $matches = array(); - preg_match($patternto, $text, $matches); + preg_match($patternto, $rawtext, $matches); if (isset($matches[1]) && isset($matches[2])) { - $outmain = $sender->firstname.' '.get_string('saidto', 'chat').' '.$matches[1].': '.$matches[2]; + $text = format_text($matches[2], FORMAT_MOODLE, $options, $courseid); + $outmain = $sender->firstname.' '.get_string('saidto', 'chat').' '.$matches[1].': '.$text; } else { // Error, we set special back to false to use the classic message output. $special = false; @@ -989,6 +1000,7 @@ function chat_format_message_theme ($message, $chatuser, $currentuser, $grouping } if (!$special) { + $text = format_text($rawtext, FORMAT_MOODLE, $options, $courseid); $outmain = $text; }