move the first functions

This commit is contained in:
HB9HIL
2024-06-03 07:45:22 +02:00
parent d32293e809
commit 6cf405e0bd
2 changed files with 306 additions and 297 deletions

View File

@@ -2,6 +2,306 @@
class Eqslmethods_model extends CI_Model {
function sync() {
ini_set('memory_limit', '-1');
set_time_limit(0);
$users = $this->get_eqsl_users();
foreach ($users as $user) {
$this->uploadUser($user->user_id, $user->user_eqsl_name, $user->user_eqsl_password);
$this->downloadUser($user->user_id, $user->user_eqsl_name, $user->user_eqsl_password);
}
}
function downloadUser($userid, $username, $password) {
$this->load->library('EqslImporter');
$config['upload_path'] = './uploads/';
$eqsl_locations = $this->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);
}
}
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) {
$data['user_eqsl_name'] = $qsl['station_callsign'];
$adif = $this->generateAdif($qsl, $data);
$status = $this->uploadQso($adif, $qsl);
}
}
// 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=WavelogUpload%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 uploadQso($adif, $qsl) {
$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";
$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.
$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 mark_all_as_sent() {
$data = array(
'COL_EQSL_QSL_SENT' => 'Y',