public function ImagemagickToolkit::escapeShellArg in ImageMagick 8
Same name and namespace in other branches
- 8.2 src/Plugin/ImageToolkit/ImagemagickToolkit.php \Drupal\imagemagick\Plugin\ImageToolkit\ImagemagickToolkit::escapeShellArg()
Escapes a string.
PHP escapeshellarg() drops non-ascii characters, this is a replacement.
Stop-gap replacement until core issue #1561214 has been solved. Solution proposed in #1502924-8.
PHP escapeshellarg() on Windows also drops % (percentage sign) characters. We prevent this by replacing it with a pattern that should be highly unlikely to appear in the string itself and does not contain any "dangerous" character at all (very wide definition of dangerous). After escaping we replace that pattern back with a % character.
Parameters
string $arg: The string to escape.
Return value
string An escaped string for use in the ::imagemagickExec method.
3 calls to ImagemagickToolkit::escapeShellArg()
- ImagemagickToolkit::imagemagickExec in src/
Plugin/ ImageToolkit/ ImagemagickToolkit.php - Executes the convert executable as shell command.
- ImagemagickToolkit::parseFileViaIdentify in src/
Plugin/ ImageToolkit/ ImagemagickToolkit.php - Parses the image file using the 'identify' executable.
- ImagemagickToolkit::runOsShell in src/
Plugin/ ImageToolkit/ ImagemagickToolkit.php - Executes a command on the operating system.
File
- src/
Plugin/ ImageToolkit/ ImagemagickToolkit.php, line 905
Class
- ImagemagickToolkit
- Provides ImageMagick integration toolkit for image manipulation.
Namespace
Drupal\imagemagick\Plugin\ImageToolkitCode
public function escapeShellArg($arg) {
static $percentage_sign_replace_pattern = '1357902468IMAGEMAGICKPERCENTSIGNPATTERN8642097531';
// Put the configured locale in a static to avoid multiple config get calls
// in the same request.
static $config_locale;
if (!isset($config_locale)) {
$config_locale = $this->configFactory
->get('imagemagick.settings')
->get('locale');
if (empty($config_locale)) {
$config_locale = FALSE;
}
}
if ($this->isWindows) {
// Temporarily replace % characters.
$arg = str_replace('%', $percentage_sign_replace_pattern, $arg);
}
// If no locale specified in config, return with standard.
if ($config_locale === FALSE) {
$arg_escaped = escapeshellarg($arg);
}
else {
// Get the current locale.
$current_locale = setlocale(LC_CTYPE, 0);
if ($current_locale != $config_locale) {
// Temporarily swap the current locale with the configured one.
setlocale(LC_CTYPE, $config_locale);
$arg_escaped = escapeshellarg($arg);
setlocale(LC_CTYPE, $current_locale);
}
else {
$arg_escaped = escapeshellarg($arg);
}
}
// Get our % characters back.
if ($this->isWindows) {
$arg_escaped = str_replace($percentage_sign_replace_pattern, '%', $arg_escaped);
}
return $arg_escaped;
}