From 76142bdf3f579bb1c8b9361617a05e55446a46d8 Mon Sep 17 00:00:00 2001 From: Nathanael Jourdane Date: Thu, 14 Sep 2017 18:49:32 +0200 Subject: [PATCH] epntap.php: Use only one function to get all GET parameters --- php/epntap.php | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/php/epntap.php b/php/epntap.php index b3d3b26..14ba03b 100644 --- a/php/epntap.php +++ b/php/epntap.php @@ -24,12 +24,16 @@ switch ($action) { } echo json_encode($response); -function getParam($paramName, $default=null) { - if(array_key_exists($paramName, $_GET)) { - return filter_var($_GET[$paramName], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); - } else { - return $default; +function getParams($paramNames, $default=null) { + $params = []; + foreach($paramNames as $paramName) { + if(array_key_exists($paramName, $_GET)) { + $params[$paramName] = filter_var($_GET[$paramName], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); + } else { + $params[$paramName] = $default; + } } + return $params; } function resolver() { @@ -117,8 +121,9 @@ function getServices() { } function getNbResults() { - $query = "SELECT COUNT(*) AS nb_rows FROM " . getParam('tableName') . createFilter(getParam('targetNames'), getParam('productTypes'), getParam('timeMin'), getParam('timeMax')); - $response = request(getParam('url'), $query); + $p = getParams(['url', 'tableName', 'targetNames', 'productTypes', 'timeMin', 'timeMax']); + $query = "SELECT COUNT(*) AS nb_rows FROM " . $p['tableName'] . createFilter($p['targetNames'], $p['productTypes'], $p['timeMin'], $p['timeMax']); + $response = request($p['url'], $query); if($response['success']) { $response['success'] = false; $response['msg'] = 'The service returned a bad value, can not get the number of results.'; @@ -144,11 +149,12 @@ function getNbResults() { } function getGranules() { - $sort = is_null(getParam('sort')) ? 'granule_uid ASC' : (getParam('sort') . ' ' . getParam('dir')); - $filter = createFilter(getParam('targetNames'), getParam('productTypes'), getParam('timeMin'), getParam('timeMax')); - $query = "SELECT TOP " . getParam('limit') . " * FROM " . getParam('tableName') . " $filter ORDER BY $sort OFFSET " . getParam('start'); + $p = getParams(['url', 'tableName', 'sort', 'dir', 'targetNames', 'productTypes', 'timeMin', 'timeMax', 'start', 'limit', 'nbRes']); + $sort = is_null($p['sort']) ? 'granule_uid ASC' : ($p['sort'] . ' ' . $p['dir']); + $filter = createFilter($p['targetNames'], $p['productTypes'], $p['timeMin'], $p['timeMax']); + $query = "SELECT TOP " . $p['limit'] . " * FROM " . $p['tableName'] . " $filter ORDER BY $sort OFFSET " . $p['start']; // error_log('getGranules query: ' . $query); - $response = request(getParam('url'), $query); + $response = request($p['url'], $query); if($response['success']) { $visibleColumns = ['granule_gid', 'obs_id', 'dataproduct_type', 'time_min', 'time_max', 'instrument_name', 'processing_level', 'access_estsize', 'thumbnail_url', 'access_url']; // rest are hidden $names = ['granule_gid' => 'GID', 'dataproduct_type' => 'Type', 'processing_level' => 'Proc. lvl', 'access_estsize' => 'Size', 'access_url' => 'URL']; // default: pretty printed key name @@ -171,7 +177,7 @@ function getGranules() { ]; } - $response['total'] = (int)getParam('nbRes'); + $response['total'] = (int)$p['nbRes']; $response['metaData']['fields'] = $fields; $response['metaData']['columns'] = $columns; $response['metaData']['metaHash'] = md5(serialize($response['metaData'])); -- libgit2 0.21.2