Merge pull request #2280 from int2001/lotw_per_user

Lotw per user / fixes lotw-download
pull/2283/head
Peter Goodhall 2023-07-13 11:16:01 +01:00 zatwierdzone przez GitHub
commit 9a02e81544
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 183 dodań i 167 usunięć

Wyświetl plik

@ -192,6 +192,9 @@ class Lotw extends CI_Controller {
*/
public function lotw_upload() {
$this->load->model('user_model');
$this->user_model->authorize(2);
// Fire OpenSSL missing error if not found
if (!extension_loaded('openssl')) {
echo "You must install php OpenSSL for LoTW functions to work";
@ -200,154 +203,165 @@ class Lotw extends CI_Controller {
// Get Station Profile Data
$this->load->model('Stations');
$station_profiles = $this->Stations->all();
if ($this->user_model->authorize(2)) {
$station_profiles = $this->Stations->all_of_user($this->session->userdata('user_id'));
$sync_user_id=$this->session->userdata('user_id');
} else {
$station_profiles = $this->Stations->all();
$sync_user_id=null;
}
// Array of QSO IDs being Uploaded
$qso_id_array = array();
// Build TQ8 Outputs
if ($station_profiles->num_rows() >= 1) {
if ($station_profiles->num_rows() >= 1) {
foreach ($station_profiles->result() as $station_profile)
{
foreach ($station_profiles->result() as $station_profile) {
// Get Certificate Data
$this->load->model('LotwCert');
$data['station_profile'] = $station_profile;
$data['lotw_cert_info'] = $this->LotwCert->lotw_cert_details($station_profile->station_callsign, $station_profile->station_dxcc);
// Get Certificate Data
$this->load->model('LotwCert');
$data['station_profile'] = $station_profile;
$data['lotw_cert_info'] = $this->LotwCert->lotw_cert_details($station_profile->station_callsign, $station_profile->station_dxcc);
// If Station Profile has no LOTW Cert continue on.
if(!isset($data['lotw_cert_info']->cert_dxcc_id)) {
continue;
}
// Check if LoTW certificate itself is valid
// Validty of QSO dates will be checked later
$current_date = date('Y-m-d H:i:s');
if ($current_date <= $data['lotw_cert_info']->date_created) {
echo $data['lotw_cert_info']->callsign.": LoTW certificate not valid yet!";
continue;
}
if ($current_date >= $data['lotw_cert_info']->date_expires) {
echo $data['lotw_cert_info']->callsign.": LoTW certificate expired!";
continue;
}
// Get QSOs
$this->load->model('Logbook_model');
$data['qsos'] = $this->Logbook_model->get_lotw_qsos_to_upload($data['station_profile']->station_id, $data['lotw_cert_info']->qso_start_date, $data['lotw_cert_info']->qso_end_date);
// Nothing to upload
if(empty($data['qsos']->result())){
echo $station_profile->station_callsign." (".$station_profile->station_profile_name.") No QSOs to Upload <br>";
continue;
}
foreach ($data['qsos']->result() as $temp_qso) {
array_push($qso_id_array, $temp_qso->COL_PRIMARY_KEY);
}
// Build File to save
$adif_to_save = $this->load->view('lotw_views/adif_views/adif_export', $data, TRUE);
// create folder to store upload file
if (!file_exists('./uploads/lotw')) {
mkdir('./uploads/lotw', 0775, true);
}
// Build Filename
$filename_for_saving = './uploads/lotw/'.preg_replace('/[^a-z0-9]+/', '-', strtolower($data['lotw_cert_info']->callsign))."-".date("Y-m-d-H-i-s")."-cloudlog.tq8";
$gzdata = gzencode($adif_to_save, 9);
$fp = fopen($filename_for_saving, "w");
fwrite($fp, $gzdata);
fclose($fp);
//The URL that accepts the file upload.
$url = 'https://lotw.arrl.org/lotw/upload';
//The name of the field for the uploaded file.
$uploadFieldName = 'upfile';
//The full path to the file that you want to upload
$filePath = realpath($filename_for_saving);
//Initiate cURL
$ch = curl_init();
//Set the URL
curl_setopt($ch, CURLOPT_URL, $url);
//Set the HTTP request to POST
curl_setopt($ch, CURLOPT_POST, true);
//Tell cURL to return the output as a string.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//If the function curl_file_create exists
if(function_exists('curl_file_create')){
//Use the recommended way, creating a CURLFile object.
$filePath = curl_file_create($filePath);
} else{
//Otherwise, do it the old way.
//Get the canonicalized pathname of our file and prepend
//the @ character.
$filePath = '@' . realpath($filePath);
//Turn off SAFE UPLOAD so that it accepts files
//starting with an @
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
}
//Setup our POST fields
$postFields = array(
$uploadFieldName => $filePath
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
//Execute the request
$result = curl_exec($ch);
//If an error occured, throw an exception
//with the error message.
if(curl_errno($ch)){
throw new Exception(curl_error($ch));
}
$pos = strpos($result, "<!-- .UPL. accepted -->");
if ($pos === false) {
// Upload of TQ8 Failed for unknown reason
echo $station_profile->station_callsign." (".$station_profile->station_profile_name.") Upload Failed"."<br>";
} else {
// Upload of TQ8 was successfull
echo "Upload Successful - ".$filename_for_saving."<br>";
$this->LotwCert->last_upload($data['lotw_cert_info']->lotw_cert_id);
// Mark QSOs as Sent
foreach ($qso_id_array as $qso_number) {
$this->Logbook_model->mark_lotw_sent($qso_number);
}
}
// Delete TQ8 File - This is done regardless of whether upload was succcessful
unlink(realpath($filename_for_saving));
// If Station Profile has no LOTW Cert continue on.
if(!isset($data['lotw_cert_info']->cert_dxcc_id)) {
continue;
}
} else {
echo "No Station Profiles found to upload to LOTW";
// Check if LoTW certificate itself is valid
// Validty of QSO dates will be checked later
$current_date = date('Y-m-d H:i:s');
if ($current_date <= $data['lotw_cert_info']->date_created) {
echo $data['lotw_cert_info']->callsign.": LoTW certificate not valid yet!";
continue;
}
if ($current_date >= $data['lotw_cert_info']->date_expires) {
echo $data['lotw_cert_info']->callsign.": LoTW certificate expired!";
continue;
}
// Get QSOs
$this->load->model('Logbook_model');
$data['qsos'] = $this->Logbook_model->get_lotw_qsos_to_upload($data['station_profile']->station_id, $data['lotw_cert_info']->qso_start_date, $data['lotw_cert_info']->qso_end_date);
// Nothing to upload
if(empty($data['qsos']->result())){
if ($this->user_model->authorize(2)) { // Only be verbose if we have a session
echo $station_profile->station_callsign." (".$station_profile->station_profile_name.") No QSOs to Upload <br>";
}
continue;
}
foreach ($data['qsos']->result() as $temp_qso) {
array_push($qso_id_array, $temp_qso->COL_PRIMARY_KEY);
}
// Build File to save
$adif_to_save = $this->load->view('lotw_views/adif_views/adif_export', $data, TRUE);
// create folder to store upload file
if (!file_exists('./uploads/lotw')) {
mkdir('./uploads/lotw', 0775, true);
}
// Build Filename
$filename_for_saving = './uploads/lotw/'.preg_replace('/[^a-z0-9]+/', '-', strtolower($data['lotw_cert_info']->callsign))."-".date("Y-m-d-H-i-s")."-cloudlog.tq8";
$gzdata = gzencode($adif_to_save, 9);
$fp = fopen($filename_for_saving, "w");
fwrite($fp, $gzdata);
fclose($fp);
//The URL that accepts the file upload.
$url = 'https://lotw.arrl.org/lotw/upload';
//The name of the field for the uploaded file.
$uploadFieldName = 'upfile';
//The full path to the file that you want to upload
$filePath = realpath($filename_for_saving);
//Initiate cURL
$ch = curl_init();
//Set the URL
curl_setopt($ch, CURLOPT_URL, $url);
//Set the HTTP request to POST
curl_setopt($ch, CURLOPT_POST, true);
//Tell cURL to return the output as a string.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//If the function curl_file_create exists
if(function_exists('curl_file_create')){
//Use the recommended way, creating a CURLFile object.
$filePath = curl_file_create($filePath);
} else{
//Otherwise, do it the old way.
//Get the canonicalized pathname of our file and prepend
//the @ character.
$filePath = '@' . realpath($filePath);
//Turn off SAFE UPLOAD so that it accepts files
//starting with an @
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
}
//Setup our POST fields
$postFields = array(
$uploadFieldName => $filePath
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
//Execute the request
$result = curl_exec($ch);
//If an error occured, throw an exception
//with the error message.
if(curl_errno($ch)){
throw new Exception(curl_error($ch));
}
$pos = strpos($result, "<!-- .UPL. accepted -->");
if ($pos === false) {
// Upload of TQ8 Failed for unknown reason
echo $station_profile->station_callsign." (".$station_profile->station_profile_name.") Upload Failed"."<br>";
} else {
// Upload of TQ8 was successfull
echo "Upload Successful - ".$filename_for_saving."<br>";
$this->LotwCert->last_upload($data['lotw_cert_info']->lotw_cert_id);
// Mark QSOs as Sent
foreach ($qso_id_array as $qso_number) {
$this->Logbook_model->mark_lotw_sent($qso_number);
}
}
// Delete TQ8 File - This is done regardless of whether upload was succcessful
unlink(realpath($filename_for_saving));
}
} else {
echo "No Station Profiles found to upload to LOTW";
}
/*
| Download QSO Matches from LoTW
*/
*/
if ($this->user_model->authorize(2)) {
echo "<br><br>";
echo $this->lotw_download();
$sync_user_id=$this->session->userdata('user_id');
} else {
$sync_user_id=null;
}
echo $this->lotw_download($sync_user_id);
}
/*
@ -583,17 +597,20 @@ class Lotw extends CI_Controller {
unlink($filepath);
if(isset($data['lotw_table_headers'])) {
if($display_view == TRUE) {
$data['page_title'] = "LoTW ADIF Information";
$this->load->view('interface_assets/header', $data);
$this->load->view('lotw/analysis');
$this->load->view('interface_assets/footer');
$this->load->model('user_model');
if ($this->user_model->authorize(2)) { // Only Output results if authorized User
if(isset($data['lotw_table_headers'])) {
if($display_view == TRUE) {
$data['page_title'] = "LoTW ADIF Information";
$this->load->view('interface_assets/header', $data);
$this->load->view('lotw/analysis');
$this->load->view('interface_assets/footer');
} else {
return $tableheaders.$table;
}
} else {
return $tableheaders.$table;
echo "Downloaded LoTW report contains no matches.";
}
} else {
echo "Downloaded LoTW report contains no matches.";
}
}
@ -606,21 +623,21 @@ class Lotw extends CI_Controller {
| downloading matching QSOs.
|
*/
function lotw_download() {
function lotw_download($sync_user_id = null) {
$this->load->model('user_model');
$this->load->model('logbook_model');
$query = $this->user_model->get_all_lotw_users();
if ($query->num_rows() >= 1) {
foreach ($query->result() as $user)
{
$results='';
foreach ($query->result() as $user) {
if ( ($sync_user_id != null) && ($sync_user_id != $user->user_id) ) { continue; }
$config['upload_path'] = './uploads/';
$file = $config['upload_path'] . 'lotwreport_download.adi';
if (file_exists($file) && ! is_writable($file)) {
return "Temporary download file ".$file." is not writable. Aborting!";
$result.= "Temporary download file ".$file." is not writable. Aborting!";
}
// Get credentials for LoTW
@ -639,7 +656,7 @@ class Lotw extends CI_Controller {
echo "You have not defined your ARRL LoTW credentials!";
}
$lotw_last_qsl_date = date('Y-m-d', strtotime($this->logbook_model->lotw_last_qsl_date()));
$lotw_last_qsl_date = date('Y-m-d', strtotime($this->logbook_model->lotw_last_qsl_date($user->user_id)));
// Build URL for LoTW report file
$lotw_url .= "?";
@ -651,18 +668,19 @@ class Lotw extends CI_Controller {
$lotw_url .= "$lotw_last_qsl_date";
if (! is_writable(dirname($file))) {
return "Temporary download directory ".dirname($file)." is not writable. Aborting!";
$results.= "Temporary download directory ".dirname($file)." is not writable. Aborting!";
continue;
}
file_put_contents($file, file_get_contents($lotw_url));
if (file_get_contents($file, false, null, 0, 39) != "ARRL Logbook of the World Status Report") {
return "LoTW downloading failed either due to it being down or incorrect logins.";
$results.= "LoTW downloading failed for User ".$data['user_lotw_name']." either due to it being down or incorrect logins.";
continue;
}
ini_set('memory_limit', '-1');
$results = $this->loadFromFile($file, false);
return $results;
$results.= $this->loadFromFile($file, false);
}
return $results;
} else {
return "No LOTW User details found to carry out matches.";
}
@ -712,7 +730,7 @@ class Lotw extends CI_Controller {
}
else {
// Query the logbook to determine when the last LoTW confirmation was
$lotw_last_qsl_date = date('Y-m-d', strtotime($this->logbook_model->lotw_last_qsl_date()));
$lotw_last_qsl_date = date('Y-m-d', strtotime($this->logbook_model->lotw_last_qsl_date($this->session->userdata['user_id'])));
}
// Build URL for LoTW report file

Wyświetl plik

@ -2705,21 +2705,19 @@ class Logbook_model extends CI_Model {
return "Updated";
}
function lotw_last_qsl_date() {
$this->db->select('COL_LOTW_QSLRDATE');
$this->db->where('COL_LOTW_QSLRDATE IS NOT NULL');
$this->db->order_by("COL_LOTW_QSLRDATE", "desc");
$this->db->limit(1);
function lotw_last_qsl_date($user_id) {
$sql="SELECT MAX(COALESCE(COL_LOTW_QSLRDATE, '1900-01-01 00:00:00')) MAXDATE
FROM ".$this->config->item('table_name')." INNER JOIN station_profile ON (".$this->config->item('table_name').".station_id = station_profile.station_id)
WHERE station_profile.user_id=".$user_id." and COL_LOTW_QSLRDATE is not null";
$query = $this->db->query($sql);
$row = $query->row();
$query = $this->db->get($this->config->item('table_name'));
$row = $query->row();
if (isset($row)) {
return $row->MAXDATE;
}
if (isset($row)) {
return $row->COL_LOTW_QSLRDATE;
}
return '1900-01-01 00:00:00.000';
}
return '1900-01-01 00:00:00.000';
}
/*
* $skipDuplicate - used in ADIF import to skip duplicate checking when importing QSOs