diff --git a/application/controllers/Clublog.php b/application/controllers/Clublog.php index c70930189..6c2bf0123 100644 --- a/application/controllers/Clublog.php +++ b/application/controllers/Clublog.php @@ -61,6 +61,10 @@ class Clublog extends CI_Controller if (!empty($users)) { foreach ($users as $user) { $r = $this->clublog_model->downloadUser($user->user_id, $user->user_clublog_name, $user->user_clublog_password); + if ($r == 'Impossible to reach Clublog') { // Stop Download immediatly, because we're blocked + log_message("Error","We're blocked by Clublog. Stopping Download!"); + break; + } } } else { $r = __("No user has configured Clublog."); diff --git a/application/models/Clublog_model.php b/application/models/Clublog_model.php index 89cfbc375..109bb06bd 100644 --- a/application/models/Clublog_model.php +++ b/application/models/Clublog_model.php @@ -18,7 +18,7 @@ class Clublog_model extends CI_Model function uploadUser($userid, $username, $password, $station_id = null) { $clean_username = $this->security->xss_clean($username); - $clean_passord = $this->security->xss_clean($password); + $clean_password = $this->security->xss_clean($password); $clean_userid = $this->security->xss_clean($userid); $return = "No QSOs to upload"; @@ -45,88 +45,106 @@ class Clublog_model extends CI_Model if ($data['qsos']->num_rows()) { $string = $this->load->view('adif/data/clublog', $data, TRUE); - $ranid = uniqid(); - - if (!write_file('uploads/clublog' . $ranid . $station_row->station_id . '.adi', $string)) { - $return = 'Unable to write the file - Make the folder Upload folder has write permissions.'; - } else { - - // initialise the curl request - $request = curl_init('https://clublog.org/putlogs.php'); - $filepath = realpath('uploads/clublog' . $ranid . $station_row->station_id . '.adi'); - - // Check if the file actually exists - if (!file_exists($filepath)) { - $return .= " Clublog upload for " . $station_row->station_callsign . ' failed. Upload file could not be created.'; - log_message('error', $return); - return $return . "\n"; - } - - if (function_exists('curl_file_create')) { // php 5.5+ - $cFile = curl_file_create($filepath); + if ($data['qsos']->num_rows() == 1) { // exactly ONE QSO --> use their realtime.api as demanded by clublog + $singlepush=$this->push_qso_to_clublog($clean_username, $clean_password, $station_row->station_callsign, $string, $station_id); + if ($singlepush['status'] == 'OK') { + $this->mark_qsos_sent($station_row->station_id); + log_message('info', 'Clublog singlepush upload for ' . $station_row->station_callsign . ' successfully sent and marked.'); } else { - $cFile = '@' . $filepath; + log_message("Error", "Singlepush for ".$station_row->station_id." / ".$station_row->station_callsign." failed: ".$singlepush['status']); } - $cFile->setPostFilename(basename($filepath)); + } else { + $ranid = uniqid(); + if (!write_file('uploads/clublog' . $ranid . $station_row->station_id . '.adi', $string)) { + $return = 'Unable to write the file - Make the folder Upload folder has write permissions.'; + } else { - // send a file - curl_setopt($request, CURLOPT_POST, true); - curl_setopt($request, CURLOPT_TIMEOUT, 10); - curl_setopt( - $request, - CURLOPT_POSTFIELDS, - array( - 'email' => $clean_username, - 'password' => $clean_passord, - 'callsign' => $station_row->station_callsign, - 'api' => $this->clublog_identifier, - 'file' => $cFile + // initialise the curl request + $request = curl_init('https://clublog.org/putlogs.php'); + $filepath = realpath('uploads/clublog' . $ranid . $station_row->station_id . '.adi'); + + // Check if the file actually exists + if (!file_exists($filepath)) { + $return .= " Clublog upload for " . $station_row->station_callsign . ' failed. Upload file could not be created.'; + log_message('error', $return); + return $return . "\n"; + } + + if (function_exists('curl_file_create')) { // php 5.5+ + $cFile = curl_file_create($filepath); + } else { + $cFile = '@' . $filepath; + } + $cFile->setPostFilename(basename($filepath)); + + // send a file + curl_setopt($request, CURLOPT_POST, true); + curl_setopt($request, CURLOPT_TIMEOUT, 10); + curl_setopt( + $request, + CURLOPT_POSTFIELDS, + array( + 'email' => $clean_username, + 'password' => $clean_password, + 'callsign' => $station_row->station_callsign, + 'api' => $this->clublog_identifier, + 'file' => $cFile ) ); - // output the response - curl_setopt($request, CURLOPT_RETURNTRANSFER, true); - $response = curl_exec($request); - $info = curl_getinfo($request); - $httpcode = curl_getinfo($request, CURLINFO_HTTP_CODE); - if (curl_errno($request)) { - $return = curl_error($request); - } - curl_close($request); - - // If Clublog Accepts mark the QSOs - if (($httpcode == 200) || (preg_match('/\baccepted\b/', $response))) { - $return = "QSOs uploaded and Logbook QSOs marked as sent to Clublog."; - $this->mark_qsos_sent($station_row->station_id); - $return .= " Clublog upload for " . $station_row->station_callsign . ' successfully sent.'; - log_message('info', 'Clublog upload for ' . $station_row->station_callsign . ' successfully sent and marked.'); - } else if (preg_match('/checksum duplicate/', $response)) { // Be safe, if Michael rolls back to 403 on duplicate - $return = "QSOs uploaded (as duplicate!) and Logbook QSOs marked as sent to Clublog"; - $this->mark_qsos_sent($station_row->station_id); - $return .= " Clublog upload for " . $station_row->station_callsign . ' successfully sent.'; - log_message('info', 'Clublog DUPLICATE upload for ' . $station_row->station_callsign . ' successfully sent and marked.'); - } else { - $return = 'Clublog upload for ' . $station_row->station_callsign . ' failed reason ' . $response.' // HTTP:'.$httpcode.' / '.$return; - log_message('error', $return); - if (substr($response,0,13) == 'Upload denied') { // Deactivate Upload for Station if Clublog rejects it due to non-configured Call (prevent being blacklisted at Clublog) - log_message('info', 'Deactivated upload for station ' . $station_row->station_callsign . ' due to non-configured Call (prevent being blacklisted at Clublog.'); - $sql = 'update station_profile set clublogignore = 1 where station_id = ?'; - $this->db->query($sql,$station_row->station_id); - } else if (substr($response,0,14) == 'Login rejected') { // Deactivate Upload for Station if Clublog rejects it due to wrong credentials (prevent being blacklisted at Clublog) - log_message('info', 'Deactivated upload for station ' . $station_row->station_callsign . ' due to wrong credentials (prevent being blacklisted at Clublog.'); - $sql = 'update station_profile set clublogignore = 1 where station_id = ?'; - $this->db->query($sql,$station_row->station_id); - } else if ($httpcode == 403) { - log_message('info', 'Deactivated upload for station ' . $station_row->station_callsign . ' due to 403 (prevent being blacklisted at Clublog.'); - $sql = 'update station_profile set clublogignore = 1 where station_id = ?'; - $this->db->query($sql,$station_row->station_id); - } else { - log_message('error', 'Some uncaught exception for station ' . $station_row->station_callsign); + // output the response + curl_setopt($request, CURLOPT_RETURNTRANSFER, true); + $response = curl_exec($request); + $info = curl_getinfo($request); + $httpcode = curl_getinfo($request, CURLINFO_HTTP_CODE); + if (curl_errno($request)) { + $return = curl_error($request); } - } + curl_close($request); - // Delete the ADIF file used for clublog - unlink('uploads/clublog' . $ranid . $station_row->station_id . '.adi'); + // If Clublog Accepts mark the QSOs + if (($httpcode == 200) || (preg_match('/\baccepted\b/', $response))) { + $return = "QSOs uploaded and Logbook QSOs marked as sent to Clublog."; + $this->mark_qsos_sent($station_row->station_id); + $return .= " Clublog upload for " . $station_row->station_callsign . ' successfully sent.'; + log_message('info', 'Clublog upload for ' . $station_row->station_callsign . ' successfully sent and marked.'); + } else if (preg_match('/too many uploads already queued/', $response)) { // New Error, Clublog has Backlog, skip for NOW + $return = 'Clublog upload for ' . $station_row->station_callsign . ' failed, clublog tells backlog there. Skipping whole account for this cycle. Detailled reason ' . $response.' // HTTP:'.$httpcode.' / '.$return; + log_message('Error', 'Clublog upload for ' . $station_row->station_callsign . ' has become a victim of clublog-Backlog. Skipping full User for this cycle.'); + unlink('uploads/clublog' . $ranid . $station_row->station_id . '.adi'); + break; + } else if (preg_match('/No QSOs to upload/', $response)) { // Means: Already uploaded (but not marked) - perhaps different logtool, who knows. + $this->mark_qsos_sent($station_row->station_id); + $return = " Clublog upload for " . $station_row->station_callsign . ' successfully sent.'; + log_message('info', 'Clublog No QSOs to upload for ' . $station_row->station_callsign . '. preventive marked.'); + } else if (preg_match('/checksum duplicate/', $response)) { // Be safe, if Michael rolls back to 403 on duplicate + $return = "QSOs uploaded (as duplicate!) and Logbook QSOs marked as sent to Clublog"; + $this->mark_qsos_sent($station_row->station_id); + $return .= " Clublog upload for " . $station_row->station_callsign . ' successfully sent.'; + log_message('info', 'Clublog DUPLICATE upload for ' . $station_row->station_callsign . ' successfully sent and marked.'); + } else { + $return = 'Clublog upload for ' . $station_row->station_callsign . ' failed reason ' . $response.' // HTTP:'.$httpcode.' / '.$return; + log_message('error', $return); + if (substr($response,0,13) == 'Upload denied') { // Deactivate Upload for Station if Clublog rejects it due to non-configured Call (prevent being blacklisted at Clublog) + log_message('Error', 'Deactivated upload for station ' . $station_row->station_callsign . ' due to non-configured Call (prevent being blacklisted at Clublog.'); + $sql = 'update station_profile set clublogignore = 1 where station_id = ?'; + $this->db->query($sql,$station_row->station_id); + } else if (substr($response,0,14) == 'Login rejected') { // Deactivate Upload for Station if Clublog rejects it due to wrong credentials (prevent being blacklisted at Clublog) + log_message('Error', 'Deactivated upload for station ' . $station_row->station_callsign . ' due to wrong credentials (prevent being blacklisted at Clublog.'); + $sql = 'update station_profile set clublogignore = 1 where station_id = ?'; + $this->db->query($sql,$station_row->station_id); + } else if ($httpcode == 403) { + log_message('Error', 'Deactivated upload for station ' . $station_row->station_callsign . ' due to 403 (prevent being blacklisted at Clublog.'); + $sql = 'update station_profile set clublogignore = 1 where station_id = ?'; + $this->db->query($sql,$station_row->station_id); + } else { + log_message('error', 'Some uncaught exception for station ' . $station_row->station_callsign); + } + } + + // Delete the ADIF file used for clublog + unlink('uploads/clublog' . $ranid . $station_row->station_id . '.adi'); + } } } else { $return = "Nothing awaiting upload to clublog for " . $station_row->station_callsign; @@ -162,7 +180,14 @@ class Clublog_model extends CI_Model foreach ($station_profiles->result() as $station_row) { $lastrec = $clublog_last_date ?? $this->clublog_last_qsl_rcvd_date($station_row->station_callsign); $lastrec = str_replace('-', '', $lastrec); - $url = 'https://clublog.org/getmatches.php?api=' . $this->clublog_identifier . '&email=' . $clean_username . '&password=' . $clean_password . '&callsign=' . $station_row->station_callsign . '&startyear=' . substr($lastrec, 0, 4) . '&startmonth=' . substr($lastrec, 4, 2) . '&startday=' . substr($lastrec, 6, 2); + $url_params=['api' => $this->clublog_identifier, + 'email' => $clean_username, + 'password' => $clean_password, + 'callsign' => trim($station_row->station_callsign), + 'startyear' => substr($lastrec, 0, 4), + 'startmonth' => substr($lastrec, 4, 2), + 'startday' => substr($lastrec, 6, 2)]; + $url = 'https://clublog.org/getmatches.php?' . http_build_query($url_params); $request = curl_init($url); // recieve a file @@ -170,10 +195,16 @@ class Clublog_model extends CI_Model curl_setopt($request, CURLOPT_TIMEOUT, 10); $response = curl_exec($request); $info = curl_getinfo($request); + $c_err=curl_errno($request); + $c_errstring=curl_error($request); curl_close($request); - if (curl_errno($request)) { - $log = curl_error($request)."
"; + if ($c_err) { + $log = $c_errstring."
"; + log_message("Error",$c_errstring."/".$c_err); + if ($c_err == 7) { // We're victim of the Clublog Firewall + return 'Impossible to reach Clublog'; + } } elseif (preg_match_all('/Login rejected/', $response)) { $this->disable_sync4call($station_row->station_callsign, $station_row->station_ids); $log = "Wrong Clublog username and password for Callsign: '" . $station_row->station_callsign . "'. 'LOGIN REJECTED'."; @@ -395,6 +426,10 @@ class Clublog_model extends CI_Model if (preg_match('/\bOK\b/', $response)) { $returner['status'] = 'OK'; + } elseif (preg_match('/\bDupe\b/', $response)) { + $returner['status'] = 'OK'; + } elseif (preg_match('/\bUpdated QSO\b/', $response)) { + $returner['status'] = 'OK'; } elseif (substr($response,0,14) == 'Login rejected') { // Deactivate Upload for Station if Clublog rejects it due to wrong credentials (prevent being blacklisted at Clublog) log_message("Error","Clublog deactivated for ".$cl_username." because of wrong creds at Realtime-Pusher"); $sql = 'update station_profile set clublogignore = 1 where station_id = ?';