cvs: pear /Crypt_GPG/GPG/Driver Php.php

[email protected] ("Michael Gauthier") Mon, 05 May 2008 01:41:29 -0000
Newsgroups php.pear.cvs
Message-ID <cvsgauthierm1209951689@cvsserver>
gauthierm		Mon May  5 01:41:29 2008 UTC

  Modified files:              
    /pear/Crypt_GPG/GPG/Driver	Php.php 
  Log:
  Small cleanups (CS, simplification, typos) and make the code so it _should_ work in Windows again. Of course it doesn't do as expected. See PHP bug #44908.
gauthierm-20080505014129.txt (text/plain, 14.1 KB)
http://cvs.php.net/viewvc.cgi/pear/Crypt_GPG/GPG/Driver/Php.php?r1=1.30&r2=1.31&diff_format=u
Index: pear/Crypt_GPG/GPG/Driver/Php.php
diff -u pear/Crypt_GPG/GPG/Driver/Php.php:1.30 pear/Crypt_GPG/GPG/Driver/Php.php:1.31
--- pear/Crypt_GPG/GPG/Driver/Php.php:1.30	Sun May  4 04:26:23 2008
+++ pear/Crypt_GPG/GPG/Driver/Php.php	Mon May  5 01:41:29 2008
@@ -32,7 +32,7 @@
  * @author    Michael Gauthier <[email protected]>
  * @copyright 2005-2008 silverorange
  * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
- * @version   CVS: $Id: Php.php,v 1.30 2008/05/04 04:26:23 gauthierm Exp $
+ * @version   CVS: $Id: Php.php,v 1.31 2008/05/05 01:41:29 gauthierm Exp $
  * @link      http://pear.php.net/package/Crypt_GPG
  * @link      http://www.gnupg.org/
  */
@@ -326,8 +326,8 @@
 
         // populate mbstring overloading cache if not set
         if (self::$_mbStringOverload === null) {
-            self::$_mbStringOverload = (extension_loaded('mbstring') &&
-            ini_get('mbstring.func_overload') & 0x02 === 0x02);
+            self::$_mbStringOverload = (extension_loaded('mbstring')
+                && (ini_get('mbstring.func_overload') & 0x02) === 0x02);
         }
 
         if (array_key_exists('homedir', $options)) {
@@ -428,9 +428,7 @@
             }
         }
 
-        $result = $this->_parseImportStatus($this->_getStatus());
-
-        return $result;
+        return $this->_parseImportStatus($this->_getStatus());
     }
 
     // }}}
@@ -1376,11 +1374,11 @@
      * operations that require input or output but do not require a passphrase
      * or detached signature data
      *
-     * @param string|resource $input   the input source. This is data to send
-     *                                 to GPG. If there is no data to send to
-     *                                 GPG, specify null.
-     * @param string|resource $output  the output location. This is where the
-     *                                 output of GPG is sent.
+     * @param string|resource $input  the input source. This is data to send
+     *                                to GPG. If there is no data to send to
+     *                                GPG, specify null.
+     * @param string|resource $output the output location. This is where the
+     *                                output of GPG is sent.
      *
      * @return void
      *
@@ -1466,35 +1464,40 @@
             $inputByteLength = self::_byteLength($input);
         }
 
