Skip to content

Commit 9a34925

Browse files
committed
Fixes and support for PHP 8.5
- PHP 8.5 support. - StreamDriver: improved issue checking (wrapper_data). - Improved URL verification. - Fixed exception messages.
1 parent e1c0158 commit 9a34925

3 files changed

Lines changed: 50 additions & 13 deletions

File tree

src/Drivers/CurlDriver.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public function exec($url, &$httpStatus, &$contentType, &$errorCode, &$errorMess
5858
if ($this->handle === null || $this->lastUpdate < $update) {
5959
$this->lastUpdate = $update;
6060

61+
$this->close();
62+
6163
$this->handle = curl_init();
6264

6365
$ch = $this->handle;
@@ -151,7 +153,7 @@ private function abort($downloaded)
151153

152154
$httpCode = curl_getinfo($this->handle, CURLINFO_HTTP_CODE);
153155

154-
if ($httpCode !== 0 && $httpCode < 200 || $httpCode >= 400) {
156+
if (($httpCode !== 0 && $httpCode < 200) || $httpCode >= 400) {
155157
$this->httpStatus = $httpCode;
156158
return 1;
157159
}
@@ -165,10 +167,17 @@ private function abort($downloaded)
165167
return 0;
166168
}
167169

168-
public function __destruct()
170+
private function close()
169171
{
170-
if ($this->handle) {
172+
if ($this->handle && PHP_VERSION_ID < 80500) {
171173
curl_close($this->handle);
172174
}
175+
176+
$this->handle = null;
177+
}
178+
179+
public function __destruct()
180+
{
181+
$this->close();
173182
}
174183
}

src/Drivers/StreamDriver.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ public function exec($url, &$httpStatus, &$contentType, &$errorCode, &$errorMess
115115

116116
$meta_data = stream_get_meta_data($handle);
117117

118+
if (empty($meta_data['wrapper_data'])) {
119+
$errorMessage = 'Missing `wrapper_data` info';
120+
fclose($handle);
121+
return false;
122+
}
123+
118124
foreach ($meta_data['wrapper_data'] as $index => $header) {
119125
if ($index === 0) {
120126
if (preg_match('#HTTP/\d+\.\d+\s+(\d+)#', $header, $match)) {

src/Proxy.php

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,14 @@ public function getTemporary()
361361
*/
362362
public function download($url)
363363
{
364+
if ($this->isHttpUrl($url, $message) === false) {
365+
$this->raise($message);
366+
}
367+
368+
if ($this->checkAllowedUrl($url) === false) {
369+
$this->raise('URL not allowed: ' . $url);
370+
}
371+
364372
if ($this->temporary === null) {
365373
$temporary = tmpfile();
366374

@@ -371,10 +379,6 @@ public function download($url)
371379
}
372380
}
373381

374-
if ($this->validateUrl($url) === false) {
375-
$this->raise('URL not allowed: ' . $url);
376-
}
377-
378382
$this->errorCode = null;
379383
$this->errorMessage = null;
380384
$this->httpStatus = null;
@@ -434,7 +438,9 @@ public function download($url)
434438
$this->errorMessage = 'An unexpected issue occurred';
435439
}
436440

437-
$this->raise($this->errorMessage, $this->errorCode);
441+
$message = get_class($this->driver) . ': ' . $this->errorMessage;
442+
443+
$this->raise($message, $this->errorCode);
438444
}
439445
}
440446

@@ -620,16 +626,34 @@ private function sendHeaders($contentType)
620626
header('Content-type: ' . $contentType);
621627
}
622628

623-
private function validateUrl($url)
629+
private function isHttpUrl($url, &$message)
630+
{
631+
if (filter_var($url, FILTER_VALIDATE_URL) === false) {
632+
$message = "Invalid URL: {$url}";
633+
return false;
634+
}
635+
636+
if (in_array(parse_url($url, PHP_URL_SCHEME), array('http', 'https')) == false) {
637+
$message = "The following URL is not http/https: {$url}";
638+
return false;
639+
}
640+
641+
return true;
642+
}
643+
644+
private function checkAllowedUrl($url)
624645
{
625646
if ($this->allowedUrls) {
626647
if ($this->allowedUrlsRegEx === null) {
627-
foreach ($this->allowedUrls as &$entry) {
648+
$allowed = array();
649+
650+
foreach ($this->allowedUrls as $entry) {
628651
$entry = preg_quote($entry, '#');
629652
$entry = str_replace('\\*', '[^/]+', $entry);
653+
$allowed[] = $entry;
630654
}
631655

632-
$this->allowedUrlsRegEx = '#^(' . implode('|', $this->allowedUrls) . ')$#';
656+
$this->allowedUrlsRegEx = '#^(' . implode('|', $allowed) . ')$#';
633657
}
634658

635659
if (preg_match($this->allowedUrlsRegEx, $url) !== 1) {
@@ -642,8 +666,6 @@ private function validateUrl($url)
642666

643667
private function raise($message, $code = 0)
644668
{
645-
$message = get_class($this->driver) . ': ' . $message;
646-
647669
if ($this->coreException) {
648670
throw new \Inphinit\Exception($message, $code, 3);
649671
} else {

0 commit comments

Comments
 (0)