diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index d42326bfcdfa..6b0762127fe2 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -2,6 +2,7 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\Settings; +use Drupal\Component\Utility\String; use Drupal\Component\Utility\Timer; use Drupal\Core\DrupalKernel; use Drupal\Core\Database\Database; @@ -1558,75 +1559,23 @@ function t($string, array $args = array(), array $options = array()) { /** * Formats a string for HTML display by replacing variable placeholders. * - * This function replaces variable placeholders in a string with the requested - * values and escapes the values so they can be safely displayed as HTML. It - * should be used on any unknown text that is intended to be printed to an HTML - * page (especially text that may have come from untrusted users, since in that - * case it prevents cross-site scripting and other security problems). - * - * In most cases, you should use t() rather than calling this function - * directly, since it will translate the text (on non-English-only sites) in - * addition to formatting it. - * - * @param $string - * A string containing placeholders. - * @param $args - * An associative array of replacements to make. Occurrences in $string of - * any key in $args are replaced with the corresponding value, after optional - * sanitization and formatting. The type of sanitization and formatting - * depends on the first character of the key: - * - @variable: Escaped to HTML using check_plain(). Use this as the default - * choice for anything displayed on a page on the site. - * - %variable: Escaped to HTML and formatted using drupal_placeholder(), - * which makes it display as emphasized text. - * - !variable: Inserted as is, with no sanitization or formatting. Only use - * this for text that has already been prepared for HTML display (for - * example, user-supplied text that has already been run through - * check_plain() previously, or is expected to contain some limited HTML - * tags and has already been run through filter_xss() previously). - * + * @see \Drupal\Component\Utility\String::format() * @see t() * @ingroup sanitization */ function format_string($string, array $args = array()) { - // Transform arguments before inserting them. - foreach ($args as $key => $value) { - switch ($key[0]) { - case '@': - // Escaped only. - $args[$key] = check_plain($value); - break; - - case '%': - default: - // Escaped and placeholder. - $args[$key] = drupal_placeholder($value); - break; - - case '!': - // Pass-through. - } - } - return strtr($string, $args); + return String::format($string, $args); } /** * Encodes special characters in a plain-text string for display as HTML. * - * Also validates strings as UTF-8. - * - * @param $text - * The text to be checked or processed. - * - * @return - * An HTML safe version of $text, or an empty string if $text is not - * valid UTF-8. - * + * @see \Drupal\Component\Utility\String::checkPlain() * @see drupal_validate_utf8() * @ingroup sanitization */ function check_plain($text) { - return htmlspecialchars($text, ENT_QUOTES, 'UTF-8'); + return String::checkPlain($text); } /** @@ -3398,16 +3347,10 @@ function drupal_is_cli() { /** * Formats text for emphasized display in a placeholder inside a sentence. * - * Used automatically by format_string(). - * - * @param $text - * The text to format (plain-text). - * - * @return - * The formatted text (html). + * @see \Drupal\Component\Utility\String::placeholder() */ function drupal_placeholder($text) { - return '' . check_plain($text) . ''; + return String::placeholder($text); } /** diff --git a/core/lib/Drupal/Component/Utility/String.php b/core/lib/Drupal/Component/Utility/String.php new file mode 100644 index 000000000000..c79b5af2b1d1 --- /dev/null +++ b/core/lib/Drupal/Component/Utility/String.php @@ -0,0 +1,108 @@ +emphasized text. + * - !variable: Inserted as is, with no sanitization or formatting. Only use + * this for text that has already been prepared for HTML display (for + * example, user-supplied text that has already been run through + * String::checkPlain() previously, or is expected to contain some limited + * HTML tags and has already been run through filter_xss() previously). + * + * + * @see t() + * @ingroup sanitization + * + * @return mixte + * The formatted string with placeholders inserted, or FALSE if no args specified. + */ + public static function format($string, array $args = array()) { + // Transform arguments before inserting them. + foreach ($args as $key => $value) { + switch ($key[0]) { + case '@': + // Escaped only. + $args[$key] = static::checkPlain($value); + break; + + case '%': + default: + // Escaped and placeholder. + $args[$key] = static::placeholder($value); + break; + + case '!': + // Pass-through. + } + } + return strtr($string, $args); + } + + /** + * Formats text for emphasized display in a placeholder inside a sentence. + * + * Used automatically by self::format(). + * + * @param string $text + * The text to format (plain-text). + * + * @return string + * The formatted text (html). + */ + public static function placeholder($text) { + return '' . static::checkPlain($text) . ''; + } + +} diff --git a/core/modules/system/lib/Drupal/system/Tests/Common/XssUnitTest.php b/core/modules/system/lib/Drupal/system/Tests/Common/XssUnitTest.php index dff85bd957fe..57863ff66aa2 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Common/XssUnitTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Common/XssUnitTest.php @@ -10,7 +10,7 @@ namespace Drupal\system\Tests\Common; use Drupal\simpletest\DrupalUnitTestBase; /** - * Tests for check_plain(), filter_xss(), format_string(), and check_url(). + * Tests for filter_xss() and check_url(). */ class XssUnitTest extends DrupalUnitTestBase { @@ -24,7 +24,7 @@ class XssUnitTest extends DrupalUnitTestBase { public static function getInfo() { return array( 'name' => 'String filtering tests', - 'description' => 'Confirm that check_plain(), filter_xss(), format_string() and check_url() work correctly, including invalid multi-byte sequences.', + 'description' => 'Confirm that filter_xss() and check_url() work correctly, including invalid multi-byte sequences.', 'group' => 'Common', ); } @@ -38,14 +38,6 @@ class XssUnitTest extends DrupalUnitTestBase { * Checks that invalid multi-byte sequences are rejected. */ function testInvalidMultiByte() { - // Ignore PHP 5.3+ invalid multibyte sequence warning. - $text = @check_plain("Foo\xC0barbaz"); - $this->assertEqual($text, '', 'check_plain() rejects invalid sequence "Foo\xC0barbaz"'); - // Ignore PHP 5.3+ invalid multibyte sequence warning. - $text = @check_plain("\xc2\""); - $this->assertEqual($text, '', 'check_plain() rejects invalid sequence "\xc2\""'); - $text = check_plain("Fooÿñ"); - $this->assertEqual($text, "Fooÿñ", 'check_plain() accepts valid sequence "Fooÿñ"'); $text = filter_xss("Foo\xC0barbaz"); $this->assertEqual($text, '', 'filter_xss() rejects invalid sequence "Foo\xC0barbaz"'); $text = filter_xss("Fooÿñ"); @@ -53,29 +45,17 @@ class XssUnitTest extends DrupalUnitTestBase { } /** - * Checks that special characters are escaped. + * Tests t() functionality. */ - function testEscaping() { - $text = check_plain("