Commit 0c74a064 authored by catch's avatar catch
Browse files

Issue #3260801 by andypost: Remove deprecated code from core/lib/Drupal/Component/Utility

parent cb550768
Loading
Loading
Loading
Loading
+0 −19
Original line number Diff line number Diff line
@@ -58,25 +58,6 @@ class Bytes {
    'yottabytes',
  ];

  /**
   * Parses a given byte size.
   *
   * @param mixed $size
   *   An integer or string size expressed as a number of bytes with optional SI
   *   or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, 6GiB, 8 bytes, 9mbytes).
   *
   * @return int
   *   An integer representation of the size in bytes.
   *
   * @deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. Use \Drupal\Component\Utility\Bytes::toNumber() instead
   *
   * @see https://www.drupal.org/node/3162663
   */
  public static function toInt($size) {
    @trigger_error('\Drupal\Component\Utility\Bytes::toInt() is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. Use \Drupal\Component\Utility\Bytes::toNumber() instead. See https://www.drupal.org/node/3162663', E_USER_DEPRECATED);
    return self::toNumber($size);
  }

  /**
   * Parses a given byte size.
   *
+0 −76
Original line number Diff line number Diff line
<?php

namespace Drupal\Component\Utility;

@trigger_error('\Drupal\Component\Utility\Mail is deprecated in drupal:9.2.0 and is removed from drupal:10.0.0. See https://www.drupal.org/node/3207439', E_USER_DEPRECATED);

/**
 * Provides helpers to ensure emails are compliant with RFCs.
 *
 * @ingroup utility
 */
class Mail {

  /**
   * RFC-2822 "specials" characters.
   */
  const RFC_2822_SPECIALS = '()<>[]:;@\,."';

