Merge pull request #1579 from nolith/eqsl-download

eQSL import from multiple QTH nicknames
pull/1616/head
Peter Goodhall 2022-09-22 08:51:45 +01:00 zatwierdzone przez GitHub
commit d0f51fa691
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
6 zmienionych plików z 292 dodań i 232 usunięć

Wyświetl plik

@ -12,257 +12,84 @@ class eqsl extends CI_Controller {
if(!$this->user_model->authorize(2)) { $this->session->set_flashdata('notice', 'You\'re not allowed to do that!'); redirect('dashboard'); }
}
private function loadFromFile($filepath)
{
ini_set('memory_limit', '-1');
set_time_limit(0);
// Figure out how we should be marking QSLs confirmed via eQSL
$query = $query = $this->db->query('SELECT eqsl_rcvd_mark FROM config');
$q = $query->row();
$config['eqsl_rcvd_mark'] = $q->eqsl_rcvd_mark;
ini_set('memory_limit', '-1');
set_time_limit(0);
$this->load->library('adif_parser');
$this->adif_parser->load_from_file($filepath);
$this->adif_parser->initialize();
$tableheaders = "<table>";
$tableheaders .= "<tr class=\"titles\">";
$tableheaders .= "<td>Date</td>";
$tableheaders .= "<td>Call</td>";
$tableheaders .= "<td>Mode</td>";
$tableheaders .= "<td>Submode</td>";
$tableheaders .= "<td>Log Status</td>";
$tableheaders .= "<td>eQSL Status</td>";
$tableheaders .= "<tr>";
$table = "";
while ($record = $this->adif_parser->get_record())
{
$time_on = date('Y-m-d', strtotime($record['qso_date'])) ." ".date('H:i', strtotime($record['time_on']));
// The report from eQSL should only contain entries that have been confirmed via eQSL
// If there's a match for the QSO from the report in our log, it's confirmed via eQSL.
// If we have a positive match from LoTW, record it in the DB according to the user's preferences
if ($record['qsl_sent'] == "Y")
{
$record['qsl_sent'] = $config['eqsl_rcvd_mark'];
}
$status = $this->logbook_model->import_check($time_on, $record['call'], $record['band']);
if ($status == "Found")
{
$dupe = $this->logbook_model->eqsl_dupe_check($time_on, $record['call'], $record['band'], $config['eqsl_rcvd_mark']);
if ($dupe == false)
{
$eqsl_status = $this->logbook_model->eqsl_update($time_on, $record['call'], $record['band'], $config['eqsl_rcvd_mark']);
}
else
{
$eqsl_status = "Already received an eQSL for this QSO.";
}
}
else
{
$eqsl_status = "QSO not found";
}
$table .= "<tr>";
$table .= "<td>".$time_on."</td>";
$table .= "<td>".str_replace("0","&Oslash;",$record['call'])."</td>";
$table .= "<td>".$record['mode']."</td>";
if (isset($record['submode']))
{
$table .= "<td>".$record['submode']."</td>";
} else {
$table .= "<td></td>";
}
$table .= "<td>QSO Record: ".$status."</td>";
$table .= "<td>eQSL Record: ".$eqsl_status."</td>";
$table .= "<tr>";
}
if ($table != "")
{
$table .= "</table>";
$data['eqsl_results_table_headers'] = $tableheaders;
$data['eqsl_results_table'] = $table;
}
unlink($filepath);
$data['page_title'] = "eQSL Import Information";
$this->load->view('interface_assets/header', $data);
$this->load->view('eqsl/analysis');
$this->load->view('interface_assets/footer');
}
public function import() {
// Check if eQSL Nicknames have been defined
$this->load->model('stations');
if($this->stations->are_eqsl_nicks_defined() == 0) {
show_error('eQSL Nicknames in Station Profiles arent defined');
exit;
}
$this->load->model('stations');
$eqsl_locations = $this->stations->all_of_user_with_eqsl_nick_defined();
if($eqsl_locations->num_rows() == 0) {
show_error("eQSL Nicknames in Station Profiles aren't defined");
exit;
}
ini_set('memory_limit', '-1');
set_time_limit(0);
$data['page_title'] = "eQSL Import";
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'adi|ADI';
$this->load->library('upload', $config);
$this->load->model('logbook_model');
$eqsl_results = array();
if ($this->input->post('eqslimport') == 'fetch')
{
//echo "import from clublog ADIF<br>";
$file = $config['upload_path'] . 'eqslreport_download.adi';
//echo "<br>Download File: ".$file."<br>";
{
$this->load->library('EqslImporter');
// Get credentials for eQSL
$query = $this->user_model->get_by_id($this->session->userdata('user_id'));
$q = $query->row();
$data['user_eqsl_name'] = $q->user_eqsl_name;
$data['user_eqsl_password'] = $q->user_eqsl_password;
$eqsl_password = $q->user_eqsl_password;
//echo "<br>Username".$data['user_eqsl_name']."<br>";
// Get URL for downloading the eqsl.cc inbox
$query = $query = $this->db->query('SELECT eqsl_download_url FROM config');
$q = $query->row();
$eqsl_url = $q->eqsl_download_url;
// Validate that LoTW credentials are not empty
if ($data['user_eqsl_name'] == '' || $data['user_eqsl_password'] == '')
// Validate that eQSL credentials are not empty
if ($eqsl_password == '')
{
$this->session->set_flashdata('warning', 'You have not defined your eQSL.cc credentials!'); redirect('eqsl/import');
$this->session->set_flashdata('warning', 'You have not defined your eQSL.cc credentials!');
redirect('eqsl/import');
}
$this->load->model('stations');
$active_station_id = $this->stations->find_active();
$station_profile = $this->stations->profile($active_station_id);
$active_station_info = $station_profile->row();
// Query the logbook to determine when the last eQSL confirmation was
$eqsl_last_qsl_date = $this->logbook_model->eqsl_last_qsl_rcvd_date();
foreach ($eqsl_locations->result_array() as $eqsl_location) {
$this->eqslimporter->from_callsign_and_QTH(
$eqsl_location['station_callsign'],
$eqsl_location['eqslqthnickname'],
$config['upload_path']
);
// Build parameters for eQSL inbox file
$eqsl_params = http_build_query(array(
'UserName' => $data['user_eqsl_name'],
'Password' => $data['user_eqsl_password'],
'RcvdSince' => $eqsl_last_qsl_date,
'QTHNickname' => $active_station_info->eqslqthnickname,
'ConfirmedOnly' => 1
));
//echo "<br><br>".$eqsl_url."<br>".$eqsl_params."<br><br>";
// At this point, what we get isn't the ADI file we need, but rather
// an HTML page, which contains a link to the generated ADI file that we want.
// Adapted from Original PHP code by Chirp Internet: www.chirp.com.au (regex)
// Let's use cURL instead of file_get_contents
// begin script
$ch = curl_init();
// basic curl options for all requests
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
// use the URL and params we built
curl_setopt($ch, CURLOPT_URL, $eqsl_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $eqsl_params);
$input = curl_exec($ch);
$chi = curl_getinfo($ch);
// "You have no log entries" -> Nothing else to do here
// "Your ADIF log file has been built" -> We've got an ADIF file we need to grab.
if ($chi['http_code'] == "200")
{
if (stristr($input, "You have no log entries"))
{
$this->session->set_flashdata('success', 'There are no QSLs waiting for download at eQSL.cc.'); redirect('eqsl/import');
}
else if (stristr($input, "Error: No such Username/Password found"))
{
$this->session->set_flashdata('warning', 'No such Username/Password found This could mean the wrong callsign or the wrong password, or the user does not exist.');
redirect('eqsl/import');
}
else
{
if (stristr($input, "Your ADIF log file has been built"))
{
// Get all the links on the page and grab the URL for the ADI file.
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
if(preg_match_all("/$regexp/siU", $input, $matches)) {
foreach( $matches[2] as $match )
{
// Look for the link that has the .adi file, and download it to $file
if (substr($match, -4, 4) == ".adi")
{
file_put_contents($file, file_get_contents("http://eqsl.cc/qslcard/" . $match));
ini_set('memory_limit', '-1');
$this->loadFromFile($file);
break;
}
}
}
}
}
$eqsl_results[] = $this->eqslimporter->fetch($eqsl_password);
}
else
{
if ($chi['http_code'] == "500")
{
$this->session->set_flashdata('warning', 'eQSL.cc is experiencing issues. Please try importing QSOs later.'); redirect('eqsl/import');
}
else
{
if ($chi['http_code'] == "404")
{
$this->session->set_flashdata('warning', 'It seems that the eQSL site has changed. Please open up an issue on GitHub.'); redirect('eqsl/import');
}
}
}
// Close cURL handle
curl_close($ch);
}
else
{
if ( ! $this->upload->do_upload())
{
$data['page_title'] = "eQSL Import";
$data['error'] = $this->upload->display_errors();
$this->load->view('interface_assets/header', $data);
$this->load->view('eqsl/import');
$this->load->view('interface_assets/footer');
return;
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->loadFromFile('./uploads/'.$data['upload_data']['file_name']);
$this->load->library('EqslImporter');
$this->eqslimporter->from_file('./uploads/'.$data['upload_data']['file_name']);
$eqsl_results[] = $this->eqslimporter->import();
}
}
$data['eqsl_results'] = $eqsl_results;
$data['page_title'] = "eQSL Import Information";
$this->load->view('interface_assets/header', $data);
$this->load->view('eqsl/analysis');
$this->load->view('interface_assets/footer');
} // end function
public function export() {
// Check if eQSL Nicknames have been defined
$this->load->model('stations');

Wyświetl plik

@ -111,6 +111,7 @@ class ADIF_Parser
};
$this->datasplit = preg_split("/<eor>/i", mb_substr($this->data, $this->i, NULL, "UTF-8"));
$this->currentarray = 0;
return 1;
}

Wyświetl plik

@ -0,0 +1,194 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
// EqslImporter handle eQSL inboxes for multiple QTH locations.
// It can fetch confirmations from eqsl.cc or analyze a user-provided ADIF file
class EqslImporter
{
private $name;
private $callsign;
private $qth_nickname;
private $adif_file;
// CodeIgniter super-ojbect
private $CI;
public function __construct() {
// Assign the CodeIgniter super-object
$this->CI =& get_instance();
$this->CI->load->model('logbook_model');
$this->CI->load->library('adif_parser');
}
private function init($name, $adif_file) {
$this->name = $name;
$this->adif_file = $adif_file;
}
public function from_callsign_and_QTH($callsign, $qth, $upload_path) {
$this->init(
$qth . " - " . $callsign,
self::safe_filepath($callsign, $qth, $upload_path)
);
$this->callsign = $callsign;
$this->qth_nickname = $qth;
}
public function from_file($adif_file) {
$this->init('ADIF upload', $adif_file);
}
// generate a sanitized file name from a callsign and a QTH nickname
private static function safe_filepath($callsign, $qth, $upload_path) {
$eqsl_id = $callsign . '-' . $qth;
// Replace anything which isn't a word, whitespace, number or any of the following caracters -_~,;[](). with a '.'
$eqsl_id = mb_ereg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '.', $eqsl_id);
$eqsl_id = mb_ereg_replace("([\.]{2,})", '', $eqsl_id);
return $upload_path . $eqsl_id . '-eqslreport_download.adi';
}
// Download confirmed QSO from eQSL inbox and import them
public function fetch($password) {
if (empty($password) || empty($this->callsign)) {
return $this->result('Missing username and/or password');
}
// Get URL for downloading the eqsl.cc inbox
$query = $this->CI->db->query('SELECT eqsl_download_url FROM config');
$q = $query->row();
$eqsl_url = $q->eqsl_download_url;
// Query the logbook to determine when the last eQSL confirmation was
$eqsl_last_qsl_date = $this->CI->logbook_model->eqsl_last_qsl_rcvd_date($this->callsign, $this->qth_nickname);
// Build parameters for eQSL inbox file
$eqsl_params = http_build_query(array(
'UserName' => $this->callsign,
'Password' => $password,
'RcvdSince' => $eqsl_last_qsl_date,
'QTHNickname' => $this->qth_nickname,
'ConfirmedOnly' => 1
));
// At this point, what we get isn't the ADI file we need, but rather
// an HTML page, which contains a link to the generated ADI file that we want.
// Adapted from Original PHP code by Chirp Internet: www.chirp.com.au (regex)
// Let's use cURL instead of file_get_contents
// begin script
$ch = curl_init();
try {
// basic curl options for all requests
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
// use the URL and params we built
curl_setopt($ch, CURLOPT_URL, $eqsl_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $eqsl_params);
$input = curl_exec($ch);
$chi = curl_getinfo($ch);
// "You have no log entries" -> Nothing else to do here
// "Your ADIF log file has been built" -> We've got an ADIF file we need to grab.
if ($chi['http_code'] == "200") {
if (stristr($input, "You have no log entries")) {
return $this->result('There are no QSLs waiting for download at eQSL.cc.'); // success
} else if (stristr($input, "Error: No such Username/Password found")) {
return $this->result('No such Username/Password found This could mean the wrong callsign or the wrong password, or the user does not exist.'); // warning
} else {
if (stristr($input, "Your ADIF log file has been built")) {
// Get all the links on the page and grab the URL for the ADI file.
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
if (preg_match_all("/$regexp/siU", $input, $matches)) {
foreach ($matches[2] as $match) {
// Look for the link that has the .adi file, and download it to $file
if (substr($match, -4, 4) == ".adi") {
file_put_contents($this->adif_file, file_get_contents("http://eqsl.cc/qslcard/" . $match));
return $this->import();
}
}
}
}
}
} else {
if ($chi['http_code'] == "500") {
return $this->result('eQSL.cc is experiencing issues. Please try importing QSOs later.'); // warning
}
}
return $this->result('It seems that the eQSL site has changed. Please open up an issue on GitHub.');
} finally {
// Close cURL handle
curl_close($ch);
}
}
// Read the ADIF file and set QSO confirmation status according to the settings
public function import(): array {
// Figure out how we should be marking QSLs confirmed via eQSL
$query = $this->CI->db->query('SELECT eqsl_rcvd_mark FROM config');
$q = $query->row();
$config['eqsl_rcvd_mark'] = $q->eqsl_rcvd_mark;
$this->CI->adif_parser->load_from_file($this->adif_file);
$this->CI->adif_parser->initialize();
$qsos = array();
$records = $updated = $not_found = $dupes = 0;
while ($record = $this->CI->adif_parser->get_record()) {
$records += 1;
$time_on = date('Y-m-d', strtotime($record['qso_date'])) . " " . date('H:i', strtotime($record['time_on']));
// The report from eQSL should only contain entries that have been confirmed via eQSL
// If there's a match for the QSO from the report in our log, it's confirmed via eQSL.
// If we have a positive match from eQSL, record it in the DB according to the user's preferences
if ($record['qsl_sent'] == "Y") {
$record['qsl_sent'] = $config['eqsl_rcvd_mark'];
}
$status = $this->CI->logbook_model->import_check($time_on, $record['call'], $record['band']);
if ($status == "Found") {
$dupe = $this->CI->logbook_model->eqsl_dupe_check($time_on, $record['call'], $record['band'], $config['eqsl_rcvd_mark']);
if ($dupe == false) {
$updated += 1;
$eqsl_status = $this->CI->logbook_model->eqsl_update($time_on, $record['call'], $record['band'], $config['eqsl_rcvd_mark']);
} else {
$dupes += 1;
$eqsl_status = "Already received an eQSL for this QSO.";
}
} else {
$not_found += 1;
$eqsl_status = "QSO not found";
}
$qsos[] = array(
'date' => $time_on,
'call' => str_replace("0", "&Oslash;", $record['call']),
'mode' => $record['mode'],
'submode' => $record['submode'] ?? null,
'status' => $status,
'eqsl_status' => $eqsl_status,
);
}
unlink($this->adif_file);
return $this->result("$records QSO: $updated updated / $dupes duplicates / $not_found not found", $qsos);
}
private function result($status, $qsos = array()): array {
return array(
'name' => $this->name,
'adif_file' => $this->adif_file,
'qsos' => $qsos,
'status' => $status,
);
}
}

