mirror of
https://github.com/wavelog/wavelog.git
synced 2026-03-22 10:24:14 +00:00
[Callbook] Added qrz.ru support
This commit is contained in:
@@ -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 //
|
||||
|
||||
125
application/libraries/Qrzru.php
Normal file
125
application/libraries/Qrzru.php
Normal file
@@ -0,0 +1,125 @@
|
||||
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
/*
|
||||
Controls the interaction with the QRZ.ru based XML API.
|
||||
*/
|
||||
|
||||
|
||||
class Qrzru {
|
||||
|
||||
// Return session key
|
||||
public function session($username, $password) {
|
||||
// URL to the XML Source
|
||||
$ci = & get_instance();
|
||||
$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);
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user