Since PHP 5.2/5.3, proc_open() http://php.net/manual/en/function.proc-open.php supports several new options for Windows:

- bypass_shell: bypass cmd.exe shell when set to TRUE

Details about that are disclosed in the manual page for exec(): http://www.php.net/manual/en/function.exec.php#101579

In Windows, exec() issues an internal call to "cmd /c your_command". This implies that your command must follow the rules imposed by cmd.exe which includes an extra set of quotes around the full command:

- http://ss64.com/nt/cmd.html

Current PHP versions take this into account and add the quotes automatically, but old versions didn't.

Apparently, the change was made in PHP/5.3.0 yet not backported to 5.2.x because it's a backwards incompatible change. To sum up:

- In PHP/5.2 and older you have to surround the full command plus arguments in double quotes
- In PHP/5.3 and greater you don't have to (if you do, your script will break)

If you are interested in the internals, this is the source code:

sprintf(cmd, "%s /c \"%s\"", TWG(comspec), command);

It can be found at http://svn.php.net/viewvc/ (please find php/php-src/trunk/TSRM/tsrm_win32.c, the comment system doesn't allow the direct link).

The entire "start" workaround exists to make exactly that command shell window not appear in the foreground.

Comments

sun’s picture

Issue tags: +Windows, +IIS

.

sun’s picture

Status: Active » Needs review
StatusFileSize
new4.05 KB
sun’s picture

StatusFileSize
new2.7 KB

Committed the error handling improvements separately.

fietserwin’s picture

I tested the bypass_shell option on this configuration:
- Windows Vista
- Apache 2.2.16
- Zend Server CE 5.0.4
- PHP 5.3.3

And it works as expected: correct error return codes, no cmd window

1 remark about imagemagick.shell_.2.patch. There's an accent on the U:

  // @todo Úse watchdog() instead? Would hide errors from users during
sun’s picture

Status: Needs review » Needs work
+++ b/imagemagick.module
@@ -142,7 +142,7 @@ function _imagemagick_check_path($file) {
-  if ($file != 'convert') {
+  if ($file != 'convert' && $file != 'convert.exe') {

@@ -437,7 +428,20 @@ function _imagemagick_convert_exec($command_args, &$output = NULL, &$error = NUL
+    // wrapper. A noteworthy impact of skipping cmd.exe is that no shell
+    // environment variables are available. E.g., the path to convert always
+    // needs to be specified on Windows. Internal details about the bypass_shell
+    // option are not documented on php.net, but have been disclosed in a user
+    // comment.
+    // @see http://php.net/manual/en/function.exec.php#101579
+    'bypass_shell' => TRUE,

mmm, I think that detail in the comments makes the check for 'convert.exe' obsolete... since there are no shell environment variables, there's also no %PATH%, so even if convert.exe would be in the path, it wouldn't be found.

Macronomicus’s picture

Subscribe

  • sun committed a0ebe33 on 8.x-1.x
    Issue #1196008 by sun: Improved error handling.
    
    

  • sun committed a0ebe33 on 8.x-2.x
    Issue #1196008 by sun: Improved error handling.
    
    
mondrake’s picture

Issue summary: View changes
Status: Needs work » Closed (outdated)

Drupal 7 is EOL