  /**
   * Return a RFC-2822 compliant "display-name" component.
   *
   * The "display-name" component is used in mail header "Originator" fields
   * (From, Sender, Reply-to) to give a human-friendly description of the
   * address, i.e. From: My Display Name <xyz@example.org>. RFC-822 and
   * RFC-2822 define its syntax and rules. This method gets as input a string
   * to be used as "display-name" and formats it to be RFC compliant.
   *
   * @param string $string
   *   A string to be used as "display-name".
   *
   * @return string
   *   A RFC compliant version of the string, ready to be used as
   *   "display-name" in mail originator header fields.
   *
   * @deprecated in drupal:9.2.0 and is removed from drupal:10.0.0. Use
   *   \Symfony\Component\Mime\Header\MailboxHeader instead.
   *
   * @see https://www.drupal.org/node/3207439
   */
  public static function formatDisplayName($string) {
    @trigger_error('\Drupal\Component\Utility\Mail::formatDisplayName() is deprecated in drupal:9.2.0 and is removed from drupal:10.0.0. Use \Symfony\Component\Mime\Header\MailboxHeader instead. See https://www.drupal.org/node/3207439', E_USER_DEPRECATED);

    // Make sure we don't process html-encoded characters. They may create
    // unneeded trouble if left encoded, besides they will be correctly
    // processed if decoded.
    $string = Html::decodeEntities($string);

    // If string contains non-ASCII characters it must be (short) encoded
    // according to RFC-2047. The output of a "B" (Base64) encoded-word is
    // always safe to be used as display-name.
    $safe_display_name = Unicode::mimeHeaderEncode($string, TRUE);

    // Encoded-words are always safe to be used as display-name because don't
    // contain any RFC 2822 "specials" characters. However
    // Unicode::mimeHeaderEncode() encodes a string only if it contains any
    // non-ASCII characters, and leaves its value untouched (un-encoded) if
    // ASCII only. For this reason in order to produce a valid display-name we
    // still need to make sure there are no "specials" characters left.
    if (preg_match('/[' . preg_quote(Mail::RFC_2822_SPECIALS) . ']/', $safe_display_name)) {

      // If string is already quoted, it may or may not be escaped properly, so
      // don't trust it and reset.
      if (preg_match('/^"(.+)"$/', $safe_display_name, $matches)) {
        $safe_display_name = str_replace(['\\\\', '\\"'], ['\\', '"'], $matches[1]);
      }

      // Transform the string in a RFC-2822 "quoted-string" by wrapping it in
      // double-quotes. Also make sure '"' and '\' occurrences are escaped.
      $safe_display_name = '"' . str_replace(['\\', '"'], ['\\\\', '\\"'], $safe_display_name) . '"';

    }

    return $safe_display_name;
  }

}
+0 −79
Original line number Diff line number Diff line
@@ -371,85 +371,6 @@ public static function strcasecmp($str1, $str2) {
    return strcmp(mb_strtoupper($str1), mb_strtoupper($str2));
  }

  /**
   * Encodes MIME/HTTP headers that contain incorrectly encoded characters.
   *
   * For example, Unicode::mimeHeaderEncode('tést.txt') returns
   * "=?UTF-8?B?dMOpc3QudHh0?=".
   *
   * See http://www.rfc-editor.org/rfc/rfc2047.txt for more information.
   *
   * Notes:
   * - Only encode strings that contain non-ASCII characters.
   * - We progressively cut-off a chunk with self::truncateBytes(). This ensures
   *   each chunk starts and ends on a character boundary.
   * - Using \n as the chunk separator may cause problems on some systems and
   *   may have to be changed to \r\n or \r.
   *
   * @param string $string
   *   The header to encode.
   * @param bool $shorten
   *   If TRUE, only return the first chunk of a multi-chunk encoded string.
   *
   * @return string
   *   The mime-encoded header.
   *
   * @deprecated in drupal:9.2.0 and is removed from drupal:10.0.0. Use
   *   \Symfony\Component\Mime\Header\UnstructuredHeader instead.
   *
   * @see https://www.drupal.org/node/3207439
   */
  public static function mimeHeaderEncode($string, $shorten = FALSE) {
    @trigger_error('\Drupal\Component\Utility\Unicode::mimeHeaderEncode() is deprecated in drupal:9.2.0 and is removed from drupal:10.0.0. Use \Symfony\Component\Mime\Header\UnstructuredHeader instead. See https://www.drupal.org/node/3207439', E_USER_DEPRECATED);
    if (preg_match('/[^\x20-\x7E]/', $string)) {
      // floor((75 - strlen("=?UTF-8?B??=")) * 0.75);
      $chunk_size = 47;
      $len = strlen($string);
      $output = '';
      while ($len > 0) {
        $chunk = static::truncateBytes($string, $chunk_size);
        $output .= ' =?UTF-8?B?' . base64_encode($chunk) . "?=\n";
        if ($shorten) {
          break;
        }
        $c = strlen($chunk);
        $string = substr($string, $c);
        $len -= $c;
      }
      return trim($output);
    }
    return $string;
  }

  /**
   * Decodes MIME/HTTP encoded header values.
   *
   * @param string $header
   *   The header to decode.
   *
   * @return string
   *   The mime-decoded header.
   *
   * @deprecated in drupal:9.2.0 and is removed from drupal:10.0.0. Use
   *   iconv_mime_decode() instead.
   *
   * @see https://www.drupal.org/node/3207439
   */
  public static function mimeHeaderDecode($header) {
    @trigger_error('\Drupal\Component\Utility\Unicode::mimeHeaderDecode() is deprecated in drupal:9.2.0 and is removed from drupal:10.0.0. Use iconv_mime_decode() instead. See https://www.drupal.org/node/3207439', E_USER_DEPRECATED);
    $callback = function ($matches) {
      $data = (strtolower($matches[2]) == 'b') ? base64_decode($matches[3]) : str_replace('_', ' ', quoted_printable_decode($matches[3]));
      if (strtolower($matches[1]) != 'utf-8') {
        $data = static::convertToUtf8($data, $matches[1]);
      }
      return $data;
    };
    // First step: encoded chunks followed by other encoded chunks (need to collapse whitespace)
    $header = preg_replace_callback('/=\?([^?]+)\?([Qq]|[Bb])\?([^?]+|\?(?!=))\?=\s+(?==\?)/', $callback, $header);
    // Second step: remaining chunks (do not collapse whitespace)
    return preg_replace_callback('/=\?([^?]+)\?([Qq]|[Bb])\?([^?]+|\?(?!=))\?=/', $callback, $header);
  }

  /**
   * Checks whether a string is valid UTF-8.
   *
+0 −20
Original line number Diff line number Diff line
@@ -17,26 +17,6 @@ class BytesTest extends TestCase {

  use ExpectDeprecationTrait;

  /**
   * Tests \Drupal\Component\Utility\Bytes::toInt().
   *
   * @param int $size
   *   The value for the size argument for
   *   \Drupal\Component\Utility\Bytes::toInt().
   * @param int $expected_int
   *   The expected return value from
   *   \Drupal\Component\Utility\Bytes::toInt().
   *
   * @dataProvider providerTestToNumber
   * @covers ::toInt
   *
   * @group legacy
   */
  public function testToInt($size, $expected_int) {
    $this->expectDeprecation('\Drupal\Component\Utility\Bytes::toInt() is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. Use \Drupal\Component\Utility\Bytes::toNumber() instead. See https://www.drupal.org/node/3162663');
    $this->assertEquals($expected_int, Bytes::toInt($size));
  }

  /**
   * Tests \Drupal\Component\Utility\Bytes::toNumber().
   *
+0 −64
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\Component\Utility;

use Drupal\Component\Utility\Mail;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;

/**
 * Test mail helpers implemented in Mail component.
 *
 * @group Utility
 * @group legacy
 *
 * @coversDefaultClass \Drupal\Component\Utility\Mail
 */
