58 lines
1.6 KiB
PHP
Executable File
58 lines
1.6 KiB
PHP
Executable File
<?php
|
|
/*
|
|
* This file is part of the Winbox packages.
|
|
*
|
|
* (c) John Stevenson <john-stevenson@blueyonder.co.uk>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Winbox;
|
|
|
|
class Args
|
|
{
|
|
/**
|
|
* Escapes a string to be used as a shell argument
|
|
*
|
|
* Provides a more robust method on Windows than escapeshellarg.
|
|
*
|
|
* Feel free to copy this function, but please keep the following notice:
|
|
* MIT Licensed (c) John Stevenson <john-stevenson@blueyonder.co.uk>
|
|
* See https://github.com/johnstevenson/winbox-args for more information.
|
|
*
|
|
* @param string $arg The argument to be escaped
|
|
* @param bool $meta Additionally escape cmd.exe meta characters
|
|
*
|
|
* @return string The escaped argument
|
|
*/
|
|
public static function escape($arg, $meta = true)
|
|
{
|
|
if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
|
|
return escapeshellarg($arg);
|
|
}
|
|
|
|
$quote = strpbrk($arg, " \t") !== false || $arg === '';
|
|
$arg = preg_replace('/(\\\\*)"/', '$1$1\\"', $arg, -1, $dquotes);
|
|
|
|
if ($meta) {
|
|
$meta = $dquotes || preg_match('/%[^%]+%/', $arg);
|
|
|
|
if (!$meta && !$quote) {
|
|
$quote = strpbrk($arg, '^&|<>()') !== false;
|
|
}
|
|
}
|
|
|
|
if ($quote) {
|
|
$arg = preg_replace('/(\\\\*)$/', '$1$1', $arg);
|
|
$arg = '"'.$arg.'"';
|
|
}
|
|
|
|
if ($meta) {
|
|
$arg = preg_replace('/(["^&|<>()%])/', '^$1', $arg);
|
|
}
|
|
|
|
return $arg;
|
|
}
|
|
}
|