68 lines
1.3 KiB
PHP
68 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace KaraDAV;
|
|
|
|
use KD2\WebDAV\WOPI;
|
|
|
|
class Server
|
|
{
|
|
public Users $users;
|
|
public WebDAV $dav;
|
|
|
|
public function __construct()
|
|
{
|
|
$users = new Users;
|
|
$this->users = new Users;
|
|
$this->dav = new WebDAV;
|
|
$this->dav->setStorage(new Storage($this->users));
|
|
}
|
|
|
|
public function route(?string $uri = null): bool
|
|
{
|
|
$method = $_SERVER['REQUEST_METHOD'] ?? null;
|
|
|
|
// Always say YES to OPTIONS
|
|
if ($method == 'OPTIONS') {
|
|
$this->dav->http_options();
|
|
return true;
|
|
}
|
|
|
|
if (WOPI_DISCOVERY_URL) {
|
|
$wopi = new WOPI;
|
|
$wopi->setServer($this->dav);
|
|
|
|
if ($wopi->route($uri)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
$nc = new NextCloud($this->dav, $this->users);
|
|
|
|
if ($r = $nc->route($uri)) {
|
|
// NextCloud route already replied something, stop here
|
|
return true;
|
|
}
|
|
|
|
// If NextCloud layer didn't return anything
|
|
// it means we fall back to the default WebDAV server
|
|
// available on the root path. We need to handle a
|
|
// classic login/password auth here.
|
|
|
|
if (0 !== strpos($uri, '/files/')) {
|
|
return false;
|
|
}
|
|
|
|
$user = $this->users->login($_SERVER['PHP_AUTH_USER'] ?? null, $_SERVER['PHP_AUTH_PW'] ?? null);
|
|
|
|
if (!$user) {
|
|
http_response_code(401);
|
|
header('WWW-Authenticate: Basic realm="Please login"');
|
|
return true;
|
|
}
|
|
|
|
$this->dav->setBaseURI('/files/' . $user->login . '/');
|
|
|
|
return $this->dav->route($uri);
|
|
}
|
|
}
|