class MailTest extends TestCase {
  use ExpectDeprecationTrait;

  /**
   * Tests RFC-2822 'display-name' formatter.
   *
   * @dataProvider providerTestDisplayName
   * @covers ::formatDisplayName
   */
  public function testFormatDisplayName($string, $safe_display_name) {
    $this->expectDeprecation('\Drupal\Component\Utility\Unicode::mimeHeaderEncode() is deprecated in drupal:9.2.0 and is removed from drupal:10.0.0. Use \Symfony\Component\Mime\Header\UnstructuredHeader instead. See https://www.drupal.org/node/3207439');
    $this->assertEquals($safe_display_name, Mail::formatDisplayName($string));
  }

  /**
   * Data provider for testFormatDisplayName().
   *
   * @see testFormatDisplayName()
   *
   * @return array
   *   An array containing a string and its 'display-name' safe value.
   */
  public function providerTestDisplayName() {
    return [
      // Simple ASCII characters.
      ['Test site', 'Test site'],
      // ASCII with html entity.
      ['Test &amp; site', 'Test & site'],
      // Non-ASCII characters.
      ['Tést site', '=?UTF-8?B?VMOpc3Qgc2l0ZQ==?='],
      // Non-ASCII with special characters.
      ['Tést; site', '=?UTF-8?B?VMOpc3Q7IHNpdGU=?='],
      // Non-ASCII with html entity.
      ['T&eacute;st; site', '=?UTF-8?B?VMOpc3Q7IHNpdGU=?='],
      // ASCII with special characters.
      ['Test; site', '"Test; site"'],
      // ASCII with special characters as html entity.
      ['Test &lt; site', '"Test < site"'],
      // ASCII with special characters and '\'.
      ['Test; \ "site"', '"Test; \\\\ \"site\""'],
      // String already RFC-2822 compliant.
      ['"Test; site"', '"Test; site"'],
      // String already RFC-2822 compliant.
      ['"Test; \\\\ \"site\""', '"Test; \\\\ \"site\""'],
    ];
  }

}
Loading