Files
wavelog/application/libraries/EqslImporter.php
HB9HIL 2df091f891 Release v1.1 (#120)
* config file for directory userdata

* userdir for eqsl

* change name to userdata

* userdata for qsl

* fix name on comment

* add generic function file

* remove model class on view, use generic function

* Re-added forum link

* add stations_id to eqsl functions

* remove 'v' in Version Dialog

* Update README.md

added Link to demo instance

* Update index.php

* We should be MIT Conform

* [Debug] Added information about when files were last updated, and links to update

* Added custom data format

* Remove leftover-index

* Migration

* Check if index is there

* [LBA] Can disable maidenhead overlay

* paths libary

* Dev (#2)

* Added Windows, MariaDB and PHP-Versions

* Release 1.0

* Re-added forum link

* add stations_id to eqsl functions

* remove 'v' in Version Dialog

* Update README.md

added Link to demo instance

* Update index.php

* We should be MIT Conform

* [Debug] Added information about when files were last updated, and links to update

* Added custom data format

* Remove leftover-index

* Migration

* Check if index is there

* [LBA] Can disable maidenhead overlay

---------

Co-authored-by: int2001 <joerg@dj7nt.de>
Co-authored-by: Joerg (DJ7NT) <int2001@users.noreply.github.com>
Co-authored-by: Andreas <6977712+AndreasK79@users.noreply.github.com>
Co-authored-by: Christoph Kottke <dg0tm@darc.de>
Co-authored-by: Florian (DF2ET) <github@florian-wolters.de>

* [eQSL] only locations with a eQSL nickname should be displayed (#74)

* only locations with a eQSL nickname should be displayed in the dropdown under eQSL Import

* add proper error message

* improve usability

---------

Co-authored-by: Christoph Kottke <dg0tm@darc.de>

* html fixes

* show flashdata if no station has eqsl nick

* removed openssl check, not required

* accumulate stats language

* multilanguage support

* prettier

* Prevent leaking data out of other station_location

* moved qrg to first tab

* Fix a PHP 8.1 deprecated bug.

* [Contesting] Fix for table qso count

* Fixed error when data was empty. Also clear table before inserting again.

* Refactor to get the table to load

* Need to clear datatable when session is deleted

* Prevent Racecondition

* Fixed a few null-checks which will fail on fresh accounts

* Allow for longer gridsquare in station profile

* Show IOTA/SOTA ref on station location tab

* [LBA] Fixed mapping of selected QSOs

* Trying to convert the QSO-Post to an ajax call PHP/Part

* JS Part

* Dev (#3)

* Added Windows, MariaDB and PHP-Versions

* Release 1.0

* Re-added forum link

* add stations_id to eqsl functions

* remove 'v' in Version Dialog

* Update README.md

added Link to demo instance

* Update index.php

* We should be MIT Conform

* [Debug] Added information about when files were last updated, and links to update

* Added custom data format

* Remove leftover-index

* Migration

* Check if index is there

* [LBA] Can disable maidenhead overlay

* [eQSL] only locations with a eQSL nickname should be displayed (#74)

* only locations with a eQSL nickname should be displayed in the dropdown under eQSL Import

* add proper error message

* improve usability

---------

Co-authored-by: Christoph Kottke <dg0tm@darc.de>

* html fixes

* show flashdata if no station has eqsl nick

* removed openssl check, not required

* accumulate stats language

* multilanguage support

* prettier

* Prevent leaking data out of other station_location

* moved qrg to first tab

* Fix a PHP 8.1 deprecated bug.

* [Contesting] Fix for table qso count

* Fixed error when data was empty. Also clear table before inserting again.

* Refactor to get the table to load

* Need to clear datatable when session is deleted

* Prevent Racecondition

* Fixed a few null-checks which will fail on fresh accounts

* Allow for longer gridsquare in station profile

* Show IOTA/SOTA ref on station location tab

* [LBA] Fixed mapping of selected QSOs

---------

Co-authored-by: int2001 <joerg@dj7nt.de>
Co-authored-by: Joerg (DJ7NT) <int2001@users.noreply.github.com>
Co-authored-by: Andreas <6977712+AndreasK79@users.noreply.github.com>
Co-authored-by: Christoph Kottke <dg0tm@darc.de>
Co-authored-by: Florian (DF2ET) <github@florian-wolters.de>
Co-authored-by: dg0tm <schieberjunge@gmx.net>

* First POC

* Handling of AJAX result (started)

* Change qso-save to ajax-return

* Added notification and reloading of QSO-Table

* JS-Part

* Set Focus to qso after (successful) log

* Resetting timers (successful)

* Info is now shown / faded

* Small adjustments

* More Beauty

* Make consequent use of SIG-References from station_profile

* Delete application/views/welcome directory

Leftovers. Security Flaw!

* Update Dashboard.php

Remove redir to welcome

* Delete application/controllers/Welcome.php

security flaw

* Hint for 8.3. friendly Usertest

* Update config.sample.php

default should be commented out

* [LBA] Checkbox to turn on/off path lines in map

* Styling for pathlines-checkbox

* [LBA] Checkbox tweaks

* updated comment in config.php (sample and installer)

* Removed popup regarding unsaved changes (wasn't functional at all)

* Added locking for sending a qso twice

* added spinner to save button

* Also include SIG and SIG_INFO

* [Logbook] Removed profiler

* [LBA] Added fitbounds to map

* [LBA] Added CQ Zone overlay

* Make clearer that overview filters on logbook rather than locations

* baesURL has a trailing slash already

* active logbook hint

* Update .gitignore

added userdata

* [LBA] ITU zone overlay

* Minor wording

* [LBA] Added custom mapping icons

* [LBA] Path line follows icon color

* [LBA] Added icon anchoring

* [LBA] Added easyprint for easy map saving

* [LBA] Added default colors when icon settings does not exist in db

* Check refs before trimming them

* [LBA] Fixed another default issue with markers and colors

* Removed dangerous functions where User can accidently shift profiles

* Rmoeve more unused leftovers from 1.0 to 2.0.

* Header refactoring (#110)

* splitted up header

* resorted header menu

* main rebuild

* header behaviour hover

* dropdown toggle rotate

* fix case where landscape is small menu and portrait normal view

* submenu thirdparty services

* removed custom maps from the header

* matching search bar

* border

* matched border to themes

* [Custom Map] Removed code

* [Gridmap] Broke map by accident. Thanks @phl0 for poke.

* Optical support for SAT directions and elevation

* [LBA] Added coordinates at the bottom of the map

* Align numbers and arrows

* Reset RST after saving via AJAX

* Cfd export (#117)

* Added CFD Export for toplist.darc.de to Wavelog

---------

Co-authored-by: phl0 <github@florian-wolters.de>

* special thx to contributors

* tag 1.1

* typo

---------

Co-authored-by: abarrau <hesky378@gmail.com>
Co-authored-by: Andreas <6977712+AndreasK79@users.noreply.github.com>
Co-authored-by: Christoph Kottke <dg0tm@darc.de>
Co-authored-by: Joerg (DJ7NT) <int2001@users.noreply.github.com>
Co-authored-by: int2001 <joerg@dj7nt.de>
Co-authored-by: Florian (DF2ET) <github@florian-wolters.de>
Co-authored-by: dg0tm <schieberjunge@gmx.net>
2024-02-09 18:12:28 +01:00

205 lines
7.3 KiB
PHP

<?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;
private $station_id;
// 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');
$this->CI->load->model('eqslmethods_model');
}
private function init($name, $adif_file) {
$this->name = $name;
$this->adif_file = $adif_file;
}
public function from_callsign_and_QTH($callsign, $qth, $upload_path, $station_id) {
$this->init(
$qth . " - " . $callsign,
self::safe_filepath($callsign, $qth, $upload_path, $station_id)
);
$this->callsign = $callsign;
$this->qth_nickname = $qth;
$this->station_id = $station_id;
}
public function from_file($adif_file, $station_callsign, $station_id) {
$this->init('ADIF upload', $adif_file);
$this->callsign = $station_callsign;
$this->station_id = $station_id;
}
// generate a sanitized file name from a callsign and a QTH nickname
private static function safe_filepath($callsign, $qth, $upload_path, $station_id) {
$eqsl_id = sprintf('%s-%s_%03d', $callsign, $qth, $station_id);
// 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, $eqsl_force_from_date = false) {
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 / Or use date input from form //
$eqsl_last_qsl_date = (strtotime($eqsl_force_from_date)!==false)?date("Ymd",strtotime($eqsl_force_from_date)):$this->CI->eqslmethods_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("https://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;
$station_callsign = $this->callsign;
$station_id = $this->station_id;
$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'], $record['mode'], $station_callsign, $station_id);
$qsoid = 0;
if ($status[0] == "Found") {
$qsoid = $status[1];
$dupe = $this->CI->eqslmethods_model->eqsl_dupe_check($time_on, $record['call'], $record['band'], $record['mode'], $config['eqsl_rcvd_mark'], $station_callsign, $station_id);
if ($dupe == false) {
$updated += 1;
$eqsl_status = $this->CI->eqslmethods_model->eqsl_update($time_on, $record['call'], $record['band'], $record['mode'], $config['eqsl_rcvd_mark'], $station_callsign, $station_id);
} 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[0],
'eqsl_status' => $eqsl_status,
'qsoid' => $qsoid,
);
}
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,
);
}
}