Wyświetl plik

@ -2109,13 +2109,21 @@ class Logbook_model extends CI_Model {
}
// Get the last date we received an eQSL
function eqsl_last_qsl_rcvd_date() {
function eqsl_last_qsl_rcvd_date($callsign, $nickname) {
$qso_table_name = $this->config->item('table_name');
$this->db->from($qso_table_name);
$this->db->join('station_profile',
'station_profile.station_id = '.$qso_table_name.'.station_id AND station_profile.eqslqthnickname != ""');
$this->db->where('station_profile.station_callsign', $callsign);
$this->db->where('station_profile.eqslqthnickname', $nickname);
$this->db->select("DATE_FORMAT(COL_EQSL_QSLRDATE,'%Y%m%d') AS COL_EQSL_QSLRDATE", FALSE);
$this->db->where('COL_EQSL_QSLRDATE IS NOT NULL');
$this->db->order_by("COL_EQSL_QSLRDATE", "desc");
$this->db->limit(1);
$query = $this->db->get($this->config->item('table_name'));
$query = $this->db->get();
$row = $query->row();
if (isset($row->COL_EQSL_QSLRDATE)){

Wyświetl plik

@ -322,6 +322,18 @@ class Stations extends CI_Model {
return $query->num_rows();
}
// Returns all the distinct callsing, eqsl nick pair for the current user
function all_of_user_with_eqsl_nick_defined() {
$this->db->where('user_id', $this->session->userdata('user_id'));
$this->db->where('eqslqthnickname IS NOT NULL');
$this->db->where('eqslqthnickname !=', '');
$this->db->from('station_profile');
$this->db->select('station_callsign, eqslqthnickname');
$this->db->distinct(TRUE);
return $this->db->get();
}
public function check_station_is_accessible($id) {
// check if station belongs to user
$this->db->select('station_id');

Wyświetl plik

@ -16,22 +16,40 @@
</ul>
</div>
<div class="card-body">
<?php $this->load->view('layout/messages'); ?>
<div class="card-body">
<?php $this->load->view('layout/messages'); ?>
<?php
if (isset($eqsl_results_table_headers))
{
echo "<p>The following QSLs have been received from eQSL.cc</p>";
echo $eqsl_results_table_headers;
echo $eqsl_results_table;
}
else
{
echo "<p>There are no QSO confirmations waiting for you at eQSL.cc</p>";
}
?>
</div>
<?php foreach ($eqsl_results as $import) { ?>
<h3><?php echo $import['name']; ?></h3>
<div class="alert alert-info" role="alert">
<?php echo $import['status']; ?>
</div>
<?php if (count($import['qsos']) > 0) { ?>
<table>
<tr class="titles">
<td>Date</td>
<td>Call</td>
<td>Mode</td>
<td>Submode</td>
<td>Log Status</td>
<td>eQSL Status</td>
</tr>
<?php foreach ($import['qsos'] as $qso) { ?>
<tr>
<td><?php echo $qso['date']; ?></td>
<td><?php echo $qso['call']; ?></td>
<td><?php echo $qso['mode']; ?></td>
<td><?php echo $qso['submode']; ?></td>
<td><?php echo $qso['status']; ?></td>
<td><?php echo $qso['eqsl_status']; ?></td>
</tr>
<?php } ?>
</table>
<?php } else { ?>
<p>There are no QSO confirmations waiting for you at eQSL.cc</p>
<?php } ?>
<?php } ?>
</div>
</div>
</div>
</div>