|
| 1 | +<?php |
| 2 | +use SVBX\Export; |
| 3 | + |
| 4 | +require 'vendor/autoload.php'; |
| 5 | +require 'session.php'; |
| 6 | + |
| 7 | +if (strcasecmp($_SERVER['REQUEST_METHOD'], 'POST')) { |
| 8 | + header('Access-Control-Allow-Methods: POST', true, 405); |
| 9 | + exit; |
| 10 | +} |
| 11 | + |
| 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 | + } |
| 28 | + |
| 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); |
| 34 | + |
| 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); |
| 39 | + |
| 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 |
| 41 | + |
| 42 | + if (($idIndex = array_search('ID', $postKeys)) !== false) unset($postKeys[$idIndex]); // don't try to match ID col name |
| 43 | + |
| 44 | + foreach ($postKeys as $key) { |
| 45 | + if (array_search(strtolower($key), $cols) === false) { |
| 46 | + header('Status: 400 Bad Request', true, 400); |
| 47 | + exit; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 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