1--TEST-- 2Using proc_open() with a command array (no shell) 3--FILE-- 4<?php 5 6$php = getenv('TEST_PHP_EXECUTABLE'); 7$ds = [ 8 0 => ['pipe', 'r'], 9 1 => ['pipe', 'w'], 10 2 => ['pipe', 'w'], 11]; 12 13echo "Empty command array:\n"; 14try { 15 proc_open([], $ds, $pipes); 16} catch (ValueError $exception) { 17 echo $exception->getMessage() . "\n"; 18} 19 20echo "\nNul byte in program name:\n"; 21try { 22 proc_open(["php\0oops"], $ds, $pipes); 23} catch (ValueError $exception) { 24 echo $exception->getMessage() . "\n"; 25} 26 27echo "\nNul byte in argument:\n"; 28try { 29 proc_open(["php", "array\0oops"], $ds, $pipes); 30} catch (ValueError $exception) { 31 echo $exception->getMessage() . "\n"; 32} 33 34echo "\nBasic usage:\n"; 35$proc = proc_open([$php, '-r', 'echo "Hello World!\n";'], $ds, $pipes); 36fpassthru($pipes[1]); 37proc_close($proc); 38 39putenv('ENV_1=ENV_1'); 40$env = ['ENV_2' => 'ENV_2']; 41$cmd = [$php, '-n', '-r', 'var_dump(getenv("ENV_1"), getenv("ENV_2"));']; 42 43echo "\nEnvironment inheritance:\n"; 44$proc = proc_open($cmd, $ds, $pipes); 45fpassthru($pipes[1]); 46proc_close($proc); 47 48echo "\nExplicit environment:\n"; 49$proc = proc_open($cmd, $ds, $pipes, null, $env); 50fpassthru($pipes[1]); 51proc_close($proc); 52 53echo "\nCheck that arguments are correctly passed through:\n"; 54$args = [ 55 "Simple", 56 "White space\ttab\nnewline", 57 '"Quoted"', 58 'Qu"ot"ed', 59 '\\Back\\slash\\', 60 '\\\\Back\\\\slash\\\\', 61 '\\"Qu\\"ot\\"ed\\"', 62]; 63$cmd = [$php, '-r', 'var_export(array_slice($argv, 1));', '--', ...$args]; 64$proc = proc_open($cmd, $ds, $pipes); 65fpassthru($pipes[1]); 66proc_close($proc); 67 68?> 69--EXPECT-- 70Empty command array: 71proc_open(): Argument #1 ($command) must have at least one element 72 73Nul byte in program name: 74Command array element 1 contains a null byte 75 76Nul byte in argument: 77Command array element 2 contains a null byte 78 79Basic usage: 80Hello World! 81 82Environment inheritance: 83string(5) "ENV_1" 84bool(false) 85 86Explicit environment: 87bool(false) 88string(5) "ENV_2" 89 90Check that arguments are correctly passed through: 91array ( 92 0 => 'Simple', 93 1 => 'White space tab 94newline', 95 2 => '"Quoted"', 96 3 => 'Qu"ot"ed', 97 4 => '\\Back\\slash\\', 98 5 => '\\\\Back\\\\slash\\\\', 99 6 => '\\"Qu\\"ot\\"ed\\"', 100) 101