From 537dfc86f92fee6872ddc032ca13bf853c821099 Mon Sep 17 00:00:00 2001 From: Neil Blakey-Milner Date: Fri, 19 Aug 2005 22:48:13 +0000 Subject: [PATCH] Add safeShellString, which converts an array of shell arguments to a strings that's safe to pass to system, popen, exec, &c. --- lib/util/ktutil.inc | 21 +++++++++++++++++++++ tests/util/ktutil/testSafeShellString.php | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 0 deletions(-) create mode 100644 tests/util/ktutil/testSafeShellString.php diff --git a/lib/util/ktutil.inc b/lib/util/ktutil.inc index 3d2a3ac..019ba1a 100644 --- a/lib/util/ktutil.inc +++ b/lib/util/ktutil.inc @@ -128,6 +128,27 @@ class KTUtil { return array(join(" AND ", $aStrings), $aParams); } // }}} + + // function safeShellString + function safeShellString () { + $aArgs = func_get_args(); + $aSafeArgs = array(); + if (is_array($aArgs[0])) { + $aArgs = $aArgs[0]; + } + $aSafeArgs[] = escapeshellarg(array_shift($aArgs)); + if (is_array($aArgs[0])) { + $aArgs = $aArgs; + } + foreach ($aArgs as $sArg) { + if (empty($sArg)) { + $aSafeArgs[] = "''"; + } else { + $aSafeArgs[] = escapeshellarg($sArg); + } + } + return join(" ", $aSafeArgs); + } } // }}} diff --git a/tests/util/ktutil/testSafeShellString.php b/tests/util/ktutil/testSafeShellString.php new file mode 100644 index 0000000..6fd6a67 --- /dev/null +++ b/tests/util/ktutil/testSafeShellString.php @@ -0,0 +1,34 @@ + -- libgit2 0.21.4