Skip to content
This repository was archived by the owner on Nov 24, 2022. It is now read-only.

Commit 16bb894

Browse files
committed
catch exceptions in new def endpoint
1 parent 256748b commit 16bb894

File tree

1 file changed

+45
-38
lines changed

1 file changed

+45
-38
lines changed

api/def.php

+45-38
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,55 @@
99
exit;
1010
}
1111

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+
}
2728

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);
3334

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);
3839

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
4041

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
4243

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+
}
4749
}
48-
}
4950

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

Comments
 (0)