fork download
  1. <?php
  2.  
  3. $tests = array(
  4. 'du' => array(
  5. 'max-depth' => 1,
  6. 'human-readable' => TRUE, 'x' => TRUE,
  7. './a/directory'
  8. ),
  9. 'ab' => array(
  10. 'n' => 1000,
  11. 'c' => 10,
  12. 'http://w...content-available-to-author-only...e.com/'
  13. ),
  14. 'misc.sh' => array("red","green","blue","yellow")
  15. );
  16.  
  17. function build_shell_args(Array $options = array(), $equals="=") {
  18.  
  19. static $ok_chars = '/^[-0-9a-z_:\/\.]+$/i';
  20.  
  21. $args = array();
  22.  
  23. foreach ($options as $key => $val) if (!is_null($val) && $val !== FALSE) {
  24.  
  25. $arg = '';
  26. $key_len = 0;
  27.  
  28. if(is_string($key) && ($key_len = strlen($key)) > 0) {
  29.  
  30. if(!preg_match($ok_chars, $key))
  31. $key = escapeshellarg($key);
  32.  
  33. $arg .= '-'.(($key_len > 1) ? '-' : '').$key;
  34. }
  35.  
  36. if($val !== TRUE) {
  37.  
  38. if((string) $val !== (string) (int) $val) {
  39. $val = print_r($val, TRUE);
  40.  
  41. if(!preg_match($ok_chars, $val))
  42. $val = escapeshellarg($val);
  43.  
  44. }
  45.  
  46. if($key_len != 0)
  47. $arg .= $equals;
  48.  
  49. $arg .= $val;
  50.  
  51. }
  52.  
  53. if(!empty($arg))
  54. $args[] = $arg;
  55.  
  56. }
  57.  
  58. return implode(' ', $args);
  59. }
  60.  
  61. foreach($tests as $script => $args)
  62. echo escapeshellcmd("$script ".build_shell_args($args))."\n";
Success #stdin #stdout 0.02s 52504KB
stdin
Standard input is empty
stdout
du --max-depth=1 --human-readable -x ./a/directory
ab -n=1000 -c=10 http://w...content-available-to-author-only...e.com/
misc.sh red green blue yellow