From cae85a6a0c4afb31f26b1b74a59bf6f04230a5e1 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Thu, 3 Oct 2024 19:57:56 +0200 Subject: [PATCH 1/4] http-driver.php: move to Data --- inc/{driver => Data/Driver}/http-driver.php | 60 ++++++++++----------- inc/context.php | 3 +- inc/service/captcha-queries.php | 2 +- post.php | 2 +- 4 files changed, 33 insertions(+), 34 deletions(-) rename inc/{driver => Data/Driver}/http-driver.php (65%) diff --git a/inc/driver/http-driver.php b/inc/Data/Driver/http-driver.php similarity index 65% rename from inc/driver/http-driver.php rename to inc/Data/Driver/http-driver.php index cfbedfad..1a714963 100644 --- a/inc/driver/http-driver.php +++ b/inc/Data/Driver/http-driver.php @@ -1,7 +1,5 @@ inner); curl_setopt_array($this->inner, array( - CURLOPT_URL => $url, - CURLOPT_TIMEOUT => $this->timeout, - CURLOPT_USERAGENT => $this->user_agent, - CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS, + \CURLOPT_URL => $url, + \CURLOPT_TIMEOUT => $timeout, + \CURLOPT_USERAGENT => $this->user_agent, + \CURLOPT_PROTOCOLS => \CURLPROTO_HTTP | \CURLPROTO_HTTPS, )); } private function setSizeLimit(): void { // Adapted from: https://stackoverflow.com/a/17642638 - curl_setopt($this->inner, CURLOPT_NOPROGRESS, false); + \curl_setopt($this->inner, \CURLOPT_NOPROGRESS, false); - if (PHP_MAJOR_VERSION >= 8 && PHP_MINOR_VERSION >= 2) { - curl_setopt($this->inner, CURLOPT_XFERINFOFUNCTION, function($res, $next_dl, $dl, $next_up, $up) { + if (\PHP_MAJOR_VERSION >= 8 && \PHP_MINOR_VERSION >= 2) { + \curl_setopt($this->inner, \CURLOPT_XFERINFOFUNCTION, function($res, $next_dl, $dl, $next_up, $up) { return (int)($dl <= $this->max_file_size); }); } else { - curl_setopt($this->inner, CURLOPT_PROGRESSFUNCTION, function($res, $next_dl, $dl, $next_up, $up) { + \curl_setopt($this->inner, \CURLOPT_PROGRESSFUNCTION, function($res, $next_dl, $dl, $next_up, $up) { return (int)($dl <= $this->max_file_size); }); } } - function __construct($timeout, $user_agent, $max_file_size) { - $this->inner = curl_init(); + function __construct(int $timeout, string $user_agent, int $max_file_size) { + $this->inner = \curl_init(); $this->timeout = $timeout; $this->user_agent = $user_agent; $this->max_file_size = $max_file_size; } function __destruct() { - curl_close($this->inner); + \curl_close($this->inner); } /** @@ -69,18 +67,18 @@ class HttpDriver { */ public function requestGet(string $endpoint, ?array $data, int $timeout = 0): string { if (!empty($data)) { - $endpoint .= '?' . http_build_query($data); + $endpoint .= '?' . \http_build_query($data); } if ($timeout == 0) { $timeout = $this->timeout; } $this->resetTowards($endpoint, $timeout); - curl_setopt($this->inner, CURLOPT_RETURNTRANSFER, true); - $ret = curl_exec($this->inner); + \curl_setopt($this->inner, \CURLOPT_RETURNTRANSFER, true); + $ret = \curl_exec($this->inner); if ($ret === false) { - throw new \RuntimeException(curl_error($this->inner)); + throw new \RuntimeException(\curl_error($this->inner)); } return $ret; } @@ -100,15 +98,15 @@ class HttpDriver { } $this->resetTowards($endpoint, $timeout); - curl_setopt($this->inner, CURLOPT_POST, true); + \curl_setopt($this->inner, \CURLOPT_POST, true); if (!empty($data)) { - curl_setopt($this->inner, CURLOPT_POSTFIELDS, http_build_query($data)); + \curl_setopt($this->inner, \CURLOPT_POSTFIELDS, \http_build_query($data)); } - curl_setopt($this->inner, CURLOPT_RETURNTRANSFER, true); - $ret = curl_exec($this->inner); + \curl_setopt($this->inner, \CURLOPT_RETURNTRANSFER, true); + $ret = \curl_exec($this->inner); if ($ret === false) { - throw new \RuntimeException(curl_error($this->inner)); + throw new \RuntimeException(\curl_error($this->inner)); } return $ret; } @@ -125,26 +123,26 @@ class HttpDriver { */ public function requestGetInto(string $endpoint, ?array $data, mixed $fd, int $timeout = 0): bool { if (!empty($data)) { - $endpoint .= '?' . http_build_query($data); + $endpoint .= '?' . \http_build_query($data); } if ($timeout == 0) { $timeout = $this->timeout; } $this->resetTowards($endpoint, $timeout); - curl_setopt($this->inner, CURLOPT_FAILONERROR, true); - curl_setopt($this->inner, CURLOPT_FOLLOWLOCATION, false); - curl_setopt($this->inner, CURLOPT_FILE, $fd); - curl_setopt($this->inner, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); + \curl_setopt($this->inner, \CURLOPT_FAILONERROR, true); + \curl_setopt($this->inner, \CURLOPT_FOLLOWLOCATION, false); + \curl_setopt($this->inner, \CURLOPT_FILE, $fd); + \curl_setopt($this->inner, \CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); $this->setSizeLimit(); - $ret = curl_exec($this->inner); + $ret = \curl_exec($this->inner); if ($ret === false) { - if (curl_errno($this->inner) === CURLE_ABORTED_BY_CALLBACK) { + if (\curl_errno($this->inner) === CURLE_ABORTED_BY_CALLBACK) { return false; } - throw new \RuntimeException(curl_error($this->inner)); + throw new \RuntimeException(\curl_error($this->inner)); } return true; } diff --git a/inc/context.php b/inc/context.php index 3f8a75d6..70242502 100644 --- a/inc/context.php +++ b/inc/context.php @@ -2,7 +2,8 @@ namespace Vichan; use RuntimeException; -use Vichan\Driver\{HttpDriver, HttpDrivers, Log, LogDrivers}; +use Vichan\Driver\{Log, LogDrivers}; +use Vichan\Data\Driver\{HttpDriver, HttpDrivers}; use Vichan\Service\HCaptchaQuery; use Vichan\Service\NativeCaptchaQuery; use Vichan\Service\ReCaptchaQuery; diff --git a/inc/service/captcha-queries.php b/inc/service/captcha-queries.php index cd9a1b84..76d7acd8 100644 --- a/inc/service/captcha-queries.php +++ b/inc/service/captcha-queries.php @@ -1,7 +1,7 @@ Date: Fri, 4 Oct 2024 00:44:30 +0200 Subject: [PATCH 2/4] http-driver.php: minor refactor --- inc/Data/Driver/http-driver.php | 53 ++++++++++----------------------- 1 file changed, 16 insertions(+), 37 deletions(-) diff --git a/inc/Data/Driver/http-driver.php b/inc/Data/Driver/http-driver.php index 1a714963..f2ba9d88 100644 --- a/inc/Data/Driver/http-driver.php +++ b/inc/Data/Driver/http-driver.php @@ -4,55 +4,29 @@ namespace Vichan\Data\Driver; defined('TINYBOARD') or exit; -class HttpDrivers { - private const DEFAULT_USER_AGENT = 'Tinyboard'; - - - public static function getHttpDriver(int $timeout, int $max_file_size): HttpDriver { - return new HttpDriver($timeout, self::DEFAULT_USER_AGENT, $max_file_size); - } -} - class HttpDriver { private mixed $inner; private int $timeout; - private string $user_agent; private int $max_file_size; private function resetTowards(string $url, int $timeout): void { - curl_reset($this->inner); - curl_setopt_array($this->inner, array( + \curl_reset($this->inner); + \curl_setopt_array($this->inner, [ \CURLOPT_URL => $url, \CURLOPT_TIMEOUT => $timeout, - \CURLOPT_USERAGENT => $this->user_agent, + \CURLOPT_USERAGENT => 'Tinyboard', \CURLOPT_PROTOCOLS => \CURLPROTO_HTTP | \CURLPROTO_HTTPS, - )); + ]); } - private function setSizeLimit(): void { - // Adapted from: https://stackoverflow.com/a/17642638 - \curl_setopt($this->inner, \CURLOPT_NOPROGRESS, false); - - if (\PHP_MAJOR_VERSION >= 8 && \PHP_MINOR_VERSION >= 2) { - \curl_setopt($this->inner, \CURLOPT_XFERINFOFUNCTION, function($res, $next_dl, $dl, $next_up, $up) { - return (int)($dl <= $this->max_file_size); - }); - } else { - \curl_setopt($this->inner, \CURLOPT_PROGRESSFUNCTION, function($res, $next_dl, $dl, $next_up, $up) { - return (int)($dl <= $this->max_file_size); - }); - } - } - - function __construct(int $timeout, string $user_agent, int $max_file_size) { + public function __construct(int $timeout, int $max_file_size) { $this->inner = \curl_init(); $this->timeout = $timeout; - $this->user_agent = $user_agent; $this->max_file_size = $max_file_size; } - function __destruct() { + public function __destruct() { \curl_close($this->inner); } @@ -130,11 +104,16 @@ class HttpDriver { } $this->resetTowards($endpoint, $timeout); - \curl_setopt($this->inner, \CURLOPT_FAILONERROR, true); - \curl_setopt($this->inner, \CURLOPT_FOLLOWLOCATION, false); - \curl_setopt($this->inner, \CURLOPT_FILE, $fd); - \curl_setopt($this->inner, \CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); - $this->setSizeLimit(); + // Adapted from: https://stackoverflow.com/a/17642638 + $opt = (\PHP_MAJOR_VERSION >= 8 && \PHP_MINOR_VERSION >= 2) ? \CURLOPT_XFERINFOFUNCTION : \CURLOPT_PROGRESSFUNCTION; + \curl_setopt_array($this->inner, [ + \CURLOPT_NOPROGRESS => false, + $opt => fn($res, $next_dl, $dl, $next_up, $up) => (int)($dl <= $this->max_file_size), + \CURLOPT_FAILONERROR => true, + \CURLOPT_FOLLOWLOCATION => false, + \CURLOPT_FILE => $fd, + \CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4, + ]); $ret = \curl_exec($this->inner); if ($ret === false) { From 1682272f75b9e6b84ac3dd1d2302612c640cd1f9 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Fri, 4 Oct 2024 01:01:47 +0200 Subject: [PATCH 3/4] http-driver.php: rename to HttpDriver --- inc/Data/Driver/{http-driver.php => HttpDriver.php} | 5 ++++- inc/context.php | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) rename inc/Data/Driver/{http-driver.php => HttpDriver.php} (97%) diff --git a/inc/Data/Driver/http-driver.php b/inc/Data/Driver/HttpDriver.php similarity index 97% rename from inc/Data/Driver/http-driver.php rename to inc/Data/Driver/HttpDriver.php index f2ba9d88..022fcbab 100644 --- a/inc/Data/Driver/http-driver.php +++ b/inc/Data/Driver/HttpDriver.php @@ -1,9 +1,12 @@ - function($c) { $config = $c->get('config'); - return HttpDrivers::getHttpDriver($config['upload_by_url_timeout'], $config['max_filesize']); + return new HttpDriver($config['upload_by_url_timeout'], $config['max_filesize']); }, RemoteCaptchaQuery::class => function($c) { $config = $c->get('config'); From 59551a2042b3845e5720c615cb11aae4cc03fe14 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Fri, 4 Oct 2024 01:21:21 +0200 Subject: [PATCH 4/4] composer.json: rely on classmap for HttpDriver --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index a2ddce03..5fdb4226 100644 --- a/composer.json +++ b/composer.json @@ -38,7 +38,6 @@ "inc/functions/num.php", "inc/functions/format.php", "inc/functions/theme.php", - "inc/driver/http-driver.php", "inc/driver/log-driver.php", "inc/service/captcha-queries.php", "inc/context.php"