|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Escaper. |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright 2014-2024 Institute of Legal Medicine, Medical University of Innsbruck |
|
6
|
|
|
* @author Andreas Erhard <[email protected]> |
|
7
|
|
|
* @license LGPL-3.0-only |
|
8
|
|
|
* @link http://www.gerichtsmedizin.at/ |
|
9
|
|
|
* |
|
10
|
|
|
* @package pdftk |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Gmi\Toolkit\Pdftk\Util; |
|
14
|
|
|
|
|
15
|
|
|
use Exception; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Escaping helper class to fix shortcomings of the PHP escapeshellarg/escapeshellcmd functions. |
|
19
|
|
|
*/ |
|
20
|
|
|
class Escaper |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* |
|
24
|
|
|
* @var string|true The best detected UTF-8 locale, or true if there was already a UTF-8 locale set. |
|
25
|
|
|
*/ |
|
26
|
|
|
private $locale = ''; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* |
|
30
|
|
|
* @param string[] $utf8Locales UTF-8 aware locales which should be used, in order of preference. |
|
31
|
|
|
* |
|
32
|
|
|
* @throws Exception if no |
|
33
|
|
|
*/ |
|
34
|
83 |
|
public function __construct(array $utf8Locales = ['de_AT.UTF-8', 'de_DE.UTF-8', 'en_US.UTF-8', 'C.UTF-8']) |
|
35
|
|
|
{ |
|
36
|
83 |
|
$this->initializeUtf8Locale($utf8Locales); |
|
37
|
82 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Escapes a shell argument. |
|
41
|
|
|
* |
|
42
|
|
|
* Wrapper around the PHP escapeshellarg() function which uses the correct locale for UTF-8 support. |
|
43
|
|
|
* |
|
44
|
|
|
* If the current locale does not support UTF-8, it switches to a supported UTF-8 locale |
|
45
|
|
|
* and back to the original one after the escape operation. |
|
46
|
|
|
*/ |
|
47
|
79 |
|
public function escapeshellarg(string $arg): string |
|
48
|
|
|
{ |
|
49
|
79 |
|
$previousLocale = null; |
|
50
|
|
|
|
|
51
|
79 |
|
if (true !== $this->locale) { |
|
52
|
46 |
|
$previousLocale = setlocale(LC_CTYPE, 0); |
|
53
|
46 |
|
setlocale(LC_CTYPE, $this->locale); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
79 |
|
$result = escapeshellarg($arg); |
|
57
|
|
|
|
|
58
|
79 |
|
if (true !== $this->locale) { |
|
59
|
46 |
|
setlocale(LC_CTYPE, $previousLocale); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
79 |
|
return $result; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Escapes a shell command, see Escaper::escapeshellarg(). |
|
67
|
|
|
*/ |
|
68
|
2 |
|
public function escapeshellcmd(string $command): string |
|
69
|
|
|
{ |
|
70
|
2 |
|
$previousLocale = ''; |
|
71
|
|
|
|
|
72
|
2 |
|
if (true !== $this->locale) { |
|
73
|
1 |
|
$previousLocale = setlocale(LC_CTYPE, 0); |
|
74
|
1 |
|
setlocale(LC_CTYPE, $this->locale); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
2 |
|
$result = escapeshellcmd($command); |
|
78
|
|
|
|
|
79
|
2 |
|
if (true !== $this->locale) { |
|
80
|
1 |
|
setlocale(LC_CTYPE, $previousLocale); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
2 |
|
return $result; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Alias for escapeshellarg(). |
|
88
|
|
|
*/ |
|
89
|
79 |
|
public function shellArg(string $arg): string |
|
90
|
|
|
{ |
|
91
|
79 |
|
return $this->escapeshellarg($arg); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Alias for escapeshellcmd(). |
|
96
|
|
|
*/ |
|
97
|
2 |
|
public function shellCmd(string $arg): string |
|
98
|
|
|
{ |
|
99
|
2 |
|
return $this->escapeshellcmd($arg); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
83 |
|
private function initializeUtf8Locale(array $utf8Locales): void |
|
103
|
|
|
{ |
|
104
|
|
|
// with 0 as the second argument, setlocale() returns the current locale |
|
105
|
83 |
|
$previousLocale = setlocale(LC_CTYPE, 0); |
|
106
|
|
|
|
|
107
|
|
|
// keep the current locale, as it is already capable of UTF-8. Store true as sentinel value. |
|
108
|
83 |
|
if (false !== strpos($previousLocale, '.UTF-8')) { |
|
109
|
2 |
|
$this->locale = true; |
|
110
|
|
|
|
|
111
|
2 |
|
return; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
81 |
|
$locale = false; |
|
115
|
81 |
|
foreach ($utf8Locales as $cur) { |
|
116
|
81 |
|
if (false !== @setlocale(LC_CTYPE, $cur)) { |
|
117
|
80 |
|
$locale = $cur; |
|
118
|
80 |
|
setlocale(LC_CTYPE, $previousLocale); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
81 |
|
if (!$locale) { |
|
123
|
1 |
|
throw new Exception('No supported UTF-8 locale found!'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
80 |
|
$this->locale = $locale; |
|
127
|
80 |
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|