note 83436 modified in function.popen by danbrown

From: Date: Mon, 02 Mar 2009 13:34:06 +0000
Subject: note 83436 modified in function.popen by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-151323@lists.php.net to get a copy of this message
To complete the examples below that use proc_open to encrypt a string using GPG, here is a decrypt function: <?php function gpg_decrypt($string, $secret) { $homedir = ''; // path to you gpg keyrings $tmp_file = '/tmp/gpg_tmp.asc' ; // tmp file to write to file_put_contents($tmp_file, $string); $text = ''; $error = ''; $descriptorspec = array( 0 => array("pipe", "r"), // stdin 1 => array("pipe", "w"), // stdout 2 => array("pipe", "w") // stderr ?? instead of a file ); $command = 'gpg --homedir ' . $homedir . ' --batch --no-verbose --passphrase-fd 0 -d ' . $tmp_file . ' '; $process = proc_open($command, $descriptorspec, $pipes); if (is_resource($process)) { fwrite($pipes[0], $secret); fclose($pipes[0]); while($s= fgets($pipes[1], 1024)) { // read from the pipe $text .= $s; } fclose($pipes[1]); // optional: while($s= fgets($pipes[2], 1024)) { $error .= $s . "\n"; } fclose($pipes[2]); } file_put_contents($tmp_file, ''); if (preg_match('/decryption failed/i', $error)) { return false; } else { return $text; } } ?> --was-- To complete the examples below that use proc_open to encrypt a string using GPG, here is a decrypt function: function gpg_decrypt($string, $secret) { $homedir = ''; // path to you gpg keyrings $tmp_file = '/tmp/gpg_tmp.asc' ; // tmp file to write to file_put_contents($tmp_file, $string); $text = ''; $error = ''; $descriptorspec = array( 0 => array("pipe", "r"), // stdin 1 => array("pipe", "w"), // stdout 2 => array("pipe", "w") // stderr ?? instead of a file ); $command = 'gpg --homedir ' . $homedir . ' --batch --no-verbose --passphrase-fd 0 -d ' . $tmp_file . ' '; $process = proc_open($command, $descriptorspec, $pipes); if (is_resource($process)) { fwrite($pipes[0], $secret); fclose($pipes[0]); while($s= fgets($pipes[1], 1024)) { // read from the pipe $text .= $s; } fclose($pipes[1]); // optional: while($s= fgets($pipes[2], 1024)) { $error .= $s . "\n"; } fclose($pipes[2]); } file_put_contents($tmp_file, ''); if (preg_match('/decryption failed/i', $error)) { return false; } else { return $text; } } http://php.net/manual/en/function.popen.php

« previous php.notes (#151323) next »