diff options
author | Christian Schneider | 2007-05-24 16:14:56 +0000 |
---|---|---|
committer | Christian Schneider | 2007-05-24 16:14:56 +0000 |
commit | 987b99cb033a4f7bd8586a3d802a0b6e72ffdb4e (patch) | |
tree | fc7edd3de9ae0dadb24b59db80c13aa5360d53e2 | |
parent | ab24be9f1d2de80ff3288ed3d7a8063a88db4156 (diff) | |
download | itools-987b99cb033a4f7bd8586a3d802a0b6e72ffdb4e.tar.gz itools-987b99cb033a4f7bd8586a3d802a0b6e72ffdb4e.tar.bz2 itools-987b99cb033a4f7bd8586a3d802a0b6e72ffdb4e.zip |
Changed it::exec to use format string
-rw-r--r-- | it.class | 56 |
1 files changed, 40 insertions, 16 deletions
@@ -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; } |