forked from GithubBackups/vichan
http-driver.php: move to Data
This commit is contained in:
parent
7377885de9
commit
cae85a6a0c
@ -1,7 +1,5 @@
|
|||||||
<?php // Honestly this is just a wrapper for cURL. Still useful to mock it and have an OOP API on PHP 7.
|
<?php // Honestly this is just a wrapper for cURL. Still useful to mock it and have an OOP API on PHP 7.
|
||||||
namespace Vichan\Driver;
|
namespace Vichan\Data\Driver;
|
||||||
|
|
||||||
use RuntimeException;
|
|
||||||
|
|
||||||
defined('TINYBOARD') or exit;
|
defined('TINYBOARD') or exit;
|
||||||
|
|
||||||
@ -25,37 +23,37 @@ class HttpDriver {
|
|||||||
private function resetTowards(string $url, int $timeout): void {
|
private function resetTowards(string $url, int $timeout): void {
|
||||||
curl_reset($this->inner);
|
curl_reset($this->inner);
|
||||||
curl_setopt_array($this->inner, array(
|
curl_setopt_array($this->inner, array(
|
||||||
CURLOPT_URL => $url,
|
\CURLOPT_URL => $url,
|
||||||
CURLOPT_TIMEOUT => $this->timeout,
|
\CURLOPT_TIMEOUT => $timeout,
|
||||||
CURLOPT_USERAGENT => $this->user_agent,
|
\CURLOPT_USERAGENT => $this->user_agent,
|
||||||
CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS,
|
\CURLOPT_PROTOCOLS => \CURLPROTO_HTTP | \CURLPROTO_HTTPS,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function setSizeLimit(): void {
|
private function setSizeLimit(): void {
|
||||||
// Adapted from: https://stackoverflow.com/a/17642638
|
// 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) {
|
if (\PHP_MAJOR_VERSION >= 8 && \PHP_MINOR_VERSION >= 2) {
|
||||||
curl_setopt($this->inner, CURLOPT_XFERINFOFUNCTION, function($res, $next_dl, $dl, $next_up, $up) {
|
\curl_setopt($this->inner, \CURLOPT_XFERINFOFUNCTION, function($res, $next_dl, $dl, $next_up, $up) {
|
||||||
return (int)($dl <= $this->max_file_size);
|
return (int)($dl <= $this->max_file_size);
|
||||||
});
|
});
|
||||||
} else {
|
} 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);
|
return (int)($dl <= $this->max_file_size);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function __construct($timeout, $user_agent, $max_file_size) {
|
function __construct(int $timeout, string $user_agent, int $max_file_size) {
|
||||||
$this->inner = curl_init();
|
$this->inner = \curl_init();
|
||||||
$this->timeout = $timeout;
|
$this->timeout = $timeout;
|
||||||
$this->user_agent = $user_agent;
|
$this->user_agent = $user_agent;
|
||||||
$this->max_file_size = $max_file_size;
|
$this->max_file_size = $max_file_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
function __destruct() {
|
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 {
|
public function requestGet(string $endpoint, ?array $data, int $timeout = 0): string {
|
||||||
if (!empty($data)) {
|
if (!empty($data)) {
|
||||||
$endpoint .= '?' . http_build_query($data);
|
$endpoint .= '?' . \http_build_query($data);
|
||||||
}
|
}
|
||||||
if ($timeout == 0) {
|
if ($timeout == 0) {
|
||||||
$timeout = $this->timeout;
|
$timeout = $this->timeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->resetTowards($endpoint, $timeout);
|
$this->resetTowards($endpoint, $timeout);
|
||||||
curl_setopt($this->inner, CURLOPT_RETURNTRANSFER, true);
|
\curl_setopt($this->inner, \CURLOPT_RETURNTRANSFER, true);
|
||||||
$ret = curl_exec($this->inner);
|
$ret = \curl_exec($this->inner);
|
||||||
|
|
||||||
if ($ret === false) {
|
if ($ret === false) {
|
||||||
throw new \RuntimeException(curl_error($this->inner));
|
throw new \RuntimeException(\curl_error($this->inner));
|
||||||
}
|
}
|
||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
@ -100,15 +98,15 @@ class HttpDriver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->resetTowards($endpoint, $timeout);
|
$this->resetTowards($endpoint, $timeout);
|
||||||
curl_setopt($this->inner, CURLOPT_POST, true);
|
\curl_setopt($this->inner, \CURLOPT_POST, true);
|
||||||
if (!empty($data)) {
|
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);
|
\curl_setopt($this->inner, \CURLOPT_RETURNTRANSFER, true);
|
||||||
$ret = curl_exec($this->inner);
|
$ret = \curl_exec($this->inner);
|
||||||
|
|
||||||
if ($ret === false) {
|
if ($ret === false) {
|
||||||
throw new \RuntimeException(curl_error($this->inner));
|
throw new \RuntimeException(\curl_error($this->inner));
|
||||||
}
|
}
|
||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
@ -125,26 +123,26 @@ class HttpDriver {
|
|||||||
*/
|
*/
|
||||||
public function requestGetInto(string $endpoint, ?array $data, mixed $fd, int $timeout = 0): bool {
|
public function requestGetInto(string $endpoint, ?array $data, mixed $fd, int $timeout = 0): bool {
|
||||||
if (!empty($data)) {
|
if (!empty($data)) {
|
||||||
$endpoint .= '?' . http_build_query($data);
|
$endpoint .= '?' . \http_build_query($data);
|
||||||
}
|
}
|
||||||
if ($timeout == 0) {
|
if ($timeout == 0) {
|
||||||
$timeout = $this->timeout;
|
$timeout = $this->timeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->resetTowards($endpoint, $timeout);
|
$this->resetTowards($endpoint, $timeout);
|
||||||
curl_setopt($this->inner, CURLOPT_FAILONERROR, true);
|
\curl_setopt($this->inner, \CURLOPT_FAILONERROR, true);
|
||||||
curl_setopt($this->inner, CURLOPT_FOLLOWLOCATION, false);
|
\curl_setopt($this->inner, \CURLOPT_FOLLOWLOCATION, false);
|
||||||
curl_setopt($this->inner, CURLOPT_FILE, $fd);
|
\curl_setopt($this->inner, \CURLOPT_FILE, $fd);
|
||||||
curl_setopt($this->inner, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
|
\curl_setopt($this->inner, \CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
|
||||||
$this->setSizeLimit();
|
$this->setSizeLimit();
|
||||||
$ret = curl_exec($this->inner);
|
$ret = \curl_exec($this->inner);
|
||||||
|
|
||||||
if ($ret === false) {
|
if ($ret === false) {
|
||||||
if (curl_errno($this->inner) === CURLE_ABORTED_BY_CALLBACK) {
|
if (\curl_errno($this->inner) === CURLE_ABORTED_BY_CALLBACK) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new \RuntimeException(curl_error($this->inner));
|
throw new \RuntimeException(\curl_error($this->inner));
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
@ -2,7 +2,8 @@
|
|||||||
namespace Vichan;
|
namespace Vichan;
|
||||||
|
|
||||||
use RuntimeException;
|
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\HCaptchaQuery;
|
||||||
use Vichan\Service\NativeCaptchaQuery;
|
use Vichan\Service\NativeCaptchaQuery;
|
||||||
use Vichan\Service\ReCaptchaQuery;
|
use Vichan\Service\ReCaptchaQuery;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<?php // Verify captchas server side.
|
<?php // Verify captchas server side.
|
||||||
namespace Vichan\Service;
|
namespace Vichan\Service;
|
||||||
|
|
||||||
use Vichan\Driver\HttpDriver;
|
use Vichan\Data\Driver\HttpDriver;
|
||||||
|
|
||||||
defined('TINYBOARD') or exit;
|
defined('TINYBOARD') or exit;
|
||||||
|
|
||||||
|
2
post.php
2
post.php
@ -6,7 +6,7 @@
|
|||||||
require_once 'inc/bootstrap.php';
|
require_once 'inc/bootstrap.php';
|
||||||
|
|
||||||
use Vichan\{Context, WebDependencyFactory};
|
use Vichan\{Context, WebDependencyFactory};
|
||||||
use Vichan\Driver\{HttpDriver, Log};
|
use Vichan\Data\Driver\{HttpDriver, Log};
|
||||||
use Vichan\Service\{RemoteCaptchaQuery, NativeCaptchaQuery};
|
use Vichan\Service\{RemoteCaptchaQuery, NativeCaptchaQuery};
|
||||||
use Vichan\Functions\Format;
|
use Vichan\Functions\Format;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user