ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
SecureString.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
29trait SecureString
30{
31 protected function secure(string $string): string
32 {
33 // Normalize UTF-8 string, remove any invalid sequences
34 $temp_string = mb_convert_encoding($string, 'UTF-8', 'UTF-8');
35
36 // Remove invalid UTF-8 sequences that could be 4-byte UTF-8 (utf8mb4)
37 $temp_string = preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $temp_string);
38
39 // Remove control characters
40 $temp_string = preg_replace('#\p{C}+#u', '', $temp_string);
41 if ($temp_string === null) {
42 return '';
43 // we could harden that by throwing an exception
44 // throw new \RuntimeException(
45 // 'Failed to remove control characters from string `' . $string . '`. with message: '
46 // . preg_last_error_msg()
47 // );
48 }
49
50 // Sanitize by stripping HTML tags and encoding special characters
51 return htmlspecialchars(
52 strip_tags($temp_string),
53 ENT_QUOTES,
54 'UTF-8',
55 false
56 );
57 }
58}