‪TYPO3CMS  9.5
JavaScriptEncoder.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
28 {
35  protected ‪$hexMatrix = [];
36 
42  protected ‪$immuneCharacters = [',', '.', '_'];
43 
49  protected ‪$charsetConversion;
50 
57  public function ‪__construct()
58  {
59  trigger_error('TYPO3\'s JavaScriptEncoder will be removed in TYPO3 v10.0, use PHPs native json_encode() or GeneralUtility::quoteJSvalue() instead.', E_USER_DEPRECATED);
60  $this->charsetConversion = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\‪TYPO3\CMS\Core\Charset\CharsetConverter::class);
61  for ($i = 0; $i < 256; $i++) {
62  if ($i >= ord('0') && $i <= ord('9') || $i >= ord('A') && $i <= ord('Z') || $i >= ord('a') && $i <= ord('z')) {
63  $this->hexMatrix[$i] = null;
64  } else {
65  $this->hexMatrix[$i] = dechex($i);
66  }
67  }
68  }
69 
76  public function ‪encode($input)
77  {
78  $stringLength = mb_strlen($input, 'utf-8');
79  $encodedString = '';
80  for ($i = 0; $i < $stringLength; $i++) {
81  $c = mb_substr($input, $i, 1, 'utf-8');
82  $encodedString .= $this->‪encodeCharacter($c);
83  }
84  return $encodedString;
85  }
86 
97  protected function ‪encodeCharacter($character)
98  {
99  if ($this->‪isImmuneCharacter($character)) {
100  return $character;
101  }
102  $ordinalValue = $this->charsetConversion->utf8CharToUnumber($character);
103  // Check for alphanumeric characters
104  $hex = $this->‪getHexForNonAlphanumeric($ordinalValue);
105  if ($hex === null) {
106  return $character;
107  }
108  // Encode up to 256 with \\xHH
109  if ($ordinalValue < 256) {
110  $pad = substr('00', strlen($hex));
111  return '\\x' . $pad . strtoupper($hex);
112  }
113  // Otherwise encode with \\uHHHH
114  $pad = substr('0000', strlen($hex));
115  return '\\u' . $pad . strtoupper($hex);
116  }
117 
124  protected function ‪isImmuneCharacter($character)
125  {
126  return in_array($character, $this->immuneCharacters, true);
127  }
128 
139  protected function ‪getHexForNonAlphanumeric($ordinalValue)
140  {
141  if ($ordinalValue <= 255) {
142  return $this->hexMatrix[$ordinalValue];
143  }
144  return dechex($ordinalValue);
145  }
146 }
‪TYPO3\CMS\Core\Encoder\JavaScriptEncoder\encode
‪string encode($input)
Definition: JavaScriptEncoder.php:73
‪TYPO3
‪TYPO3\CMS\Core\Encoder\JavaScriptEncoder\$charsetConversion
‪TYPO3 CMS Core Charset CharsetConverter $charsetConversion
Definition: JavaScriptEncoder.php:46
‪TYPO3\CMS\Core\Encoder\JavaScriptEncoder\$immuneCharacters
‪array $immuneCharacters
Definition: JavaScriptEncoder.php:40
‪TYPO3\CMS\Core\Encoder\JavaScriptEncoder\isImmuneCharacter
‪bool isImmuneCharacter($character)
Definition: JavaScriptEncoder.php:121
‪TYPO3\CMS\Core\Encoder\JavaScriptEncoder
Definition: JavaScriptEncoder.php:28
‪TYPO3\CMS\Core\Encoder
Definition: JavaScriptEncoder.php:2
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Core\Encoder\JavaScriptEncoder\getHexForNonAlphanumeric
‪string getHexForNonAlphanumeric($ordinalValue)
Definition: JavaScriptEncoder.php:136
‪TYPO3\CMS\Core\Encoder\JavaScriptEncoder\$hexMatrix
‪array $hexMatrix
Definition: JavaScriptEncoder.php:34
‪TYPO3\CMS\Core\Encoder\JavaScriptEncoder\__construct
‪__construct()
Definition: JavaScriptEncoder.php:54
‪TYPO3\CMS\Core\Encoder\JavaScriptEncoder\encodeCharacter
‪string encodeCharacter($character)
Definition: JavaScriptEncoder.php:94