Commit 76142bdf3f579bb1c8b9361617a05e55446a46d8

Authored by Nathanael Jourdane
1 parent 76d60878

epntap.php: Use only one function to get all GET parameters

Showing 1 changed file with 18 additions and 12 deletions   Show diff stats
php/epntap.php
@@ -24,12 +24,16 @@ switch ($action) { @@ -24,12 +24,16 @@ switch ($action) {
24 } 24 }
25 echo json_encode($response); 25 echo json_encode($response);
26 26
27 -function getParam($paramName, $default=null) {  
28 - if(array_key_exists($paramName, $_GET)) {  
29 - return filter_var($_GET[$paramName], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);  
30 - } else {  
31 - return $default; 27 +function getParams($paramNames, $default=null) {
  28 + $params = [];
  29 + foreach($paramNames as $paramName) {
  30 + if(array_key_exists($paramName, $_GET)) {
  31 + $params[$paramName] = filter_var($_GET[$paramName], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
  32 + } else {
  33 + $params[$paramName] = $default;
  34 + }
32 } 35 }
  36 + return $params;
33 } 37 }
34 38
35 function resolver() { 39 function resolver() {
@@ -117,8 +121,9 @@ function getServices() { @@ -117,8 +121,9 @@ function getServices() {
117 } 121 }
118 122
119 function getNbResults() { 123 function getNbResults() {
120 - $query = "SELECT COUNT(*) AS nb_rows FROM " . getParam('tableName') . createFilter(getParam('targetNames'), getParam('productTypes'), getParam('timeMin'), getParam('timeMax'));  
121 - $response = request(getParam('url'), $query); 124 + $p = getParams(['url', 'tableName', 'targetNames', 'productTypes', 'timeMin', 'timeMax']);
  125 + $query = "SELECT COUNT(*) AS nb_rows FROM " . $p['tableName'] . createFilter($p['targetNames'], $p['productTypes'], $p['timeMin'], $p['timeMax']);
  126 + $response = request($p['url'], $query);
122 if($response['success']) { 127 if($response['success']) {
123 $response['success'] = false; 128 $response['success'] = false;
124 $response['msg'] = 'The service returned a bad value, can not get the number of results.'; 129 $response['msg'] = 'The service returned a bad value, can not get the number of results.';
@@ -144,11 +149,12 @@ function getNbResults() { @@ -144,11 +149,12 @@ function getNbResults() {
144 } 149 }
145 150
146 function getGranules() { 151 function getGranules() {
147 - $sort = is_null(getParam('sort')) ? 'granule_uid ASC' : (getParam('sort') . ' ' . getParam('dir'));  
148 - $filter = createFilter(getParam('targetNames'), getParam('productTypes'), getParam('timeMin'), getParam('timeMax'));  
149 - $query = "SELECT TOP " . getParam('limit') . " * FROM " . getParam('tableName') . " $filter ORDER BY $sort OFFSET " . getParam('start'); 152 + $p = getParams(['url', 'tableName', 'sort', 'dir', 'targetNames', 'productTypes', 'timeMin', 'timeMax', 'start', 'limit', 'nbRes']);
  153 + $sort = is_null($p['sort']) ? 'granule_uid ASC' : ($p['sort'] . ' ' . $p['dir']);
  154 + $filter = createFilter($p['targetNames'], $p['productTypes'], $p['timeMin'], $p['timeMax']);
  155 + $query = "SELECT TOP " . $p['limit'] . " * FROM " . $p['tableName'] . " $filter ORDER BY $sort OFFSET " . $p['start'];
150 // error_log('getGranules query: ' . $query); 156 // error_log('getGranules query: ' . $query);
151 - $response = request(getParam('url'), $query); 157 + $response = request($p['url'], $query);
152 if($response['success']) { 158 if($response['success']) {
153 $visibleColumns = ['granule_gid', 'obs_id', 'dataproduct_type', 'time_min', 'time_max', 'instrument_name', 'processing_level', 'access_estsize', 'thumbnail_url', 'access_url']; // rest are hidden 159 $visibleColumns = ['granule_gid', 'obs_id', 'dataproduct_type', 'time_min', 'time_max', 'instrument_name', 'processing_level', 'access_estsize', 'thumbnail_url', 'access_url']; // rest are hidden
154 $names = ['granule_gid' => 'GID', 'dataproduct_type' => 'Type', 'processing_level' => 'Proc. lvl', 'access_estsize' => 'Size', 'access_url' => 'URL']; // default: pretty printed key name 160 $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() { @@ -171,7 +177,7 @@ function getGranules() {
171 ]; 177 ];
172 } 178 }
173 179
174 - $response['total'] = (int)getParam('nbRes'); 180 + $response['total'] = (int)$p['nbRes'];
175 $response['metaData']['fields'] = $fields; 181 $response['metaData']['fields'] = $fields;
176 $response['metaData']['columns'] = $columns; 182 $response['metaData']['columns'] = $columns;
177 $response['metaData']['metaHash'] = md5(serialize($response['metaData'])); 183 $response['metaData']['metaHash'] = md5(serialize($response['metaData']));