当我在 php 脚本中运行 exec 命令时,它没有给出任何输出,但返回值为 0

问题描述 投票:0回答:1

当我在 php 脚本中运行

exec
命令时,它没有给出任何输出,但返回值为 0

背景:我正在尝试使用 AS2Secure PHP 代码实现 AS2 通信功能。当我尝试在签名和加密后向合作伙伴发送消息时。但在加密时,它并没有加密,我们只发送签名的消息。

这里我提供了加密函数,它在内部调用 exec 函数,如下

'''


public function encrypt($input, $cypher = 'des3'){
   try {
   if (!$this->partner_to->sec_certificate)
      $certificate = self::getPublicFromPKCS12($this-      
   >partner_to->sec_pkcs12, $this->partner_to->sec_pkcs12_password);
   else
      $certificate = $this->partner_to->sec_certificate;

   if (!$certificate) throw new Exception('Unable to    
   extract private key from PKCS12 file. (' . $this->partner_to-   
   >sec_pkcs12.' - using:' . $this->partner_to-
   >sec_pkcs12_password.')');

   $output = self::getTempFilename();

   $command = self::$ssl_openssl . ' smime '   . 
                                            ' -encrypt' .
                                            ' -in '     .    
   escapeshellarg($input) .
                                            ' -out '    . 
   escapeshellarg($output) .
                                            ' -des3 '   . 
   escapeshellarg($certificate);
   $result = $this->exec($command);
   AS2Log::info(false, 'Encryption successful');
            $headers = 'Content-Type: application/pkcs7-mime; 
   smime-   
   type="enveloped-data"; name="smime.p7m"' . "\n" .
                       'Content-Disposition: attachment;    
   filename="smime.p7m"' . "\n" .
                       'Content-Transfer-Encoding: binary' . "\n\n";
   $content = file_get_contents($output);

   // we remove header auto-added by openssl
   $content = substr($content, strpos($content, "\n\n") + 
   2);
   $content = base64_decode($content);

   $content = $headers . $content;
   file_put_contents($output, $content);

   /* if ($this->partner_to->sec_pkcs12)
   $security = ' -pkcs12 '.escapeshellarg($this-   
   >partner_to->sec_pkcs12).
                            ($this->partner_to-
   >sec_pkcs12_password?' -password '.escapeshellarg($this-   
   >partner_to->sec_pkcs12_password):' -nopassword');
            else
   $security = ' -cert '.escapeshellarg($this-   
   >partner_to->sec_certificate);
            
   $command = self::$javapath.' -jar    
   '.escapeshellarg(AS2_DIR_BIN.self::$ssl_adapter).
                                       ' encrypt'.
                                       $security.
   //                                       ' -cypher    
   '.escapeshellarg($cypher).
                                       ' -in 
   '.escapeshellarg($input).
                                       ' -out 
   '.escapeshellarg($output);

   $result = $this->exec($command);*/

            return $output;
        }
   catch(Exception $e){
            throw $e;
        }
    }
'''

Here is the `exec` command function:

openssl smime  -encrypt -in 
"C:\\Users\\Maggy\\AppData\\Local\\Temp\\as2A5B3.tmp" -out 
"C:\\Users\\Maggy\\AppData\\Local\\Temp\\as2B18B.tmp" -des3 
"D:\\as2secure_code_build- 
0.9.0\\partners\\maggy\\clientcertificate.pem"

I ran the the same command in the command line and it is     
successfully giving output.

Here is the PHP code:
    public static function exec($command, $return_output = false){
        $output = array();
        $return_var = 0;
        try{

    AS2Log::info(false, 'This is AS2Adapter File and IN EXEC     
    FUNCTION CALLED'.$command);
    // AS2Log::info(false, 'INPUT DATA as     
    FOLLOWS'.file_get_contents($input));
            exec($command , $output, $return_var);
    //          proc_open($command , $output, $return_var);
    //            passthru ("$command , 2>&1");
    AS2Log::info(false, 'This is AS2Adapter File and IN EXEC     
    FUNCTION EXECUTED and Result will             
    be ~~~~~~~~~~~~~~~~~~~~~');


    AS2Log::info(false, 'This is AS2Adapter File and IN EXEC     
    FUNCTION COMPLETED                         
    SUCCESSFULLY'.$output[0]);

    $line = (isset($output[0])?$output[0]:'Unexpected error in     
    command line : ' . $command);
            if ($return_var) throw new Exception($line,     
    (int)$return_var);
        }
        catch(Exception $e){
            throw $e;
        }
        
    AS2Log::info(false, 'This is RETURN OUTPUT LOOKS LIKE :     
    '.file_get_contents($output));

    AS2Log::info(false, 'This is RETURN VAR LOOKS LIKE : 
    '.file_get_contents($return_var));

    if ($return_output){
        return $output;
    AS2Log::info(false, 'This is AS2Adapter File and FINAL OUTPUT     
    LOOKS LIKE : '.$output);

    }
    else
        return $return_var;
    }

你能帮我解决这个问题吗?

我已经尝试过

shell_exec
,它给出了相同的结果。

我尝试过

passthru
命令,但没有给出任何结果。

我正在尝试对消息进行签名和加密,但它只是对消息进行签名,而不是加密。

php shell communication edi
1个回答
0
投票

您确定 shell 中的命令将输出输出到 stdout 而不是 stderr 吗? 尝试将“1>file_stdout 2>file_stderr”添加到您的命令中以查看内容。

尝试将 $command 与“2>&1”连接起来,这会将所有内容发送到 $output。 您还应该尝试记录所有行,而不仅仅是 $output[0]。 var_dump()、print_r()、implode() 等函数会对您有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.