From 032e77957fab0d45b260a55b556b4ea3d588e7a0 Mon Sep 17 00:00:00 2001 From: int2001 Date: Mon, 27 Nov 2023 12:55:16 +0000 Subject: [PATCH] Clublog-RT-Upload --- application/controllers/Clublog.php | 67 ---------------- application/models/Logbook_model.php | 110 ++++++++++++++++++++++++--- 2 files changed, 98 insertions(+), 79 deletions(-) diff --git a/application/controllers/Clublog.php b/application/controllers/Clublog.php index e4b77fdf..8472b5da 100644 --- a/application/controllers/Clublog.php +++ b/application/controllers/Clublog.php @@ -137,73 +137,6 @@ class Clublog extends CI_Controller { } - public function realtime($username) { - $clean_username = $this->security->xss_clean($username); - - $this->load->model('stations'); - $this->load->model('clublog_model'); - - $clublog_info = $this->clublog_model->get_clublog_auth_info($clean_username); - - if(!isset($clublog_info['user_name'])) { - echo "Username unknown"; - exit; - } - - $station_profiles = $this->stations->all_with_count(); - - // if station profiles exist - if($station_profiles->num_rows()){ - // Loop through station profiles - foreach ($station_profiles->result() as $station_row) - { - // if the station profile has more than 1 qso - if($station_row->qso_total > 0) { - $myqsos = $this->clublog_model->get_last_five($station_row->station_id); - - foreach ($myqsos->result() as $qso) - { - $data['qso'] = $qso; - $adif_string = $this->load->view('adif/data/clublog_realtime', $data, true); - - // initialise the curl request - $request = curl_init('https://clublog.org/realtime.php'); - - curl_setopt($request, CURLOPT_POST, true); - curl_setopt( - $request, - CURLOPT_POSTFIELDS, - array( - 'email' => $clublog_info['user_clublog_name'], - 'password' => $clublog_info['user_clublog_password'], - 'callsign' => $station_row->station_callsign, - 'adif' => $adif_string, - 'api' => "a11c3235cd74b88212ce726857056939d52372bd", - )); - - // output the response - curl_setopt($request, CURLOPT_RETURNTRANSFER, true); - $response = curl_exec($request); - $info = curl_getinfo($request); - - // If Clublog Accepts mark the QSOs - if (preg_match('/\bOK\b/', $response)) { - echo "QSOs uploaded and Logbook QSOs marked as sent to Clublog
"; - - $this->clublog_model->mark_qso_sent($qso->COL_PRIMARY_KEY); - echo "Clublog upload for ".$station_row->station_callsign."
"; - } else { - echo "Error ".$response."
"; - } - curl_close ($request); - } - } else { - echo "No QSOs to upload"; - } - } - } - } - // Find DXCC function find_dxcc($callsign) { $clean_callsign = $this->security->xss_clean($callsign); diff --git a/application/models/Logbook_model.php b/application/models/Logbook_model.php index 69f06e15..f19d5fb5 100755 --- a/application/models/Logbook_model.php +++ b/application/models/Logbook_model.php @@ -565,6 +565,20 @@ class Logbook_model extends CI_Model { if (!$skipexport) { + $result = $this->exists_clublog_credentials($data['station_id']); + if (isset($result->user_clublog_password) && isset($result->user_clublog_name)) { // && $result->hrdlogrealtime == 1) { + $CI =& get_instance(); + $CI->load->library('AdifHelper'); + $qso = $this->get_qso($last_id,true)->result(); + + $adif = $CI->adifhelper->getAdifLine($qso[0]); + $result = $this->push_qso_to_clublog($result->user_clublog_name, $result->user_clublog_password, $data['COL_STATION_CALLSIGN'], $adif); + if ( ($result['status'] == 'OK') || ( ($result['status'] == 'error') || ($result['status'] == 'duplicate') || ($result['status'] == 'auth_error') )){ + $this->mark_clublog_qso_sent($last_id); + } + } + + $result = ''; $result = $this->exists_hrdlog_code($data['station_id']); // Push qso to hrdlog if code is set, and realtime upload is enabled, and we're not importing an adif-file if (isset($result->hrdlog_code) && $result->hrdlogrealtime == 1) { @@ -632,6 +646,26 @@ class Logbook_model extends CI_Model { } } + /* + * Function checks if a Cloudlog Credebtials exists in the table with the given station id + */ + function exists_clublog_credentials($station_id) { + $sql = 'select auth.user_clublog_name, auth.user_clublog_password from '.$this->config->item('auth_table').' auth inner join station_profile prof on (auth.user_id=prof.user_id) where prof.station_id = ' . $station_id; + + log_message("Error",$sql); + $query = $this->db->query($sql); + + $result = $query->row(); + + if ($result) { + return $result; + } + else { + return false; + } + } + + /* * Function checks if a QRZ API Key exists in the table with the given station id */ @@ -654,21 +688,55 @@ class Logbook_model extends CI_Model { /* * Function checks if a WebADIF API Key exists in the table with the given station id */ - function exists_webadif_api_key($station_id) { - $sql = 'select webadifapikey, webadifapiurl, webadifrealtime from station_profile - where station_id = ' . $station_id; + function exists_webadif_api_key($station_id) { + $sql = 'select webadifapikey, webadifapiurl, webadifrealtime from station_profile + where station_id = ' . $station_id; - $query = $this->db->query($sql); + $query = $this->db->query($sql); - $result = $query->row(); + $result = $query->row(); - if ($result) { - return $result; - } - else { - return false; - } - } + if ($result) { + return $result; + } + else { + return false; + } + } + + function push_qso_to_clublog($cl_username, $cl_password, $station_callsign, $adif) { + + // initialise the curl request + $returner=[]; + $request = curl_init('https://clublog.org/realtime.php'); + + curl_setopt($request, CURLOPT_POST, true); + curl_setopt( + $request, + CURLOPT_POSTFIELDS, + array( + 'email' => $cl_username, + 'password' => $cl_password, + 'callsign' => $station_callsign, + 'adif' => $adif, + 'api' => "a11c3235cd74b88212ce726857056939d52372bd", + )); + + // output the response + curl_setopt($request, CURLOPT_RETURNTRANSFER, true); + $response = curl_exec($request); + $info = curl_getinfo($request); + + // If Clublog Accepts mark the QSOs + if (preg_match('/\bOK\b/', $response)) { + $returner['status']='OK'; + } else { + $returner['status']=$response; + } + curl_close ($request); + log_message("Error","Req: ".$adif." /// Resp: ".$response); + return ($returner); + } /* * Function uploads a QSO to HRDLog with the API given. @@ -795,6 +863,24 @@ class Logbook_model extends CI_Model { return $response === 200; } + /* + * Function marks QSOs as uploaded to Clublog + * $primarykey is the unique id for that QSO in the logbook + */ + function mark_clublog_qsos_sent($primarykey) { + $data = array( + 'COL_CLUBLOG_QSO_UPLOAD_DATE' => date("Y-m-d H:i:s", strtotime("now")), + 'COL_CLUBLOG_QSO_UPLOAD_STATUS' => 'Y', + ); + + $this->db->where('COL_PRIMARY_KEY', $primarykey); + + $this->db->update($this->config->item('table_name'), $data); + + return true; + } + + /* * Function marks QSOs as uploaded to HRDLog. * $primarykey is the unique id for that QSO in the logbook