Skip to content

Commit 77d95fd

Browse files
authored
Merge pull request wavelog#2663 from int2001/eqsl_rm_deprecated
2 parents d11ab51 + 2326467 commit 77d95fd

File tree

1 file changed

+68
-10
lines changed

1 file changed

+68
-10
lines changed

application/controllers/Eqsl.php

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,15 @@ function image($id) {
306306
$this->load->model('logbook_model');
307307
$this->load->model('user_model');
308308
$qso_query = $this->logbook_model->get_qso($id);
309+
310+
// Check if QSO exists and is accessible
311+
if (!$qso_query || $qso_query->num_rows() == 0) {
312+
show_error(__('QSO not found or not accessible'), 404);
313+
return;
314+
}
315+
309316
$qso = $qso_query->row();
317+
310318
$qso_timestamp = strtotime($qso->COL_TIME_ON);
311319
$callsign = $qso->COL_CALL;
312320
$band = $qso->COL_BAND;
@@ -318,40 +326,90 @@ function image($id) {
318326
$minute = date('i', $qso_timestamp);
319327

320328
$query = $this->user_model->get_by_id($this->session->userdata('user_id'));
329+
if ($query->num_rows() == 0) {
330+
show_error(__('User not found'), 404);
331+
return;
332+
}
321333
$q = $query->row();
322334
$username = $qso->COL_STATION_CALLSIGN;
323335
$password = $q->user_eqsl_password;
324336

337+
// Check if eQSL password is set
338+
if (empty($password)) {
339+
show_error(__('eQSL password not configured for this user'), 400);
340+
return;
341+
}
342+
325343
$image_url = $this->electronicqsl->card_image($username, urlencode($password), $callsign, $band, $mode, $year, $month, $day, $hour, $minute);
326-
$file = file_get_contents($image_url, true); // TODO use curl instead
344+
345+
// Use curl for better error handling instead of file_get_contents
346+
$ch = curl_init();
347+
curl_setopt($ch, CURLOPT_URL, $image_url);
348+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
349+
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
350+
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
351+
curl_setopt($ch, CURLOPT_USERAGENT, 'Wavelog-eQSL/1.0');
352+
$file = curl_exec($ch);
353+
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
354+
curl_close($ch);
355+
356+
if ($file === false || $http_code != 200) {
357+
show_error(__('Failed to fetch eQSL image data'), 503);
358+
return;
359+
}
327360

328361
$dom = new domDocument;
362+
// Suppress warnings for malformed HTML
363+
libxml_use_internal_errors(true);
329364
$dom->loadHTML($file);
365+
libxml_clear_errors();
330366
$dom->preserveWhiteSpace = false;
331367
$images = $dom->getElementsByTagName('img');
332368

333369
if (!isset($images) || count($images) == 0) {
334370
$h3 = $dom->getElementsByTagName('h3');
335371
if (isset($h3) && ($h3->item(0) !== null)) {
336-
echo $h3->item(0)->nodeValue;
372+
$error_message = $h3->item(0)->nodeValue;
337373
} else {
338-
echo "Rate Limited";
374+
$error_message = "Rate Limited";
339375
}
340-
exit;
376+
show_error(__('eQSL image not available') . ': ' . $error_message, 503);
377+
return;
341378
}
342379

343380
foreach ($images as $image) {
344-
header('Content-Type: image/jpg');
345-
$content = file_get_contents("https://www.eqsl.cc" . $image->getAttribute('src'));
346-
if ($content === false) {
347-
echo "No response";
348-
exit;
381+
$image_src = "https://www.eqsl.cc" . $image->getAttribute('src');
382+
383+
// Use curl for downloading the actual image
384+
$ch = curl_init();
385+
curl_setopt($ch, CURLOPT_URL, $image_src);
386+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
387+
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
388+
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
389+
curl_setopt($ch, CURLOPT_USERAGENT, 'Wavelog-eQSL/1.0');
390+
$content = curl_exec($ch);
391+
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
392+
curl_close($ch);
393+
394+
if ($content === false || $http_code != 200) {
395+
show_error(__('Failed to download eQSL image'), 503);
396+
return;
349397
}
398+
399+
header('Content-Type: image/jpg');
350400
echo $content;
401+
351402
$filename = uniqid() . '.jpg';
352-
if (file_put_contents($this->Eqsl_images->get_imagePath('p') . '/' . $filename, $content) !== false) {
403+
$image_path = $this->Eqsl_images->get_imagePath('p') . '/' . $filename;
404+
$save_result = file_put_contents($image_path, $content);
405+
406+
if ($save_result !== false) {
353407
$this->Eqsl_images->save_image($id, $filename);
408+
} else {
409+
log_message('error', 'Failed to save eQSL image to: ' . $image_path);
354410
}
411+
412+
return; // Only process the first image found
355413
}
356414
} else {
357415
header('Content-Type: image/jpg');

0 commit comments

Comments
 (0)