load->helper(array('form', 'url'));
$this->load->library('encryption');
}
// Default view when loading controller.
public function index()
{
$this->lang->load('qslcard');
$this->load->helper('storage');
$folder_name = "images/eqsl_card_images";
$data['storage_used'] = sizeFormat(folderSize($folder_name));
// Render Page
$data['page_title'] = "eQSL Cards";
$this->load->model('eqsl_images');
$data['qslarray'] = $this->eqsl_images->eqsl_qso_list();
$this->load->view('interface_assets/header', $data);
$this->load->view('eqslcard/index');
$this->load->view('interface_assets/footer');
}
public function import()
{
$this->load->model('user_model');
if (!$this->user_model->authorize(2)) {
$this->session->set_flashdata('notice', 'You\'re not allowed to do that!');
redirect('dashboard');
}
$this->load->model('stations');
$this->load->model('eqsl_mappings_model');
$data['station_profile'] = $this->stations->all_of_user();
$active_station_id = $this->stations->find_active();
$station_profile = $this->stations->profile($active_station_id);
$data['active_station_info'] = $station_profile->row();
$this->load->model('eqslmethods_model');
$user_id = (int) $this->session->userdata('user_id');
$active_mappings = $this->eqsl_mappings_model->get_active_mappings_for_user($user_id);
$use_mapping_mode = !empty($active_mappings);
$data['mapped_station_ids'] = array_map(function ($mapping) {
return (int) $mapping['station_id'];
}, $active_mappings);
$data['eqsl_mapping_mode'] = $use_mapping_mode;
$data['eqsl_legacy_fallback'] = !$use_mapping_mode;
$data['eqsl_mappings_count'] = count($active_mappings);
$eqsl_locations = null;
if (!$use_mapping_mode) {
$eqsl_locations = $this->eqslmethods_model->all_of_user_with_eqsl_nick_defined();
if ($eqsl_locations->num_rows() == 0) {
$this->session->set_flashdata('error', 'eQSL Nicknames in Station Profiles aren\'t defined!');
}
}
ini_set('memory_limit', '-1');
set_time_limit(0);
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'adi|ADI';
$this->load->library('upload', $config);
$eqsl_results = array();
if ($this->input->post('eqslimport') == 'fetch') {
$this->load->library('EqslImporter');
$eqsl_force_from_date = (!$this->input->post('eqsl_force_from_date') == "") ? $this->input->post('eqsl_force_from_date') : "";
if ($use_mapping_mode) {
foreach ($active_mappings as $mapping) {
$this->eqslimporter->from_callsign_and_QTH(
$mapping['station_callsign'],
$mapping['eqsl_qth_nickname'],
$config['upload_path'],
$mapping['station_id']
);
$eqsl_results[] = $this->eqslimporter->fetch($mapping['eqsl_password'], $eqsl_force_from_date);
}
} else {
// Get credentials for eQSL
$query = $this->user_model->get_by_id($this->session->userdata('user_id'));
$q = $query->row();
$eqsl_password = $this->decode_eqsl_password($q->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');
}
foreach ($eqsl_locations->result_array() as $eqsl_location) {
$this->eqslimporter->from_callsign_and_QTH(
$eqsl_location['station_callsign'],
$eqsl_location['eqslqthnickname'],
$config['upload_path'],
$eqsl_location['station_id']
);
$eqsl_results[] = $this->eqslimporter->fetch($eqsl_password, $eqsl_force_from_date);
}
}
} elseif ($this->input->post('eqslimport') == 'upload') {
$station_id4upload = $this->input->post('station_profile');
if ($this->stations->check_station_is_accessible($station_id4upload)) {
$station_callsign = $this->stations->profile($station_id4upload)->row()->station_callsign;
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->load->library('EqslImporter');
$this->eqslimporter->from_file('./uploads/' . $data['upload_data']['file_name'], $station_callsign, $station_id4upload);
$eqsl_results[] = $this->eqslimporter->import();
}
} else {
log_message('error', $station_id4upload . " is not valid for user!");
}
} else {
$data['page_title'] = "eQSL Import";
$this->load->view('interface_assets/header', $data);
$this->load->view('eqsl/import');
$this->load->view('interface_assets/footer');
return;
}
$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()
{
$this->load->model('user_model');
if (!$this->user_model->authorize(2)) {
$this->session->set_flashdata('notice', 'You\'re not allowed to do that!');
redirect('dashboard');
}
$this->load->model('stations');
$this->load->model('eqsl_mappings_model');
$this->load->model('logbooks_model');
ini_set('memory_limit', '-1');
set_time_limit(0);
$this->load->model('eqslmethods_model');
$user_id = (int) $this->session->userdata('user_id');
$active_mappings = $this->eqsl_mappings_model->get_active_mappings_for_user($user_id);
$use_mapping_mode = !empty($active_mappings);
$data['eqsl_mapping_mode'] = $use_mapping_mode;
$data['eqsl_legacy_fallback'] = !$use_mapping_mode;
$data['eqsl_mappings_count'] = count($active_mappings);
if (!$use_mapping_mode && $this->stations->are_eqsl_nicks_defined() == 0) {
$this->session->set_flashdata('error', 'eQSL Nicknames in Station Profiles aren\'t defined!');
}
$data['page_title'] = "eQSL QSO Upload";
$custom_date_format = $this->session->userdata('user_date_format');
$active_logbook_station_ids = $this->logbooks_model->list_logbook_relationships($this->session->userdata('active_station_logbook'));
if (!is_array($active_logbook_station_ids)) {
$active_logbook_station_ids = array();
}
if ($this->input->post('eqslexport') == "export") {
$rows = '';
$successful_uploads = array(); // Collect successful QSO primary keys for batch update
$processed_qso_ids = array();
if ($use_mapping_mode) {
foreach ($active_mappings as $mapping) {
// Mapping mode intentionally processes all active mappings.
// This avoids missing QSOs when station-logbook relationships are incomplete.
$credentials = array(
'user_eqsl_name' => $mapping['eqsl_username'],
'user_eqsl_password' => $mapping['eqsl_password'],
);
$qslsnotsent = $this->eqslmethods_model->eqsl_not_yet_sent_for_station($mapping['station_id'], $mapping['eqsl_qth_nickname']);
foreach ($qslsnotsent->result_array() as $qsl) {
$qso_id = (int) $qsl['COL_PRIMARY_KEY'];
if (isset($processed_qso_ids[$qso_id])) {
continue;
}
$processed_qso_ids[$qso_id] = true;
$qsl['eqslqthnickname'] = $mapping['eqsl_qth_nickname'];
$rows .= "
";
$adif = $this->generateAdif($qsl, $credentials);
$status = $this->uploadQso($adif, $qsl, $successful_uploads);
$timestamp = strtotime($qsl['COL_TIME_ON']);
$rows .= "| " . date($custom_date_format, $timestamp) . " | ";
$rows .= "" . date('H:i', $timestamp) . " | ";
$rows .= "" . str_replace("0", "Ø", $qsl['COL_CALL']) . " | ";
$rows .= "" . $qsl['COL_MODE'] . " | ";
if (isset($qsl['COL_SUBMODE'])) {
$rows .= "" . $qsl['COL_SUBMODE'] . " | ";
} else {
$rows .= " | ";
}
$rows .= "" . $qsl['COL_BAND'] . " | ";
$rows .= "" . $status . " | ";
$rows .= "
";
}
}
} else {
// Get credentials for eQSL
$query = $this->user_model->get_by_id($this->session->userdata('user_id'));
$q = $query->row();
$credentials['user_eqsl_name'] = $q->user_eqsl_name;
$credentials['user_eqsl_password'] = $this->decode_eqsl_password($q->user_eqsl_password);
// Validate that eQSL credentials are not empty
if ($credentials['user_eqsl_name'] == '' || $credentials['user_eqsl_password'] == '') {
$this->session->set_flashdata('warning', 'You have not defined your eQSL.cc credentials!');
redirect('eqsl/import');
}
$qslsnotsent = $this->eqslmethods_model->eqsl_not_yet_sent();
foreach ($qslsnotsent->result_array() as $qsl) {
$rows .= "";
$adif = $this->generateAdif($qsl, $credentials);
$status = $this->uploadQso($adif, $qsl, $successful_uploads);
$timestamp = strtotime($qsl['COL_TIME_ON']);
$rows .= "| " . date($custom_date_format, $timestamp) . " | ";
$rows .= "" . date('H:i', $timestamp) . " | ";
$rows .= "" . str_replace("0", "Ø", $qsl['COL_CALL']) . " | ";
$rows .= "" . $qsl['COL_MODE'] . " | ";
if (isset($qsl['COL_SUBMODE'])) {
$rows .= "" . $qsl['COL_SUBMODE'] . " | ";
} else {
$rows .= " | ";
}
$rows .= "" . $qsl['COL_BAND'] . " | ";
$rows .= "" . $status . " | ";
$rows .= "
";
}
}
// Batch update all successful uploads at the end for better performance
if (!empty($successful_uploads)) {
$affected_rows = $this->eqslmethods_model->eqsl_mark_sent_batch($successful_uploads);
log_message('info', 'eQSL export: Marked ' . $affected_rows . ' QSOs as sent to eQSL');
}
if ($rows != '') {
$data['eqsl_table'] = $this->generateResultTable($custom_date_format, $rows);
}
} else {
if ($use_mapping_mode) {
$processed_qso_ids = array();
$pending_qsos = array();
foreach ($active_mappings as $mapping) {
$qslsnotsent = $this->eqslmethods_model->eqsl_not_yet_sent_for_station($mapping['station_id'], $mapping['eqsl_qth_nickname']);
foreach ($qslsnotsent->result_array() as $qsl) {
$qso_id = (int) $qsl['COL_PRIMARY_KEY'];
if (isset($processed_qso_ids[$qso_id])) {
continue;
}
$processed_qso_ids[$qso_id] = true;
$qsl['eqslqthnickname'] = $mapping['eqsl_qth_nickname'];
$pending_qsos[] = $qsl;
}
}
if (!empty($pending_qsos)) {
$data['eqsl_table'] = $this->writeEqslNotSent($pending_qsos, $custom_date_format);
}
} else {
$qslsnotsent = $this->eqslmethods_model->eqsl_not_yet_sent();
if ($qslsnotsent->num_rows() > 0) {
$data['eqsl_table'] = $this->writeEqslNotSent($qslsnotsent->result_array(), $custom_date_format);
}
}
}
$this->load->view('interface_assets/header', $data);
$this->load->view('eqsl/export');
$this->load->view('interface_assets/footer');
}
function uploadQso($adif, $qsl, &$successful_uploads = null)
{
$this->load->model('eqslmethods_model');
$status = "";
// 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 we built
curl_setopt($ch, CURLOPT_URL, $adif);
$result = curl_exec($ch);
$chi = curl_getinfo($ch);
curl_close($ch);
/* Time for some error handling
Things we might get back
Result: 0 out of 0 records added -> eQSL didn't understand the format
Result: 1 out of 1 records added -> Fantastic
Error: No match on eQSL_User/eQSL_Pswd -> eQSL credentials probably wrong
Warning: Y=2013 M=08 D=11 F6ARS 15M JT65 Bad record: Duplicate
Result: 0 out of 1 records added -> Dupe, OM!
*/
if ($chi['http_code'] == "200") {
if (stristr($result, "Result: 1 out of 1 records added")) {
$status = "Sent";
// If batch array is provided, add to it instead of immediate update
if ($successful_uploads !== null) {
$successful_uploads[] = $qsl['COL_PRIMARY_KEY'];
} else {
// Legacy: immediate update (for backward compatibility)
$this->eqslmethods_model->eqsl_mark_sent($qsl['COL_PRIMARY_KEY']);
}
} else {
if (stristr($result, "Error: No match on eQSL_User/eQSL_Pswd")) {
$this->session->set_flashdata('warning', 'Your eQSL username and/or password is incorrect.');
redirect('eqsl/export');
} else {
if (stristr($result, "Result: 0 out of 0 records added")) {
$this->session->set_flashdata('warning', 'Something went wrong with eQSL.cc!');
redirect('eqsl/export');
} else {
if (stristr($result, "Bad record: Duplicate")) {
$status = "Duplicate";
# Mark the QSL as sent if this is a dupe.
if ($successful_uploads !== null) {
$successful_uploads[] = $qsl['COL_PRIMARY_KEY'];
} else {
$this->eqslmethods_model->eqsl_mark_sent($qsl['COL_PRIMARY_KEY']);
}
}
}
}
}
} else {
if ($chi['http_code'] == "500") {
$this->session->set_flashdata('warning', 'eQSL.cc is experiencing issues. Please try exporting QSOs later.');
redirect('eqsl/export');
} else {
if ($chi['http_code'] == "400") {
$this->session->set_flashdata('warning', 'There was an error in one of the QSOs. You might want to manually upload them.');
redirect('eqsl/export');
$status = "Error";
} 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/export');
}
}
}
}
log_message('debug', $result);
return $status;
}
function generateResultTable($custom_date_format, $rows)
{
$table = '';
$table .= "";
$table .= "| Date | ";
$table .= "Time | ";
$table .= "Call | ";
$table .= "Mode | ";
$table .= "Submode | ";
$table .= "Band | ";
$table .= "Status | ";
$table .= "
";
$table .= $rows;
$table .= "
";
return $table;
}
// Build out the ADIF info string according to specs https://eqsl.cc/qslcard/ADIFContentSpecs.cfm
function generateAdif($qsl, $data)
{
$COL_QSO_DATE = date('Ymd', strtotime($qsl['COL_TIME_ON']));
$COL_TIME_ON = date('Hi', strtotime($qsl['COL_TIME_ON']));
# Set up the single record file
$adif = "https://www.eqsl.cc/qslcard/importADIF.cfm?";
$adif .= "ADIFData=CloudlogUpload%20";
/* Handy reference of escaping chars
"<" = 3C
">" = 3E
":" = 3A
" " = 20
"_" = 5F
"-" = 2D
"." = 2E
"&" = 26
*/
$adif .= "%3C";
$adif .= "ADIF%5FVER";
$adif .= "%3A";
$adif .= "4";
$adif .= "%3E";
$adif .= "1%2E00 ";
$adif .= "%20";
$adif .= "%3C";
$adif .= "EQSL%5FUSER";
$adif .= "%3A";
$adif .= strlen($data['user_eqsl_name']);
$adif .= "%3E";
$adif .= $data['user_eqsl_name'];
$adif .= "%20";
$adif .= "%3C";
$adif .= "EQSL%5FPSWD";
$adif .= "%3A";
$adif .= strlen($data['user_eqsl_password']);
$adif .= "%3E";
$adif .= urlencode($data['user_eqsl_password']);
$adif .= "%20";
$adif .= "%3C";
$adif .= "EOH";
$adif .= "%3E";
# Lay out the required fields
$adif .= "%3C";
$adif .= "QSO%5FDATE";
$adif .= "%3A";
$adif .= "8";
$adif .= "%3E";
$adif .= $COL_QSO_DATE;
$adif .= "%20";
$adif .= "%3C";
$adif .= "TIME%5FON";
$adif .= "%3A";
$adif .= "4";
$adif .= "%3E";
$adif .= $COL_TIME_ON;
$adif .= "%20";
$adif .= "%3C";
$adif .= "CALL";
$adif .= "%3A";
$adif .= strlen($qsl['COL_CALL']);
$adif .= "%3E";
$adif .= $qsl['COL_CALL'];
$adif .= "%20";
$adif .= "%3C";
$adif .= "MODE";
$adif .= "%3A";
$adif .= strlen($qsl['COL_MODE']);
$adif .= "%3E";
$adif .= $qsl['COL_MODE'];
$adif .= "%20";
if (isset($qsl['COL_SUBMODE'])) {
$adif .= "%3C";
$adif .= "SUBMODE";
$adif .= "%3A";
$adif .= strlen($qsl['COL_SUBMODE']);
$adif .= "%3E";
$adif .= $qsl['COL_SUBMODE'];
$adif .= "%20";
}
$adif .= "%3C";
$adif .= "BAND";
$adif .= "%3A";
$adif .= strlen($qsl['COL_BAND']);
$adif .= "%3E";
$adif .= $qsl['COL_BAND'];
$adif .= "%20";
# End all the required fields
// adding RST_Sent
$adif .= "%3C";
$adif .= "RST%5FSENT";
$adif .= "%3A";
$adif .= strlen($qsl['COL_RST_SENT']);
$adif .= "%3E";
$adif .= $qsl['COL_RST_SENT'];
$adif .= "%20";
// adding prop mode if it isn't blank
if ($qsl['COL_PROP_MODE']) {
$adif .= "%3C";
$adif .= "PROP%5FMODE";
$adif .= "%3A";
$adif .= strlen($qsl['COL_PROP_MODE']);
$adif .= "%3E";
$adif .= $qsl['COL_PROP_MODE'];
$adif .= "%20";
}
// adding sat name if it isn't blank
if ($qsl['COL_SAT_NAME'] != '') {
$adif .= "%3C";
$adif .= "SAT%5FNAME";
$adif .= "%3A";
$adif .= strlen($qsl['COL_SAT_NAME']);
$adif .= "%3E";
$adif .= str_replace('-', '%2D', $qsl['COL_SAT_NAME']);
$adif .= "%20";
}
// adding sat mode if it isn't blank
if ($qsl['COL_SAT_MODE'] != '') {
$adif .= "%3C";
$adif .= "SAT%5FMODE";
$adif .= "%3A";
$adif .= strlen($qsl['COL_SAT_MODE']);
$adif .= "%3E";
$adif .= $qsl['COL_SAT_MODE'];
$adif .= "%20";
}
// adding qslmsg if it isn't blank
if ($qsl['COL_QSLMSG'] != '') {
$qsl['COL_QSLMSG'] = str_replace(array(chr(10), chr(13)), array(' ', ' '), $qsl['COL_QSLMSG']);
$adif .= "%3C";
$adif .= "QSLMSG";
$adif .= "%3A";
$adif .= strlen($qsl['COL_QSLMSG']);
$adif .= "%3E";
$adif .= str_replace('&', '%26', $qsl['COL_QSLMSG']);
$adif .= "%20";
}
if ($qsl['eqslqthnickname'] != '') {
$adif .= "%3C";
$adif .= "APP%5FEQSL%5FQTH%5FNICKNAME";
$adif .= "%3A";
$adif .= strlen($qsl['eqslqthnickname']);
$adif .= "%3E";
$adif .= $qsl['eqslqthnickname'];
$adif .= "%20";
}
// adding sat mode if it isn't blank
if ($qsl['station_gridsquare'] != '') {
$adif .= "%3C";
$adif .= "MY%5FGRIDSQUARE";
$adif .= "%3A";
$adif .= strlen($qsl['station_gridsquare']);
$adif .= "%3E";
$adif .= $qsl['station_gridsquare'];
$adif .= "%20";
}
# Tie a bow on it!
$adif .= "%3C";
$adif .= "EOR";
$adif .= "%3E";
# Make sure we don't have any spaces
$adif = str_replace(" ", '%20', $adif);
return $adif;
}
function writeEqslNotSent($qslsnotsent, $custom_date_format)
{
$table = '';
$table .= "";
$table .= "| Date | ";
$table .= "Time | ";
$table .= "Call | ";
$table .= "Mode | ";
$table .= "Submode | ";
$table .= "Band | ";
$table .= "eQSL QTH Nickname | ";
$table .= "
";
foreach ($qslsnotsent as $qsl) {
$table .= "";
$timestamp = strtotime($qsl['COL_TIME_ON']);
$table .= "| " . date($custom_date_format, $timestamp) . " | ";
$table .= "" . date('H:i', $timestamp) . " | ";
$table .= "" . str_replace("0", "Ø", strtoupper($qsl['COL_CALL'])) . " | ";
$table .= "" . $qsl['COL_MODE'] . " | ";
if (isset($qsl['COL_SUBMODE'])) {
$table .= "" . $qsl['COL_SUBMODE'] . " | ";
} else {
$table .= " | ";
}
$table .= "" . $qsl['COL_BAND'] . " | ";
$table .= "" . $qsl['eqslqthnickname'] . " | ";
$table .= "
";
}
$table .= "
";
return $table;
}
function image($id)
{
$this->load->library('electronicqsl');
$this->load->model('Eqsl_images');
if ($this->Eqsl_images->get_image($id) == "No Image") {
$this->load->model('logbook_model');
$this->load->model('user_model');
$qso_query = $this->logbook_model->get_qso($id);
$qso = $qso_query->row();
$qso_timestamp = strtotime($qso->COL_TIME_ON);
$callsign = $qso->COL_CALL;
$band = $qso->COL_BAND;
$mode = $qso->COL_MODE;
$year = date('Y', $qso_timestamp);
$month = date('m', $qso_timestamp);
$day = date('d', $qso_timestamp);
$hour = date('H', $qso_timestamp);
$minute = date('i', $qso_timestamp);
$query = $this->user_model->get_by_id($this->session->userdata('user_id'));
$q = $query->row();
$username = $qso->COL_STATION_CALLSIGN;
$password = $this->decode_eqsl_password($q->user_eqsl_password);
$this->load->model('eqsl_mappings_model');
$station_mapping = $this->eqsl_mappings_model->get_preferred_mapping_for_station($this->session->userdata('user_id'), $qso->station_id);
if ($station_mapping != null) {
$password = $station_mapping['eqsl_password'];
}
$image_url = $this->electronicqsl->card_image($username, urlencode($password), $callsign, $band, $mode, $year, $month, $day, $hour, $minute);
$file = file_get_contents($image_url, true);
$dom = new domDocument;
$dom->loadHTML($file);
$dom->preserveWhiteSpace = false;
$images = $dom->getElementsByTagName('img');
if (!isset($images) || count($images) == 0) {
echo "Rate Limited";
exit;
}
foreach ($images as $image) {
header('Content-Type: image/jpg');
$content = file_get_contents("https://www.eqsl.cc" . $image->getAttribute('src'));
if ($content === false) {
echo "No response";
exit;
}
echo $content;
$filename = uniqid() . '.jpg';
if (file_put_contents('images/eqsl_card_images/' . '/' . $filename, $content) !== false) {
$this->Eqsl_images->save_image($id, $filename);
}
}
} else {
header('Content-Type: image/jpg');
$image_url = base_url('images/eqsl_card_images/' . $this->Eqsl_images->get_image($id));
header('Location: ' . $image_url);
}
}
function bulk_download_image($id)
{
$this->load->library('electronicqsl');
$this->load->model('Eqsl_images');
$this->load->model('logbook_model');
$this->load->model('user_model');
$qso_query = $this->logbook_model->get_qso($id);
$qso = $qso_query->row();
$qso_timestamp = strtotime($qso->COL_TIME_ON);
$callsign = $qso->COL_CALL;
$band = $qso->COL_BAND;
$mode = $qso->COL_MODE;
$year = date('Y', $qso_timestamp);
$month = date('m', $qso_timestamp);
$day = date('d', $qso_timestamp);
$hour = date('H', $qso_timestamp);
$minute = date('i', $qso_timestamp);
$query = $this->user_model->get_by_id($this->session->userdata('user_id'));
$q = $query->row();
$username = $qso->COL_STATION_CALLSIGN;
$password = $this->decode_eqsl_password($q->user_eqsl_password);
$this->load->model('eqsl_mappings_model');
$station_mapping = $this->eqsl_mappings_model->get_preferred_mapping_for_station($this->session->userdata('user_id'), $qso->station_id);
if ($station_mapping != null) {
$password = $station_mapping['eqsl_password'];
}
$error = '';
$image_url = $this->electronicqsl->card_image($username, urlencode($password), $callsign, $band, $mode, $year, $month, $day, $hour, $minute);
$file = file_get_contents($image_url, true);
if (strpos($file, 'Error') !== false) {
$error = rtrim(preg_replace('/^\s*Error: /', '', $file));
return $error;
}
$dom = new domDocument;
$dom->loadHTML($file);
$dom->preserveWhiteSpace = false;
$images = $dom->getElementsByTagName('img');
if (!isset($images) || count($images) == 0) {
$error = "Rate Limited";
return $error;
}
foreach ($images as $image) {
$content = file_get_contents("https://www.eqsl.cc" . $image->getAttribute('src'));
if ($content === false) {
$error = "No response";
return $error;
}
$filename = uniqid() . '.jpg';
if (file_put_contents('images/eqsl_card_images/' . '/' . $filename, $content) !== false) {
$this->Eqsl_images->save_image($id, $filename);
}
}
return $error;
}
public function tools()
{
// Check logged in
$this->load->model('user_model');
if (!$this->user_model->authorize(2)) {
$this->session->set_flashdata('notice', 'You\'re not allowed to do that!');
redirect('dashboard');
}
$data['page_title'] = "eQSL Tools";
// Load frontend
$this->load->view('interface_assets/header', $data);
$this->load->view('eqsl/tools');
$this->load->view('interface_assets/footer');
}
public function mappings()
{
$this->load->model('user_model');
if (!$this->user_model->authorize(2)) {
$this->session->set_flashdata('notice', 'You\'re not allowed to do that!');
redirect('dashboard');
}
$this->load->model('eqsl_mappings_model');
$this->load->model('stations');
$data['page_title'] = 'eQSL Mappings';
$data['station_profile'] = $this->stations->all_of_user();
$data['mappings'] = $this->eqsl_mappings_model->get_user_mappings($this->session->userdata('user_id'));
$editing_mapping_id = (int) $this->input->get('edit');
$data['editing_mapping'] = null;
if ($editing_mapping_id > 0) {
$data['editing_mapping'] = $this->eqsl_mappings_model->get_mapping_by_id_for_user($editing_mapping_id, $this->session->userdata('user_id'));
}
$this->load->view('interface_assets/header', $data);
$this->load->view('eqsl/mappings');
$this->load->view('interface_assets/footer');
}
public function save_mapping()
{
$this->load->model('user_model');
if (!$this->user_model->authorize(2)) {
$this->session->set_flashdata('notice', 'You\'re not allowed to do that!');
redirect('dashboard');
}
$this->load->model('eqsl_mappings_model');
$this->load->model('stations');
$user_id = (int) $this->session->userdata('user_id');
$mapping_id = (int) $this->input->post('mapping_id');
$station_id = (int) $this->input->post('station_id');
$eqsl_username = $this->security->xss_clean((string) $this->input->post('eqsl_username', true));
$eqsl_password = $this->security->xss_clean((string) $this->input->post('eqsl_password', true));
$eqsl_qth_nickname = $this->security->xss_clean((string) $this->input->post('eqsl_qth_nickname', true));
$enabled = $this->input->post('enabled') ? 1 : 0;
$preferred_for_download = $this->input->post('preferred_for_download') ? 1 : 0;
if (!$this->stations->check_station_is_accessible($station_id)) {
$this->session->set_flashdata('error', 'Selected station location is not accessible.');
redirect('eqsl/mappings');
}
if (trim($eqsl_username) === '' || trim($eqsl_qth_nickname) === '') {
$this->session->set_flashdata('error', 'Username and QTH nickname are required.');
redirect('eqsl/mappings');
}
if ($mapping_id > 0) {
$mapping = $this->eqsl_mappings_model->get_mapping_by_id_for_user($mapping_id, $user_id);
if ($mapping == null) {
$this->session->set_flashdata('error', 'Mapping not found.');
redirect('eqsl/mappings');
}
if (trim($eqsl_password) === '') {
$eqsl_password = $mapping['eqsl_password'];
}
if (trim($eqsl_password) === '') {
$reused_password = $this->eqsl_mappings_model->get_password_for_user_and_username($user_id, $eqsl_username);
if ($reused_password !== null) {
$eqsl_password = $reused_password;
}
}
if (trim($eqsl_password) === '') {
$this->session->set_flashdata('error', 'Password is required for a new eQSL username.');
redirect('eqsl/mappings');
}
$updated = $this->eqsl_mappings_model->update_mapping($mapping_id, $user_id, $station_id, $eqsl_username, $eqsl_password, $eqsl_qth_nickname, $enabled, $preferred_for_download);
if ($updated === false) {
$reason = $this->eqsl_mappings_model->get_last_failure_reason();
$db_error = $this->eqsl_mappings_model->get_last_db_error();
if ($reason === 'encryption_failed') {
$this->session->set_flashdata('error', 'Unable to secure the eQSL password. Please ask your administrator to check encryption configuration.');
} elseif ((int) ($db_error['code'] ?? 0) === 1406) {
$this->session->set_flashdata('error', 'Unable to save mapping because the database schema is out of date. Please run database migration 275 (encrypt eQSL passwords).');
} else {
$this->session->set_flashdata('error', 'Unable to update mapping due to a database error. Please check logs and try again.');
}
log_message('error', 'eQSL mapping update failed for user ' . (int) $user_id . ': reason=' . $reason . ', db_code=' . (int) ($db_error['code'] ?? 0) . ', db_message=' . ($db_error['message'] ?? ''));
} else {
$this->session->set_flashdata('success', 'eQSL mapping updated.');
}
} else {
$duplicate_mapping = $this->eqsl_mappings_model->find_duplicate_mapping(
$user_id,
$station_id,
$eqsl_username,
$eqsl_qth_nickname
);
if ($duplicate_mapping != null) {
$updated = $this->eqsl_mappings_model->update_mapping(
$duplicate_mapping['mapping_id'],
$user_id,
$station_id,
$eqsl_username,
$eqsl_password,
$eqsl_qth_nickname,
$enabled,
$preferred_for_download
);
if ($updated === false) {
$this->session->set_flashdata('error', 'Mapping already exists, but updating it failed. Please verify encryption settings and try again.');
} else {
$this->session->set_flashdata('success', 'Mapping already existed. Existing mapping was updated instead.');
}
$this->user_model->update_session($user_id);
redirect('eqsl/mappings');
}
if (trim($eqsl_password) === '') {
$reused_password = $this->eqsl_mappings_model->get_password_for_user_and_username($user_id, $eqsl_username);
if ($reused_password !== null) {
$eqsl_password = $reused_password;
}
}
if (trim($eqsl_password) === '') {
$this->session->set_flashdata('error', 'Password is required the first time you use an eQSL username.');
redirect('eqsl/mappings');
}
$created = $this->eqsl_mappings_model->create_mapping($user_id, $station_id, $eqsl_username, $eqsl_password, $eqsl_qth_nickname, $enabled, $preferred_for_download);
if ($created === false) {
$fuzzy_duplicate_mapping = $this->eqsl_mappings_model->find_duplicate_mapping_fuzzy(
$user_id,
$station_id,
$eqsl_username,
$eqsl_qth_nickname
);
if ($fuzzy_duplicate_mapping != null) {
$updated = $this->eqsl_mappings_model->update_mapping(
$fuzzy_duplicate_mapping['mapping_id'],
$user_id,
$station_id,
$eqsl_username,
$eqsl_password,
$eqsl_qth_nickname,
$enabled,
$preferred_for_download
);
if ($updated === false) {
$reason = $this->eqsl_mappings_model->get_last_failure_reason();
$db_error = $this->eqsl_mappings_model->get_last_db_error();
if ($reason === 'encryption_failed') {
$this->session->set_flashdata('error', 'Mapping exists, but password encryption failed. Please ask your administrator to check encryption configuration.');
} elseif ((int) ($db_error['code'] ?? 0) === 1406) {
$this->session->set_flashdata('error', 'Mapping exists, but the database schema is out of date. Please run database migration 275 (encrypt eQSL passwords).');
} else {
$this->session->set_flashdata('error', 'Mapping exists, but updating it failed due to a database error. Please check logs and try again.');
}
log_message('error', 'eQSL duplicate mapping auto-update failed for user ' . (int) $user_id . ': reason=' . $reason . ', db_code=' . (int) ($db_error['code'] ?? 0) . ', db_message=' . ($db_error['message'] ?? ''));
} else {
$this->session->set_flashdata('success', 'Mapping already existed. Existing mapping was updated instead.');
}
} else {
$reason = $this->eqsl_mappings_model->get_last_failure_reason();
$db_error = $this->eqsl_mappings_model->get_last_db_error();
if ($reason === 'encryption_failed') {
$this->session->set_flashdata('error', 'Unable to secure the eQSL password. Please ask your administrator to check encryption configuration.');
} elseif ((int) ($db_error['code'] ?? 0) === 1406) {
$this->session->set_flashdata('error', 'Unable to create mapping because the database schema is out of date. Please run database migration 275 (encrypt eQSL passwords).');
} else {
$this->session->set_flashdata('error', 'Unable to create mapping due to a database error. Please check logs and try again.');
}
log_message('error', 'eQSL mapping create failed for user ' . (int) $user_id . ': reason=' . $reason . ', db_code=' . (int) ($db_error['code'] ?? 0) . ', db_message=' . ($db_error['message'] ?? ''));
}
} else {
$this->session->set_flashdata('success', 'eQSL mapping created.');
}
}
$this->user_model->update_session($user_id);
redirect('eqsl/mappings');
}
public function delete_mapping()
{
$this->load->model('user_model');
if (!$this->user_model->authorize(2)) {
$this->session->set_flashdata('notice', 'You\'re not allowed to do that!');
redirect('dashboard');
}
$this->load->model('eqsl_mappings_model');
$user_id = (int) $this->session->userdata('user_id');
$mapping_id = (int) $this->input->post('mapping_id');
if ($mapping_id <= 0) {
$this->session->set_flashdata('error', 'Invalid mapping selected.');
redirect('eqsl/mappings');
}
if ($this->eqsl_mappings_model->delete_mapping($mapping_id, $user_id)) {
$this->session->set_flashdata('success', 'eQSL mapping deleted.');
} else {
$this->session->set_flashdata('error', 'Unable to delete mapping.');
}
$this->user_model->update_session($user_id);
redirect('eqsl/mappings');
}
public function download()
{
// Check logged in
$this->load->model('user_model');
if (!$this->user_model->authorize(2)) {
$this->session->set_flashdata('notice', 'You\'re not allowed to do that!');
redirect('dashboard');
}
$errors = 0;
$this->load->model('eqsl_mappings_model');
$user_id = (int) $this->session->userdata('user_id');
$active_mappings = $this->eqsl_mappings_model->get_active_mappings_for_user($user_id);
$use_mapping_mode = !empty($active_mappings);
$mapped_station_ids = array_map(function ($mapping) {
return (int) $mapping['station_id'];
}, $active_mappings);
$data['eqsl_mapping_mode'] = $use_mapping_mode;
$data['eqsl_legacy_fallback'] = !$use_mapping_mode;
$data['eqsl_mappings_count'] = count($active_mappings);
if ($this->input->post('eqsldownload') == 'download') {
$i = 0;
$this->load->model('eqslmethods_model');
if ($use_mapping_mode) {
$qslsnotdownloaded = $this->eqslmethods_model->eqsl_not_yet_downloaded_for_station_ids($mapped_station_ids);
} else {
$qslsnotdownloaded = $this->eqslmethods_model->eqsl_not_yet_downloaded();
}
$eqsl_results = array();
foreach ($qslsnotdownloaded->result_array() as $qsl) {
$result = $this->bulk_download_image($qsl['COL_PRIMARY_KEY']);
if ($result != '') {
$errors++;
if ($result == 'Rate Limited') {
break;
} else {
$eqsl_results[] = array(
'date' => $qsl['COL_TIME_ON'],
'call' => $qsl['COL_CALL'],
'mode' => $qsl['COL_MODE'],
'submode' => $qsl['COL_SUBMODE'],
'status' => $result,
'qsoid' => $qsl['COL_PRIMARY_KEY']
);
continue;
}
} else {
$i++;
}
if ($i > 0) {
sleep(15);
}
}
$data['eqsl_results'] = $eqsl_results;
$data['eqsl_stats'] = "Successfully downloaded: " . $i . " / Errors: " . count($eqsl_results);
$data['page_title'] = "eQSL Download Information";
$this->load->view('interface_assets/header', $data);
$this->load->view('eqsl/result');
$this->load->view('interface_assets/footer');
} else {
$data['page_title'] = "eQSL Card Image Download";
$this->load->model('eqslmethods_model');
$data['custom_date_format'] = $this->session->userdata('user_date_format');
if ($use_mapping_mode) {
$data['qslsnotdownloaded'] = $this->eqslmethods_model->eqsl_not_yet_downloaded_for_station_ids($mapped_station_ids);
} else {
$data['qslsnotdownloaded'] = $this->eqslmethods_model->eqsl_not_yet_downloaded();
}
$this->load->view('interface_assets/header', $data);
$this->load->view('eqsl/download');
$this->load->view('interface_assets/footer');
}
}
public function mark_all_sent()
{
// Check logged in
$this->load->model('user_model');
if (!$this->user_model->authorize(2)) {
$this->session->set_flashdata('notice', 'You\'re not allowed to do that!');
redirect('dashboard');
}
// mark all eqsls as sent
$this->load->model('eqslmethods_model');
$this->eqslmethods_model->mark_all_as_sent();
$this->session->set_flashdata('success', 'All eQSLs Marked as Uploaded');
redirect('eqsl/tools');
}
/*
* Used for CRON job
*/
public function sync()
{
ini_set('memory_limit', '-1');
set_time_limit(0);
$this->load->model('eqslmethods_model');
$this->load->model('eqsl_mappings_model');
$eqsl_mappings = $this->eqsl_mappings_model->get_active_mappings_for_sync();
if (!empty($eqsl_mappings)) {
foreach ($eqsl_mappings as $mapping) {
$this->uploadMapping($mapping);
if ((int) $mapping['preferred_for_download'] === 1) {
$this->downloadMapping($mapping);
}
}
return;
}
$users = $this->eqslmethods_model->get_eqsl_users();
foreach ($users as $user) {
$decoded_password = $this->decode_eqsl_password($user->user_eqsl_password);
$this->uploadUser($user->user_id, $user->user_eqsl_name, $decoded_password);
$this->downloadUser($user->user_id, $user->user_eqsl_name, $decoded_password);
}
}
public function downloadUser($userid, $username, $password)
{
$this->load->library('EqslImporter');
$this->load->model('eqslmethods_model');
$config['upload_path'] = './uploads/';
$eqsl_locations = $this->eqslmethods_model->all_of_user_with_eqsl_nick_defined($userid);
$eqsl_results = array();
foreach ($eqsl_locations->result_array() as $eqsl_location) {
$this->eqslimporter->from_callsign_and_QTH(
$eqsl_location['station_callsign'],
$eqsl_location['eqslqthnickname'],
$config['upload_path'],
$eqsl_location['station_id']
);
$eqsl_results[] = $this->eqslimporter->fetch($password);
}
}
private function downloadMapping($mapping)
{
$this->load->library('EqslImporter');
$config['upload_path'] = './uploads/';
$this->eqslimporter->from_callsign_and_QTH(
$mapping['station_callsign'],
$mapping['eqsl_qth_nickname'],
$config['upload_path'],
$mapping['station_id']
);
$this->eqslimporter->fetch($mapping['eqsl_password']);
}
private function uploadMapping($mapping)
{
$this->load->model('eqslmethods_model');
$data['user_eqsl_name'] = $mapping['eqsl_username'];
$data['user_eqsl_password'] = $mapping['eqsl_password'];
$qslsnotsent = $this->eqslmethods_model->eqsl_not_yet_sent_for_station($mapping['station_id'], $mapping['eqsl_qth_nickname']);
$successful_uploads = array();
foreach ($qslsnotsent->result_array() as $qsl) {
$qsl['eqslqthnickname'] = $mapping['eqsl_qth_nickname'];
$adif = $this->generateAdif($qsl, $data);
$this->uploadQso($adif, $qsl, $successful_uploads);
}
if (!empty($successful_uploads)) {
$this->eqslmethods_model->eqsl_mark_sent_batch($successful_uploads);
}
}
function uploadUser($userid, $username, $password)
{
$data['user_eqsl_name'] = $this->security->xss_clean($username);
$data['user_eqsl_password'] = $this->security->xss_clean($password);
$clean_userid = $this->security->xss_clean($userid);
$qslsnotsent = $this->eqslmethods_model->eqsl_not_yet_sent($clean_userid);
foreach ($qslsnotsent->result_array() as $qsl) {
$adif = $this->generateAdif($qsl, $data);
$status = $this->uploadQso($adif, $qsl);
}
}
private function decode_eqsl_password($stored_password)
{
$stored_password = (string) $stored_password;
if ($stored_password === '') {
return '';
}
if (strpos($stored_password, 'enc:') === 0) {
$cipher = substr($stored_password, 4);
$decrypted = $this->encryption->decrypt($cipher);
if ($decrypted !== false && $decrypted !== null) {
return $decrypted;
}
return '';
}
// Backward compatibility for existing plaintext credentials.
return $stored_password;
}
} // end class