PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/wp-content/plugins/contact-form-7-to-database-extension/Spout-2.7.1/Common/Helper/EncodingHelper.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 175 lines | 83 code | 26 blank | 66 comment | 8 complexity | b97e0d069461e6c1ea40675d9ac59fa2 MD5 | raw file
  1. <?php
  2. namespace Box\Spout\Common\Helper;
  3. use Box\Spout\Common\Exception\EncodingConversionException;
  4. /**
  5. * Class EncodingHelper
  6. * This class provides helper functions to work with encodings.
  7. *
  8. * @package Box\Spout\Common\Helper
  9. */
  10. class EncodingHelper
  11. {
  12. /** Definition of the encodings that can have a BOM */
  13. const ENCODING_UTF8 = 'UTF-8';
  14. const ENCODING_UTF16_LE = 'UTF-16LE';
  15. const ENCODING_UTF16_BE = 'UTF-16BE';
  16. const ENCODING_UTF32_LE = 'UTF-32LE';
  17. const ENCODING_UTF32_BE = 'UTF-32BE';
  18. /** Definition of the BOMs for the different encodings */
  19. const BOM_UTF8 = "\xEF\xBB\xBF";
  20. const BOM_UTF16_LE = "\xFF\xFE";
  21. const BOM_UTF16_BE = "\xFE\xFF";
  22. const BOM_UTF32_LE = "\xFF\xFE\x00\x00";
  23. const BOM_UTF32_BE = "\x00\x00\xFE\xFF";
  24. /** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */
  25. protected $globalFunctionsHelper;
  26. /** @var array Map representing the encodings supporting BOMs (key) and their associated BOM (value) */
  27. protected $supportedEncodingsWithBom;
  28. /**
  29. * @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
  30. */
  31. public function __construct($globalFunctionsHelper)
  32. {
  33. $this->globalFunctionsHelper = $globalFunctionsHelper;
  34. $this->supportedEncodingsWithBom = [
  35. self::ENCODING_UTF8 => self::BOM_UTF8,
  36. self::ENCODING_UTF16_LE => self::BOM_UTF16_LE,
  37. self::ENCODING_UTF16_BE => self::BOM_UTF16_BE,
  38. self::ENCODING_UTF32_LE => self::BOM_UTF32_LE,
  39. self::ENCODING_UTF32_BE => self::BOM_UTF32_BE,
  40. ];
  41. }
  42. /**
  43. * Returns the number of bytes to use as offset in order to skip the BOM.
  44. *
  45. * @param resource $filePointer Pointer to the file to check
  46. * @param string $encoding Encoding of the file to check
  47. * @return int Bytes offset to apply to skip the BOM (0 means no BOM)
  48. */
  49. public function getBytesOffsetToSkipBOM($filePointer, $encoding)
  50. {
  51. $byteOffsetToSkipBom = 0;
  52. if ($this->hasBOM($filePointer, $encoding)) {
  53. $bomUsed = $this->supportedEncodingsWithBom[$encoding];
  54. // we skip the N first bytes
  55. $byteOffsetToSkipBom = strlen($bomUsed);
  56. }
  57. return $byteOffsetToSkipBom;
  58. }
  59. /**
  60. * Returns whether the file identified by the given pointer has a BOM.
  61. *
  62. * @param resource $filePointer Pointer to the file to check
  63. * @param string $encoding Encoding of the file to check
  64. * @return bool TRUE if the file has a BOM, FALSE otherwise
  65. */
  66. protected function hasBOM($filePointer, $encoding)
  67. {
  68. $hasBOM = false;
  69. $this->globalFunctionsHelper->rewind($filePointer);
  70. if (array_key_exists($encoding, $this->supportedEncodingsWithBom)) {
  71. $potentialBom = $this->supportedEncodingsWithBom[$encoding];
  72. $numBytesInBom = strlen($potentialBom);
  73. $hasBOM = ($this->globalFunctionsHelper->fgets($filePointer, $numBytesInBom + 1) === $potentialBom);
  74. }
  75. return $hasBOM;
  76. }
  77. /**
  78. * Attempts to convert a non UTF-8 string into UTF-8.
  79. *
  80. * @param string $string Non UTF-8 string to be converted
  81. * @param string $sourceEncoding The encoding used to encode the source string
  82. * @return string The converted, UTF-8 string
  83. * @throws \Box\Spout\Common\Exception\EncodingConversionException If conversion is not supported or if the conversion failed
  84. */
  85. public function attemptConversionToUTF8($string, $sourceEncoding)
  86. {
  87. return $this->attemptConversion($string, $sourceEncoding, self::ENCODING_UTF8);
  88. }
  89. /**
  90. * Attempts to convert a UTF-8 string into the given encoding.
  91. *
  92. * @param string $string UTF-8 string to be converted
  93. * @param string $targetEncoding The encoding the string should be re-encoded into
  94. * @return string The converted string, encoded with the given encoding
  95. * @throws \Box\Spout\Common\Exception\EncodingConversionException If conversion is not supported or if the conversion failed
  96. */
  97. public function attemptConversionFromUTF8($string, $targetEncoding)
  98. {
  99. return $this->attemptConversion($string, self::ENCODING_UTF8, $targetEncoding);
  100. }
  101. /**
  102. * Attempts to convert the given string to the given encoding.
  103. * Depending on what is installed on the server, we will try to iconv or mbstring.
  104. *
  105. * @param string $string string to be converted
  106. * @param string $sourceEncoding The encoding used to encode the source string
  107. * @param string $targetEncoding The encoding the string should be re-encoded into
  108. * @return string The converted string, encoded with the given encoding
  109. * @throws \Box\Spout\Common\Exception\EncodingConversionException If conversion is not supported or if the conversion failed
  110. */
  111. protected function attemptConversion($string, $sourceEncoding, $targetEncoding)
  112. {
  113. // if source and target encodings are the same, it's a no-op
  114. if ($sourceEncoding === $targetEncoding) {
  115. return $string;
  116. }
  117. $convertedString = null;
  118. if ($this->canUseIconv()) {
  119. $convertedString = $this->globalFunctionsHelper->iconv($string, $sourceEncoding, $targetEncoding);
  120. } else if ($this->canUseMbString()) {
  121. $convertedString = $this->globalFunctionsHelper->mb_convert_encoding($string, $sourceEncoding, $targetEncoding);
  122. } else {
  123. throw new EncodingConversionException("The conversion from $sourceEncoding to $targetEncoding is not supported. Please install \"iconv\" or \"PHP Intl\".");
  124. }
  125. if ($convertedString === false) {
  126. throw new EncodingConversionException("The conversion from $sourceEncoding to $targetEncoding failed.");
  127. }
  128. return $convertedString;
  129. }
  130. /**
  131. * Returns whether "iconv" can be used.
  132. *
  133. * @return bool TRUE if "iconv" is available and can be used, FALSE otherwise
  134. */
  135. protected function canUseIconv()
  136. {
  137. return $this->globalFunctionsHelper->function_exists('iconv');
  138. }
  139. /**
  140. * Returns whether "mb_string" functions can be used.
  141. * These functions come with the PHP Intl package.
  142. *
  143. * @return bool TRUE if "mb_string" functions are available and can be used, FALSE otherwise
  144. */
  145. protected function canUseMbString()
  146. {
  147. return $this->globalFunctionsHelper->function_exists('mb_convert_encoding');
  148. }
  149. }