From 7323eed7897177c780071ee7749852b9790b234b Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 20 Apr 2026 07:59:06 +0200 Subject: [PATCH] fix: enhance error handling and improve URL decoding in http2pic class and index.php --- src/http2pic.class.php | 19 ++++++++++++------- web/index.php | 14 +++++++------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/http2pic.class.php b/src/http2pic.class.php index c9d357a..b916fa6 100755 --- a/src/http2pic.class.php +++ b/src/http2pic.class.php @@ -104,7 +104,7 @@ class http2pic //validate URL and check if exists if ($this->isBase64($this->params['url'])) - $this->params['url'] = base64_decode($url); + $this->params['url'] = base64_decode($this->params['url']); else $this->params['url'] = rawurldecode($_GET['url']); @@ -171,22 +171,25 @@ class http2pic $cmd.=' -f png'; //add url to cmd - $cmd.=' \''.addslashes($this->params['url']).'\''; - + $cmd.=' '.escapeshellarg($this->params['url']); + //add storage path to cmd $cmd.=' '.escapeshellarg($this->params['file']); $cmd.=' --wait-for-network-idle'; - $cmd = escapeshellcmd($cmd); $output = []; $rc = 0; exec($cmd . ' 2>&1', $output, $rc); $this->params['cmd'] = $cmd; if ($rc !== 0) { $this->params['render_error'] = implode("\n", $output); + header('Content-Type: image/jpeg'); + $result = imagecreatefromjpeg($this->params['onfail']); + imagejpeg($result, NULL, 100); + return $cmd; } - + $this->postRender(); if(DEBUG) @@ -274,8 +277,10 @@ class http2pic */ function isURLReachable($url) { - $ch = curl_init($url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); + curl_setopt($ch, CURLOPT_TIMEOUT, 15); if(curl_exec($ch) != false){ //We were able to connect to a webserver, what did it return? $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); diff --git a/web/index.php b/web/index.php index 68bd5bf..a716723 100755 --- a/web/index.php +++ b/web/index.php @@ -55,15 +55,13 @@ switch ($url[0]) { $driver = null; + $error = null; try { $driver = RemoteWebDriver::create($serverUrl, $capabilities, 30000, 60); $driver->get($target); //hide scroll bars $driver->executeScript('document.body.style.overflow = "hidden";'); - //set screenshot size to 1920x1080 - //$driver->manage()->window()->setSize(new \Facebook\WebDriver\WebDriverDimension(1024, 768)); - //if $viewport is set, set window size if ($viewport) { $viewport = explode('x', $viewport); $driver->manage()->window()->setSize(new \Facebook\WebDriver\WebDriverDimension($viewport[0], $viewport[1])); @@ -80,15 +78,17 @@ switch ($url[0]) { header('Content-Length: ' . strlen($screenshot)); echo $screenshot; } catch (Exception $e) { - header('HTTP/1.0 500 Internal Server Error'); - addToLog("$ip\tRequested $target but resulted in error:\t" . $e->getMessage()); - echo 'Error: ' . $e->getMessage(); - exit; + $error = $e->getMessage(); + addToLog("$ip\tRequested $target but resulted in error:\t" . $error); } finally { if ($driver instanceof \Facebook\WebDriver\Remote\RemoteWebDriver) { try { $driver->quit(); } catch (Exception $q) {} } } + if ($error !== null) { + header('HTTP/1.0 500 Internal Server Error'); + echo 'Error: ' . $error; + } break; default: