From 6600fc6cad5f6d43acdcc9a303be4ce91ed48f2e Mon Sep 17 00:00:00 2001
From: 3D-I <480857+3D-I@users.noreply.github.com>
Date: Sun, 8 Sep 2019 03:29:27 +0200
Subject: [PATCH 1/2] [ticket/16151] Enable Emojis and rich text in forum name

PHPBB3-16151
---
 phpBB/includes/acp/acp_forums.php | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/phpBB/includes/acp/acp_forums.php b/phpBB/includes/acp/acp_forums.php
index cb0593b14a..0bbaf96dec 100644
--- a/phpBB/includes/acp/acp_forums.php
+++ b/phpBB/includes/acp/acp_forums.php
@@ -986,10 +986,30 @@ class acp_forums
 			$errors[] = $user->lang['FORUM_NAME_EMPTY'];
 		}
 
-		// No Emojis
+		/**
+		 * Replace Emojis and other 4bit UTF-8 chars not allowed by MySql to NCR.
+		 * Using their Numeric Character Reference's Hexadecimal notation.
+		 * Doesn't interfere with Japanese or Cyrillic etc.
+		 *
+		 * @see https://www.w3.org/TR/xml11/
+		 * @see https://www.opentag.com/xfaq_charrep.htm
+		 */
+		if (preg_match_all('/[\x{10000}-\x{10FFFF}]/u', $forum_data_ary['forum_name'], $matches))
+		{
+			foreach ($matches as $key => $emoji)
+			{
+				$forum_data_ary['forum_name'] = str_replace($emoji, utf8_encode_ncr($emoji), $forum_data_ary['forum_name']);
+			}
+		}
+
+		/**
+		 * This should never happen again.
+		 * Leaving the fallback hre just in case there will be the need of it.
+		 */
 		if (preg_match_all('/[\x{10000}-\x{10FFFF}]/u', $forum_data_ary['forum_name'], $matches))
 		{
 			$character_list = implode('<br>', $matches[0]);
+
 			$errors[] = $user->lang('FORUM_NAME_EMOJI', $character_list);
 		}
 

From 2cdc2e751eb03b6ba6c34889dc3db2a301b8c326 Mon Sep 17 00:00:00 2001
From: 3D-I <480857+3D-I@users.noreply.github.com>
Date: Mon, 28 Oct 2019 02:34:59 +0100
Subject: [PATCH 2/2] [ticket/16151] Enable Emojis and rich text in forum name

Move to a new function()

PHPBB3-16151
---
 phpBB/includes/acp/acp_forums.php | 16 +++---------
 phpBB/includes/utf/utf_tools.php  | 43 ++++++++++++++++++++++---------
 2 files changed, 34 insertions(+), 25 deletions(-)

diff --git a/phpBB/includes/acp/acp_forums.php b/phpBB/includes/acp/acp_forums.php
index 0bbaf96dec..d31404fa22 100644
--- a/phpBB/includes/acp/acp_forums.php
+++ b/phpBB/includes/acp/acp_forums.php
@@ -987,24 +987,14 @@ class acp_forums
 		}
 
 		/**
-		 * Replace Emojis and other 4bit UTF-8 chars not allowed by MySql to NCR.
+		 * Replace Emojis and other 4bit UTF-8 chars not allowed by MySql to UCR / NCR.
 		 * Using their Numeric Character Reference's Hexadecimal notation.
-		 * Doesn't interfere with Japanese or Cyrillic etc.
-		 *
-		 * @see https://www.w3.org/TR/xml11/
-		 * @see https://www.opentag.com/xfaq_charrep.htm
 		 */
-		if (preg_match_all('/[\x{10000}-\x{10FFFF}]/u', $forum_data_ary['forum_name'], $matches))
-		{
-			foreach ($matches as $key => $emoji)
-			{
-				$forum_data_ary['forum_name'] = str_replace($emoji, utf8_encode_ncr($emoji), $forum_data_ary['forum_name']);
-			}
-		}
+		$forum_data_ary['forum_name'] = utf8_encode_ucr($forum_data_ary['forum_name']);
 
 		/**
 		 * This should never happen again.
-		 * Leaving the fallback hre just in case there will be the need of it.
+		 * Leaving the fallback here just in case there will be the need of it.
 		 */
 		if (preg_match_all('/[\x{10000}-\x{10FFFF}]/u', $forum_data_ary['forum_name'], $matches))
 		{
diff --git a/phpBB/includes/utf/utf_tools.php b/phpBB/includes/utf/utf_tools.php
index 89de454427..bb155aeae5 100644
--- a/phpBB/includes/utf/utf_tools.php
+++ b/phpBB/includes/utf/utf_tools.php
@@ -418,24 +418,43 @@ function utf8_recode($string, $encoding)
 }
 
 /**
-* Replace all UTF-8 chars that are not in ASCII with their NCR
-*
-* @param	string	$text		UTF-8 string in NFC
-* @return	string				ASCII string using NCRs for non-ASCII chars
-*/
+ * Replace some special UTF-8 chars that are not in ASCII with their UCR.
+ * using their Numeric Character Reference's Hexadecimal notation.
+ *
+ * Doesn't interfere with Japanese or Cyrillic etc.
+ * Unicode character visualization will depend on the character support
+ * of your web browser and the fonts installed on your system.
+ *
+ * @see https://en.wikibooks.org/wiki/Unicode/Character_reference/1F000-1FFFF
+ *
+ * @param	string	$text		UTF-8 string in NFC
+ * @return	string				ASCII string using NCR for non-ASCII chars
+ */
+function utf8_encode_ucr($text)
+{
+	return preg_replace_callback('/[\\xF0-\\xF4].../', 'utf8_encode_ncr_callback', $text);
+}
+
+/**
+ * Replace all UTF-8 chars that are not in ASCII with their NCR
+ * using their Numeric Character Reference's Hexadecimal notation.
+ *
+ * @param	string	$text		UTF-8 string in NFC
+ * @return	string				ASCII string using NCRs for non-ASCII chars
+ */
 function utf8_encode_ncr($text)
 {
 	return preg_replace_callback('#[\\xC2-\\xF4][\\x80-\\xBF]{1,3}#', 'utf8_encode_ncr_callback', $text);
 }
 
 /**
-* Callback used in encode_ncr()
-*
-* Takes a UTF-8 char and replaces it with its NCR. Attention, $m is an array
-*
-* @param	array	$m			0-based numerically indexed array passed by preg_replace_callback()
-* @return	string				A HTML NCR if the character is valid, or the original string otherwise
-*/
+ * Callback used in utf8_encode_ncr() and utf8_encode_ucr()
+ *
+ * Takes a UTF-8 char and replaces it with its NCR. Attention, $m is an array
+ *
+ * @param	array	$m			0-based numerically indexed array passed by preg_replace_callback()
+ * @return	string				A HTML NCR if the character is valid, or the original string otherwise
+ */
 function utf8_encode_ncr_callback($m)
 {
 	return '&#' . utf8_ord($m[0]) . ';';