From 987b99cb033a4f7bd8586a3d802a0b6e72ffdb4e Mon Sep 17 00:00:00 2001 From: Christian Schneider Date: Thu, 24 May 2007 16:14:56 +0000 Subject: Changed it::exec to use format string --- it.class | 56 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/it.class b/it.class index 0e1844c..8e7dbcd 100644 --- a/it.class +++ b/it.class @@ -275,37 +275,61 @@ function filter_keys($array, $keys) /** - * Construct shell command, log it, execute it and return output as string. Options ONLY allowed in assoc arrays. - * First scalar (usually command name) is not quoted, option names are not quoted (but option values are). - * @param varargs: scalars for cmd and arguments, assoc arrays to specify opts and opt values, '-q' => true for opts without args + * Construct shell command, log it, execute it and return output as string. + * Keywords {keyword} are replace a la ET(), {-opts} takes an array and + * inserts options a la it_html attributes (value, true, false or null) + * @param $cmd Format string with {keywords} replace a la ET() + * @param $values (zero, one or more arrays can be passed) * @return output of command. shell errors not detectable, see error_log in /www/server/logs */ -function exec(/* ... */) +function exec(/* $cmd, $values1 = array(), ... */) { $args = func_get_args(); - $parts = array(); + $cmd = array_shift($args); + $values = array(); + # Merge values into one array foreach ($args as $arg) + $values += (array)$arg; + + while (list($tag, $option, $key) = it::match('({(-?)(\w+)})', $cmd)) { - foreach ((array)$arg as $key => $val) + $parts = array(); + + if ($option) + { + foreach ((array)$values["-$key"] as $key => $value) + { + if ($value === true || $value === false || $value === null) + $parts[] = $value ? $key : ""; + else + $parts[] = "$key " . it::_exec_quotevalue($value); + } + } + else { - if (it::match('^-', $val)) - it::fatal("leading - in value"); - else if ($val === true || $val === false || $val === null) - $parts[] = $val ? $key : ""; - else if (is_int($key)) - $parts[] = $parts ? escapeshellarg($val) : $val; - else - $parts[] = "$key " . escapeshellarg($val); + foreach ((array)$values[$key] as $value) + $parts[] = it::_exec_quotevalue($value); } + + $cmd = str_replace($tag, join(" ", $parts), $cmd); } - $cmd = join(" ", $parts); it::log('exec', $cmd); return (string)shell_exec($cmd); } +function _exec_quotevalue($value) +{ + $result = strval($value); + + if (it::match('^-', $result)) + it::fatal("leading - in value"); + + return escapeshellarg($result); +} + /** * Convert an image to a given size and type (ensures input is an image) * @param $p['in'] Input filename (mandatory) @@ -322,7 +346,7 @@ function imageconvert($p) $p += array('type' => $type, 'types' => "gif,jpg,png,bmp,tif,jp2"); if (it::match(",$type,", ",{$p['types']},")) # Valid type? - $result = it::exec('convert 2>&1 -flatten', array('-thumbnail' => $p['size']), $p['in'], $p['type'] . ":" . $p['out']) === ""; + $result = it::exec("convert 2>&1 -flatten {-opts} {in} {type}:{out}", '-opts' => array('-thumbnail' => $p['size']), $p) === ""; return $result; } -- cgit v1.2.3