note 52602 added to function.popen
| From: | PGPDude at osu1 dot php dot net | Date: | Fri, 06 May 2005 19:30:47 +0000 |
| Subject: | note 52602 added to function.popen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-89032@lists.php.net to get a copy of this message | ||
LinixDude010's srcipt did not work for me. Seems wrong to read and write with popen, according
to the manual.
The script produced pgp text, but there was something wrong with the text and I could not decode it.
This replacement script, using proc_open, which can read and write, DOES work:
<?php
function pgp_encrypt($keyring_location, $public_key_id, $plain_text) {
$encrypted_text='';
$key_id = EscapeShellArg($public_key_id);
putenv("PGPPATH=$keyring_location");
// encrypt the message
$descriptorspec = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
2 => array("pipe", "w") // stderr ?? instead of a file
);
$process = proc_open("pgpe -r $key_id -af", $descriptorspec, $pipes);
if (is_resource($process)) {
fwrite($pipes[0], $plain_text);
fclose($pipes[0]);
while($s= fgets($pipes[1], 1024)) {
// read from the pipe
$encrypted_text .= $s;
}
fclose($pipes[1]);
// optional:
while($s= fgets($pipes[2], 1024)) {
$encrypted_text.= "\n<p>Error: $s</p>\n";
}
fclose($pipes[2]);
}
return $encrypted_text;
}
$message = pgp_encrypt("/home/username/.pgp", "to@domain.com", "dummy text
to be encrypted");
print nl2br($message);
?>
----
Manual Page -- http://www.php.net/manual/en/function.popen.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52602
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52602&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52602&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52602&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52602&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52602&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52602&report=yes
Search -- http://master.php.net/manage/user-notes.php