You are not logged in.

Dear visitor, welcome to krpano.com Forum. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

  • "Douglas Rhiner" started this thread

Posts: 98

Location: Fairfax, CA

Occupation: Code-Slave

  • Send private message

1

Monday, October 15th 2018, 10:36pm

On-the-fly encryption - SOLVED

From Klaus’s input and a bunch of testing, I’ve finally cracked the nut of on-the-fly encryption via PHP. *thumbsup*

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.


Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?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();

  • "Douglas Rhiner" started this thread

Posts: 98

Location: Fairfax, CA

Occupation: Code-Slave

  • Send private message

2

Wednesday, October 17th 2018, 9:05pm

I've redone the logic flow a bit and added in error reporting to the main guts of this.


Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
ob_start();include_once XP_LIBRARY . "xml/get_tour_xml.php";
$obContent = ob_get_contents();
ob_end_clean();

$my_filename = "tour_xml_";
$my_filename .= md5(microtime());
$my_filename .= ".xml";

$rec_log = true;

$log_output = 'TOUR, ' . date('Y-m-d H:i:s') . ", ";

if ($fpc = file_put_contents($my_filename, $obContent)) {

    $my_file_path = KRENC_PATH;
    $my_file_path .= $my_filename;

    $this_command = KRTOOLS_PATH . ' encrypt -p -in=' . $my_file_path . ' -out=stdout';

    $this_process = new process($this_command);

    if (is_resource($this_process->resource)) {

        $counter = 0;

        while ($this_process->is_running()) {
            $counter = $counter + 1;
        }

        $exit_data = $this_process->get_exitcode();
        $log_output .= "command -> " . $exit_data['command'] . ", ";
        $log_output .= "\n";
        $log_output .= $this_process->get_elapsed();
        $log_output .= "\n";
        $log_output .= "pid -> " . $exit_data['pid'] . ", ";
        $log_output .= "running -> " . $exit_data['running'] . ", ";
        $log_output .= "signaled -> " . $exit_data['signaled'] . ", ";
        $log_output .= "stopped -> " . $exit_data['stopped'] . ", ";
        $log_output .= "exitcode -> " . $exit_data['exitcode'] . ", ";
        $log_output .= "termsig -> " . $exit_data['termsig'] . ", ";
        $log_output .= "stopsig -> " . $exit_data['stopsig'] . ", ";

        if ($err = stream_get_contents($this_process->pipes[2])) {

            $log_output .= "PIPED ERROR -> $err, ";
            fclose($this_process->pipes[2]);
            $the_output = $obContent;

        } else {

            fclose($this_process->pipes[2]);

            if ($out = stream_get_contents($this_process->pipes[1])) {

                $the_output = $out;
                unlink($my_filename);
                $rec_log = false;

            } else {

                $log_output .= "\nERROR NOT DELETED, ";

            }

            fclose($this_process->pipes[1]);

        }

        proc_close($this_process->resource);

    }

} else {

    $the_output = $obContent;
    $log_output .= "file_put_contents ERROR";

};

echo $the_output;

if ($rec_log) {

    file_put_contents('enc_error_log.txt', $log_output . "\n\n", FILE_APPEND);

}

exit();

Tuur

Sage

Posts: 3,164

Location: Netherlands

Occupation: Krpano custom coding / Virtual Tours / Photography / Musician / Recording engineer

  • Send private message

3

Thursday, October 18th 2018, 2:13pm

Hi Douglas,

thanks for sharing!
Very much appreciated!
I did some server side stuff in the past, but not like this.
Interesting to see.

I might have a try on this and possibly come back with stupid questions at a later moment... haha.

Cheers!
Tuur *thumbsup*
http://www.virtualtuur.com
Skype:studiotuur

Similar threads