diff --git a/application/config/database.sample.php b/application/config/database.sample.php index 630d6848..ea1a1508 100644 --- a/application/config/database.sample.php +++ b/application/config/database.sample.php @@ -83,7 +83,7 @@ $db['default'] = array( // The following values can probably stay the same. 'dbdriver' => 'mysqli', 'dbprefix' => '', - 'pconnect' => TRUE, + 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', diff --git a/application/controllers/Logbook.php b/application/controllers/Logbook.php index 2ed4684b..e231f62c 100644 --- a/application/controllers/Logbook.php +++ b/application/controllers/Logbook.php @@ -826,72 +826,18 @@ class Logbook extends CI_Controller $html .= ""; return $html; } else { - // if session data callbook_type is qrz - if ($this->session->userdata('callbook_type') == "QRZ") { - // Lookup using QRZ - $this->load->library('qrz'); - - // Load the encryption library - $this->load->library('encryption'); - - // Decrypt the password - $decrypted_password = $this->encryption->decrypt($this->session->userdata('callbook_password')); - - if (!$this->session->userdata('qrz_session_key')) { - $qrz_session_key = $this->qrz->session($this->session->userdata('callbook_username'), $decrypted_password); - $this->session->set_userdata('qrz_session_key', $qrz_session_key); - } - $callsign['callsign'] = $this->qrz->search($id, $this->session->userdata('qrz_session_key'), $this->config->item('use_fullname')); - - if (empty($callsign['callsign']['callsign'])) { - $qrz_session_key = $this->qrz->session($this->session->userdata('callbook_username'), $decrypted_password); - $this->session->set_userdata('qrz_session_key', $qrz_session_key); - $callsign['callsign'] = $this->qrz->search($id, $this->session->userdata('qrz_session_key'), $this->config->item('use_fullname')); - } + // Use the loadCallBook method from the model which handles fallback logic + $this->load->model('logbook_model'); + $callsign['callsign'] = $this->logbook_model->loadCallBook($id, $this->config->item('use_fullname')); + + if (!empty($callsign['callsign'])) { if (isset($callsign['callsign']['dxcc'])) { - $this->load->model('logbook_model'); $entity = $this->logbook_model->get_entity($callsign['callsign']['dxcc']); $callsign['callsign']['dxcc_name'] = $entity['name']; } - } elseif ($this->session->userdata('callbook_type') == "HamQTH") { - // Load the HamQTH library - $this->load->library('hamqth'); - - // Load the encryption library - $this->load->library('encryption'); - - // Decrypt the password - $decrypted_password = $this->encryption->decrypt($this->session->userdata('callbook_password')); - - - if (!$this->session->userdata('hamqth_session_key')) { - $hamqth_session_key = $this->hamqth->session($this->session->userdata('callbook_username'), $decrypted_password); - $this->session->set_userdata('hamqth_session_key', $hamqth_session_key); - } - - $callsign['callsign'] = $this->hamqth->search($id, $this->session->userdata('hamqth_session_key')); - - // If HamQTH session has expired, start a new session and retry the search. - if ($callsign['callsign']['error'] == "Session does not exist or expired") { - $hamqth_session_key = $this->hamqth->session($this->session->userdata('callbook_username'), $decrypted_password); - $this->session->set_userdata('hamqth_session_key', $hamqth_session_key); - $callsign['callsign'] = $this->hamqth->search($callsign, $this->session->userdata('hamqth_session_key')); - } - if (isset($data['callsign']['gridsquare'])) { - $this->load->model('logbook_model'); - $callsign['grid_worked'] = $this->logbook_model->check_if_grid_worked_in_logbook(strtoupper(substr($data['callsign']['gridsquare'], 0, 4)), 0, $this->session->userdata('user_default_band')); - } - if (isset($callsign['callsign']['dxcc'])) { - $this->load->model('logbook_model'); - $entity = $this->logbook_model->get_entity($callsign['callsign']['dxcc']); - $callsign['callsign']['dxcc_name'] = $entity['name']; - } - if (isset($callsign['callsign']['error'])) { - $callsign['error'] = $callsign['callsign']['error']; - } } else { - // No callbook type set, return error message - $callsign['error'] = 'Online callbook not configured. Go to Account Settings and select either QRZ or HamQTH in the "Callbook" section.'; + // No callbook type set, return error message with better guidance + $callsign['error'] = 'Online callbook not configured. You can configure QRZ or HamQTH credentials either in Account Settings or in the config.php file.'; } if (isset($callsign['callsign']['gridsquare'])) { @@ -943,70 +889,18 @@ class Logbook extends CI_Controller $this->load->view('view_log/partial/log_ajax.php', $data); } else { - // if session data callbook_type is qrz - if ($this->session->userdata('callbook_type') == "QRZ") { - // Lookup using QRZ - $this->load->library('qrz'); - - // Load the encryption library - $this->load->library('encryption'); - - // Decrypt the password - $decrypted_password = $this->encryption->decrypt($this->session->userdata('callbook_password')); - - if (!$this->session->userdata('qrz_session_key')) { - $qrz_session_key = $this->qrz->session($this->session->userdata('callbook_username'), $decrypted_password); - $this->session->set_userdata('qrz_session_key', $qrz_session_key); - } - $data['callsign'] = $this->qrz->search($fixedid, $this->session->userdata('qrz_session_key'), $this->config->item('use_fullname')); - - if (empty($data['callsign']['callsign'])) { - $qrz_session_key = $this->qrz->session($this->session->userdata('callbook_username'), $decrypted_password); - $this->session->set_userdata('qrz_session_key', $qrz_session_key); - $data['callsign'] = $this->qrz->search($fixedid, $this->session->userdata('qrz_session_key'), $this->config->item('use_fullname')); - } + // Use the loadCallBook method from the model which handles fallback logic + $this->load->model('logbook_model'); + $data['callsign'] = $this->logbook_model->loadCallBook($fixedid, $this->config->item('use_fullname')); + + if (!empty($data['callsign'])) { if (isset($data['callsign']['dxcc'])) { - $this->load->model('logbook_model'); $entity = $this->logbook_model->get_entity($data['callsign']['dxcc']); $data['callsign']['dxcc_name'] = $entity['name']; } if (isset($data['callsign']['gridsquare'])) { - $this->load->model('logbook_model'); $data['grid_worked'] = $this->logbook_model->check_if_grid_worked_in_logbook(strtoupper(substr($data['callsign']['gridsquare'], 0, 4)), 0, $this->session->userdata('user_default_band')); } - } elseif ($this->session->userdata('callbook_type') == "HamQTH") { - // Load the HamQTH library - $this->load->library('hamqth'); - - // Load the encryption library - $this->load->library('encryption'); - - // Decrypt the password - $decrypted_password = $this->encryption->decrypt($this->session->userdata('callbook_password')); - - - if (!$this->session->userdata('hamqth_session_key')) { - $hamqth_session_key = $this->hamqth->session($this->session->userdata('callbook_username'), $decrypted_password); - $this->session->set_userdata('hamqth_session_key', $hamqth_session_key); - } - - $data['callsign'] = $this->hamqth->search($fixedid, $this->session->userdata('hamqth_session_key')); - - // If HamQTH session has expired, start a new session and retry the search. - if ($data['callsign']['error'] == "Session does not exist or expired") { - $hamqth_session_key = $this->hamqth->session($this->session->userdata('callbook_username'), $decrypted_password); - $this->session->set_userdata('hamqth_session_key', $hamqth_session_key); - $data['callsign'] = $this->hamqth->search($fixedid, $this->session->userdata('hamqth_session_key')); - } - if (isset($data['callsign']['gridsquare'])) { - $this->load->model('logbook_model'); - $data['grid_worked'] = $this->logbook_model->check_if_grid_worked_in_logbook(strtoupper(substr($data['callsign']['gridsquare'], 0, 4)), 0, $this->session->userdata('user_default_band')); - } - if (isset($data['callsign']['dxcc'])) { - $this->load->model('logbook_model'); - $entity = $this->logbook_model->get_entity($data['callsign']['dxcc']); - $data['callsign']['dxcc_name'] = $entity['name']; - } if (isset($data['callsign']['error'])) { $data['error'] = $data['callsign']['error']; } diff --git a/application/models/Logbook_model.php b/application/models/Logbook_model.php index 49a7d9ce..11f7db9e 100755 --- a/application/models/Logbook_model.php +++ b/application/models/Logbook_model.php @@ -4576,61 +4576,136 @@ class Logbook_model extends CI_Model } + /** + * Get effective callbook configuration, falling back from session to config file + * Returns array with 'type', 'username', 'password', 'use_config' + */ + private function getCallbookConfig() + { + $callbook_type = $this->session->userdata('callbook_type'); + $callbook_username = $this->session->userdata('callbook_username'); + $callbook_password = $this->session->userdata('callbook_password'); + + // If session has valid callbook configuration, use it + if (!empty($callbook_type) && $callbook_type != "None" && !empty($callbook_username) && !empty($callbook_password)) { + // Load the encryption library and decrypt password + $this->load->library('encryption'); + $decrypted_password = $this->encryption->decrypt($callbook_password); + + return array( + 'type' => $callbook_type, + 'username' => $callbook_username, + 'password' => $decrypted_password, + 'use_config' => false + ); + } + + // Fall back to config file settings + $config_qrz_username = $this->config->item('qrz_username'); + $config_qrz_password = $this->config->item('qrz_password'); + $config_hamqth_username = $this->config->item('hamqth_username'); + $config_hamqth_password = $this->config->item('hamqth_password'); + $config_callbook = $this->config->item('callbook'); + + // Check QRZ config first + if (!empty($config_qrz_username) && !empty($config_qrz_password)) { + return array( + 'type' => 'QRZ', + 'username' => $config_qrz_username, + 'password' => $config_qrz_password, + 'use_config' => true + ); + } + + // Check HamQTH config + if (!empty($config_hamqth_username) && !empty($config_hamqth_password)) { + return array( + 'type' => 'HamQTH', + 'username' => $config_hamqth_username, + 'password' => $config_hamqth_password, + 'use_config' => true + ); + } + + // Check legacy callbook config setting + if ($config_callbook == 'qrz' && !empty($config_qrz_username) && !empty($config_qrz_password)) { + return array( + 'type' => 'QRZ', + 'username' => $config_qrz_username, + 'password' => $config_qrz_password, + 'use_config' => true + ); + } + + if ($config_callbook == 'hamqth' && !empty($config_hamqth_username) && !empty($config_hamqth_password)) { + return array( + 'type' => 'HamQTH', + 'username' => $config_hamqth_username, + 'password' => $config_hamqth_password, + 'use_config' => true + ); + } + + // No valid configuration found + return array( + 'type' => null, + 'username' => null, + 'password' => null, + 'use_config' => false + ); + } + public function loadCallBook($callsign, $use_fullname = false) { $callbook = null; try { - if ($this->session->userdata('callbook_type') == "QRZ") { + $config = $this->getCallbookConfig(); + + if ($config['type'] == "QRZ" && !empty($config['username']) && !empty($config['password'])) { // Lookup using QRZ $this->load->library('qrz'); - // Load the encryption library - $this->load->library('encryption'); - - // Decrypt the password - $decrypted_password = $this->encryption->decrypt($this->session->userdata('callbook_password')); - - if(!$this->session->userdata('qrz_session_key')) { - $qrz_session_key = $this->qrz->session($this->session->userdata('callbook_username'), $decrypted_password); - $this->session->set_userdata('qrz_session_key', $qrz_session_key); + // Use a different session key name for config-based credentials to avoid conflicts + $session_key_name = $config['use_config'] ? 'qrz_config_session_key' : 'qrz_session_key'; + + if(!$this->session->userdata($session_key_name)) { + $qrz_session_key = $this->qrz->session($config['username'], $config['password']); + $this->session->set_userdata($session_key_name, $qrz_session_key); } - $callbook = $this->qrz->search($callsign, $this->session->userdata('qrz_session_key'), $use_fullname); + $callbook = $this->qrz->search($callsign, $this->session->userdata($session_key_name), $use_fullname); // if we got nothing, it's probably because our session key is invalid, try again if (($callbook['callsign'] ?? '') == '') { - $qrz_session_key = $this->qrz->session($this->session->userdata('callbook_username'), $decrypted_password); - $this->session->set_userdata('qrz_session_key', $qrz_session_key); - $callbook = $this->qrz->search($callsign, $this->session->userdata('qrz_session_key'), $use_fullname); + $qrz_session_key = $this->qrz->session($config['username'], $config['password']); + $this->session->set_userdata($session_key_name, $qrz_session_key); + $callbook = $this->qrz->search($callsign, $this->session->userdata($session_key_name), $use_fullname); // if we still got nothing, and it's a compound callsign, then try a search for the base call if (($callbook['callsign'] ?? '') == '' && strpos($callsign, "/") !== false) { - $callbook = $this->qrz->search($this->get_plaincall($callsign), $this->session->userdata('qrz_session_key'), $use_fullname); + $callbook = $this->qrz->search($this->get_plaincall($callsign), $this->session->userdata($session_key_name), $use_fullname); } } } - if ($this->session->userdata('callbook_type') == "HamQTH") { + if ($config['type'] == "HamQTH" && !empty($config['username']) && !empty($config['password'])) { // Load the HamQTH library $this->load->library('hamqth'); - // Load the encryption library - $this->load->library('encryption'); - - // Decrypt the password - $decrypted_password = $this->encryption->decrypt($this->session->userdata('callbook_password')); + // Use a different session key name for config-based credentials to avoid conflicts + $session_key_name = $config['use_config'] ? 'hamqth_config_session_key' : 'hamqth_session_key'; - if(!$this->session->userdata('hamqth_session_key')) { - $hamqth_session_key = $this->hamqth->session($this->session->userdata('callbook_username'), $decrypted_password); - $this->session->set_userdata('hamqth_session_key', $hamqth_session_key); + if(!$this->session->userdata($session_key_name)) { + $hamqth_session_key = $this->hamqth->session($config['username'], $config['password']); + $this->session->set_userdata($session_key_name, $hamqth_session_key); } - $callbook = $this->hamqth->search($callsign, $this->session->userdata('hamqth_session_key')); + $callbook = $this->hamqth->search($callsign, $this->session->userdata($session_key_name)); // If HamQTH session has expired, start a new session and retry the search. if ($callbook['error'] == "Session does not exist or expired") { - $hamqth_session_key = $this->hamqth->session($this->session->userdata('callbook_username'), $decrypted_password); - $this->session->set_userdata('hamqth_session_key', $hamqth_session_key); - $callbook = $this->hamqth->search($callsign, $this->session->userdata('hamqth_session_key')); + $hamqth_session_key = $this->hamqth->session($config['username'], $config['password']); + $this->session->set_userdata($session_key_name, $hamqth_session_key); + $callbook = $this->hamqth->search($callsign, $this->session->userdata($session_key_name)); } } } finally { diff --git a/install/config/database.php b/install/config/database.php index 51fb3414..f95f82cc 100644 --- a/install/config/database.php +++ b/install/config/database.php @@ -83,7 +83,7 @@ $db['default'] = array( // The following values can probably stay the same. 'dbdriver' => 'mysqli', 'dbprefix' => '', - 'pconnect' => TRUE, + 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '',