diff --git a/config.sample.php b/config.sample.php index 099170c..9755b41 100644 --- a/config.sample.php +++ b/config.sample.php @@ -10,10 +10,11 @@ 'server' => 'localhost', 'port' => 3306, 'type' => 'mysql', - 'table_blacklist' => array(), - 'column_blacklist' => array(), + 'table_blocklist' => array(), + 'column_blocklist' => array(), + 'table_allowlist' => array(), ); register_db_api( 'dataset-name', $args ); -*/ \ No newline at end of file +*/ diff --git a/includes/.DS_Store b/includes/.DS_Store deleted file mode 100644 index 5008ddf..0000000 Binary files a/includes/.DS_Store and /dev/null differ diff --git a/includes/class.db-api.php b/includes/class.db-api.php index f88d4de..3bbac19 100644 --- a/includes/class.db-api.php +++ b/includes/class.db-api.php @@ -10,6 +10,7 @@ class DB_API { public $ttl = 3600; public $cache = array(); public $connections = array(); + public $format = 'json'; function __construct() { @@ -40,8 +41,9 @@ function register_db( $name = null, $args = array() ) { 'server' => 'localhost', 'port' => 3306, 'type' => 'mysql', - 'table_blacklist' => array(), - 'column_blacklist' => array(), + 'table_blocklist' => array(), + 'table_allowlist' => array(), + 'column_blocklist' => array(), 'ttl' => $this->ttl, ); @@ -166,9 +168,13 @@ function parse_query( $query = null ) { $db = $this->get_db( $parts['db'] ); - if ( in_array( $parts['table'], $db->table_blacklist ) ) { + if ( in_array( $parts['table'], $db->table_blocklist ) ) { $this->error( 'Invalid table', 404 ); } + + if ( sizeof($db->table_allowlist)>0 && !in_array( $parts['table'], $db->table_allowlist ) ) { + $this->error( 'Invalid method: '.$parts['table'], 404 ); + } if ( !in_array( $parts['direction'], array( 'ASC', 'DESC' ) ) ) { $parts['direction'] = null; @@ -177,7 +183,7 @@ function parse_query( $query = null ) { if ( !in_array( $parts['format'], array( 'html', 'xml', 'json' ) ) ) { $parts['format'] = null; } - + return $parts; } @@ -335,6 +341,8 @@ function get_first_column( $table, $db = null ) { * @return array an array of results */ function query( $query, $db = null ) { + + $this->format = $query['format']; $key = md5( serialize( $query ) . $this->get_db( $db )->name ); @@ -400,18 +408,18 @@ function query( $query, $db = null ) { } /** - * Remove any blacklisted columns from the data set. + * Remove any blocklisted columns from the data set. */ function sanitize_results( $results, $db = null ) { $db = $this->get_db( $db ); - if ( empty( $db->column_blacklist ) ) { + if ( empty( $db->column_blocklist ) ) { return $results; } foreach ( $results as $ID => $result ) { - foreach ( $db->column_blacklist as $column ) { + foreach ( $db->column_blocklist as $column ) { unset( $results[ $ID ] -> $column ); } @@ -427,11 +435,16 @@ function sanitize_results( $results, $db = null ) { * @param int $code (optional) the error code with which to respond */ function error( $error, $code = '500' ) { - - if ( is_object( $error ) && method_exists( $error, 'get_message' ) ) { - $error = $error->get_message(); - } - + + if ( is_object( $error ) && method_exists( $error, 'get_message' ) ) { + $error = $error->get_message(); + } + + if('json'==$this->format) { + $this->render_json(['error'=>$error], []); + exit; + } + http_response_code( $code ); die( $error ); return false; @@ -443,6 +456,9 @@ function error( $error, $code = '500' ) { * @todo Support JSONP, with callback filtering. */ function render_json( $data, $query ) { + if(false === $data) { + $data = ['error'=>"No data found"]; + } header('Content-type: application/json'); $output = json_encode( $data ); @@ -480,9 +496,9 @@ function jsonp_callback_filter( $callback ) { */ function render_html( $data ) { - require_once( dirname( __FILE__ ) . '/bootstrap/header.html' ); + require_once( dirname( __FILE__ ) . '/bootstrap/header.html' ); - //err out if no results + //err out if no results if ( empty( $data ) ) { $this->error( 'No results found', 404 ); return; @@ -495,29 +511,29 @@ function render_html( $data ) { echo "
$heading | \n"; + echo "\t$heading | \n"; } echo "
---|---|
$cell | \n"; - - } - - echo "|
$cell | \n"; + + } + + echo "