diff --git a/application/controllers/Api.php b/application/controllers/Api.php index 8eb0e72ee..958be0d78 100644 --- a/application/controllers/Api.php +++ b/application/controllers/Api.php @@ -237,6 +237,106 @@ class API extends CI_Controller { } + /* + * + * Function: get_contacts_adif + * Task: allows third party software to pull ADIF QSO data from wavelog after a baseline of the last fetched QSO id + */ + function get_contacts_adif() { + + //set header + header('Content-type: application/json'); + + //load API model + $this->load->model('api_model'); + + // Decode JSON and store + $obj = json_decode(file_get_contents("php://input"), true); + if ($obj === NULL) { + http_response_code(400); + echo json_encode(['status' => 'failed', 'reason' => "wrong JSON"]); + return; + } + + //do authorization + if(!isset($obj['key']) || $this->api_model->authorize($obj['key']) == 0) { + http_response_code(401); + echo json_encode(['status' => 'failed', 'reason' => "missing api key"]); + return; + } + + //check for relevant fields in JSON input + if(!isset($obj['station_id']) or !isset($obj['fetchfromid'])) + { + http_response_code(400); + echo json_encode(['status' => 'failed', 'reason' => "Not all required fields were present in input JSON"]); + return; + } + + //extract relevant data to variables + $key = $obj['key']; + $station_id = $obj['station_id']; + $fetchfromid = $obj['fetchfromid']; + + //check if goalpost is numeric as an additional layer of SQL injection prevention + if(!is_numeric($fetchfromid)) + { + http_response_code(400); + echo json_encode(['status' => 'failed', 'reason' => "Invalid fetchfromid."]); + return; + } + + //make sure the goalpost is an integer + $fetchfromid = (int)$fetchfromid; + + //load stations API + $this->load->model('stations'); + + //get all stations of user to check if station_id should be readable + $userid = $this->api_model->key_userid($key); + $station_ids = array(); + $stations=$this->stations->all_of_user($userid); + + //extract to array + foreach ($stations->result() as $row) { + array_push($station_ids, $row->station_id); + } + + //return error if station not accessible for the API key + 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; + } + + //load adif data module + $this->load->model('adif_data'); + $data['qsos'] = $this->adif_data->export_past_id($station_id, $fetchfromid); + $qso_count = count($data['qsos']->result()); + + //if no new QSOs are ready, return that + 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]); + } + + //convert data to ADIF + $adif_content = $this->load->view('adif/data/exportapi', $data, TRUE); + + //get new goalpost + $lastfetchedid = 0; + foreach ($data['qsos']->result() as $row) { + $lastfetchedid = max($lastfetchedid, $row->COL_PRIMARY_KEY); + } + + //return API result + http_response_code(200); + echo json_encode(['status' => 'successfull', 'message' => 'Export successfull', 'lastfetchedid' => $lastfetchedid, 'exported_qsos' => $qso_count, 'adif' => $adif_content]); + } + + // API function to check if a callsign is in the logbook already function logbook_check_callsign() { header('Content-type: application/json'); diff --git a/application/models/Adif_data.php b/application/models/Adif_data.php index edfb96fbd..f4a6694e4 100644 --- a/application/models/Adif_data.php +++ b/application/models/Adif_data.php @@ -137,6 +137,20 @@ class adif_data extends CI_Model { return $this->db->get(); } + function export_past_id($station_id, $fetchfromid) { + //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')); + $this->db->where($this->config->item('table_name').'.station_id', $station_id); + $this->db->where($this->config->item('table_name').".COL_PRIMARY_KEY > " , $fetchfromid); //only get values past the fetchfromid value + $this->db->order_by($this->config->item('table_name').".COL_TIME_ON", "ASC"); + $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'); + + //return result + return $this->db->get(); + } + function export_lotw() { $this->load->model('stations'); $active_station_id = $this->stations->find_active(); diff --git a/application/views/adif/data/exportapi.php b/application/views/adif/data/exportapi.php new file mode 100755 index 000000000..3f44b6bbc --- /dev/null +++ b/application/views/adif/data/exportapi.php @@ -0,0 +1,13 @@ +Wavelog ADIF export +3.1.4 +config->item('app_name')); ?>>config->item('app_name')."\r\n"; ?> +optionslib->get_option('version')); ?>>optionslib->get_option('version')."\r\n"; ?> + + +load->library('AdifHelper'); + +foreach ($qsos->result() as $qso) { + echo $CI->adifhelper->getAdifLine($qso); +} diff --git a/assets/lang_src/messages.pot b/assets/lang_src/messages.pot index 0c6e18756..c30bfee6d 100644 --- a/assets/lang_src/messages.pot +++ b/assets/lang_src/messages.pot @@ -6,7 +6,7 @@ msgid "" msgstr "" "Report-Msgid-Bugs-To: translations@wavelog.org\n" -"POT-Creation-Date: 2024-07-30 21:59+0000\n" +"POT-Creation-Date: 2024-07-31 04:20+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" diff --git a/install/includes/gettext/lang_src/installer.pot b/install/includes/gettext/lang_src/installer.pot index d64fa0e0f..632504a32 100644 --- a/install/includes/gettext/lang_src/installer.pot +++ b/install/includes/gettext/lang_src/installer.pot @@ -6,7 +6,7 @@ msgid "" msgstr "" "Report-Msgid-Bugs-To: translations@wavelog.org\n" -"POT-Creation-Date: 2024-07-30 21:59+0000\n" +"POT-Creation-Date: 2024-07-31 04:20+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n"