|
9 | 9 | exit;
|
10 | 10 | }
|
11 | 11 |
|
12 |
| -// check Session vars against DB |
13 |
| -$link = new MySqliDB(DB_CREDENTIALS); |
14 |
| -$fields = [ 'username', 'userID', 'firstname', 'lastname', 'role' ]; |
15 |
| - |
16 |
| -$link->where('userID', $_SESSION['userID']); |
17 |
| -$result = $link->getOne('users_enc', $fields); |
18 |
| - |
19 |
| -if ($result['username'] !== $_SESSION['username'] |
20 |
| - || $result['role'] !== $_SESSION['role'] |
21 |
| - || $result['firstname'] !== $_SESSION['firstname'] |
22 |
| - || $result['lastname'] !== $_SESSION['lastname']) |
23 |
| -{ |
24 |
| - header('Status: 403 Forbidden', true, 403); |
25 |
| - exit; |
26 |
| -} |
| 12 | +try { |
| 13 | + // check Session vars against DB |
| 14 | + $link = new MySqliDB(DB_CREDENTIALS); |
| 15 | + $fields = [ 'username', 'userID', 'firstname', 'lastname', 'role' ]; |
| 16 | + |
| 17 | + $link->where('userID', $_SESSION['userID']); |
| 18 | + $result = $link->getOne('users_enc', $fields); |
| 19 | + |
| 20 | + if ($result['username'] !== $_SESSION['username'] |
| 21 | + || $result['role'] !== $_SESSION['role'] |
| 22 | + || $result['firstname'] !== $_SESSION['firstname'] |
| 23 | + || $result['lastname'] !== $_SESSION['lastname']) |
| 24 | + { |
| 25 | + header('Status: 403 Forbidden', true, 403); |
| 26 | + exit; |
| 27 | + } |
27 | 28 |
|
28 |
| -// if Auth ok, validate fields on first data element of POST against fields in DB |
29 |
| -// note: element at index 0 is heading names, not table data |
30 |
| -$post = trim(file_get_contents('php://input')); |
31 |
| -$post = json_decode($post, true); |
32 |
| -$post = filter_var_array($post, FILTER_SANITIZE_SPECIAL_CHARS); |
| 29 | + // if Auth ok, validate fields on first data element of POST against fields in DB |
| 30 | + // note: element at index 0 is heading names, not table data |
| 31 | + $post = trim(file_get_contents('php://input')); |
| 32 | + $post = json_decode($post, true); |
| 33 | + $post = filter_var_array($post, FILTER_SANITIZE_SPECIAL_CHARS); |
33 | 34 |
|
34 |
| -$link->where('table_name', 'CDL'); |
35 |
| -$link->orWhere('table_name', 'BARTDL'); |
36 |
| -$cols = $link->getValue('information_schema.columns', 'column_name', null); // returns 50+ columns |
37 |
| -$cols = array_map('strtolower', $cols); |
| 35 | + $link->where('table_name', 'CDL'); |
| 36 | + $link->orWhere('table_name', 'BARTDL'); |
| 37 | + $cols = $link->getValue('information_schema.columns', 'column_name', null); // returns 50+ columns |
| 38 | + $cols = array_map('strtolower', $cols); |
38 | 39 |
|
39 |
| -$postKeys = array_keys($post[1] + $post[count($post) - 1] + $post[floor((count($post) / 2))]); // grab keys from first, middle, and last element of post data |
| 40 | + $postKeys = array_keys($post[1] + $post[count($post) - 1] + $post[floor((count($post) / 2))]); // grab keys from first, middle, and last element of post data |
40 | 41 |
|
41 |
| -if (($idIndex = array_search('ID', $postKeys)) !== false) unset($postKeys[$idIndex]); // don't try to match ID col name |
| 42 | + if (($idIndex = array_search('ID', $postKeys)) !== false) unset($postKeys[$idIndex]); // don't try to match ID col name |
42 | 43 |
|
43 |
| -foreach ($postKeys as $key) { |
44 |
| - if (array_search(strtolower($key), $cols) === false) { |
45 |
| - header('Status: 400 Bad Request', true, 400); |
46 |
| - exit; |
| 44 | + foreach ($postKeys as $key) { |
| 45 | + if (array_search(strtolower($key), $cols) === false) { |
| 46 | + header('Status: 400 Bad Request', true, 400); |
| 47 | + exit; |
| 48 | + } |
47 | 49 | }
|
48 |
| -} |
49 | 50 |
|
50 |
| -header('Content-Type: text/csv', true); |
51 |
| - |
52 |
| -echo Export::csv($post); |
53 |
| - |
54 |
| -if (is_a($link, 'MySqliDB')) $link->disconnect(); |
55 |
| - |
56 |
| -exit; |
| 51 | + header('Content-Type: text/csv', true); |
| 52 | + |
| 53 | + echo Export::csv($post); |
| 54 | +} catch (\Exception $e) { |
| 55 | + error_log($e); |
| 56 | + header('500 Internal server error', true, 500); |
| 57 | +} catch (\Error $e) { |
| 58 | + error_log($e); |
| 59 | + header('500 Internal server error', true, 500); |
| 60 | +} finally { |
| 61 | + if (is_a($link, 'MySqliDB')) $link->disconnect(); |
| 62 | + exit; |
| 63 | +} |
0 commit comments