2022-11-14 22:53:15 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace KD2\WebDAV
|
|
|
|
{
|
2022-11-15 12:36:01 +01:00
|
|
|
//__KD2\WebDAV\Server__
|
2022-11-14 22:53:15 +01:00
|
|
|
|
2022-11-15 12:36:01 +01:00
|
|
|
//__KD2\WebDAV\AbstractStorage__
|
2022-11-14 22:53:15 +01:00
|
|
|
}
|
|
|
|
|
2022-11-21 18:45:54 +01:00
|
|
|
namespace PicoDAV
|
2022-11-14 22:53:15 +01:00
|
|
|
{
|
|
|
|
use KD2\WebDAV\AbstractStorage;
|
|
|
|
use KD2\WebDAV\Exception as WebDAV_Exception;
|
|
|
|
|
|
|
|
class Storage extends AbstractStorage
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* These file names will be ignored when doing a PUT
|
|
|
|
* as they are garbage, coming from some OS
|
|
|
|
*/
|
|
|
|
const PUT_IGNORE_PATTERN = '!^~(?:lock\.|^\._)|^(?:\.DS_Store|Thumbs\.db|desktop\.ini)$!';
|
|
|
|
|
|
|
|
protected string $path;
|
2022-11-21 22:37:51 +01:00
|
|
|
protected ?string $user = null;
|
|
|
|
|
|
|
|
public array $users = [];
|
2022-11-14 22:53:15 +01:00
|
|
|
|
2023-01-28 03:30:10 +01:00
|
|
|
public function __construct(string $path)
|
2022-11-14 22:53:15 +01:00
|
|
|
{
|
2023-01-28 03:30:10 +01:00
|
|
|
$this->path = $path . '/';
|
2022-11-14 22:53:15 +01:00
|
|
|
}
|
|
|
|
|
2022-11-21 22:37:51 +01:00
|
|
|
public function auth(): bool
|
|
|
|
{
|
|
|
|
if (ANONYMOUS_WRITE && ANONYMOUS_READ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->user) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = $_SERVER['PHP_AUTH_USER'] ?? null;
|
|
|
|
$password = $_SERVER['PHP_AUTH_PW'] ?? null;
|
|
|
|
|
2023-07-23 00:52:00 +02:00
|
|
|
if (!array_key_exists($user, $this->users)) {
|
2022-11-21 22:37:51 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-23 00:52:00 +02:00
|
|
|
$hash = $this->users[$user]['password'] ?? null;
|
|
|
|
|
|
|
|
// If no password is set, we accept any password as we consider that a .htaccess/.htpasswd
|
|
|
|
// access has been granted
|
|
|
|
if (null !== $hash && !password_verify($password, $hash)) {
|
2022-11-21 22:37:51 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->user = $user;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-15 13:07:07 +01:00
|
|
|
static protected function glob(string $path, string $pattern = '', int $flags = 0): array
|
|
|
|
{
|
|
|
|
$path = preg_replace('/[\*\?\[\]]/', '\\\\$0', $path);
|
|
|
|
return glob($path . $pattern, $flags);
|
|
|
|
}
|
|
|
|
|
2022-11-21 22:37:51 +01:00
|
|
|
public function canRead(string $uri): bool
|
|
|
|
{
|
|
|
|
if (in_array($uri, INTERNAL_FILES)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preg_match('/\.(?:php\d?|phtml|phps)$|^\./i', $uri)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ANONYMOUS_READ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-22 01:32:38 +01:00
|
|
|
if (!$this->auth()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$restrict = $this->users[$this->user]['restrict'] ?? [];
|
|
|
|
|
|
|
|
if (!is_array($restrict) || empty($restrict)) {
|
2022-11-21 22:37:51 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-22 01:32:38 +01:00
|
|
|
foreach ($restrict as $match) {
|
|
|
|
if (0 === strpos($uri, $match)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-21 22:37:51 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canWrite(string $uri): bool
|
|
|
|
{
|
2022-11-22 01:32:38 +01:00
|
|
|
if (!$this->auth() && !ANONYMOUS_WRITE) {
|
2022-11-21 22:37:51 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->canRead($uri)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ANONYMOUS_WRITE) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-22 01:32:38 +01:00
|
|
|
if (!$this->auth() || empty($this->users[$this->user]['write'])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$restrict = $this->users[$this->user]['restrict_write'] ?? [];
|
|
|
|
|
|
|
|
if (!is_array($restrict) || empty($restrict)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($restrict as $match) {
|
|
|
|
if (0 === strpos($uri, $match)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canOnlyCreate(string $uri): bool
|
|
|
|
{
|
|
|
|
$restrict = $this->users[$this->user]['restrict_write'] ?? [];
|
|
|
|
|
|
|
|
if (in_array($uri, $restrict, true)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$restrict = $this->users[$this->user]['restrict'] ?? [];
|
|
|
|
|
|
|
|
if (in_array($uri, $restrict, true)) {
|
2022-11-21 22:37:51 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-14 22:53:15 +01:00
|
|
|
public function list(string $uri, ?array $properties): iterable
|
|
|
|
{
|
2022-11-22 01:32:38 +01:00
|
|
|
if (!$this->canRead($uri . '/')) {
|
|
|
|
//throw new WebDAV_Exception('Access forbidden', 403);
|
2022-11-21 22:37:51 +01:00
|
|
|
}
|
|
|
|
|
2022-11-15 13:07:07 +01:00
|
|
|
$dirs = self::glob($this->path . $uri, '/*', \GLOB_ONLYDIR);
|
2022-11-14 22:53:15 +01:00
|
|
|
$dirs = array_map('basename', $dirs);
|
2022-11-22 01:32:38 +01:00
|
|
|
$dirs = array_filter($dirs, fn($a) => $this->canRead(ltrim($uri . '/' . $a, '/') . '/'));
|
2022-11-14 22:53:15 +01:00
|
|
|
natcasesort($dirs);
|
|
|
|
|
2022-11-15 13:07:07 +01:00
|
|
|
$files = self::glob($this->path . $uri, '/*');
|
2022-11-14 22:53:15 +01:00
|
|
|
$files = array_map('basename', $files);
|
|
|
|
$files = array_diff($files, $dirs);
|
|
|
|
|
2022-11-21 22:37:51 +01:00
|
|
|
// Remove PHP files and dot-files from listings
|
|
|
|
$files = array_filter($files, fn($a) => $this->canRead(ltrim($uri . '/' . $a, '/')));
|
2022-11-14 22:53:15 +01:00
|
|
|
|
|
|
|
natcasesort($files);
|
|
|
|
|
|
|
|
$files = array_flip(array_merge($dirs, $files));
|
|
|
|
$files = array_map(fn($a) => null, $files);
|
2022-11-22 01:32:38 +01:00
|
|
|
|
2022-11-14 22:53:15 +01:00
|
|
|
return $files;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get(string $uri): ?array
|
|
|
|
{
|
2022-11-21 22:37:51 +01:00
|
|
|
if (!$this->canRead($uri)) {
|
|
|
|
throw new WebDAV_Exception('Access forbidden', 403);
|
2022-11-21 18:45:54 +01:00
|
|
|
}
|
|
|
|
|
2022-11-14 22:53:15 +01:00
|
|
|
$path = $this->path . $uri;
|
|
|
|
|
|
|
|
if (!file_exists($path)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ['path' => $path];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function exists(string $uri): bool
|
|
|
|
{
|
|
|
|
return file_exists($this->path . $uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_file_property(string $uri, string $name, int $depth)
|
|
|
|
{
|
|
|
|
$target = $this->path . $uri;
|
|
|
|
|
|
|
|
switch ($name) {
|
2022-12-23 21:25:05 +01:00
|
|
|
case 'DAV::displayname':
|
|
|
|
return basename($uri);
|
2022-11-14 22:53:15 +01:00
|
|
|
case 'DAV::getcontentlength':
|
|
|
|
return is_dir($target) ? null : filesize($target);
|
|
|
|
case 'DAV::getcontenttype':
|
|
|
|
// ownCloud app crashes if mimetype is provided for a directory
|
|
|
|
// https://github.com/owncloud/android/issues/3768
|
|
|
|
return is_dir($target) ? null : mime_content_type($target);
|
|
|
|
case 'DAV::resourcetype':
|
|
|
|
return is_dir($target) ? 'collection' : '';
|
|
|
|
case 'DAV::getlastmodified':
|
2022-11-15 12:36:01 +01:00
|
|
|
if (!$uri && $depth == 0 && is_dir($target)) {
|
|
|
|
$mtime = self::getDirectoryMTime($target);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$mtime = filemtime($target);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$mtime) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new \DateTime('@' . $mtime);
|
2022-11-14 22:53:15 +01:00
|
|
|
case 'DAV::ishidden':
|
|
|
|
return basename($target)[0] == '.';
|
|
|
|
case 'DAV::getetag':
|
|
|
|
$hash = filemtime($target) . filesize($target);
|
|
|
|
return md5($hash . $target);
|
|
|
|
case 'DAV::lastaccessed':
|
|
|
|
return new \DateTime('@' . fileatime($target));
|
|
|
|
case 'DAV::creationdate':
|
|
|
|
return new \DateTime('@' . filectime($target));
|
2022-11-21 18:45:54 +01:00
|
|
|
case 'http://owncloud.org/ns:permissions':
|
|
|
|
$permissions = 'G';
|
|
|
|
|
2022-11-22 01:32:38 +01:00
|
|
|
if (is_dir($target)) {
|
|
|
|
$uri .= '/';
|
|
|
|
}
|
|
|
|
|
2022-11-21 22:37:51 +01:00
|
|
|
if (is_writeable($target) && $this->canWrite($uri)) {
|
2022-11-22 01:32:38 +01:00
|
|
|
// If the directory is one of the restricted paths,
|
|
|
|
// then we can only do stuff INSIDE, and not delete/rename the directory itself
|
|
|
|
if ($this->canOnlyCreate($uri)) {
|
|
|
|
$permissions .= 'CK';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$permissions .= 'DNVWCK';
|
|
|
|
}
|
2022-11-21 18:45:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $permissions;
|
2022-11-22 01:32:38 +01:00
|
|
|
case Server::PROP_DIGEST_MD5:
|
2023-02-14 13:37:05 +01:00
|
|
|
if (!is_file($target) || is_dir($target) || !is_readable($target)) {
|
2022-11-14 22:53:15 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return md5_file($target);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function properties(string $uri, ?array $properties, int $depth): ?array
|
|
|
|
{
|
|
|
|
$target = $this->path . $uri;
|
|
|
|
|
|
|
|
if (!file_exists($target)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null === $properties) {
|
2022-12-22 14:15:09 +01:00
|
|
|
$properties = Server::BASIC_PROPERTIES;
|
2022-11-14 22:53:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$out = [];
|
|
|
|
|
|
|
|
foreach ($properties as $name) {
|
|
|
|
$v = $this->get_file_property($uri, $name, $depth);
|
|
|
|
|
|
|
|
if (null !== $v) {
|
|
|
|
$out[$name] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
2023-02-14 13:37:05 +01:00
|
|
|
public function put(string $uri, $pointer, ?string $hash_algo, ?string $hash, ?int $mtime): bool
|
2022-11-14 22:53:15 +01:00
|
|
|
{
|
|
|
|
if (preg_match(self::PUT_IGNORE_PATTERN, basename($uri))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-21 22:37:51 +01:00
|
|
|
if (!$this->canWrite($uri)) {
|
|
|
|
throw new WebDAV_Exception('Access forbidden', 403);
|
2022-11-21 18:45:54 +01:00
|
|
|
}
|
|
|
|
|
2022-11-14 22:53:15 +01:00
|
|
|
$target = $this->path . $uri;
|
|
|
|
$parent = dirname($target);
|
|
|
|
|
|
|
|
if (is_dir($target)) {
|
|
|
|
throw new WebDAV_Exception('Target is a directory', 409);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file_exists($parent)) {
|
|
|
|
mkdir($parent, 0770, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$new = !file_exists($target);
|
|
|
|
$delete = false;
|
|
|
|
$size = 0;
|
|
|
|
$quota = disk_free_space($this->path);
|
|
|
|
|
2022-11-23 00:46:06 +01:00
|
|
|
$tmp_file = $this->path . '.tmp.' . sha1($target);
|
2022-11-14 22:53:15 +01:00
|
|
|
$out = fopen($tmp_file, 'w');
|
|
|
|
|
|
|
|
while (!feof($pointer)) {
|
|
|
|
$bytes = fread($pointer, 8192);
|
|
|
|
$size += strlen($bytes);
|
|
|
|
|
|
|
|
if ($size > $quota) {
|
|
|
|
$delete = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fwrite($out, $bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose($out);
|
|
|
|
fclose($pointer);
|
|
|
|
|
|
|
|
if ($delete) {
|
|
|
|
@unlink($tmp_file);
|
2023-02-18 13:57:24 +01:00
|
|
|
throw new WebDAV_Exception('Your quota is exhausted', 507);
|
2022-11-14 22:53:15 +01:00
|
|
|
}
|
2023-02-14 13:37:05 +01:00
|
|
|
elseif ($hash && $hash_algo == 'MD5' && md5_file($tmp_file) != $hash) {
|
2022-11-14 22:53:15 +01:00
|
|
|
@unlink($tmp_file);
|
|
|
|
throw new WebDAV_Exception('The data sent does not match the supplied MD5 hash', 400);
|
|
|
|
}
|
2023-02-14 13:37:05 +01:00
|
|
|
elseif ($hash && $hash_algo == 'SHA1' && sha1_file($tmp_file) != $hash) {
|
|
|
|
@unlink($tmp_file);
|
|
|
|
throw new WebDAV_Exception('The data sent does not match the supplied SHA1 hash', 400);
|
|
|
|
}
|
2022-11-14 22:53:15 +01:00
|
|
|
else {
|
|
|
|
rename($tmp_file, $target);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($mtime) {
|
|
|
|
@touch($target, $mtime);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $new;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete(string $uri): void
|
|
|
|
{
|
2022-11-21 22:37:51 +01:00
|
|
|
if (!$this->canWrite($uri)) {
|
|
|
|
throw new WebDAV_Exception('Access forbidden', 403);
|
2022-11-21 18:45:54 +01:00
|
|
|
}
|
|
|
|
|
2022-11-22 01:32:38 +01:00
|
|
|
if ($this->canOnlyCreate($uri)) {
|
|
|
|
throw new WebDAV_Exception('Access forbidden', 403);
|
|
|
|
}
|
|
|
|
|
2022-11-14 22:53:15 +01:00
|
|
|
$target = $this->path . $uri;
|
|
|
|
|
|
|
|
if (!file_exists($target)) {
|
|
|
|
throw new WebDAV_Exception('Target does not exist', 404);
|
|
|
|
}
|
|
|
|
|
2022-11-21 18:45:54 +01:00
|
|
|
if (!is_writeable($target)) {
|
|
|
|
throw new WebDAV_Exception('File permissions says that you cannot delete this, sorry.', 403);
|
|
|
|
}
|
|
|
|
|
2022-11-14 22:53:15 +01:00
|
|
|
if (is_dir($target)) {
|
2022-11-15 13:07:07 +01:00
|
|
|
foreach (self::glob($target, '/*') as $file) {
|
2022-11-14 22:53:15 +01:00
|
|
|
$this->delete(substr($file, strlen($this->path)));
|
|
|
|
}
|
|
|
|
|
|
|
|
rmdir($target);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
unlink($target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function copymove(bool $move, string $uri, string $destination): bool
|
|
|
|
{
|
2022-11-22 01:32:38 +01:00
|
|
|
if (!$this->canWrite($uri)
|
|
|
|
|| !$this->canWrite($destination)
|
|
|
|
|| $this->canOnlyCreate($uri)) {
|
2022-11-21 22:37:51 +01:00
|
|
|
throw new WebDAV_Exception('Access forbidden', 403);
|
2022-11-21 18:45:54 +01:00
|
|
|
}
|
|
|
|
|
2022-11-14 22:53:15 +01:00
|
|
|
$source = $this->path . $uri;
|
|
|
|
$target = $this->path . $destination;
|
|
|
|
$parent = dirname($target);
|
|
|
|
|
|
|
|
if (!file_exists($source)) {
|
|
|
|
throw new WebDAV_Exception('File not found', 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
$overwritten = file_exists($target);
|
|
|
|
|
|
|
|
if (!is_dir($parent)) {
|
|
|
|
throw new WebDAV_Exception('Target parent directory does not exist', 409);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (false === $move) {
|
|
|
|
$quota = disk_free_space($this->path);
|
|
|
|
|
|
|
|
if (filesize($source) > $quota) {
|
2023-02-18 13:57:24 +01:00
|
|
|
throw new WebDAV_Exception('Your quota is exhausted', 507);
|
2022-11-14 22:53:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($overwritten) {
|
|
|
|
$this->delete($destination);
|
|
|
|
}
|
|
|
|
|
|
|
|
$method = $move ? 'rename' : 'copy';
|
|
|
|
|
|
|
|
if ($method == 'copy' && is_dir($source)) {
|
|
|
|
@mkdir($target, 0770, true);
|
|
|
|
|
|
|
|
foreach ($iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source), \RecursiveIteratorIterator::SELF_FIRST) as $item)
|
|
|
|
{
|
|
|
|
if ($item->isDir()) {
|
|
|
|
@mkdir($target . DIRECTORY_SEPARATOR . $iterator->getSubPathname());
|
|
|
|
} else {
|
|
|
|
copy($item, $target . DIRECTORY_SEPARATOR . $iterator->getSubPathname());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$method($source, $target);
|
|
|
|
|
|
|
|
$this->getResourceProperties($uri)->move($destination);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $overwritten;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function copy(string $uri, string $destination): bool
|
|
|
|
{
|
|
|
|
return $this->copymove(false, $uri, $destination);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function move(string $uri, string $destination): bool
|
|
|
|
{
|
|
|
|
return $this->copymove(true, $uri, $destination);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function mkcol(string $uri): void
|
|
|
|
{
|
2022-11-21 22:37:51 +01:00
|
|
|
if (!$this->canWrite($uri)) {
|
|
|
|
throw new WebDAV_Exception('Access forbidden', 403);
|
2022-11-21 18:45:54 +01:00
|
|
|
}
|
|
|
|
|
2022-11-14 22:53:15 +01:00
|
|
|
if (!disk_free_space($this->path)) {
|
2023-02-18 13:57:24 +01:00
|
|
|
throw new WebDAV_Exception('Your quota is exhausted', 507);
|
2022-11-14 22:53:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$target = $this->path . $uri;
|
|
|
|
$parent = dirname($target);
|
|
|
|
|
|
|
|
if (file_exists($target)) {
|
|
|
|
throw new WebDAV_Exception('There is already a file with that name', 405);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file_exists($parent)) {
|
|
|
|
throw new WebDAV_Exception('The parent directory does not exist', 409);
|
|
|
|
}
|
|
|
|
|
|
|
|
mkdir($target, 0770);
|
|
|
|
}
|
2022-11-15 12:36:01 +01:00
|
|
|
|
|
|
|
static public function getDirectoryMTime(string $path): int
|
|
|
|
{
|
|
|
|
$last = 0;
|
|
|
|
$path = rtrim($path, '/');
|
|
|
|
|
2022-11-15 13:07:07 +01:00
|
|
|
foreach (self::glob($path, '/*', GLOB_NOSORT) as $f) {
|
2022-11-15 12:36:01 +01:00
|
|
|
if (is_dir($f)) {
|
|
|
|
$m = self::getDirectoryMTime($f);
|
|
|
|
|
|
|
|
if ($m > $last) {
|
|
|
|
$last = $m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$m = filemtime($f);
|
|
|
|
|
|
|
|
if ($m > $last) {
|
|
|
|
$last = $m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $last;
|
|
|
|
}
|
2022-11-14 22:53:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class Server extends \KD2\WebDAV\Server
|
|
|
|
{
|
|
|
|
protected function html_directory(string $uri, iterable $list): ?string
|
|
|
|
{
|
|
|
|
$out = parent::html_directory($uri, $list);
|
|
|
|
|
|
|
|
if (null !== $out) {
|
2022-11-22 02:05:11 +01:00
|
|
|
$out = str_replace('<body>', sprintf('<body style="opacity: 0"><script type="text/javascript" src="%s/.webdav/webdav.js"></script>', rtrim($this->base_uri, '/')), $out);
|
2022-11-14 22:53:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
2022-11-21 22:37:51 +01:00
|
|
|
|
2022-11-22 16:10:03 +01:00
|
|
|
public function route(?string $uri = null): bool
|
2022-11-21 22:37:51 +01:00
|
|
|
{
|
2022-11-22 16:11:28 +01:00
|
|
|
if (!ANONYMOUS_WRITE && !ANONYMOUS_READ && !$this->storage->auth()) {
|
2022-11-22 16:10:03 +01:00
|
|
|
$this->requireAuth();
|
|
|
|
return true;
|
|
|
|
}
|
2022-11-21 22:37:51 +01:00
|
|
|
|
2022-11-22 16:10:03 +01:00
|
|
|
return parent::route($uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function requireAuth(): void
|
|
|
|
{
|
|
|
|
http_response_code(401);
|
|
|
|
header('WWW-Authenticate: Basic realm="Please login"');
|
|
|
|
echo '<h2>Error 401</h2><h1>You need to login to access this.</h1>';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function error(WebDAV_Exception $e)
|
|
|
|
{
|
|
|
|
if ($e->getCode() == 403 && !$this->storage->auth() && count($this->storage->users)) {
|
2022-11-21 22:37:51 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::error($e);
|
|
|
|
}
|
2022-12-23 19:40:37 +01:00
|
|
|
|
|
|
|
protected string $_log = '';
|
|
|
|
|
|
|
|
public function log(string $message, ...$params): void
|
|
|
|
{
|
|
|
|
if (!HTTP_LOG_FILE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_log .= vsprintf($message, $params) . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
if (!$this->_log) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
file_put_contents(HTTP_LOG_FILE, $this->_log, \FILE_APPEND);
|
|
|
|
}
|
2022-11-14 22:53:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2022-11-21 18:45:54 +01:00
|
|
|
use PicoDAV\Server;
|
|
|
|
use PicoDAV\Storage;
|
2022-11-14 22:53:15 +01:00
|
|
|
|
|
|
|
$uri = strtok($_SERVER['REQUEST_URI'], '?');
|
2023-01-28 03:30:10 +01:00
|
|
|
$self = $_SERVER['SCRIPT_FILENAME'];
|
|
|
|
$self_dir = dirname($self);
|
|
|
|
$root = substr(dirname($_SERVER['SCRIPT_FILENAME']), strlen($_SERVER['DOCUMENT_ROOT']));
|
|
|
|
$root = '/' . ltrim($root, '/');
|
2022-11-14 22:53:15 +01:00
|
|
|
|
|
|
|
if (false !== strpos($uri, '..')) {
|
|
|
|
http_response_code(404);
|
|
|
|
die('Invalid URL');
|
|
|
|
}
|
|
|
|
|
|
|
|
$relative_uri = ltrim(substr($uri, strlen($root)), '/');
|
|
|
|
|
2023-01-28 03:30:10 +01:00
|
|
|
if (!empty($_SERVER['SERVER_SOFTWARE']) && stristr($_SERVER['SERVER_SOFTWARE'], 'apache') && !file_exists($self_dir . '/.htaccess')) {
|
|
|
|
file_put_contents($self_dir . '/.htaccess', str_replace('index.php', basename($self), /*__HTACCESS__*/));
|
2022-11-21 18:45:54 +01:00
|
|
|
}
|
|
|
|
|
2022-11-22 02:05:11 +01:00
|
|
|
if ($relative_uri == '.webdav/webdav.js' || $relative_uri == '.webdav/webdav.css') {
|
2022-11-14 22:53:15 +01:00
|
|
|
http_response_code(200);
|
|
|
|
|
2022-11-22 02:05:11 +01:00
|
|
|
if ($relative_uri == '.webdav/webdav.js') {
|
2022-11-14 22:53:15 +01:00
|
|
|
header('Content-Type: text/javascript', true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
header('Content-Type: text/css', true);
|
|
|
|
}
|
|
|
|
|
2022-11-22 03:41:47 +01:00
|
|
|
$seconds_to_cache = 3600 * 24 * 5;
|
2022-11-14 22:53:15 +01:00
|
|
|
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
|
|
|
|
header("Expires: " . $ts);
|
|
|
|
header("Pragma: cache");
|
|
|
|
header("Cache-Control: max-age=" . $seconds_to_cache);
|
|
|
|
|
|
|
|
$fp = fopen(__FILE__, 'r');
|
|
|
|
|
2022-11-22 02:05:11 +01:00
|
|
|
if ($relative_uri == '.webdav/webdav.js') {
|
2022-11-14 22:53:15 +01:00
|
|
|
fseek($fp, __PHP_SIZE__, SEEK_SET);
|
|
|
|
echo fread($fp, __JS_SIZE__);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fseek($fp, __PHP_SIZE__ + __JS_SIZE__, SEEK_SET);
|
|
|
|
echo fread($fp, __CSS_SIZE__);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose($fp);
|
|
|
|
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2023-01-28 03:30:10 +01:00
|
|
|
$config_file = $self_dir . '/.picodav.ini';
|
|
|
|
define('PicoDAV\INTERNAL_FILES', ['.picodav.ini', $self_dir, '.webdav/webdav.js', '.webdav/webdav.css']);
|
2022-11-21 22:37:51 +01:00
|
|
|
|
|
|
|
const DEFAULT_CONFIG = [
|
2022-11-22 01:46:15 +01:00
|
|
|
'ANONYMOUS_READ' => true,
|
2022-11-21 22:37:51 +01:00
|
|
|
'ANONYMOUS_WRITE' => false,
|
2022-12-23 19:40:37 +01:00
|
|
|
'HTTP_LOG_FILE' => null,
|
2022-11-21 22:37:51 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$config = [];
|
2023-01-28 03:30:10 +01:00
|
|
|
$storage = new Storage($self_dir);
|
2022-11-21 22:37:51 +01:00
|
|
|
|
2023-01-28 03:30:10 +01:00
|
|
|
if (file_exists($config_file)) {
|
|
|
|
$config = parse_ini_file($config_file, true);
|
2022-11-21 22:37:51 +01:00
|
|
|
$users = array_filter($config, 'is_array');
|
|
|
|
$config = array_diff_key($config, $users);
|
|
|
|
$config = array_change_key_case($config, \CASE_UPPER);
|
|
|
|
$replace = [];
|
|
|
|
|
|
|
|
// Encrypt plaintext passwords
|
|
|
|
foreach ($users as $name => $properties) {
|
|
|
|
if (isset($properties['password']) && substr($properties['password'], 0, 1) != '$') {
|
|
|
|
$users[$name]['password'] = $replace[$name] = password_hash($properties['password'], null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($replace)) {
|
2023-01-28 03:30:10 +01:00
|
|
|
$lines = file($config_file);
|
2022-11-21 22:37:51 +01:00
|
|
|
$current = null;
|
|
|
|
|
|
|
|
foreach ($lines as &$line) {
|
|
|
|
if (preg_match('/^\s*\[(\w+)\]\s*$/', $line, $match)) {
|
|
|
|
$current = $match[1];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($current && isset($replace[$current]) && preg_match('/^\s*password\s*=/', $line)) {
|
|
|
|
$line = sprintf("password = %s\n", var_export($replace[$current], true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($line, $current);
|
|
|
|
|
2023-01-28 03:30:10 +01:00
|
|
|
file_put_contents($config_file, implode('', $lines));
|
2022-11-21 22:37:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$storage->users = $users;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (DEFAULT_CONFIG as $key => $value) {
|
|
|
|
if (array_key_exists($key, $config)) {
|
|
|
|
$value = $config[$key];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_bool(DEFAULT_CONFIG[$key])) {
|
|
|
|
$value = boolval($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
define('PicoDAV\\' . $key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-28 03:30:10 +01:00
|
|
|
$dav = new Server();
|
2022-11-21 22:37:51 +01:00
|
|
|
$dav->setStorage($storage);
|
2022-11-14 22:53:15 +01:00
|
|
|
|
|
|
|
$dav->setBaseURI($root);
|
|
|
|
|
|
|
|
if (!$dav->route($uri)) {
|
|
|
|
http_response_code(404);
|
2023-02-14 13:37:05 +01:00
|
|
|
die('Unknown URL, sorry.');
|
2022-11-14 22:53:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exit;
|
|
|
|
}
|