146 lines
3.9 KiB
PHP
146 lines
3.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace FlysystemOffload\Filesystem\Adapters;
|
|
|
|
use FlysystemOffload\Filesystem\AdapterInterface; // ← CORRECTO (sin Adapters)
|
|
use League\Flysystem\FilesystemAdapter;
|
|
use League\Flysystem\WebDAV\WebDAVAdapter as LeagueWebDAVAdapter;
|
|
use Sabre\DAV\Client;
|
|
use InvalidArgumentException;
|
|
|
|
/**
|
|
* Adaptador WebDAV para Flysystem Offload
|
|
*/
|
|
class WebdavAdapter implements AdapterInterface
|
|
{
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function createAdapter(array $config): FilesystemAdapter
|
|
{
|
|
$this->validateConfig($config);
|
|
|
|
// Configurar cliente WebDAV
|
|
$clientConfig = [
|
|
'baseUri' => rtrim($config['base_uri'], '/') . '/',
|
|
'userName' => $config['username'] ?? null,
|
|
'password' => $config['password'] ?? null,
|
|
];
|
|
|
|
// Configurar tipo de autenticación
|
|
if (!empty($config['auth_type'])) {
|
|
$authType = strtolower($config['auth_type']);
|
|
$clientConfig['authType'] = match ($authType) {
|
|
'basic' => Client::AUTH_BASIC,
|
|
'digest' => Client::AUTH_DIGEST,
|
|
'ntlm' => Client::AUTH_NTLM,
|
|
default => Client::AUTH_BASIC,
|
|
};
|
|
}
|
|
|
|
// Crear cliente
|
|
$client = new Client($clientConfig);
|
|
|
|
// Configurar permisos
|
|
$permissions = [
|
|
'file' => [
|
|
'public' => $this->normalizePermission($config['file_public'] ?? '0644'),
|
|
'private' => $this->normalizePermission($config['file_private'] ?? '0600'),
|
|
],
|
|
'dir' => [
|
|
'public' => $this->normalizePermission($config['dir_public'] ?? '0755'),
|
|
'private' => $this->normalizePermission($config['dir_private'] ?? '0700'),
|
|
],
|
|
];
|
|
|
|
error_log('[WebDAV Adapter] Creating adapter with permissions: ' . print_r($permissions, true));
|
|
|
|
// Crear adaptador
|
|
$adapter = new LeagueWebDAVAdapter(
|
|
$client,
|
|
$config['prefix'] ?? '',
|
|
$permissions
|
|
);
|
|
|
|
error_log('[WebDAV Adapter] Adapter created successfully');
|
|
|
|
return $adapter;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getRequiredConfigKeys(): array
|
|
{
|
|
return ['base_uri'];
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getOptionalConfigKeys(): array
|
|
{
|
|
return [
|
|
'username',
|
|
'password',
|
|
'auth_type',
|
|
'prefix',
|
|
'file_public',
|
|
'file_private',
|
|
'dir_public',
|
|
'dir_private',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Valida la configuración
|
|
*
|
|
* @param array $config
|
|
* @throws InvalidArgumentException
|
|
*/
|
|
private function validateConfig(array $config): void
|
|
{
|
|
foreach ($this->getRequiredConfigKeys() as $key) {
|
|
if (empty($config[$key])) {
|
|
throw new InvalidArgumentException("WebDAV config key '{$key}' is required");
|
|
}
|
|
}
|
|
|
|
if (!filter_var($config['base_uri'], FILTER_VALIDATE_URL)) {
|
|
throw new InvalidArgumentException('WebDAV base_uri must be a valid URL');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Normaliza un permiso a entero octal
|
|
*
|
|
* @param string|int $permission
|
|
* @return int
|
|
*/
|
|
private function normalizePermission($permission): int
|
|
{
|
|
if (is_int($permission)) {
|
|
return $permission;
|
|
}
|
|
|
|
if (is_string($permission)) {
|
|
$permission = trim($permission);
|
|
|
|
// Si es octal string (ej: "0644")
|
|
if (preg_match('/^0[0-7]{3}$/', $permission)) {
|
|
return intval($permission, 8);
|
|
}
|
|
|
|
// Si es decimal string sin el 0 (ej: "644")
|
|
if (preg_match('/^[0-7]{3}$/', $permission)) {
|
|
return intval('0' . $permission, 8);
|
|
}
|
|
}
|
|
|
|
// Default
|
|
return 0644;
|
|
}
|
|
}
|