-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuery.inc
225 lines (184 loc) · 5.33 KB
/
Query.inc
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
include 'debug.inc';
class Query
{
var $param;
// Class Constructor
function Query()
{
session_start();
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w") // stdout is a pipe that the child will write to
);
/*
// if the GET parameter "generateKeypair" is set
if(isset($_GET["getPublicKey"])) {
$arrOutput = array(
"publickey" => file_get_contents('rsa_1024_pub.pem')
);
// Convert the response to JSON, and send it to the client
echo json_encode($arrOutput);
// else if the GET parameter "decrypttest" is set
} elseif (isset($_GET["handshake"])) {
// Decrypt the client's request
$cmd = sprintf("openssl rsautl -decrypt -inkey rsa_1024_priv.pem");
$process = proc_open($cmd, $descriptorspec, $pipes);
if (is_resource($process)) {
fwrite($pipes[0], base64_decode($_POST['key']));
fclose($pipes[0]);
$key = stream_get_contents($pipes[1]);
fclose($pipes[1]);
proc_close($process);
}
// Save the AES key into the session
$_SESSION["key"] = $key;
// JSON encode the challenge
$cmd = sprintf("openssl enc -aes-256-cbc -pass pass:'$key' -a -e");
$process = proc_open($cmd, $descriptorspec, $pipes);
if (is_resource($process)) {
fwrite($pipes[0], $key);
fclose($pipes[0]);
// we have to trim all newlines and whitespaces by ourself
$challenge = trim(str_replace("\n", "", stream_get_contents($pipes[1])));
fclose($pipes[1]);
proc_close($process);
}
echo json_encode(array("challenge" => $challenge));
// echo json_encode(array("challenge" => AesCtr::encrypt($key, $key, 256)));
} elseif (isset($_GET["decrypttest"])) {
// set timezone just in case
date_default_timezone_set('UTC');
// Get some test data to encrypt, this is an ISO 8601 timestamp
$toEncrypt = date("c");
// get the key from the session
$key = $_SESSION["key"];
$cmd = sprintf("openssl enc -aes-256-cbc -pass pass:'$key' -a -e");
$process = proc_open($cmd, $descriptorspec, $pipes);
if (is_resource($process)) {
fwrite($pipes[0], $toEncrypt);
fclose($pipes[0]);
$encrypted = stream_get_contents($pipes[1]);
fclose($pipes[1]);
proc_close($process);
}
echo json_encode(
array(
"encrypted" => $encrypted,
"unencrypted" => $toEncrypt
)
);
// else if the GET parameter "handshake" is set
} else */ if (isset($_POST['jCryption'])) {
$key = $_SESSION["key"];
// Decrypt the client's request and send it to the clients(uncrypted)
$cmd = sprintf("openssl enc -aes-256-cbc -pass pass:'$key' -d");
$process = proc_open($cmd, $descriptorspec, $pipes);
if (is_resource($process)) {
fwrite($pipes[0], base64_decode($_POST['jCryption']));
fclose($pipes[0]);
$data = stream_get_contents($pipes[1]);
fclose($pipes[1]);
proc_close($process);
}
parse_str($data, $this->param);
//debug::log($this->param);
/*
if(strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
echo json_encode(array("data" => $data));
} else {
parse_str($data, $output);
print_r($output);
}
*/
}
else
{
$this->param = array();
$this->setParams();
}
//debug($this->param, 'Query');
}
function echoo($name, $d = '')
{
echo $this->get($name, $d);
}
function checked($name, $value)
{
echo $this->get($name) == $value? 'checked' : '';
}
function selected($name, $value)
{
echo $this->get($name) == $value? 'selected' : '';
}
function get($name, $default = '')
{
if(!isset($this->param[$name]))
{
$this->param[$name] = $default;
}
return $this->param[$name];
}
function set($name, $value, $overwrite = true)
{
if(!isset($this->param[$name]) || $overwrite)
{
$this->param[$name] = $value;
}
}
function has($name)
{
return isset($this->param[$name]) ? true : false;
}
function isEmpty($name)
{
return isset($this->param[$name]) && !empty($this->param[$name])? false : true;
}
// Class Methods
// read form variables
// read GET variables, but let POST variables override them
function setParams()
{
foreach( $_GET as $k => $v )
{
if(is_string($v))
{
$this->param[$k] = $v;
}
else if(is_array($v))
{
$this->param[$k] = $v;
//$this->param[$k.'[]'] = $v;
}
else
{
$this->param[$k] = NULL;
}
//$this->param[$k] = is_string($v) ? strip_tags($v) : $v;
//$this->param[$k] = $this->param[$k] == '' ? NULL : $this->param[$k];
}
foreach( $_POST as $k => $v )
{
if(is_string($v))
{
$this->param[$k] = $v;
}
else if(is_array($v))
{
$this->param[$k] = $v;
//$this->param[$k.'[]'] = $v;
}
else
{
$this->param[$k] = NULL;
}
// $this->param[$k] = is_string($v) ? strip_tags($v) : $v;
// $this->param[$k] = $this->param[$k] == '' ? NULL : $this->param[$k];
}
}
function clearParams()
{
foreach( $_GET as $k => $v ) unset($_GET[$k]);
foreach( $_POST as $k => $v ) unset($_POST[$k]);
}
}