From Klaus’s input and a bunch of testing, I’ve finally cracked the nut of on-the-fly encryption via PHP.
I’ve gotten a bunch of help from this forum so now time to return the favor.
If elaboration is needed, post a question and I’ll see what I can do.
PHP
<?php
// This class handles the execution of the encryption command and helps us to determine when the encryption process has ended.
class process {
public $cmd = '';
private $descriptors = array(
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('pipe', 'w')
);
public $pipes = NULL;
public $desc = '';
private $strt_tm = 0;
public $resource = NULL;
private $exitcode = NULL;
function __construct($cmd = '', $desc = '')
{
$this->cmd = $cmd;
$this->desc = $desc;
$this->resource = proc_open($this->cmd, $this->descriptors, $this->pipes, NULL, $_ENV);
$this->strt_tm = microtime(TRUE);
}
public function is_running()
{
$status = proc_get_status($this->resource);
/**
* proc_get_status will only pull valid exitcode one
* time after process has ended, so cache the exitcode
* if the process is finished and $exitcode is uninitialized
*/
if ($status['running'] === FALSE && $this->exitcode === NULL)
$this->exitcode = $status;
return $status['running'];
}
public function get_exitcode()
{
return $this->exitcode;
}
public function get_elapsed()
{
return microtime(TRUE) - $this->strt_tm;
}
}
// Defined paths. XP_LIBRARY is relative, while the other two are absolute to your server environement.
define( "XP_LIBRARY", $_SERVER['DOCUMENT_ROOT'] . "/library/" );
define( "KRTOOLS_PATH", "/Users/me/mydomain.com/frameworks/xplorit/krpanotools" );
define( "KRENC_PATH", "/Users/me/mydomain.com/library/xml/" );
// Load the file to be encrypted into the output buffer. In this case we are encrypting a set/document of xml generated by PHP.
ob_start();
include_once XP_LIBRARY . "xml/get_tour_xml.php";
$obContent = ob_get_contents();
ob_end_clean();
// Create the filename of the
$my_filename = "this_xml_";
$my_filename .= md5(microtime());
$my_filename .= ".xml";
if ($fpc = file_put_contents($my_filename, $obContent)) {
// Construct the path to the file to be encrypted.
$my_file_path = KRENC_PATH;
$my_file_path .= $my_filename;
// create the command to be executed
$this_command = KRTOOLS_PATH . ' encrypt -p -in=' . $my_file_path . ' -out=stdout';
// instantiate the processing of the encryption command.
$this_process = new process($this_command);
// is this working at all?
if (is_resource($this_process->resource)) {
$counter = 0;
// lets do something while we wait for the encryption process to complete
while ($this_process->is_running()) {
$counter = $counter + 1;
}
// Let's check the output pipe.
if ($out = stream_get_contents($this_process->pipes[1])) {
// if the process has been successful and the output pipe has returned something let's spit it out.
echo $out;
// since this encryption has been successful and this file now contains the encrypted XML, let's delete the original/raw XML file that we encrypted.
unlink($my_filename);
} else {
// Ooops! someing went wrong. Handle for error as you wish...
}
// Close the output pipe.
fclose($this_process->pipes[1]);
// Let's check the error pipe.
if ($err = stream_get_contents($this_process->pipes[2])) {
// Ooops! someing went wrong. Handle for error as you wish...
}
// Close the error pipe.
fclose($this_process->pipes[2]);
// Close the process.
proc_close($this_process->resource);
}
} else {
// if we can't put the contents of the output buffer into the target file, lets just put it here. In-theory this will work just fine, but display your unencrypted XML.
echo $obContent;
};
exit();
Display More