GnuPG PHP Class
Here’s a quick little class I wrote to encrypt text files from a web-server using GnuPG. I’ll post updates to it as the code improves, but this was a simple and useful start that helped me encrypt messages and then send them via email from the webserver.
Some uses could be:
- Encrypt and send credit card information via email without storing the numbers on the server
- Encrypt and send sales order information
- Encrypt and send membership information
- Encrypt and send database and server statististics
- …basically anything you don’t want others to see!
/** * GnuPG Class Wrapper * (c)2003 Patrick A. Ward * * This class provides a wrapper for commonly used GnuPG
* encryption functions from PHP. It is meant to simplify
* the use of GnuPG from PHP as well as maintain a consistent
* interface to the command line functions available.
*
* In order for this to work, the apache user must already have a
* valid signing key set in the home dir. For example, on my server
* the apache user is "apache" and the home directory for that user
* is /var/www/. It was necessary to create a new directory called
* /var/www/.gnupg and to generate a key (gpg --gen-key) for signing
* purposes only. In addition, the generated key does not have a
* password so that the webpage can generate the encryption automatically
* without user intervention.
*
* Example Usage:
* $gpg = new GnuPG($gpg_path, $home_dir);
* $gpg->setOption('armor',false);
* $gpg->setRecipient($recipient);
* $gpg->setSender($sender);
* $gpg->setMessage($msg);
* echo $gpg->encrypt();
*
* @package GnuPG
* @author Patrick A. Ward
* @version $Revision: 1.0.1 $
* @access public
* @see http://www.simplespaces.com
*/
class GnuPG {
var $mstrRecipient;
var $mstrSender;
var $mstrMessage;
var $mstrGnuPath;
var $mstrHomeDir;
/* The following variables represent GnuPG specific options
- they are set to true by default */
var $options = array();
/**
* Constructor.
*
* @param string $pstrGpgPath The path to the gpg command line tool (e.g. /usr/bin/gpg)
* @param string $pstrHomeDir The path to the webserver user's GnuPG directory (e.g. /var/www/.gnupg)
* @return null
* @access private
*/
function GnuPG ($pstrGpgPath, $pstrHomeDir) {
if (file_exists($pstrGpgPath) && file_exists($pstrHomeDir)) {
$this->mstrGpgPath = $pstrGpgPath;
$this->mstrHomeDir = $pstrHomeDir;
// set the option defaults
$this->options['quiet'] = true;
$this->options['always-trust'] = true;
$this->options['no-secmem-warning'] = true;
$this->options['encrypt'] = true;
$this->options['sign'] = true;
$this->options['armor'] = true;
print("Set $pstrGpgPath and $pstrHomeDir } else {
trigger_error('GPG FilePaths Incorrect',E_USER_ERROR);
}
}
/**
* Encrypt
*
* Encapsulates the actual command-line function of encryption
*
* @return string The encrypted string resulting from the command-line
* @access public
*/
function Encrypt() {
$cmd = "";
$quiet = ($this->options['quiet']?'--quiet':'');
$always_trust = ($this->options['always-trust']?'--always-trust':'');
$no_secmem_warning = ($this->options['no-secmem-warning']?'--no-secmem-warning':'');
$encrypt = ($this->options['encrypt']?'--encrypt':'');
$sign = ($this->options['sign']?'--sign':'');
$armor = ($this->options['armor']?'--armor':'');
$cmd = "echo $this->mstrMessage | $this->mstrGpgPath " .
"$always_trust $quiet $no_secmem_warning $encrypt $sign $armor " .
"--recipient $this->mstrRecipient --local-user $this->mstrSender --homedir $this->mstrHomeDir ";
$output = `$cmd`;
return $output;
}
/**
* setRecipient
*
* Sets the recipient key that GnuPG will encrypt the message for
* @param string $pstrRecipient In the format "FirstName LastName (Comments) "
* @return null
* @access public
*/
function setRecipient($pstrRecipient) {
$this->mstrRecipient = escapeshellarg($pstrRecipient);
}
/**
* setSender
*
* Sets the sender key that GnuPG will sign the message with
* @param string $pstrSender In the format "FirstName LastName (Comments) "
* @return null
* @access public
*/
function setSender($pstrSender) {
$this->mstrSender = escapeshellarg($pstrSender);
}
/**
* setMessage
*
* Sets the message that GnuPG will encrypt
* @param string $pstrMessage A string value to encrypt and send to the recipient
* @return null
* @access public
*/
function setMessage($pstrMessage) {
$this->mstrMessage = escapeshellarg($pstrMessage);
}
/**
* getOption
*
* This function works with the $this->options array in order to ensure that
* values retrieved are only valid key items as set by default in the class constructor.
* Any other value will return with false.
* @param string $property_name The name of the options key to look up
* @param string $property_value The name of the options value to return (by reference)
* @return boolean
* @access public
*/
function getOption($property_name, &$property_value) {
// determine if the item exists in the array
if (isset($this->options[$property_name])) {
$property_value = $this->options[$property_name];
return true;
}
// returns incomplete if first check failed
return false;
}
/**
* setOption
*
* This function works with the $this->options array in order to ensure that
* values set are only set for valid key items as set by default in the class constructor.
* Any other value will return with false. The function will also ensure that values to be used
* are of a boolean nature. All other value types will return as false.
* @param string $property_name The name of the options key value to retrieve
* @param string $property_value The value to set the option key to
* @return boolean
* @access public
*/
function setOption($property_name, $property_value) {
if (isset($this->options[$property_name])) {
if (is_bool($property_value)) {
$this->options[$property_name] = $property_value;
return true;
}
}
return false;
}
}
?>
<
p>