karadav/lib/KaraDAV/Server.php

73 lines
1.7 KiB
PHP
Raw Normal View History

2022-08-30 05:01:39 +00:00
<?php
namespace KaraDAV;
2022-09-29 23:39:24 +00:00
use KD2\WebDAV\Server as WebDAV_Server;
use KD2\WebDAV\Exception;
2022-08-30 05:01:39 +00:00
2022-09-29 23:39:24 +00:00
class Server extends WebDAV_Server
2022-08-30 05:01:39 +00:00
{
2022-08-31 06:06:27 +00:00
protected Users $users;
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-09-29 23:39:24 +00:00
$storage = new Storage($this->users);
$this->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-09-29 23:39:24 +00:00
$nc = new NextCloud($this->users);
2022-08-30 05:01:39 +00:00
2022-09-29 23:39:24 +00:00
if ($r = $nc->route($uri)) {
if ($r['route'] == 'direct') {
$this->http_get($r['uri']);
return true;
}
2022-09-29 23:39:24 +00:00
elseif ($r['route'] == 'webdav') {
$this->setBaseURI($r['base_uri']);
2022-08-31 06:06:27 +00:00
}
2022-09-29 23:39:24 +00:00
else {
// NextCloud route already replied something, stop here
return true;
2022-08-30 05:01:39 +00:00
}
}
else {
2022-09-29 23:39:24 +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
2022-09-29 23:39:24 +00:00
if (0 !== strpos($uri, '/files/')) {
return false;
2022-08-31 06:06:27 +00:00
}
2022-08-30 05:01:39 +00:00
2022-09-29 23:39:24 +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-09-29 23:39:24 +00:00
if (!$user) {
http_response_code(401);
header('WWW-Authenticate: Basic realm="Please login"');
return true;
2022-08-30 05:01:39 +00:00
}
2022-09-29 23:39:24 +00:00
$this->setBaseURI('/files/' . $user->login . '/');
2022-08-30 05:01:39 +00:00
}
2022-09-29 23:39:24 +00:00
return parent::route($uri);
2022-08-30 05:01:39 +00:00
}
2022-08-31 07:57:49 +00:00
protected function html_directory(string $uri, iterable $list, array $strings = self::LANGUAGE_STRINGS): ?string
2022-08-30 05:01:39 +00:00
{
$out = parent::html_directory($uri, $list, $strings);
2022-08-31 07:57:49 +00:00
if (null !== $out) {
$out = str_replace('</head>', sprintf('<link rel="stylesheet" type="text/css" href="%sfiles.css" /></head>', WWW_URL), $out);
$out = str_replace('</body>', sprintf('<script type="text/javascript" src="%sfiles.js"></script></body>', WWW_URL), $out);
}
2022-08-31 06:06:27 +00:00
return $out;
}
2022-08-30 05:01:39 +00:00
}