wavelog/application/controllers/Dcl.php

255 lines
8.7 KiB
PHP
Raw Permalink Normal View History

2025-04-24 10:03:44 +00:00
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dcl extends CI_Controller {
/* Controls who can access the controller and its functions */
function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
2025-08-23 15:01:25 +00:00
if (!($this->config->item('enable_dcl_interface') ?? false)) { $this->session->set_flashdata('error', __("You're not allowed to do that!")); redirect('dashboard'); exit; }
2025-04-24 10:03:44 +00:00
if (ENVIRONMENT == 'maintenance' && $this->session->userdata('user_id') == '') {
echo __("Maintenance Mode is active. Try again later.")."\n";
redirect('user/login');
}
}
2025-06-08 06:51:12 +00:00
public function key_import() {
if (!$this->user_model->authorize(2) || !clubaccess_check(9)) { $this->session->set_flashdata('error', __("You're not allowed to do that!")); redirect('dashboard'); }
2025-06-08 06:51:12 +00:00
$this->load->library('Permissions');
2025-06-08 12:18:24 +00:00
$this->load->model('dcl_model');
2025-06-20 10:25:32 +00:00
$data['date_format']=$this->session->userdata('user_date_format') ?? $this->config->item('qso_date_format');
2025-06-08 06:51:12 +00:00
$sig=($this->input->get('sig',true) ?? '');
2025-06-08 06:51:12 +00:00
$token=($this->input->get('token',true) ?? '');
if ( ($sig != '') && ($token != '')) {
2025-06-08 12:18:24 +00:00
$data['is_valid']=$this->dcl_model->check_dcl_sig($token,$sig);
2025-06-08 06:51:12 +00:00
$data['page_title'] = __("DCL Key Import");
$data['token'] = $token;
2025-06-20 06:05:12 +00:00
if ($data['is_valid']) {
2025-06-20 10:25:32 +00:00
$data['dcl_info']=$this->dcl_model->get_dcl_info($token);
2025-08-17 15:33:39 +00:00
$this->dcl_model->store_key(json_encode($data['dcl_info'] ?? ''));
2025-06-20 06:05:12 +00:00
} else {
$data['dcl_info']='';
}
2025-06-08 06:51:12 +00:00
$this->load->view('interface_assets/header', $data);
$this->load->view('dcl_views/key_import',$data);
$this->load->view('interface_assets/footer');
} else {
redirect('https://api.dcl.darc.de/api/v1/get-token?wohin='.site_url().'/dcl/key_import');
2025-06-08 06:51:12 +00:00
}
}
2025-04-24 10:03:44 +00:00
public function index() {
if (!$this->user_model->authorize(2) || !clubaccess_check(9)) { $this->session->set_flashdata('error', __("You're not allowed to do that!")); redirect('dashboard'); }
2025-04-24 10:03:44 +00:00
$this->load->library('Permissions');
if(!$this->user_model->authorize(2) || !clubaccess_check(9)) { $this->session->set_flashdata('error', __("You're not allowed to do that!")); redirect('dashboard'); }
// Load required models for page generation
$this->load->model('Dcl_model');
2025-08-17 15:33:39 +00:00
$data['date_format']=$this->session->userdata('user_date_format') ?? $this->config->item('qso_date_format');
2025-04-24 10:03:44 +00:00
// Get Array of the logged in users LoTW certs.
$dclkeys=($this->Dcl_model->dcl_keys($this->session->userdata('user_id')) ?? '');
$i=0;
foreach ($dclkeys as $dclkey) {
$data['dcl_keys'][$i] = json_decode($dclkey->option_value ?? '');
$i++;
}
// Set Page Title
$data['page_title'] = __("DCL");
$this->load->model('cron_model');
2025-09-09 11:55:32 +00:00
$data['next_run'] = $this->cron_model->get_next_run("sync_dcl");
2025-04-24 10:03:44 +00:00
// Load Views
$this->load->view('interface_assets/header', $data);
$this->load->view('dcl_views/index');
$this->load->view('interface_assets/footer');
}
2025-09-03 13:20:36 +00:00
public function dcl_sync() {
$this->dcl_upload();
}
2025-04-24 16:10:44 +00:00
public function dcl_upload() {
// Called as User: Upload for User (if manual sync isn't disabled
// Called from cron / without Session: iterate through stations, check for DCL-Key and upload
2025-08-26 05:57:41 +00:00
ini_set('memory_limit', '-1');
2025-04-24 10:03:44 +00:00
$this->load->model('Dcl_model');
2025-04-24 10:03:44 +00:00
// set the last run in cron table for the correct cron id
$this->load->model('cron_model');
2025-09-09 05:04:58 +00:00
$this->cron_model->set_last_run('sync_dcl');
2025-04-24 10:03:44 +00:00
// Get Station Profile Data
$this->load->model('Stations');
2025-04-24 16:10:44 +00:00
if (!$this->load->is_loaded('AdifHelper')) {
$this->load->library('AdifHelper');
}
2025-04-24 10:03:44 +00:00
if ($this->user_model->authorize(2)) {
2025-08-21 08:58:27 +00:00
if (!($this->config->item('disable_manual_dcl'))) {
2025-04-24 10:03:44 +00:00
$station_profiles = $this->Stations->all_of_user($this->session->userdata('user_id'));
$sync_user_id=$this->session->userdata('user_id');
} else {
echo "Manual syncing is disabled by configuration";
redirect('dashboard');
exit();
}
} else {
$station_profiles = $this->Stations->all();
$sync_user_id=null;
}
// Array of QSO IDs being Uploaded
$qso_id_array = array();
if ($station_profiles->num_rows() >= 1) {
foreach ($station_profiles->result() as $station_profile) {
// Get Certificate Data
$data['station_profile'] = $station_profile;
2025-04-24 16:10:44 +00:00
$key_info = $this->Dcl_model->find_key($station_profile->station_callsign, $station_profile->user_id);
// If Station Profile has no DCL Key continue on.
2025-08-17 15:33:39 +00:00
if (($key_info ?? '') == '') {
2025-04-24 10:03:44 +00:00
continue;
}
$this->load->model('Logbook_model');
2025-08-17 15:47:46 +00:00
$data['qsos'] = $this->Logbook_model->get_dcl_qsos_to_upload($data['station_profile']->station_id,$key_info['vf'],$key_info['vt']);
2025-04-24 10:03:44 +00:00
// Nothing to upload
if(empty($data['qsos']->result())){
if ($this->user_model->authorize(2)) { // Only be verbose if we have a session
2025-08-26 07:37:09 +00:00
echo $station_profile->station_callsign." (".$station_profile->station_profile_name."): ".__("No QSOs to upload.")."<br>";
2025-04-24 10:03:44 +00:00
}
continue;
}
foreach ($data['qsos']->result() as $temp_qso) {
array_push($qso_id_array, $temp_qso->COL_PRIMARY_KEY);
}
// Build File to save
2025-08-21 08:58:27 +00:00
$adif_to_post = $this->load->view('adif/data/dcl.php', $data, TRUE);
2025-08-26 05:55:14 +00:00
$data['qsos']='';
2025-04-24 16:10:44 +00:00
2025-04-24 10:03:44 +00:00
//The URL that accepts the file upload.
$url = 'https://api.dcl.darc.de/api/v1/adif-import'; // todo: change to final URL b4 release
2025-04-24 10:03:44 +00:00
//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);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
2025-08-26 09:40:44 +00:00
$headers = [
'Content-Type: application/json',
'Accept: application/json'
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$payload=[];
$payload['key']=$key_info['token'];
$payload['adif']=$adif_to_post;
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload, true));
2025-04-24 10:03:44 +00:00
$result = curl_exec($ch);
2025-08-26 06:00:40 +00:00
$adif_to_post=''; // Clean Mem
2025-09-06 15:14:56 +00:00
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
2025-04-24 10:03:44 +00:00
if(curl_errno($ch)){
2025-08-26 07:37:09 +00:00
echo $station_profile->station_callsign." (".$station_profile->station_profile_name."): ".__("Upload Failed")." - ".curl_strerror(curl_errno($ch))." (".curl_errno($ch).")<br>";
2025-04-24 10:03:44 +00:00
if (curl_errno($ch) == 28) { // break on timeout
2025-08-26 07:37:09 +00:00
echo __("Timeout reached. Stopping subsequent uploads.")."<br>";
2025-04-24 10:03:44 +00:00
break;
} else {
continue;
}
}
2025-09-06 15:14:56 +00:00
$pos = ($httpcode == 200);
2025-04-24 10:03:44 +00:00
if ($pos === false) {
2025-09-06 15:14:56 +00:00
echo $station_profile->station_callsign." (".$station_profile->station_profile_name."): ".__("Upload Failed")." - Errorcode: ".$httpcode."<br>";
2025-09-07 06:14:57 +00:00
continue;
2025-04-24 10:03:44 +00:00
} else {
2025-08-26 07:37:09 +00:00
echo $station_profile->station_callsign." (".$station_profile->station_profile_name."): ".__("Upload Successful")." ".count($qso_id_array)." QSOs<br>";
2025-04-24 10:03:44 +00:00
// Mark QSOs as Sent
foreach ($qso_id_array as $qso_number) {
2025-04-24 16:20:12 +00:00
// todo: uncomment when ready
2025-09-05 10:43:41 +00:00
$this->Logbook_model->mark_dcl_sent($qso_number);
2025-04-24 10:03:44 +00:00
}
}
2025-08-26 09:00:18 +00:00
$qso_id_array=[];
2025-04-24 10:03:44 +00:00
}
} else {
2025-08-26 07:37:09 +00:00
echo __("No Station Profiles found to upload to DCL");
2025-04-24 10:03:44 +00:00
}
if ($this->user_model->authorize(2)) {
echo "<br><br>";
$sync_user_id=$this->session->userdata('user_id');
} else {
$sync_user_id=null;
}
2025-08-21 10:46:39 +00:00
// echo $this->dcl_download($sync_user_id);
2025-04-24 10:03:44 +00:00
}
2025-08-17 15:33:39 +00:00
public function delete_key() {
if (!$this->user_model->authorize(2) || !clubaccess_check(9)) { $this->session->set_flashdata('error', __("You're not allowed to do that!")); redirect('dashboard'); }
2025-04-24 10:03:44 +00:00
$this->load->model('Dcl_model');
2025-08-17 15:33:39 +00:00
$this->Dcl_model->delete_key();
2025-08-26 07:41:44 +02:00
$this->session->set_flashdata('success', __("Key(s) Deleted."));
2025-04-24 10:03:44 +00:00
redirect('dcl');
}
/*
|--------------------------------------------------------------------------
2025-08-21 12:25:19 +00:00
| Function: dcl_download
2025-04-24 10:03:44 +00:00
|--------------------------------------------------------------------------
|
2025-08-21 12:25:19 +00:00
| Collects users with DCL tokens and runs through them
2025-04-24 10:03:44 +00:00
| downloading matching QSOs.
|
*/
2025-04-24 16:20:12 +00:00
function dcl_download($sync_user_id = null) {
2025-04-24 10:03:44 +00:00
$this->load->model('logbook_model');
$this->load->model('Stations');
2025-08-21 12:25:19 +00:00
// Needs complete refactoring. Pseudocode:
// Loop through all users with token present (find_key at Dcl_model) or only single_users if sync_user_is has been provided
// Fetch data for call from DCL (todo: which URL? Where?)
// Mark as received
// all on the fly (no tempfiles)
2025-04-24 10:03:44 +00:00
}
public function import() { // Is only called via frontend. Cron uses "upload". within download the download is called
$this->load->model('Stations');
if(!$this->user_model->authorize(2)) {
$this->session->set_flashdata('error', __("You're not allowed to do that!"));
redirect('dashboard');
exit();
}
2025-08-21 12:25:19 +00:00
// Refactoring needed. This function provides manual download (via uploaded file into Wavelog) of DCL-Confirmations as well as triggering dcl_download
2025-04-24 10:25:13 +00:00
}
2025-04-24 10:03:44 +00:00
} // end class