From 6486294b307e86f7ca59963abd53b078f1e16a0d Mon Sep 17 00:00:00 2001 From: int2001 Date: Wed, 18 Sep 2024 09:04:05 +0000 Subject: [PATCH] Adds an optional "limit" to the "get_contacts_adif"-API --- application/controllers/Api.php | 12 +++++++----- application/models/Adif_data.php | 6 +++++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/application/controllers/Api.php b/application/controllers/Api.php index f0ba2f1b5..c7e53f9a4 100644 --- a/application/controllers/Api.php +++ b/application/controllers/Api.php @@ -279,6 +279,10 @@ class API extends CI_Controller { $key = $obj['key']; $station_id = $obj['station_id']; $fetchfromid = $obj['fetchfromid']; + $limit = -1; + if ( (array_key_exists('limit',$obj)) && (is_numeric($obj['limit']*1)) ) { + $limit = $obj['limit']; + } //check if goalpost is numeric as an additional layer of SQL injection prevention if(!is_numeric($fetchfromid)) @@ -305,8 +309,7 @@ class API extends CI_Controller { } //return error if station not accessible for the API key - if(!in_array($station_id, $station_ids)) - { + if(!in_array($station_id, $station_ids)) { http_response_code(401); echo json_encode(['status' => 'failed', 'reason' => "Station ID not accessible for this API key"]); return; @@ -316,15 +319,14 @@ class API extends CI_Controller { $this->load->model('adif_data'); //get qso data - $data['qsos'] = $this->adif_data->export_past_id($station_id, $fetchfromid); + $data['qsos'] = $this->adif_data->export_past_id($station_id, $fetchfromid, $limit); //set internalonly attribute for adif creation $data['internalrender'] = true; //if no new QSOs are ready, return that $qso_count = count($data['qsos']->result()); - if($qso_count <= 0) - { + if($qso_count <= 0) { http_response_code(200); echo json_encode(['status' => 'successfull', 'message' => 'No new QSOs available.', 'lastfetchedid' => $fetchfromid, 'exported_qsos' => 0, 'adif' => null]); return; diff --git a/application/models/Adif_data.php b/application/models/Adif_data.php index 399f1ed24..decdcd199 100644 --- a/application/models/Adif_data.php +++ b/application/models/Adif_data.php @@ -151,7 +151,7 @@ class adif_data extends CI_Model { } } - function export_past_id($station_id, $fetchfromid) { + function export_past_id($station_id, $fetchfromid, $limit) { //create query $this->db->select(''.$this->config->item('table_name').'.*, station_profile.*, dxcc_entities.name as station_country'); $this->db->from($this->config->item('table_name')); @@ -161,6 +161,10 @@ class adif_data extends CI_Model { $this->db->join('station_profile', 'station_profile.station_id = '.$this->config->item('table_name').'.station_id'); $this->db->join('dxcc_entities', 'station_profile.station_dxcc = dxcc_entities.adif', 'left outer'); + if ($limit > -1) { + $this->db->limit($limit); + } + //return result return $this->db->get(); }