From 50789da43bbb761c53e7a35aba68fcde359c379a Mon Sep 17 00:00:00 2001 From: HB9HIL Date: Tue, 30 Apr 2024 23:35:05 +0200 Subject: [PATCH] api key test button in station location --- application/controllers/Qrz.php | 39 ++++++++++++++++++++ application/views/station_profile/create.php | 6 ++- application/views/station_profile/edit.php | 6 ++- assets/js/sections/station_locations.js | 35 ++++++++++++++++++ 4 files changed, 84 insertions(+), 2 deletions(-) diff --git a/application/controllers/Qrz.php b/application/controllers/Qrz.php index d6725a332..cb7bb3718 100644 --- a/application/controllers/Qrz.php +++ b/application/controllers/Qrz.php @@ -21,6 +21,45 @@ class Qrz extends CI_Controller { $this->config->load('config'); } + /* + * API Key Status Test + */ + + public function qrz_apitest() { + $apikey = xss_clean($this->input->post('APIKEY')); + $url = 'http://logbook.qrz.com/api'; // TODO: Move this to database + + $post_data['KEY'] = $apikey; + $post_data['ACTION'] = 'STATUS'; + + $ch = curl_init( $url ); + curl_setopt( $ch, CURLOPT_POST, true); + curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_data); + curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1); + curl_setopt( $ch, CURLOPT_HEADER, 0); + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true); + + $content = curl_exec($ch); + curl_close($ch); + + if ($content){ + if (stristr($content,'RESULT=OK')) { + $result['status'] = 'OK'; + $result['message'] = $content; + } + else { + $result['status'] = 'Failed'; + $result['message'] = $content; + } + } + if(curl_errno($ch)){ + $result['status'] = 'error'; + $result['message'] = 'Curl error: '. curl_errno($ch); + } + header('Content-Type: application/json'); + echo json_encode($result); + } + /* * Upload QSO to QRZ.com * When called from the url wavelog/qrz/upload, the function loops through all station_id's with a qrz api key defined. diff --git a/application/views/station_profile/create.php b/application/views/station_profile/create.php index 799fefc28..58e531b10 100644 --- a/application/views/station_profile/create.php +++ b/application/views/station_profile/create.php @@ -227,7 +227,11 @@
- +
+ + +
+
diff --git a/application/views/station_profile/edit.php b/application/views/station_profile/edit.php index f832ccc1e..7667a59cf 100644 --- a/application/views/station_profile/edit.php +++ b/application/views/station_profile/edit.php @@ -298,7 +298,11 @@
- qrzapikey; } ?>"> +
+ qrzapikey; } ?>"> + +
+
diff --git a/assets/js/sections/station_locations.js b/assets/js/sections/station_locations.js index 8fbd37d6b..43268951d 100644 --- a/assets/js/sections/station_locations.js +++ b/assets/js/sections/station_locations.js @@ -12,6 +12,41 @@ $(document).ready(function () { $("#dxcc_id").change(function () { updateStateDropdown('#dxcc_id', '#stateInputLabel', '#location_us_county', '#stationCntyInputEdit'); }); + + $('#qrz_apitest_btn').click(function(){ + + var apikey = $('#qrzApiKey').val(); + var msg_div = $('#qrz_apitest_msg'); + + msg_div.hide(); + msg_div.removeClass('alert-success alert-danger') + + $.ajax({ + url: base_url+'index.php/qrz/qrz_apitest', + type: 'POST', + data: { + 'APIKEY': apikey + }, + success: function(res) { + if(res.status == 'OK') { + msg_div.addClass('alert-success'); + msg_div.text('Your API Key works. You are good to go!'); + msg_div.show(); + } else { + msg_div.addClass('alert-danger'); + msg_div.text('Your API Key failed. Are you sure you have a valid QRZ subsription?'); + msg_div.show(); + $('#qrzrealtime').val(-1); + } + }, + error: function(res) { + msg_div.addClass('alert-danger'); + msg_div.text('ERROR: Something went wrong on serverside. We\'re sorry..'); + msg_div.show(); + }, + }); + }); + } });