2022-08-30 05:01:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace KaraDAV;
|
|
|
|
|
2022-10-15 00:15:39 +00:00
|
|
|
use KD2\WebDAV\WOPI;
|
|
|
|
|
2022-10-03 01:03:19 +00:00
|
|
|
class Server
|
2022-08-30 05:01:39 +00:00
|
|
|
{
|
2022-10-03 01:03:19 +00:00
|
|
|
public Users $users;
|
|
|
|
public WebDAV $dav;
|
2022-11-04 19:52:02 +00:00
|
|
|
public NextCloud $nc;
|
2022-08-30 05:01:39 +00:00
|
|
|
|
2022-08-31 06:06:27 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
2022-09-29 23:39:24 +00:00
|
|
|
$users = new Users;
|
2022-08-31 06:06:27 +00:00
|
|
|
$this->users = new Users;
|
2022-10-03 01:03:19 +00:00
|
|
|
$this->dav = new WebDAV;
|
2022-11-04 19:52:02 +00:00
|
|
|
$this->nc = new NextCloud($this->users);
|
|
|
|
$storage = new Storage($this->users, $this->nc);
|
|
|
|
$this->dav->setStorage($storage);
|
2022-08-31 06:06:27 +00:00
|
|
|
}
|
|
|
|
|
2022-08-30 05:01:39 +00:00
|
|
|
public function route(?string $uri = null): bool
|
|
|
|
{
|
2022-10-10 12:01:20 +00:00
|
|
|
$method = $_SERVER['REQUEST_METHOD'] ?? null;
|
|
|
|
|
|
|
|
// Always say YES to OPTIONS
|
|
|
|
if ($method == 'OPTIONS') {
|
|
|
|
$this->dav->http_options();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-15 00:15:39 +00:00
|
|
|
if (WOPI_DISCOVERY_URL) {
|
|
|
|
$wopi = new WOPI;
|
|
|
|
$wopi->setServer($this->dav);
|
|
|
|
|
|
|
|
if ($wopi->route($uri)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 19:52:02 +00:00
|
|
|
$this->nc->setServer($this->dav);
|
2022-08-30 05:01:39 +00:00
|
|
|
|
2022-11-04 19:52:02 +00:00
|
|
|
if ($r = $this->nc->route($uri)) {
|
2022-10-03 01:03:19 +00:00
|
|
|
// NextCloud route already replied something, stop here
|
|
|
|
return true;
|
2022-08-30 05:01:39 +00:00
|
|
|
}
|
|
|
|
|
2022-10-03 01:03:19 +00:00
|
|
|
// 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.
|
2022-08-30 05:01:39 +00:00
|
|
|
|
2023-03-12 19:40:55 +00:00
|
|
|
$base = rtrim(parse_url(WWW_URL, PHP_URL_PATH), '/');
|
|
|
|
|
|
|
|
if (0 !== strpos($uri, $base . '/files/')) {
|
2022-10-03 01:03:19 +00:00
|
|
|
return false;
|
2022-08-30 05:01:39 +00:00
|
|
|
}
|
|
|
|
|
2022-10-03 01:03:19 +00:00
|
|
|
$user = $this->users->login($_SERVER['PHP_AUTH_USER'] ?? null, $_SERVER['PHP_AUTH_PW'] ?? null);
|
2022-08-30 05:01:39 +00:00
|
|
|
|
2022-10-03 01:03:19 +00:00
|
|
|
if (!$user) {
|
|
|
|
http_response_code(401);
|
|
|
|
header('WWW-Authenticate: Basic realm="Please login"');
|
|
|
|
return true;
|
2022-08-31 07:57:49 +00:00
|
|
|
}
|
2022-08-31 06:06:27 +00:00
|
|
|
|
2023-03-12 19:40:55 +00:00
|
|
|
$this->dav->setBaseURI($base . '/files/' . $user->login . '/');
|
2022-10-03 01:03:19 +00:00
|
|
|
|
|
|
|
return $this->dav->route($uri);
|
2022-08-31 06:06:27 +00:00
|
|
|
}
|
2022-08-30 05:01:39 +00:00
|
|
|
}
|