annotate core/lib/Drupal/Component/Utility/SafeMarkup.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Component\Utility;
Chris@0 4
Chris@0 5 use Drupal\Component\Render\HtmlEscapedText;
Chris@0 6 use Drupal\Component\Render\FormattableMarkup;
Chris@0 7 use Drupal\Component\Render\MarkupInterface;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Contains deprecated functionality related to sanitization of markup.
Chris@0 11 *
Chris@0 12 * @deprecated Will be removed before Drupal 9.0.0. Use the appropriate
Chris@0 13 * @link sanitization sanitization functions @endlink or the @link theme_render theme and render systems @endlink
Chris@0 14 * so that the output can can be themed, escaped, and altered properly.
Chris@0 15 *
Chris@0 16 * @see https://www.drupal.org/node/2549395
Chris@0 17 *
Chris@0 18 * @see TwigExtension::escapeFilter()
Chris@0 19 * @see twig_render_template()
Chris@0 20 * @see sanitization
Chris@0 21 * @see theme_render
Chris@0 22 */
Chris@0 23 class SafeMarkup {
Chris@0 24
Chris@0 25 /**
Chris@0 26 * Checks if a string is safe to output.
Chris@0 27 *
Chris@0 28 * @param string|\Drupal\Component\Render\MarkupInterface $string
Chris@0 29 * The content to be checked.
Chris@0 30 * @param string $strategy
Chris@0 31 * (optional) This value is ignored.
Chris@0 32 *
Chris@0 33 * @return bool
Chris@0 34 * TRUE if the string has been marked secure, FALSE otherwise.
Chris@0 35 *
Chris@0 36 * @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0.
Chris@0 37 * Instead, you should just check if a variable is an instance of
Chris@0 38 * \Drupal\Component\Render\MarkupInterface.
Chris@0 39 *
Chris@0 40 * @see https://www.drupal.org/node/2549395
Chris@0 41 */
Chris@0 42 public static function isSafe($string, $strategy = 'html') {
Chris@0 43 return $string instanceof MarkupInterface;
Chris@0 44 }
Chris@0 45
Chris@0 46 /**
Chris@0 47 * Encodes special characters in a plain-text string for display as HTML.
Chris@0 48 *
Chris@0 49 * Also validates strings as UTF-8. All processed strings are also
Chris@0 50 * automatically flagged as safe markup strings for rendering.
Chris@0 51 *
Chris@0 52 * @param string $text
Chris@0 53 * The text to be checked or processed.
Chris@0 54 *
Chris@0 55 * @return \Drupal\Component\Render\HtmlEscapedText
Chris@0 56 * An HtmlEscapedText object that escapes when rendered to string.
Chris@0 57 *
Chris@0 58 * @deprecated Will be removed before Drupal 9.0.0. Rely on Twig's
Chris@0 59 * auto-escaping feature, or use the @link theme_render #plain_text @endlink
Chris@0 60 * key when constructing a render array that contains plain text in order to
Chris@0 61 * use the renderer's auto-escaping feature. If neither of these are
Chris@0 62 * possible, \Drupal\Component\Utility\Html::escape() can be used in places
Chris@0 63 * where explicit escaping is needed.
Chris@0 64 *
Chris@0 65 * @see https://www.drupal.org/node/2549395
Chris@0 66 * @see drupal_validate_utf8()
Chris@0 67 */
Chris@0 68 public static function checkPlain($text) {
Chris@0 69 return new HtmlEscapedText($text);
Chris@0 70 }
Chris@0 71
Chris@0 72 /**
Chris@0 73 * Formats a string for HTML display by replacing variable placeholders.
Chris@0 74 *
Chris@0 75 * @param string $string
Chris@0 76 * A string containing placeholders. The string itself will not be escaped,
Chris@0 77 * any unsafe content must be in $args and inserted via placeholders.
Chris@0 78 * @param array $args
Chris@0 79 * An array with placeholder replacements, keyed by placeholder. See
Chris@0 80 * \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for
Chris@0 81 * additional information about placeholders.
Chris@0 82 *
Chris@0 83 * @return string|\Drupal\Component\Render\MarkupInterface
Chris@0 84 * The formatted string, which is an instance of MarkupInterface unless
Chris@0 85 * sanitization of an unsafe argument was suppressed (see above).
Chris@0 86 *
Chris@0 87 * @see \Drupal\Component\Render\FormattableMarkup::placeholderFormat()
Chris@0 88 * @see \Drupal\Component\Render\FormattableMarkup
Chris@0 89 *
Chris@0 90 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
Chris@0 91 * Use \Drupal\Component\Render\FormattableMarkup.
Chris@0 92 *
Chris@0 93 * @see https://www.drupal.org/node/2549395
Chris@0 94 */
Chris@0 95 public static function format($string, array $args) {
Chris@0 96 return new FormattableMarkup($string, $args);
Chris@0 97 }
Chris@0 98
Chris@0 99 }