This commit is contained in:
Bastian Allgeier
2020-07-07 12:40:13 +02:00
parent 5f025ac2c2
commit f79d2e960c
176 changed files with 10532 additions and 5343 deletions

View File

@@ -3,6 +3,8 @@
namespace Kirby\Http;
use Exception;
use Kirby\Cms\App;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Toolkit\F;
use Kirby\Toolkit\Str;
@@ -18,6 +20,9 @@ use Kirby\Toolkit\Str;
*/
class Remote
{
const CA_INTERNAL = 1;
const CA_SYSTEM = 2;
/**
* @var array
*/
@@ -25,6 +30,7 @@ class Remote
'agent' => null,
'basicAuth' => null,
'body' => true,
'ca' => self::CA_INTERNAL,
'data' => [],
'encoding' => 'utf-8',
'file' => null,
@@ -96,8 +102,17 @@ class Remote
*/
public function __construct(string $url, array $options = [])
{
$defaults = static::$defaults;
// update the defaults with App config if set;
// request the App instance lazily
$app = App::instance(null, true);
if ($app !== null) {
$defaults = array_merge($defaults, $app->option('remote', []));
}
// set all options
$this->options = array_merge(static::$defaults, $options);
$this->options = array_merge($defaults, $options);
// add the url
$this->options['url'] = $url;
@@ -138,7 +153,6 @@ class Remote
*/
public function fetch()
{
// curl options
$this->curlopt = [
CURLOPT_URL => $this->options['url'],
@@ -149,7 +163,6 @@ class Remote
CURLOPT_RETURNTRANSFER => $this->options['body'],
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HEADER => false,
CURLOPT_HEADERFUNCTION => function ($curl, $header) {
$parts = Str::split($header, ':');
@@ -163,6 +176,24 @@ class Remote
}
];
// determine the TLS CA to use
if (is_file($this->options['ca']) === true) {
$this->curlopt[CURLOPT_SSL_VERIFYPEER] = true;
$this->curlopt[CURLOPT_CAINFO] = $this->options['ca'];
} elseif (is_dir($this->options['ca']) === true) {
$this->curlopt[CURLOPT_SSL_VERIFYPEER] = true;
$this->curlopt[CURLOPT_CAPATH] = $this->options['ca'];
} elseif ($this->options['ca'] === self::CA_INTERNAL) {
$this->curlopt[CURLOPT_SSL_VERIFYPEER] = true;
$this->curlopt[CURLOPT_CAINFO] = dirname(__DIR__, 2) . '/cacert.pem';
} elseif ($this->options['ca'] === self::CA_SYSTEM) {
$this->curlopt[CURLOPT_SSL_VERIFYPEER] = true;
} elseif ($this->options['ca'] === false) {
$this->curlopt[CURLOPT_SSL_VERIFYPEER] = false;
} else {
throw new InvalidArgumentException('Invalid "ca" option for the Remote class');
}
// add the progress
if (is_callable($this->options['progress']) === true) {
$this->curlopt[CURLOPT_NOPROGRESS] = false;