Skip to content

Commit

Permalink
Merge pull request wavelog#543 from HB9HIL/fix_qrz
Browse files Browse the repository at this point in the history
  • Loading branch information
HB9HIL authored Jul 6, 2024
2 parents b17843f + 1dfa020 commit 384fc4a
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 66 deletions.
20 changes: 1 addition & 19 deletions application/controllers/Logbook.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function json($tempcallsign, $tempband, $tempmode, $tempstation_id = null, $date

$return['dxcc'] = $this->dxcheck($callsign,$date);

$lookupcall=$this->get_plaincall($callsign);
$lookupcall=$this->logbook_model->get_plaincall($callsign);

$return['partial'] = $this->partial($lookupcall, $band);

Expand Down Expand Up @@ -169,24 +169,6 @@ function json($tempcallsign, $tempband, $tempmode, $tempstation_id = null, $date
return;
}

function get_plaincall($callsign) {
$split_callsign=explode('/',$callsign);
if (count($split_callsign)==1) { // case F0ABC --> return cel 0 //
$lookupcall = $split_callsign[0];
} else if (count($split_callsign)==3) { // case EA/F0ABC/P --> return cel 1 //
$lookupcall = $split_callsign[1];
} else { // case F0ABC/P --> return cel 0 OR case EA/FOABC --> retunr 1 (normaly not exist) //
if (in_array(strtoupper($split_callsign[1]), array('P','M','MM','QRP','0','1','2','3','4','5','6','7','8','9'))) {
$lookupcall = $split_callsign[0];
} else if (strlen($split_callsign[1])>3) { // Last Element longer than 3 chars? Take that as call
$lookupcall = $split_callsign[1];
} else { // Last Element up to 3 Chars? Take first element as Call
$lookupcall = $split_callsign[0];
}
}
return $lookupcall;
}

// Returns $val2 first if it has value, even if it is null or empty string, if not return $val1.
function nval($val1, $val2) {
return (($val2 ?? "") === "" ? ($val1 ?? "") : ($val2 ?? ""));
Expand Down
59 changes: 39 additions & 20 deletions application/libraries/Hamqth.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ public function set_session($username, $password) {
}


public function search($callsign, $key)
{
public function search($callsign, $key, $reduced = false) {
$data = null;
try {
// URL to the XML Source
Expand All @@ -79,24 +78,44 @@ public function search($callsign, $key)
$xml = simplexml_load_string($xml);
if (!empty($xml->session->error)) return $data['error'] = $xml->session->error;

// Return Required Fields
$data['callsign'] = (string)$xml->search->callsign;
$data['name'] = (string)$xml->search->nick;
$data['gridsquare'] = (string)$xml->search->grid;
$data['city'] = (string)$xml->search->adr_city;
$data['lat'] = (string)$xml->search->latitude;
$data['long'] = (string)$xml->search->longitude;
$data['dxcc'] = (string)$xml->search->adif;
$data['iota'] = (string)$xml->search->iota;
$data['image'] = (string)$xml->search->picture;
$data['state'] = (string)$xml->search->us_state;
$data['error'] = (string)$xml->session->error;

if ($xml->search->country == "United States") {
$data['us_county'] = (string)$xml->search->us_county;
} else {
$data['us_county'] = null;
}
// we always want to return name and callsign
$data['callsign'] = (string)$xml->search->callsign;
$data['name'] = (string)$xml->search->nick;

// only return certain data of a callsign which does not contain a pre- or suffix (see https://github.com/wavelog/wavelog/issues/452)
if ($reduced == false) {

$data['gridsquare'] = (string)$xml->search->grid;
$data['city'] = (string)$xml->search->adr_city;
$data['lat'] = (string)$xml->search->latitude;
$data['long'] = (string)$xml->search->longitude;
$data['dxcc'] = (string)$xml->search->adif;
$data['iota'] = (string)$xml->search->iota;
$data['image'] = (string)$xml->search->picture;
$data['state'] = (string)$xml->search->us_state;
$data['error'] = (string)$xml->session->error;

if ($xml->search->country == "United States") {
$data['us_county'] = (string)$xml->search->us_county;
} else {
$data['us_county'] = null;
}

} else {

$data['gridsquare'] = '';
$data['city'] = '';
$data['lat'] = '';
$data['long'] = '';
$data['dxcc'] = '';
$data['iota'] = '';
$data['image'] = (string)$xml->search->picture;
$data['state'] = '';
$data['error'] = (string)$xml->session->error;

$data['us_county'] = '';

}
} finally {
return $data;
}
Expand Down
64 changes: 43 additions & 21 deletions application/libraries/Qrz.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Qrz {
// Return session key
public function session($username, $password) {
// URL to the XML Source
$xml_feed_url = 'http://xmldata.qrz.com/xml/current/?username='.$username.';password='.urlencode($password).';agent=wavelog';
$xml_feed_url = 'https://xmldata.qrz.com/xml/current/?username='.$username.';password='.urlencode($password).';agent=wavelog';

// CURL Functions
$ch = curl_init();
Expand All @@ -35,7 +35,7 @@ public function set_session($username, $password) {
$ci = & get_instance();

// URL to the XML Source
$xml_feed_url = 'http://xmldata.qrz.com/xml/current/?username='.$username.';password='.urlencode($password).';agent=wavelog';
$xml_feed_url = 'https://xmldata.qrz.com/xml/current/?username='.$username.';password='.urlencode($password).';agent=wavelog';

// CURL Functions
$ch = curl_init();
Expand All @@ -58,11 +58,11 @@ public function set_session($username, $password) {
}


public function search($callsign, $key, $use_fullname = false) {
public function search($callsign, $key, $use_fullname = false, $reduced = false) {
$data = null;
try {
// URL to the XML Source
$xml_feed_url = 'http://xmldata.qrz.com/xml/current/?s=' . $key . ';callsign=' . $callsign . '';
$xml_feed_url = 'https://xmldata.qrz.com/xml/current/?s=' . $key . ';callsign=' . $callsign . '';

// CURL Functions
$ch = curl_init();
Expand All @@ -75,10 +75,13 @@ public function search($callsign, $key, $use_fullname = false) {
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpcode != 200) return $data['error'] = 'Problems with qrz.com 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;

if (!empty($xml->Session->Error)) {
return $data['error'] = $xml->Session->Error;
}

// Return Required Fields
$data['callsign'] = (string)$xml->Callsign->call;

Expand All @@ -87,26 +90,45 @@ public function search($callsign, $key, $use_fullname = false) {
} else {
$data['name'] = (string)$xml->Callsign->fname;
}

// we always give back the name, no matter if reduced data or not
$data['name'] = trim($data['name']);

// Sanitise gridsquare to only allow up to 8 characters
// Sanitize gridsquare to allow only up to 8 characters
$unclean_gridsquare = (string)$xml->Callsign->grid; // Get the gridsquare from QRZ convert to string
$clean_gridsquare = strlen($unclean_gridsquare) > 8 ? substr($unclean_gridsquare,0,8) : $unclean_gridsquare; // Trim gridsquare to 8 characters max
$data['gridsquare'] = $clean_gridsquare;

$data['city'] = (string)$xml->Callsign->addr2;
$data['lat'] = (string)$xml->Callsign->lat;
$data['long'] = (string)$xml->Callsign->lon;
$data['dxcc'] = (string)$xml->Callsign->dxcc;
$data['state'] = (string)$xml->Callsign->state;
$data['iota'] = (string)$xml->Callsign->iota;
$data['qslmgr'] = (string)$xml->Callsign->qslmgr;
$data['image'] = (string)$xml->Callsign->image;

if ($xml->Callsign->country == "United States") {
$data['us_county'] = (string)$xml->Callsign->county;

if ($reduced == false) {

$data['gridsquare'] = $clean_gridsquare;
$data['city'] = (string)$xml->Callsign->addr2;
$data['lat'] = (string)$xml->Callsign->lat;
$data['long'] = (string)$xml->Callsign->lon;
$data['dxcc'] = (string)$xml->Callsign->dxcc;
$data['state'] = (string)$xml->Callsign->state;
$data['iota'] = (string)$xml->Callsign->iota;
$data['qslmgr'] = (string)$xml->Callsign->qslmgr;
$data['image'] = (string)$xml->Callsign->image;

if ($xml->Callsign->country == "United States") {
$data['us_county'] = (string)$xml->Callsign->county;
} else {
$data['us_county'] = null;
}

} else {
$data['us_county'] = null;

$data['gridsquare'] = '';
$data['city'] = '';
$data['lat'] = '';
$data['long'] = '';
$data['dxcc'] = '';
$data['state'] = '';
$data['iota'] = '';
$data['qslmgr'] = (string)$xml->Callsign->qslmgr;
$data['image'] = (string)$xml->Callsign->image;
$data['us_county'] = '';

}
} finally {

Expand Down
41 changes: 35 additions & 6 deletions application/models/Logbook_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -4703,6 +4703,24 @@ public function check_for_station_id() {
}
}

function get_plaincall($callsign) {
$split_callsign=explode('/',$callsign);
if (count($split_callsign)==1) { // case F0ABC --> return cel 0 //
$lookupcall = $split_callsign[0];
} else if (count($split_callsign)==3) { // case EA/F0ABC/P --> return cel 1 //
$lookupcall = $split_callsign[1];
} else { // case F0ABC/P --> return cel 0 OR case EA/FOABC --> retunr 1 (normaly not exist) //
if (in_array(strtoupper($split_callsign[1]), array('P','M','MM','QRP','0','1','2','3','4','5','6','7','8','9'))) {
$lookupcall = $split_callsign[0];
} else if (strlen($split_callsign[1])>3) { // Last Element longer than 3 chars? Take that as call
$lookupcall = $split_callsign[1];
} else { // Last Element up to 3 Chars? Take first element as Call
$lookupcall = $split_callsign[0];
}
}
return $lookupcall;
}

public function loadCallBook($callsign, $use_fullname=false)
{
$callbook = null;
Expand All @@ -4718,13 +4736,18 @@ public function loadCallBook($callsign, $use_fullname=false)

$callbook = $this->qrz->search($callsign, $this->session->userdata('qrz_session_key'), $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->config->item('qrz_username'), $this->config->item('qrz_password'));
$this->session->set_userdata('qrz_session_key', $qrz_session_key);
// We need to handle, if the sessionkey is invalid
if ($callbook['error'] ?? '' == 'Invalid session key') {
$this->qrz->set_session($this->config->item('qrz_username'), $this->config->item('qrz_password'));
$callbook = $this->qrz->search($callsign, $this->session->userdata('qrz_session_key'), $use_fullname);
}

// If the callsign contains a slash we have a pre- or suffix. If then the result is "Not found" we can try again with the plain call
if (strpos($callbook['error'] ?? '', '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->qrz->search($plaincall, $this->session->userdata('qrz_session_key'), $use_fullname, true);
}
}

if ($this->config->item('callbook') == "hamqth" && $this->config->item('hamqth_username') != null && $this->config->item('hamqth_password') != null) {
Expand All @@ -4736,7 +4759,13 @@ public function loadCallBook($callsign, $use_fullname=false)
$this->session->set_userdata('hamqth_session_key', $hamqth_session_key);
}

$callbook = $this->hamqth->search($callsign, $this->session->userdata('hamqth_session_key'));
// if the callsign contains a pre- or suffix we only give back reduced data to avoid wrong data (location and other things are not valid then)
if (strpos($callsign, "/") !== false) {
$reduced = true;
} else {
$reduced = false;
}
$callbook = $this->hamqth->search($callsign, $this->session->userdata('hamqth_session_key'), $reduced);

// If HamQTH session has expired, start a new session and retry the search.
if ($callbook['error'] == "Session does not exist or expired") {
Expand Down

0 comments on commit 384fc4a

Please sign in to comment.