note 83635 added to function.proc-open
| From: | snowleopard at amused dot NOSPAMPLEASE dot com dot au | Date: | Thu, 05 Jun 2008 14:46:29 +0000 |
| Subject: | note 83635 added to function.proc-open | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-140143@lists.php.net to get a copy of this message | ||
I managed to make a set of functions to work with GPG, since my hosting provider refused to use
GPG-ME.
Included below is an example of decryption using a higher descriptor to push a passphrase.
Comments and emails welcome. :)
<?php
function GPGDecrypt($InputData, $Identity, $PassPhrase, $HomeDir="~/.gnupg",
$GPGPath="/usr/bin/gpg") {
if(!is_executable($GPGPath)) {
trigger_error($GPGPath . " is not executable",
E_USER_ERROR);
die();
} else {
// Set up the descriptors
$Descriptors = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w"),
3 => array("pipe", "r") // This is the pipe we can feed the password into
);
// Build the command line and start the process
$CommandLine = $GPGPath . ' --homedir ' . $HomeDir . ' --quiet --batch --local-user
"' . $Identity . '" --passphrase-fd 3 --decrypt -';
$ProcessHandle = proc_open( $CommandLine, $Descriptors, $Pipes);
if(is_resource($ProcessHandle)) {
// Push passphrase to custom pipe
fwrite($Pipes[3], $PassPhrase);
fclose($Pipes[3]);
// Push input into StdIn
fwrite($Pipes[0], $InputData);
fclose($Pipes[0]);
// Read StdOut
$StdOut = '';
while(!feof($Pipes[1])) {
$StdOut .= fgets($Pipes[1], 1024);
}
fclose($Pipes[1]);
// Read StdErr
$StdErr = '';
while(!feof($Pipes[2])) {
$StdErr .= fgets($Pipes[2], 1024);
}
fclose($Pipes[2]);
// Close the process
$ReturnCode = proc_close($ProcessHandle);
} else {
trigger_error("cannot create resource", E_USER_ERROR);
die();
}
}
if (strlen($StdOut) >= 1) {
if ($ReturnCode <= 0) {
$ReturnValue = $StdOut;
} else {
$ReturnValue = "Return Code: " . $ReturnCode . "\nOutput on StdErr:\n" .
$StdErr . "\n\nStandard Output Follows:\n\n";
}
} else {
if ($ReturnCode <= 0) {
$ReturnValue = $StdErr;
} else {
$ReturnValue = "Return Code: " . $ReturnCode . "\nOutput on StdErr:\n" .
$StdErr;
}
}
return $ReturnValue;
}
?>
----
Server IP: 203.88.112.146
Probable Submitter: 203.206.159.110
----
Manual Page -- http://www.php.net/manual/en/function.proc-open.php
Edit -- https://master.php.net/note/edit/83635
Del: integrated -- https://master.php.net/note/delete/83635/integrated
Del: useless -- https://master.php.net/note/delete/83635/useless
Del: bad code -- https://master.php.net/note/delete/83635/bad+code
Del: spam -- https://master.php.net/note/delete/83635/spam
Del: non-english -- https://master.php.net/note/delete/83635/non-english
Del: in docs -- https://master.php.net/note/delete/83635/in+docs
Del: other reasons-- https://master.php.net/note/delete/83635
Reject -- https://master.php.net/note/reject/83635
Search -- https://master.php.net/manage/user-notes.php