More robust error handling and cleanup

这个提交包含在:
Alessio Caiazza 2022-08-25 19:18:53 +02:00
父节点 27a8d235fd
当前提交 d8c8260097
找不到此签名对应的密钥

查看文件

@ -53,9 +53,7 @@ class EqslImporter
// Download confirmed QSO from eQSL inbox and import them // Download confirmed QSO from eQSL inbox and import them
public function fetch($password) { public function fetch($password) {
if (empty($password) || empty($this->callsign)) { if (empty($password) || empty($this->callsign)) {
$this->status = "Missing username and/or password"; return $this->result('Missing username and/or password');
return;
} }
// Get URL for downloading the eqsl.cc inbox // Get URL for downloading the eqsl.cc inbox
@ -82,54 +80,53 @@ class EqslImporter
// Let's use cURL instead of file_get_contents // Let's use cURL instead of file_get_contents
// begin script // begin script
$ch = curl_init(); $ch = curl_init();
try {
// basic curl options for all requests
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
// basic curl options for all requests // use the URL and params we built
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $eqsl_url);
curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $eqsl_params);
// use the URL and params we built $input = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, $eqsl_url); $chi = curl_getinfo($ch);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $eqsl_params);
$input = curl_exec($ch); // "You have no log entries" -> Nothing else to do here
$chi = curl_getinfo($ch); // "Your ADIF log file has been built" -> We've got an ADIF file we need to grab.
// "You have no log entries" -> Nothing else to do here if ($chi['http_code'] == "200") {
// "Your ADIF log file has been built" -> We've got an ADIF file we need to grab. if (stristr($input, "You have no log entries")) {
return $this->result('There are no QSLs waiting for download at eQSL.cc.'); // success
if ($chi['http_code'] == "200") { } else if (stristr($input, "Error: No such Username/Password found")) {
if (stristr($input, "You have no log entries")) { return $this->result('No such Username/Password found This could mean the wrong callsign or the wrong password, or the user does not exist.'); // warning
return $this->result('There are no QSLs waiting for download at eQSL.cc.'); // success } else {
} else if (stristr($input, "Error: No such Username/Password found")) { if (stristr($input, "Your ADIF log file has been built")) {
return $this->result('No such Username/Password found This could mean the wrong callsign or the wrong password, or the user does not exist.'); // warning // Get all the links on the page and grab the URL for the ADI file.
} else { $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
if (stristr($input, "Your ADIF log file has been built")) { if (preg_match_all("/$regexp/siU", $input, $matches)) {
// Get all the links on the page and grab the URL for the ADI file. foreach ($matches[2] as $match) {
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>"; // Look for the link that has the .adi file, and download it to $file
if (preg_match_all("/$regexp/siU", $input, $matches)) { if (substr($match, -4, 4) == ".adi") {
foreach ($matches[2] as $match) { file_put_contents($this->adif_file, file_get_contents("http://eqsl.cc/qslcard/" . $match));
// Look for the link that has the .adi file, and download it to $file return $this->import();
if (substr($match, -4, 4) == ".adi") { }
file_put_contents($this->adif_file, file_get_contents("http://eqsl.cc/qslcard/" . $match));
return $this->import();
} }
} }
} }
} }
}
} else {
if ($chi['http_code'] == "500") {
return $this->result('eQSL.cc is experiencing issues. Please try importing QSOs later.'); // warning
} else { } else {
if ($chi['http_code'] == "404") { if ($chi['http_code'] == "500") {
return $this->result('It seems that the eQSL site has changed. Please open up an issue on GitHub.'); return $this->result('eQSL.cc is experiencing issues. Please try importing QSOs later.'); // warning
} }
} }
}
// Close cURL handle return $this->result('It seems that the eQSL site has changed. Please open up an issue on GitHub.');
curl_close($ch); } finally {
// Close cURL handle
curl_close($ch);
}
} }
// Read the ADIF file and set QSO confirmation status according to the settings // Read the ADIF file and set QSO confirmation status according to the settings