From 024f2e0affbdc300bdffe151023f2f72ed80b052 Mon Sep 17 00:00:00 2001 From: Andreas Kristiansen <6977712+AndreasK79@users.noreply.github.com> Date: Mon, 3 Feb 2025 10:06:07 +0100 Subject: [PATCH] [Callbook] Added qrz.ru support --- application/libraries/Callbook.php | 39 +++++++++ application/libraries/Qrzru.php | 125 +++++++++++++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 application/libraries/Qrzru.php diff --git a/application/libraries/Callbook.php b/application/libraries/Callbook.php index 06cd40f91..f126e06b5 100644 --- a/application/libraries/Callbook.php +++ b/application/libraries/Callbook.php @@ -40,6 +40,13 @@ class Callbook { } return $this->hamqth($this->ci->config->item('hamqth_username'), $this->ci->config->item('hamqth_password'), $callsign); break; + case 'qrzru': + if ($this->ci->config->item('qrzru_username') == null || $this->ci->config->item('qrzru_password') == null) { + $callbook['error'] = 'Lookup not configured. Please review configuration.'; + return $callbook; + } + return $this->qrzru($this->ci->config->item('qrzru_username'), $this->ci->config->item('qrzru_password'), $callsign); + break; default: $callbook['error'] = 'No callbook defined. Please review configuration.'; return $callbook; @@ -134,6 +141,38 @@ class Callbook { return $callbook; } + function qrzru($username, $password, $callsign) { + if (!$this->ci->load->is_loaded('qrzcq')) { + $this->ci->load->library('qrzru'); + } + + if (!$this->ci->session->userdata('qrzru_session_key')) { + $result = $this->ci->qrzru->session($username, $password); + if ($result[0] == 0) { + $this->ci->session->set_userdata('qrzru_session_key', $result[1]); + } else { + $data['error'] = __("QRZ.RU Error").": ".$result[1]; + return $data; + } + } + + $callbook = $this->ci->qrztu->search($callsign, $this->ci->session->userdata('qrzru_session_key')); + + if ($callbook['error'] ?? '' == 'Invalid session key') { + $qrzru_session_key = $this->ci->qrzru->session($username, $password); + $this->ci->session->set_userdata('qrzru_session_key', $qrzru_session_key); + $callbook = $this->ci->qrzru->search($callsign, $this->ci->session->userdata('qrzru_session_key')); + } + + if (strpos($callbook['error'] ?? '', 'Callsign not found') !== false && strpos($callsign, "/") !== false) { + $plaincall = $this->get_plaincall($callsign); + // Now try again but give back reduced data, as we can't validate location and stuff (true at the end) + $callbook = $this->ci->qrzru->search($plaincall, $this->ci->session->userdata('qrzru_session_key'), true); + } + + return $callbook; + } + function get_plaincall($callsign) { $split_callsign = explode('/', $callsign); if (count($split_callsign) == 1) { // case F0ABC --> return cel 0 // diff --git a/application/libraries/Qrzru.php b/application/libraries/Qrzru.php new file mode 100644 index 000000000..5f347a43d --- /dev/null +++ b/application/libraries/Qrzru.php @@ -0,0 +1,125 @@ +optionslib->get_option('version')); + $xml = curl_exec($ch); + curl_close($ch); + + // Create XML object + $xml = simplexml_load_string($xml); + + // Return Session Key + return (string) $xml->Session->Key; + } + + // Set Session Key session. + public function set_session($username, $password) { + + $ci = & get_instance(); + + // URL to the XML Source + $xml_feed_url = 'https://api.qrz.ru/login?u='.$username.';p='.urlencode($password).';agent=wavelog'; + + // CURL Functions + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $xml_feed_url); + curl_setopt($ch, CURLOPT_HEADER, false); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); + curl_setopt($ch, CURLOPT_TIMEOUT, 10); + curl_setopt($ch, CURLOPT_USERAGENT, 'Wavelog/'.$ci->optionslib->get_option('version')); + $xml = curl_exec($ch); + curl_close($ch); + + // Create XML object + $xml = simplexml_load_string($xml); + + $key = (string) $xml->Session->Key; + + $ci->session->set_userdata('qrzru_session_key', $key); + + return true; + } + + + public function search($callsign, $key, $reduced = false) { + $data = null; + $ci = & get_instance(); + try { + // URL to the XML Source + $xml_feed_url = 'https://api.qrz.ru/callsign?id=' . $key . ';callsign=' . $callsign . ''; + + // CURL Functions + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $xml_feed_url); + curl_setopt($ch, CURLOPT_HEADER, false); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); + curl_setopt($ch, CURLOPT_TIMEOUT, 10); + curl_setopt($ch, CURLOPT_USERAGENT, 'Wavelog/'.$ci->optionslib->get_option('version')); + $xml = curl_exec($ch); + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + if ($httpcode != 200) return $data['error'] = 'Problems with qrz.ru communication'; // Exit function if no 200. If request fails, 0 is returned + + // Create XML object + $xml = simplexml_load_string($xml); + if (!empty($xml->Session->Error)) { + return $data['error'] = $xml->Session->Error; + } + + // Return Required Fields + $data['callsign'] = (string)$xml->Callsign->call; + + $data['name'] = (string)$xml->Callsign->ename; + + // we always give back the name, no matter if reduced data or not + $data['name'] = trim($data['name']); + + if ($reduced == false) { + $data['city'] = (string)$xml->Callsign->city; + $data['gridsquare'] = (string)$xml->Callsign->qthloc; + $data['lat'] = (string)$xml->Callsign->latitude; + $data['long'] = (string)$xml->Callsign->longitude; + $data['dxcc'] = ''; + $data['state'] = !empty((string)$xml->Callsign->state) ? substr((string)$xml->Callsign->state, 0, 2) : ''; + $data['iota'] = ''; + $data['county'] = (string)$xml->Callsign->state; + $data['ituz'] = (string)$xml->Callsign->itu_zone; + $data['cqz'] = (string)$xml->Callsign->cq_zone; + } else { + $data['gridsquare'] = ''; + $data['city'] = ''; + $data['lat'] = ''; + $data['long'] = ''; + $data['dxcc'] = ''; + $data['state'] = ''; + $data['iota'] = ''; + $data['county'] = ''; + $data['ituz'] = ''; + $data['cqz'] = ''; + } + } finally { + return $data; + } + } +}