From 8c4d7b6e38a09457ffd47c0ab9e933f65195e7bf Mon Sep 17 00:00:00 2001 From: int2001 Date: Thu, 19 Dec 2024 06:44:55 +0000 Subject: [PATCH 01/11] Added check-routine for LoTW-Creds (Backendpart) --- application/controllers/Lotw.php | 56 ++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/application/controllers/Lotw.php b/application/controllers/Lotw.php index b16f8edb6..3df1beb2b 100644 --- a/application/controllers/Lotw.php +++ b/application/controllers/Lotw.php @@ -723,6 +723,62 @@ class Lotw extends CI_Controller { } } + public function check_lotw_credentials () { + $this->load->model('user_model'); + if(!$this->user_model->authorize(2)) { + $this->session->set_flashdata('error', __("You're not allowed to do that!")); + redirect('dashboard'); + exit(); + } + $ret=[]; + + $this->load->model('logbook_model'); + $query = $this->user_model->get_by_id($this->session->userdata('user_id')); + $q = $query->row(); + $data['user_lotw_name'] = urlencode($q->user_lotw_name ?? ''); + $data['user_lotw_password'] = urlencode($q->user_lotw_password ?? ''); + + // Get URL for downloading LoTW + $query = $query = $this->db->query('SELECT lotw_login_url FROM config'); + $q = $query->row(); + $lotw_url = $q->lotw_login_url; + + // Validate that LoTW credentials are not empty + // TODO: We don't actually see the error message + if ($data['user_lotw_name'] == '' || $data['user_lotw_password'] == '') { + $ret='No Creds set'; + } + + // Build URL for LoTW report file + $lotw_url .= "?"; + $lotw_url .= "login=" . $data['user_lotw_name']; + $lotw_url .= "&password=" . $data['user_lotw_password']; + + log_message("Error",$lotw_url); + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $lotw_url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); + $content = curl_exec($ch); + if ($content) { + if(curl_errno($ch)) { + $ret['status']='failed'; + $ret['details']== __("LoTW check failed for user ").$data['user_lotw_name'].": ".curl_strerror(curl_errno($ch))." (".curl_errno($ch).")."; + } else if (str_contains($content,"Username/password incorrect")) { + $ret['status']='failed_wrong_creds'; + $ret['details']= __("LoTW check failed for user ").$data['user_lotw_name'].__(": Username/password incorrect"); + } else { + $ret['status']='OK'; + } + } else { + $ret['status']='failed_na'; + $ret['details']= __("LoTW not available at present"); + } + header("Content-type: application/json"); + echo json_encode($ret); + return $ret; + } + public function import() { // Is only called via frontend. Cron uses "upload". within download the download is called $this->load->model('user_model'); $this->load->model('Stations'); From 921193517440d241cae22a0374a6ff426157cbc1 Mon Sep 17 00:00:00 2001 From: int2001 Date: Thu, 19 Dec 2024 06:45:31 +0000 Subject: [PATCH 02/11] Remove Debug --- application/controllers/Lotw.php | 1 - 1 file changed, 1 deletion(-) diff --git a/application/controllers/Lotw.php b/application/controllers/Lotw.php index 3df1beb2b..6f8f774b7 100644 --- a/application/controllers/Lotw.php +++ b/application/controllers/Lotw.php @@ -754,7 +754,6 @@ class Lotw extends CI_Controller { $lotw_url .= "login=" . $data['user_lotw_name']; $lotw_url .= "&password=" . $data['user_lotw_password']; - log_message("Error",$lotw_url); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $lotw_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); From fb555ecdefe874dcc73e92d866061cb910e1a861 Mon Sep 17 00:00:00 2001 From: int2001 Date: Thu, 19 Dec 2024 07:49:00 +0000 Subject: [PATCH 03/11] AJAXing (PHP) --- application/controllers/Lotw.php | 91 +++++++++++++++++++++----------- application/views/user/edit.php | 5 +- 2 files changed, 62 insertions(+), 34 deletions(-) diff --git a/application/controllers/Lotw.php b/application/controllers/Lotw.php index 6f8f774b7..1078b8e8f 100644 --- a/application/controllers/Lotw.php +++ b/application/controllers/Lotw.php @@ -731,47 +731,74 @@ class Lotw extends CI_Controller { exit(); } $ret=[]; + $ret['status']=''; - $this->load->model('logbook_model'); - $query = $this->user_model->get_by_id($this->session->userdata('user_id')); - $q = $query->row(); - $data['user_lotw_name'] = urlencode($q->user_lotw_name ?? ''); - $data['user_lotw_password'] = urlencode($q->user_lotw_password ?? ''); - // Get URL for downloading LoTW - $query = $query = $this->db->query('SELECT lotw_login_url FROM config'); - $q = $query->row(); - $lotw_url = $q->lotw_login_url; + $raw = file_get_contents("php://input"); + try { + $obj = json_decode($raw,true); + } catch (e) { + $ret['status']='failed_wrongcall'; + log_message("Error",$ret['status']); + } finally { + $lotw_user=$obj['lotw_user'] ?? ''; + $lotw_pass=$obj['lotw_pass'] ?? ''; + } + $raw=''; - // Validate that LoTW credentials are not empty - // TODO: We don't actually see the error message - if ($data['user_lotw_name'] == '' || $data['user_lotw_password'] == '') { - $ret='No Creds set'; + $pw_placeholder = '**********'; + if ($lotw_pass == $pw_placeholder) { // User comes with unaltered credentials - take them from database + $query = $this->user_model->get_by_id($this->session->userdata('user_id')); + $q = $query->row(); + $data['user_lotw_name'] = urlencode($q->user_lotw_name ?? ''); + $data['user_lotw_password'] = urlencode($q->user_lotw_password ?? ''); + } else { + $data['user_lotw_name'] = urlencode($lotw_user ?? ''); + $data['user_lotw_password'] = urlencode($lotw_pass ?? ''); } - // Build URL for LoTW report file - $lotw_url .= "?"; - $lotw_url .= "login=" . $data['user_lotw_name']; - $lotw_url .= "&password=" . $data['user_lotw_password']; + if ((($data['user_lotw_name'] ?? '') != '') && (($data['user_lotw_password'] ?? '') != '') && ($ret['status'] != 'failed_wrongcall')) { - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $lotw_url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); - $content = curl_exec($ch); - if ($content) { - if(curl_errno($ch)) { - $ret['status']='failed'; - $ret['details']== __("LoTW check failed for user ").$data['user_lotw_name'].": ".curl_strerror(curl_errno($ch))." (".curl_errno($ch).")."; - } else if (str_contains($content,"Username/password incorrect")) { - $ret['status']='failed_wrong_creds'; - $ret['details']= __("LoTW check failed for user ").$data['user_lotw_name'].__(": Username/password incorrect"); + // Get URL for downloading LoTW + $query = $query = $this->db->query('SELECT lotw_login_url FROM config'); + $q = $query->row(); + $lotw_url = $q->lotw_login_url; + + // Validate that LoTW credentials are not empty + // TODO: We don't actually see the error message + if ($data['user_lotw_name'] == '' || $data['user_lotw_password'] == '') { + $ret='No Creds set'; + } + + // Build URL for LoTW report file + $lotw_url .= "?"; + $lotw_url .= "login=" . $data['user_lotw_name']; + $lotw_url .= "&password=" . $data['user_lotw_password']; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $lotw_url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); + $content = curl_exec($ch); + if ($content) { + if(curl_errno($ch)) { + $ret['status']='failed'; + $ret['details']== __("LoTW check failed for user ").$data['user_lotw_name'].": ".curl_strerror(curl_errno($ch))." (".curl_errno($ch).")."; + } else if (str_contains($content,"Username/password incorrect")) { + $ret['status']='failed_wrong_creds'; + $ret['details']= __("LoTW check failed for user ").$data['user_lotw_name'].__(": Username/password incorrect"); + } else { + $ret['status']='OK'; + } } else { - $ret['status']='OK'; + $ret['status']='failed_na'; + $ret['details']= __("LoTW not available at present"); } } else { - $ret['status']='failed_na'; - $ret['details']= __("LoTW not available at present"); + if (($ret['status'] ?? '') == '') { + $ret['status']='failed_nocred'; + $ret['details']= __("No LoTW credentials provided"); + } } header("Content-type: application/json"); echo json_encode($ret); diff --git a/application/views/user/edit.php b/application/views/user/edit.php index c5e28b548..d25993e4d 100644 --- a/application/views/user/edit.php +++ b/application/views/user/edit.php @@ -706,15 +706,16 @@
- + ".$userlotwname_error.""; } ?>
- + +
".$lotwpassword_error.""; From 01dfbe908d6a896edea1ecefca61b550f6604d84 Mon Sep 17 00:00:00 2001 From: int2001 Date: Thu, 19 Dec 2024 07:49:28 +0000 Subject: [PATCH 04/11] AJAXing (Wip / jS) --- assets/js/sections/user.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/assets/js/sections/user.js b/assets/js/sections/user.js index 72eb9ce46..451814aaf 100644 --- a/assets/js/sections/user.js +++ b/assets/js/sections/user.js @@ -57,6 +57,19 @@ $(document).ready(function(){ }, 300); }); + $('#lotw_test_btn').click(function() { + $.ajax({ + url: base_url + 'index.php/lotw/check_lotw_credentials', + type: 'POST', + contentType: "application/json", + data: JSON.stringify({lotw_user: $("#user_lotw_name").val(), lotw_pass: $("#user_lotw_password").val()}), + success: function(result) { + console.log(result); + } + } + ); + }); + $('.admin_pwd_reset').click(function() { var pwd_reset_user_name = $(this).data('username'); var pwd_reset_user_callsign = $(this).data('callsign'); From 1ddf01aaa5e0eb198e29b9171ffae8dfcd23dff7 Mon Sep 17 00:00:00 2001 From: int2001 Date: Thu, 19 Dec 2024 08:12:09 +0000 Subject: [PATCH 05/11] Adjustments --- application/views/user/edit.php | 1 + 1 file changed, 1 insertion(+) diff --git a/application/views/user/edit.php b/application/views/user/edit.php index d25993e4d..aae071d32 100644 --- a/application/views/user/edit.php +++ b/application/views/user/edit.php @@ -717,6 +717,7 @@
+ ".$lotwpassword_error.""; } else if (!isset($user_add)) { ?> From c27d6e888a28489e8c46f5327031b576d1b84bcb Mon Sep 17 00:00:00 2001 From: int2001 Date: Thu, 19 Dec 2024 08:12:19 +0000 Subject: [PATCH 06/11] Add logic for banner --- assets/js/sections/user.js | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/assets/js/sections/user.js b/assets/js/sections/user.js index 451814aaf..f32f44855 100644 --- a/assets/js/sections/user.js +++ b/assets/js/sections/user.js @@ -58,16 +58,41 @@ $(document).ready(function(){ }); $('#lotw_test_btn').click(function() { + var btn_div = $('#lotw_test_btn'); + var msg_div = $('#lotw_test_txt'); + + msg_div.hide(); + msg_div.removeClass('alert-success alert-danger') + btn_div.hide(); + btn_div.removeClass('alert-success alert-danger') + $.ajax({ url: base_url + 'index.php/lotw/check_lotw_credentials', type: 'POST', contentType: "application/json", data: JSON.stringify({lotw_user: $("#user_lotw_name").val(), lotw_pass: $("#user_lotw_password").val()}), - success: function(result) { - console.log(result); - } - } - ); + success: function(res) { + if(res.status == 'OK') { + btn_div.addClass('alert-success'); + msg_div.addClass('alert-success'); + msg_div.text('LoTW OK!'); + msg_div.show(); + btn_div.show(); + } else { + btn_div.addClass('alert-danger'); + msg_div.addClass('alert-danger'); + msg_div.text('Error: '+res.details); + msg_div.show(); + btn_div.show(); + } + }, + error: function(res) { + msg_div.addClass('alert-danger'); + msg_div.text('ERROR'); + msg_div.show(); + btn_div.show(); + }, + }) }); $('.admin_pwd_reset').click(function() { From 8e279cef00035a1a53b954cbf90f787743799db1 Mon Sep 17 00:00:00 2001 From: int2001 Date: Thu, 19 Dec 2024 08:42:53 +0000 Subject: [PATCH 07/11] Fixed typo --- application/controllers/Lotw.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/controllers/Lotw.php b/application/controllers/Lotw.php index 1078b8e8f..693092155 100644 --- a/application/controllers/Lotw.php +++ b/application/controllers/Lotw.php @@ -469,7 +469,7 @@ class Lotw extends CI_Controller { private function loadFromFile($filepath, $station_ids, $display_view = "TRUE") { // Figure out how we should be marking QSLs confirmed via LoTW - $query = $query = $this->db->query('SELECT lotw_rcvd_mark FROM config'); + $query = $this->db->query('SELECT lotw_rcvd_mark FROM config'); $q = $query->row(); $config['lotw_rcvd_mark'] = $q->lotw_rcvd_mark; From 7bcf60ca9c2062e41d59e50e396f2e4e17c7ac90 Mon Sep 17 00:00:00 2001 From: "Joerg (DJ7NT)" Date: Thu, 19 Dec 2024 09:59:29 +0100 Subject: [PATCH 08/11] Lotw.php aktualisieren Co-authored-by: Fabian Berg --- application/controllers/Lotw.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/controllers/Lotw.php b/application/controllers/Lotw.php index 693092155..c7b1be17d 100644 --- a/application/controllers/Lotw.php +++ b/application/controllers/Lotw.php @@ -737,7 +737,7 @@ class Lotw extends CI_Controller { $raw = file_get_contents("php://input"); try { $obj = json_decode($raw,true); - } catch (e) { + } catch (Exception $e) { $ret['status']='failed_wrongcall'; log_message("Error",$ret['status']); } finally { From 531c91911b94a9211d5b84c3927f265a8a2529ff Mon Sep 17 00:00:00 2001 From: phl0 Date: Thu, 19 Dec 2024 10:15:04 +0100 Subject: [PATCH 09/11] Optimize wording and translations --- application/controllers/Lotw.php | 4 ++-- application/views/user/edit.php | 2 +- assets/js/sections/user.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/application/controllers/Lotw.php b/application/controllers/Lotw.php index c7b1be17d..7be7c9aad 100644 --- a/application/controllers/Lotw.php +++ b/application/controllers/Lotw.php @@ -783,10 +783,10 @@ class Lotw extends CI_Controller { if ($content) { if(curl_errno($ch)) { $ret['status']='failed'; - $ret['details']== __("LoTW check failed for user ").$data['user_lotw_name'].": ".curl_strerror(curl_errno($ch))." (".curl_errno($ch).")."; + $ret['details']== __("LoTW login failed for user ").$data['user_lotw_name'].": ".curl_strerror(curl_errno($ch))." (".curl_errno($ch).")."; } else if (str_contains($content,"Username/password incorrect")) { $ret['status']='failed_wrong_creds'; - $ret['details']= __("LoTW check failed for user ").$data['user_lotw_name'].__(": Username/password incorrect"); + $ret['details']= __("LoTW login failed for user ").$data['user_lotw_name'].": ". __("Username/password incorrect"); } else { $ret['status']='OK'; } diff --git a/application/views/user/edit.php b/application/views/user/edit.php index aae071d32..04c619a70 100644 --- a/application/views/user/edit.php +++ b/application/views/user/edit.php @@ -715,7 +715,7 @@
- +
Date: Thu, 19 Dec 2024 10:19:17 +0100 Subject: [PATCH 10/11] use sprintf to build gettext phrase --- application/controllers/Lotw.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/application/controllers/Lotw.php b/application/controllers/Lotw.php index 7be7c9aad..dbbea7482 100644 --- a/application/controllers/Lotw.php +++ b/application/controllers/Lotw.php @@ -783,21 +783,21 @@ class Lotw extends CI_Controller { if ($content) { if(curl_errno($ch)) { $ret['status']='failed'; - $ret['details']== __("LoTW login failed for user ").$data['user_lotw_name'].": ".curl_strerror(curl_errno($ch))." (".curl_errno($ch).")."; + $ret['details']== sprintf(__("LoTW login failed for user %s: %s."), $data['user_lotw_name'], curl_strerror(curl_errno($ch))." (".curl_errno($ch).")"); } else if (str_contains($content,"Username/password incorrect")) { $ret['status']='failed_wrong_creds'; - $ret['details']= __("LoTW login failed for user ").$data['user_lotw_name'].": ". __("Username/password incorrect"); + $ret['details']= sprintf(__("LoTW login failed for user %s: %s."), $data['user_lotw_name'], __("Username/password incorrect")); } else { $ret['status']='OK'; } } else { $ret['status']='failed_na'; - $ret['details']= __("LoTW not available at present"); + $ret['details']= __("LoTW currently not available. Try again later."); } } else { if (($ret['status'] ?? '') == '') { $ret['status']='failed_nocred'; - $ret['details']= __("No LoTW credentials provided"); + $ret['details']= __("No LoTW credentials provided."); } } header("Content-type: application/json"); From 42a5e29a94ff2378b37bfbc9605d6913100433ec Mon Sep 17 00:00:00 2001 From: HB9HIL Date: Thu, 19 Dec 2024 10:25:07 +0100 Subject: [PATCH 11/11] also translated OK messages --- application/controllers/Lotw.php | 1 + assets/js/sections/user.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/application/controllers/Lotw.php b/application/controllers/Lotw.php index dbbea7482..b7497816c 100644 --- a/application/controllers/Lotw.php +++ b/application/controllers/Lotw.php @@ -789,6 +789,7 @@ class Lotw extends CI_Controller { $ret['details']= sprintf(__("LoTW login failed for user %s: %s."), $data['user_lotw_name'], __("Username/password incorrect")); } else { $ret['status']='OK'; + $ret['details']= __("LoTW login OK!"); } } else { $ret['status']='failed_na'; diff --git a/assets/js/sections/user.js b/assets/js/sections/user.js index 8896b7752..a9e75cafd 100644 --- a/assets/js/sections/user.js +++ b/assets/js/sections/user.js @@ -75,7 +75,7 @@ $(document).ready(function(){ if(res.status == 'OK') { btn_div.addClass('alert-success'); msg_div.addClass('alert-success'); - msg_div.text('LoTW login OK!'); + msg_div.text(res.details); msg_div.show(); btn_div.show(); } else {