-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport-data.php
More file actions
302 lines (236 loc) · 7.23 KB
/
export-data.php
File metadata and controls
302 lines (236 loc) · 7.23 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
require_once('config.php');
function export_data(){
/* If we're not already using SSL don't allow continue, redirect instead */
if (empty ($_SERVER['HTTPS'])) {
$correct_url = 'https://';
$correct_url .= $_SERVER['HTTP_HOST'];
$correct_url .= $_SERVER['REQUEST_URI'];
echo '<a href='.$correct_url.'>Please go to secure server first</a>';
exit();
}
$db = llg_db_connection();
$config = config();
$pass = mysqli_real_escape_string($db, $_POST['password']);
$event_id = mysqli_real_escape_string($db, $_POST['event_id']);
$event_name = mysqli_real_escape_string($db, $_POST['event_name']);
if (!isset ($pass)) {
return;
}
if(!llg_validate_pass($db, $event_id, $pass)){
exit();
}
$salt = file_get_contents($config['saltfile'], FILE_USE_INCLUDE_PATH);
if ($salt === false){
http_response_code(500);
exit();
}
$select_booking_data = '
SELECT bookings.id, bookings.submit_timestamp,
AES_DECRYPT(data, CONCAT(SHA2("'.$pass.'", 256), "'.$salt.'")) AS data,
AES_DECRYPT(admin_notes, CONCAT(SHA2("'.$pass.'", 256), "'.$salt.'")) AS notes
FROM bookings WHERE bookings.event_id='.$event_id.' ORDER BY id ASC';
/* Possible join for getting event information
* RIGHT JOIN events ON bookings.event_id=events.id
* WHERE bookings.event_id='.$event_id.'';
*/
$res = mysqli_query($db, $select_booking_data) or die (mysqli_error ($db));
switch ($_POST['output_type']){
case 'csv':
output_as_csv($res, $event_name);
break;
case 'pdf':
output_as_pdf($res, $event_name);
break;
case 'html':
output_as_html($res, $event_name, $event_id);
break;
default:
echo "Err Unknown output type";
exit();
}
}
function skip_keys($key){
return in_array($key, ['anti_spam', 'llg_post_action', 'event_id']);
}
function output_as_html($res, $event_name, $event_id){
$table_content = array();
$col_headers_previous = array();
while ($row_arr = mysqli_fetch_assoc($res)) {
$col_headers = array();
$row = array();
foreach ($row_arr as $key => $value) {
if (skip_keys($key)){
continue;
}
if ($key == 'data'){
$json_data = json_decode($value, true);
foreach ($json_data as $key => $value){
if (skip_keys($key)){
continue;
}
$row[] = array('value' => $value,
'key' => $key,
);
$col_headers[] = $key;
}
} else if($key == 'notes'){
$row[] = array(
'value' => $value,
'key' => $key,
'admin_notes' => True,
'booking_id' => $row_arr['id'],
);
$col_headers[] = $key;
} else {
$row[] = array('value' => $value,
'key' => $key);
$col_headers[] = $key;
}
}
/* If we're past the first row in the table and the keys suddenly change
* then write the keys out as a new row
*/
if (count(array_diff($col_headers, $col_headers_previous)) != 0 &&
count($table_content) > 0) {
$table_content[] = $col_headers;
}
if (!isset($col_headers_initial)){
$col_headers_initial = $col_headers;
}
$col_headers_previous = $col_headers;
$table_content[] = $row;
}
$context = array(
'col_headers' => $col_headers_initial,
'table_content' => $table_content,
'event_name' => $event_name,
'event_id' => $event_id,
'org_name' => config()['org_name'],
'js_dir' => plugins_url('/js/', __FILE__),
'csrf' => wp_nonce_field("llg_event_dash", "llg_event_dash_csrf"),
);
$m = new Mustache_Engine(array(
'loader' => new Mustache_Loader_FilesystemLoader(
dirname(__FILE__) . '/views'),
));
try {
echo $m->render("html-data-output", $context);
} catch (Exception $e){
echo $e;
}
exit();
}
/* We want to avoid breaking the csv document so if the string contains a
* quote then we replace this and also quote the whole value.
*/
function clean_up_str($string){
$string = str_replace("\"", "'", $string);
$string = '"'.$string.'"';
return $string;
}
function output_as_csv($res, $event_name){
$config = config();
header ('Content-type:text/csv',true);
header ('Content-Disposition: attachment; filename="'.$config['org_name'].'-'.$event_name.'-'.date("d-m-y").'.csv"', true);
$csv_out = "";
$col_headers_previous = '';
while ($row_arr = mysqli_fetch_assoc($res)) {
$csv_row = "";
$col_headers = "";
foreach ($row_arr as $key => $value) {
if ($key == 'event_name'){
$event_name = $value;
continue;
}
if (skip_keys($key)){
continue;
}
if ($key == 'data'){
$json_data = json_decode($value, true);
foreach ($json_data as $key => $value){
if (skip_keys($key)){
continue;
}
/* first row so get the keys for the column header from the json */
$col_headers .= clean_up_str($key) . ',';
$csv_row .= clean_up_str($value) . ',';
}
} else {
$col_headers .= clean_up_str($key) . ',';
$csv_row .= clean_up_str($value) . ',';
}
}
/* Remove trailing comma */
$csv_row = substr ($csv_row, 0, -1);
$col_headers = substr($col_headers, 0, -1);
/* If the previous column headers didn't match then we output them again,
* this should avoid values appearing under the wrong column header in the
* case that someone changes the form half way through collecting the data
*/
if ($col_headers != $col_headers_previous){
$csv_out .= $col_headers . "\n";
}
$col_headers_previous = $col_headers;
$csv_out .= $csv_row . "\n";
}
echo $csv_out;
exit();
}
/* format the text a bit nicer */
function write_kv($pdf, $key, $value){
if ($value == ''){
$value = " - ";
}
$pdf->SetFont('Arial', '', 12);
$pdf->Write(5, $key.': ');
$pdf->SetFont('Arial', 'B', 12);
$pdf->Write(5, $value);
$pdf->SetY($pdf->GetY() + 5);
}
function output_as_pdf($res, $event_name){
require_once('fpdf.php');
$config = config();
class PDF extends FPDF {
function Footer(){
$config = config();
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Arial italic 8
$this->SetFont('Arial','I',8);
// Page number
$this->Cell(0,10, $config['org_name'].' CONFIDENTIAL - Destroy after use - Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}
$pdf = new PDF('P', 'mm', 'A4');
$pdf->AliasNbPages();
$pdf->SetTitle($config['org_name'].' '.$event_name);
$pdf->SetAutoPageBreak(True, 20);
$pdf->SetLineWidth(2);
$pdf->SetDrawColor(255,255,255);
$pdf->AddPage();
$pdf->SetFont('Arial','',15);
$pdf->Write(5, $config['org_name'].": ".$event_name);
$pdf->SetY($pdf->GetY() + 10);
$pdf->Line(0, 100, 10, 100);
$pdf->SetFont('Arial','',12);
while ($row_arr = mysqli_fetch_assoc($res)) {
foreach ($row_arr as $key => $value) {
if ($key == 'data'){
$json_data = json_decode($value, true);
foreach ($json_data as $key => $value){
if (skip_keys($key)){
continue;
}
write_kv($pdf, $key, $value);
}
} else {
write_kv($pdf, $key, $value);
}
}
$pdf->SetY($pdf->GetY() + 10);
}
$pdf->Output();
exit();
}
?>