Add WOPI discovery service and connection diagnostics

This commit is contained in:
Sebastian Krupinski
2026-09-06 21:05:47 -04:00
committed by Sebastian Krupinski
parent 069f7068c8
commit 661e0e1213
14 changed files with 1238 additions and 2 deletions
+44
View File
@@ -0,0 +1,44 @@
<?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);
}
}
}