-        if (is_string($message)) {
+        if (is_string($message) && !$this->_isWin) {
             $messagePosition   = 0;
             $messageByteLength = self::_byteLength($message);
         }
 
-        $pipeInput   = $this->_pipes[self::FD_INPUT];
-        $pipeOutput  = $this->_pipes[self::FD_OUTPUT];
-        $pipeError   = $this->_pipes[self::FD_ERROR];
-        $pipeStatus  = $this->_pipes[self::FD_STATUS];
-        $pipeMessage = $this->_pipes[self::FD_MESSAGE];
-
+        $pipeInput  = $this->_pipes[self::FD_INPUT];
+        $pipeOutput = $this->_pipes[self::FD_OUTPUT];
+        $pipeError  = $this->_pipes[self::FD_ERROR];
         stream_set_blocking($pipeInput, 0);
-        stream_set_blocking($pipeMessage, 0);
-        stream_set_blocking($pipeError, 0);
-        stream_set_blocking($pipeMessage, 0);
         stream_set_blocking($pipeOutput, 0);
+        stream_set_blocking($pipeError, 0);
+
+        if (!$this->_isWin) {
+            $pipeStatus  = $this->_pipes[self::FD_STATUS];
+            $pipeMessage = $this->_pipes[self::FD_MESSAGE];
+            stream_set_blocking($pipeMessage, 0);
+            stream_set_blocking($pipeStatus, 0);
+        }
 
         // set up input stream array
         $openInputStreams = array(
             'pipe' . self::FD_OUTPUT => $pipeOutput,
-            'pipe' . self::FD_ERROR  => $pipeError,
-            'pipe' . self::FD_STATUS => $pipeStatus
+            'pipe' . self::FD_ERROR  => $pipeError
         );
 
+        if (!$this->_isWin) {
+            $openInputStreams['pipe' . self::FD_STATUS] = $pipeStatus;
+        }
+
         if (is_resource($input)) {
             $openInputStreams['input'] = $input;
         }
 
-        if (is_resource($message)) {
+        if (is_resource($message) && !$this->_isWin) {
             $openInputStreams['message'] = $message;
         }
 
@@ -1505,7 +1508,7 @@
             $openOutputStreams['pipe' . self::FD_INPUT] = $pipeInput;
         }
 
-        if (isset($message)) {
+        if (isset($message) && !$this->_isWin) {
             $openOutputStreams['pipe' . self::FD_MESSAGE] = $pipeMessage;
         }
 
@@ -1544,7 +1547,7 @@
                 // input
                 if (in_array($pipeInput, $outputStreams)) {
                     if (is_string($input)) {
-                        $idle = false;
+                        $idle  = false;
                         $chunk = substr($input, $inputPosition,
                             self::CHUNK_SIZE);
 
@@ -1553,7 +1556,7 @@
                         $inputPosition += $length;
                     } else {
                         if (in_array($input, $inputStreams)) {
-                            $idle = false;
+                            $idle  = false;
                             $chunk = fread($input, self::CHUNK_SIZE);
                             fwrite($pipeInput, $chunk,
                                 self::_byteLength($chunk));
@@ -1562,9 +1565,9 @@
                 }
 
                 // message
-                if (in_array($pipeMessage, $outputStreams)) {
+                if (!$this->_isWin && in_array($pipeMessage, $outputStreams)) {
                     if (is_string($message)) {
-                        $idle = false;
+                        $idle  = false;
                         $chunk = substr($message, $messagePosition,
                             self::CHUNK_SIZE);
 
@@ -1573,7 +1576,7 @@
                         $messagePosition += $length;
                     } else {
                         if (in_array($message, $inputStreams)) {
-                            $idle = false;
+                            $idle  = false;
                             $chunk = fread($message, self::CHUNK_SIZE);
                             fwrite($pipeMessage, $chunk,
                                 self::_byteLength($chunk));
@@ -1584,12 +1587,12 @@
                 // output
                 if (in_array($pipeOutput, $inputStreams)) {
                     if (is_string($output)) {
-                        $idle = false;
+                        $idle  = false;
                         $chunk = fread($pipeOutput, self::CHUNK_SIZE);
                         $output .= $chunk;
                     } else {
                         if (in_array($output, $outputStreams)) {
-                            $idle = false;
+                            $idle  = false;
                             $chunk = fread($pipeOutput, self::CHUNK_SIZE);
                             fwrite($output, $chunk, self::_byteLength($chunk));
                         }
@@ -1599,12 +1602,12 @@
                 // error
                 if (in_array($pipeError, $inputStreams)) {
                     if (is_string($error)) {
-                        $idle = false;
+                        $idle  = false;
                         $chunk = fread($pipeError, self::CHUNK_SIZE);
                         $error .= $chunk;
                     } else {
                         if (in_array($error, $errorStreams)) {
-                            $idle = false;
+                            $idle  = false;
                             $chunk = fread($pipeError, self::CHUNK_SIZE);
                             fwrite($error, $chunk, self::_byteLength($chunk));
                         }
@@ -1612,14 +1615,14 @@
                 }
 
                 // status
-                if (in_array($pipeStatus, $inputStreams)) {
+                if (!$this->_isWin && in_array($pipeStatus, $inputStreams)) {
                     if (is_string($status)) {
-                        $idle = false;
+                        $idle  = false;
                         $chunk = fread($pipeStatus, self::CHUNK_SIZE);
                         $status .= $chunk;
                     } else {
                         if (in_array($status, $statusStreams)) {
-                            $idle = false;
+                            $idle  = false;
                             $chunk = fread($pipeStatus, self::CHUNK_SIZE);
                             fwrite($status, $chunk, self::_byteSize($chunk));
                         }
@@ -1649,19 +1652,21 @@
                 }
 
                 // message
-                if (is_string($message)) {
-                    if ($messagePosition >= $messageByteLength) {
-                        $this->_closePipe(self::FD_MESSAGE);
-                        unset($openOutputStreams['pipe' . self::FD_MESSAGE]);
-                    }
-                } else {
-                    if (   in_array($message, $openInputStreams)
-                        && feof($message)
-                    ) {
-                        fclose($message);
-                        unset($openInputStreams['message']);
-                        $this->_closePipe(self::FD_MESSAGE);
-                        unset($openOutputStreams['pipe' . self::FD_MESSAGE]);
+                if (!$this->_isWin) {
+                    if (is_string($message)) {
+                        if ($messagePosition >= $messageByteLength) {
+                            $this->_closePipe(self::FD_MESSAGE);
+                            unset($openOutputStreams['pipe' . self::FD_MESSAGE]);
+                        }
+                    } else {
+                        if (   in_array($message, $openInputStreams)
+                            && feof($message)
+                        ) {
+                            fclose($message);
+                            unset($openInputStreams['message']);
+                            $this->_closePipe(self::FD_MESSAGE);
+                            unset($openOutputStreams['pipe' . self::FD_MESSAGE]);
+                        }
                     }
                 }
 
@@ -1692,15 +1697,17 @@
                 }
 
                 // status
-                if (   in_array($pipeStatus, $openInputStreams)
-                    && feof($pipeStatus)
-                ) {
-                    $this->_closePipe(self::FD_STATUS);
-                    unset($openInputStreams['pipe' . self::FD_STATUS]);
-                    if (is_resource($status)) {
-                        fflush($status);
-                        fclose($status);
-                        unset($openOutputStreams['status']);
+                if (!$this->_isWin) {
+                    if (   in_array($pipeStatus, $openInputStreams)
+                        && feof($pipeStatus)
+                    ) {
+                        $this->_closePipe(self::FD_STATUS);
+                        unset($openInputStreams['pipe' . self::FD_STATUS]);
+                        if (is_resource($status)) {
+                            fflush($status);
+                            fclose($status);
+                            unset($openOutputStreams['status']);
+                        }
                     }
                 }
             } // end if not idle
@@ -1788,17 +1795,21 @@
 
         $command .= ' ' . implode(' ', $args);
 
+        // binary operations will not work on Windows with PHP < 5.2.6
+        $rb = (version_compare(PHP_VERSION, '5.2.6') < 0) ? 'r' : 'rb';
+        $wb = (version_compare(PHP_VERSION, '5.2.6') < 0) ? 'w' : 'wb';
+
         $descriptorSpec = array(
-            self::FD_INPUT   => array('pipe', 'r'), // stdin
-            self::FD_OUTPUT  => array('pipe', 'w'), // stdout
-            self::FD_ERROR   => array('pipe', 'w'), // stderr
+            self::FD_INPUT  => array('pipe', $rb), // stdin
+            self::FD_OUTPUT => array('pipe', $wb), // stdout
+            self::FD_ERROR  => array('pipe', $wb), // stderr
         );
 
         if (!$this->_isWin) {
             // extra output (status)
-            $descriptorSpec[self::FD_STATUS]  = array('pipe', 'w');
+            $descriptorSpec[self::FD_STATUS]  = array('pipe', $wb);
             // extra input
-            $descriptorSpec[self::FD_MESSAGE] = array('pipe', 'r');
+            $descriptorSpec[self::FD_MESSAGE] = array('pipe', $rb);
         }
 
         $this->_debug('Opening subprocess with the following command:');
@@ -1843,14 +1854,14 @@
 
             $exitCode = proc_close($this->_process);
 
+            $error  = $this->_getError();
+            $status = $this->_getStatus();
+
             // delete any remaining temp files
             foreach (array_keys($this->_tempFiles) as $fileNumber) {
                 $this->_deleteTempFile($fileNumber);
             }
 
-            $error  = $this->_getError();
-            $status = $this->_getStatus();
-
             if ($exitCode != 0) {
                 $this->_debug('Subprocess returned an unexpected exit code: ' .
                     $exitCode);
@@ -1988,9 +1999,7 @@
      * Gets the status output from the GPG subprocess
      *
      * If the operating system is Windows, this reads and caches the contents
-     * of the status file after the subprocess has been closed. Otherwise, this
-     * caches the content of the status file descriptor while the GPG
-     * subprocess is open.
+     * of the status file after the subprocess has been closed.
      *
      * @return string the status output from the open GPG subprocess. If there
      *                is no status output or there is no open GPG subprocess,
@@ -2000,18 +2009,9 @@
      */
     private function _getStatus()
     {
-/*        if ($this->_status == '') {
-            if ($this->_isWin) {
-                $this->_status = $this->_readStatusFile();
-            } else {
-                if (array_key_exists(self::FD_STATUS, $this->_openPipes)) {
-                    while (!feof($this->_pipes[self::FD_STATUS])) {
-                        $this->_status .= fread($this->_pipes[self::FD_STATUS],
-                            8192);
-                    }
-                }
-            }
-        }*/
+        if ($this->_status == '' && $this->_isWin) {
+            $this->_status = $this->_readStatusFile();
+        }
 
         return $this->_status;
     }