generated from Nodarx/template
45 lines
1.6 KiB
PHP
45 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KTXM\ServiceWopi\Discovery;
|
|
|
|
use RuntimeException;
|
|
|
|
class DiscoveryClient
|
|
{
|
|
public function fetch(ServerAddress $server): DiscoveryDocument
|
|
{
|
|
$curl = curl_init($server->discoveryUrl());
|
|
$xml = '';
|
|
curl_setopt_array($curl, [
|
|
CURLOPT_CONNECTTIMEOUT => 5,
|
|
CURLOPT_TIMEOUT => 15,
|
|
CURLOPT_FOLLOWLOCATION => false,
|
|
CURLOPT_PROTOCOLS => CURLPROTO_HTTPS,
|
|
CURLOPT_SSL_VERIFYPEER => true,
|
|
CURLOPT_SSL_VERIFYHOST => 2,
|
|
CURLOPT_HTTPHEADER => ['Accept: application/xml, text/xml'],
|
|
CURLOPT_WRITEFUNCTION => static function ($handle, string $chunk) use (&$xml): int {
|
|
if (strlen($xml) + strlen($chunk) > DiscoveryDocument::MAX_BYTES) { return 0; }
|
|
$xml .= $chunk;
|
|
return strlen($chunk);
|
|
},
|
|
]);
|
|
try {
|
|
$ok = curl_exec($curl);
|
|
$status = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
|
|
if ($ok === false) {
|
|
throw new RuntimeException('Discovery request failed (cURL ' . curl_errno($curl) . '). Check DNS, TLS, connectivity, and response size.');
|
|
}
|
|
if ($status !== 200) {
|
|
throw new RuntimeException('Discovery returned HTTP ' . $status
|
|
. ($status === 404 ? '. Enable WOPI and forward /hosting/discovery through the proxy.' : '. Expected HTTP 200; redirects are not followed.'));
|
|
}
|
|
return DiscoveryDocument::parse($xml, $server);
|
|
} finally {
|
|
curl_close($curl);
|
|
}
|
|
}
|
|
}
|