forked from lz430/assist-portal
/
functions-enrollment.php
287 lines (247 loc) · 7.78 KB
/
functions-enrollment.php
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
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/admin/includes/config.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/admin/lib/aws/aws-autoloader.php');
use Aws\S3\S3Client;
/**
* A helper function for creating the sql query to encrypt a field on an insert or update
* @param $value
* @return string
*/
function db_aes_encrypt($value) {
if($value) {
return "AES_ENCRYPT('".$value."', '".AES_ENCRYPTION_KEY."')";
} else {
return "''";
}
return $value;
}
/**
* A helper function for creating the sql query to select a field and decrypt it from the database at the same time.
* @param $field
* @return string
*/
function db_aes_decrypt($field) {
return "AES_DECRYPT(`".$field."`, '".AES_ENCRYPTION_KEY."') as ".$field;
}
/**
* Helper function for wrapping a normal field in quotes
* @param $value
* @return string
*/
function db_normal_field($value) {
if($value) {
return "'".$value."'";
} else {
return "''";
}
return $value;
}
function get_encryption_fields() {
return array('first_name', 'last_name', 'email', 'ssn', 'dob', 'phone', 'middle_initial');
}
function needs_encryption($field) {
$fields = get_encryption_fields();
return in_array($field, $fields);
}
/************************************************************
* ENROLLMENT FORM
***********************************************************/
function retrieve_enrollment_uid($enrollmentId=0) {
global $wpdb;
if($enrollmentId) {
$qry = $wpdb->prepare("select uid from enrollment where id=%d", $enrollmentId);
$uid = $wpdb->get_var($qry);
return $uid;
}
}
function retrieve_enrollment($enrollmentId) {
global $wpdb;
if($enrollmentId) {
$fields = "* ";
foreach(get_encryption_fields() as $field) {
$fields.=", ".db_aes_decrypt($field);
}
$row = $wpdb->get_row($wpdb->prepare('SELECT '.$fields.' from enrollment WHERE id = %d', $enrollmentId), OBJECT);
return $row;
}
}
function retrieve_enrollment_by_uid($uid='') {
global $wpdb;
if($uid) {
$fields = "* ";
foreach(get_encryption_fields() as $field) {
$fields.=", ".db_aes_decrypt($field);
}
$row = $wpdb->get_row($wpdb->prepare('SELECT '.$fields.' from enrollment WHERE uid = %s', $uid), OBJECT);
return $row;
}
}
function retrieve_enrollment_by_wpid($uid='') {
global $wpdb;
if($uid) {
$fields = "* ";
foreach(get_encryption_fields() as $field) {
$fields.=", ".db_aes_decrypt($field);
}
$row = $wpdb->get_row($wpdb->prepare('SELECT '.$fields.' from enrollment WHERE wpid = %s', $uid), OBJECT);
return $row;
}
}
/**
* Creates the enrollment with an SQL Insert
*/
function create_enrollment($enrollmentId, $data) {
global $wpdb;
$data['uid'] = uniqid();
if($enrollmentId) {
$data['id'] = $enrollmentId;
}
foreach($data as $key => $value) {
$fields[] = $key;
if(needs_encryption($key)) {
$value = db_aes_encrypt($value);
} else {
$value = db_normal_field($value);
}
$values[] = $value;
}
$wpdb->show_errors();
$sql = "INSERT INTO enrollment(".implode($fields,", ").") VALUES (".implode($values,", ").");";
$wpdb->query($sql);
return $wpdb->insert_id;
}
/**
* Update the existing enrollment with an SQL Update
*/
function update_enrollment($enrollmentId, $data) {
global $wpdb;
//$wpdb->show_errors();
foreach($data as $key => $value) {
if(needs_encryption($key)) {
$value = db_aes_encrypt($value);
} else {
$value = db_normal_field($value);
}
$values[] = "`".$key."` = ".$value;
}
$sql = "UPDATE enrollment set ".implode($values,", ")." where id = ".$enrollmentId;
error_log ( $sql);
$wpdb->query($sql);
}
function get_enrollment_post_data() {
$data = array();
$fields = explode(",",$_POST['fieldNames']);
foreach($fields as $field) {
/** We don't want to insert/update the enrollment Id **/
if($field!='enrollmentId')
$data[$field] = $_POST[$field];
}
return $data;
}
function process_enrollment_form() {
global $wpdb;
$enrollmentId = intval($_POST['enrollmentId']);
$step = $_POST['step'];
$data = get_enrollment_post_data();
$data['step'] = $step;
$uid = retrieve_enrollment_uid($enrollmentId);
if($uid) {
update_enrollment($enrollmentId, $data);
} else {
create_enrollment($enrollmentId, $data);
}
$qry = "update enrollment set last_visit=NOW() where id=" . $enrollmentId;
$wpdb->get_results($qry);
/* Update the session */
if($_POST['next_step']) {
$_SESSION['enrollment_step'] = $_POST['next_step'];
}
if($enrollmentId) {
$_SESSION['enrollmentId'] = $enrollmentId;
}
}
/**
* Uploads the enrollment doc to Amazon S3. It encrypts the file with AES256 encryption
* and stores the key for the file in the enrollment_doc table
* @param $enrollmentId
* @return int Returns the number of files it uploaded
*/
function upload_enrollment_doc_s3($enrollmentId) {
global $wpdb;
$client = S3Client::factory(array(
'key' => S3_ACCESS_KEY_ID,
'secret' => S3_SECRET_KEY
));
$count = 0;
if(isset($_REQUEST['type'])) {
$source = $_FILES["webcam"]["tmp_name"];
if(!$source) {
continue;
}
$name = $_FILES["webcam"]["tmp_name"].'.jpg';
/* Create a random 32 character key */
$key = wp_generate_password(32, false);
$result = $client->putObject(array(
'Bucket' => S3_BUCKET,
'Key' => $key,
'SourceFile' => $source,
'ServerSideEncryption' => 'AES256'
));
$wpdb->insert(
'enrollment_doc',
array(
'enrollment_id' => $enrollmentId,
'name' => $name,
'url' => $result['ObjectURL'],
'key' => $key
),
array(
'%d',
'%s',
'%s',
'%s'
)
);
$count++;
} else {
try {
foreach ($_FILES["documents"]["error"] as $key => $error) {
$source = $_FILES["documents"]["tmp_name"][$key];
if(!$source) {
continue;
}
$name = $_FILES["documents"]["name"][$key];
/* Create a random 32 character key */
$key = wp_generate_password(32, false);
$result = $client->putObject(array(
'Bucket' => S3_BUCKET,
'Key' => $key,
'SourceFile' => $source,
'ServerSideEncryption' => 'AES256'
));
$wpdb->insert(
'enrollment_doc',
array(
'enrollment_id' => $enrollmentId,
'name' => $name,
'url' => $result['ObjectURL'],
'key' => $key
),
array(
'%d',
'%s',
'%s',
'%s'
)
);
$count++;
}
} catch (Exception $e) {
echo $e->getMessage();
}
}
return $count;
}
add_action('wp_ajax_enrollment_form', 'process_enrollment_form');
add_action('wp_ajax_nopriv_enrollment_form', 'process_enrollment_form');
?>