mirror of
https://github.com/Terranom674/Piwigo_Bratonien_Tools.git
synced 2026-09-19 22:14:33 +00:00
Compare commits
24 Commits
cleanup/re
...
9eb38d7dcf
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9eb38d7dcf | ||
|
|
1a7e9a13ee | ||
|
|
e1d2598e22 | ||
|
|
29063b296a | ||
|
|
210c180e49 | ||
|
|
7ef6c24db4 | ||
|
|
1e57e6d300 | ||
|
|
57eec81801 | ||
|
|
0ea01d9a26 | ||
|
|
b901b4e887 | ||
|
|
bc5a94dc10 | ||
|
|
34a473fa07 | ||
|
|
7fc038e107 | ||
|
|
3b2a8306ad | ||
|
|
dae5f9a610 | ||
|
|
929f18a5f5 | ||
|
|
0c45576e2f | ||
|
|
ee01842cb4 | ||
|
|
a872f9238c | ||
|
|
fdcf045f39 | ||
|
|
7831a9513f | ||
|
|
fed089db4c | ||
|
|
77a295d74f | ||
|
|
ecabf2da2f |
44
main.inc.php
44
main.inc.php
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
/*
|
||||
Plugin Name: Bratonien Tools
|
||||
Version: 0.9.6.2
|
||||
Version: 0.9.6.8.5
|
||||
Description: Erweiterbare Administrationswerkzeuge fuer die Bratonien-Piwigo-Installation.
|
||||
Plugin URI: https://github.com/Terranom674/Piwigo_Bratonien_Tools
|
||||
Author: Bratonien
|
||||
@@ -81,36 +81,46 @@ function bratonien_tools_prepare_private_album_permissions()
|
||||
return;
|
||||
}
|
||||
|
||||
$result = pwg_query('SELECT status FROM '.CATEGORIES_TABLE.' WHERE id = '.$category_id.' LIMIT 1');
|
||||
if (!pwg_db_num_rows($result))
|
||||
bratonien_tools_grant_private_album_access($category_id, (int)$user['id']);
|
||||
}
|
||||
|
||||
function bratonien_tools_grant_private_album_access($category_id, $user_id)
|
||||
{
|
||||
$category_id = (int)$category_id;
|
||||
$user_id = (int)$user_id;
|
||||
if ($category_id < 1 || $user_id < 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
$category = pwg_db_fetch_assoc($result);
|
||||
if ($category['status'] !== 'public')
|
||||
$query = '
|
||||
SELECT 1
|
||||
FROM '.USER_ACCESS_TABLE.'
|
||||
WHERE user_id = '.$user_id.'
|
||||
AND cat_id = '.$category_id.'
|
||||
LIMIT 1
|
||||
;';
|
||||
$result = pwg_query($query);
|
||||
if (pwg_db_num_rows($result) > 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
$users = isset($_POST['users']) && is_array($_POST['users']) ? $_POST['users'] : array();
|
||||
$current_user_id = (int)$user['id'];
|
||||
$normalized_user_ids = array_map('intval', $users);
|
||||
|
||||
if (!in_array($current_user_id, $normalized_user_ids, true))
|
||||
{
|
||||
$users[] = $current_user_id;
|
||||
}
|
||||
|
||||
$_POST['users'] = $users;
|
||||
single_insert(
|
||||
USER_ACCESS_TABLE,
|
||||
array(
|
||||
'user_id' => $user_id,
|
||||
'cat_id' => $category_id,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function bratonien_tools_admin_menu($menu)
|
||||
{
|
||||
$menu[] = array(
|
||||
'NAME' => 'Bratonien Tools',
|
||||
'URL' => get_root_url() . 'admin.php?page=plugin-' . BRATONIEN_TOOLS_ID,
|
||||
'URL' => get_root_url() . 'admin.php?page=plugin-' . BRATONIEN_TOOLS_ID,
|
||||
);
|
||||
|
||||
return $menu;
|
||||
}
|
||||
?>
|
||||
101
runtime/repair-webdav-orphans.php
Normal file
101
runtime/repair-webdav-orphans.php
Normal file
@@ -0,0 +1,101 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
if (PHP_SAPI !== 'cli')
|
||||
{
|
||||
fwrite(STDERR, "CLI only\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$pluginRoot = dirname(__DIR__);
|
||||
$piwigoRoot = dirname($pluginRoot, 2);
|
||||
define('PHPWG_ROOT_PATH', rtrim($piwigoRoot, '/').'/');
|
||||
|
||||
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
||||
$_SERVER['SERVER_ADDR'] = '127.0.0.1';
|
||||
$_SERVER['SERVER_NAME'] = 'localhost';
|
||||
$_SERVER['HTTP_HOST'] = 'localhost';
|
||||
$_SERVER['SERVER_PORT'] = '80';
|
||||
$_SERVER['REQUEST_METHOD'] = 'GET';
|
||||
$_SERVER['REQUEST_URI'] = '/';
|
||||
$_SERVER['SCRIPT_NAME'] = '/plugins/bratonien_tools/runtime/repair-webdav-orphans.php';
|
||||
$_SERVER['PHP_SELF'] = $_SERVER['SCRIPT_NAME'];
|
||||
$_SERVER['QUERY_STRING'] = '';
|
||||
$_SERVER['HTTPS'] = 'off';
|
||||
|
||||
require_once(PHPWG_ROOT_PATH.'include/common.inc.php');
|
||||
require_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
|
||||
|
||||
$stateRoot = '/var/lib/bratonien-tools/nc-connector';
|
||||
$marker = $stateRoot.'/.webdav-orphan-repair-0963.done';
|
||||
|
||||
try
|
||||
{
|
||||
if (is_file($marker))
|
||||
{
|
||||
echo "NC WebDAV Altlasten-Reparatur: bereits abgeschlossen.\n";
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (!is_dir($stateRoot) && !mkdir($stateRoot, 0750, true))
|
||||
{
|
||||
throw new RuntimeException('State-Verzeichnis konnte nicht angelegt werden: '.$stateRoot);
|
||||
}
|
||||
|
||||
$relativePrefix = './_data/bratonien-tools/nc-webdav-gallery/connection-';
|
||||
$absolutePrefix = rtrim(PHPWG_ROOT_PATH, '/').'/_data/bratonien-tools/nc-webdav-gallery/connection-';
|
||||
$relativeLength = strlen($relativePrefix);
|
||||
$absoluteLength = strlen($absolutePrefix);
|
||||
|
||||
$query = "SELECT id,path FROM ".IMAGES_TABLE.
|
||||
" WHERE LEFT(path,".$relativeLength.")='".pwg_db_real_escape_string($relativePrefix)."'".
|
||||
" OR LEFT(path,".$absoluteLength.")='".pwg_db_real_escape_string($absolutePrefix)."'";
|
||||
$result = pwg_query($query);
|
||||
|
||||
$staleIds = array();
|
||||
while ($row = pwg_db_fetch_assoc($result))
|
||||
{
|
||||
$id = (int)$row['id'];
|
||||
$storedPath = (string)$row['path'];
|
||||
if ($id < 1 || $storedPath === '') continue;
|
||||
|
||||
if (strpos($storedPath, $relativePrefix) === 0)
|
||||
{
|
||||
$filesystemPath = rtrim(PHPWG_ROOT_PATH, '/').'/'.ltrim(substr($storedPath, 2), '/');
|
||||
}
|
||||
elseif (strpos($storedPath, $absolutePrefix) === 0)
|
||||
{
|
||||
$filesystemPath = $storedPath;
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!is_file($filesystemPath))
|
||||
{
|
||||
$staleIds[] = $id;
|
||||
}
|
||||
}
|
||||
|
||||
$staleIds = array_values(array_unique(array_map('intval', $staleIds)));
|
||||
if ($staleIds)
|
||||
{
|
||||
delete_elements($staleIds, false);
|
||||
invalidate_user_cache(true);
|
||||
}
|
||||
|
||||
$payload = date('c').' removed='.count($staleIds)."\n";
|
||||
if (file_put_contents($marker, $payload, LOCK_EX) === false)
|
||||
{
|
||||
throw new RuntimeException('Abschlussmarker konnte nicht geschrieben werden: '.$marker);
|
||||
}
|
||||
@chmod($marker, 0640);
|
||||
|
||||
echo 'NC WebDAV Altlasten-Reparatur: entfernte verwaiste Bilder='.count($staleIds)."\n";
|
||||
exit(0);
|
||||
}
|
||||
catch (Throwable $e)
|
||||
{
|
||||
fwrite(STDERR, 'NC WebDAV Altlasten-Reparatur: '.$e->getMessage()."\n");
|
||||
exit(1);
|
||||
}
|
||||
@@ -10,6 +10,11 @@ if ! php "$SCRIPT_DIR/reconcile-webdav.php"; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! php "$SCRIPT_DIR/repair-webdav-orphans.php"; then
|
||||
echo "NC Connector: verwaiste WebDAV-Bilddatensaetze konnten nicht repariert werden." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! php "$SCRIPT_DIR/cleanup-webdav-piwigo.php"; then
|
||||
echo "NC Connector: Piwigo-Inhalte geloeschter WebDAV-Verbindungen konnten nicht bereinigt werden." >&2
|
||||
exit 1
|
||||
|
||||
183
webdav-derivative-debug.php
Normal file
183
webdav-derivative-debug.php
Normal file
@@ -0,0 +1,183 @@
|
||||
<?php
|
||||
define('PHPWG_ROOT_PATH', '../../');
|
||||
include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
|
||||
|
||||
if (!defined('BRATONIEN_TOOLS_PATH'))
|
||||
{
|
||||
define('BRATONIEN_TOOLS_ID', basename(__DIR__));
|
||||
define('BRATONIEN_TOOLS_PATH', PHPWG_ROOT_PATH.'plugins/'.BRATONIEN_TOOLS_ID.'/');
|
||||
}
|
||||
require_once(BRATONIEN_TOOLS_PATH.'include/webdav_image_runtime.inc.php');
|
||||
|
||||
function bratonien_tools_webdav_debug_out($status, array $data)
|
||||
{
|
||||
http_response_code((int)$status);
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('Cache-Control: no-store');
|
||||
echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
global $user;
|
||||
if (!isset($user['status']) || !in_array($user['status'], array('admin', 'webmaster'), true))
|
||||
{
|
||||
bratonien_tools_webdav_debug_out(403, array('ok'=>false, 'stage'=>'auth', 'error'=>'Nur fuer Administratoren/Webmaster.'));
|
||||
}
|
||||
|
||||
$image_id = (int)($_GET['id'] ?? 0);
|
||||
if ($image_id < 1)
|
||||
{
|
||||
bratonien_tools_webdav_debug_out(400, array('ok'=>false, 'stage'=>'input', 'error'=>'Bild-ID fehlt.'));
|
||||
}
|
||||
|
||||
$debug = array(
|
||||
'ok'=>false,
|
||||
'image_id'=>$image_id,
|
||||
'stage'=>'start',
|
||||
);
|
||||
|
||||
$result = pwg_query('SELECT id, path, coi FROM '.IMAGES_TABLE.' WHERE id='.$image_id.' LIMIT 1');
|
||||
if (!pwg_db_num_rows($result))
|
||||
{
|
||||
$debug['stage'] = 'image-row';
|
||||
$debug['error'] = 'Bild nicht gefunden.';
|
||||
bratonien_tools_webdav_debug_out(404, $debug);
|
||||
}
|
||||
$row = pwg_db_fetch_assoc($result);
|
||||
$image_path = (string)($row['path'] ?? '');
|
||||
$debug['images_path'] = $image_path;
|
||||
|
||||
$absolute = $image_path;
|
||||
if (strpos($absolute, '/') !== 0)
|
||||
{
|
||||
$absolute = PHPWG_ROOT_PATH.ltrim(preg_replace('#^\./#', '', $absolute), '/');
|
||||
}
|
||||
$debug['absolute_path'] = $absolute;
|
||||
$debug['absolute_exists'] = file_exists($absolute);
|
||||
$debug['absolute_is_file'] = is_file($absolute);
|
||||
$debug['absolute_is_link'] = is_link($absolute);
|
||||
if (is_link($absolute))
|
||||
{
|
||||
$debug['link_target'] = readlink($absolute);
|
||||
}
|
||||
|
||||
$resolved = realpath($absolute);
|
||||
$debug['realpath'] = $resolved === false ? null : $resolved;
|
||||
if ($resolved === false)
|
||||
{
|
||||
$debug['stage'] = 'realpath';
|
||||
$debug['error'] = 'realpath() konnte images.path nicht aufloesen.';
|
||||
bratonien_tools_webdav_debug_out(200, $debug);
|
||||
}
|
||||
|
||||
$normalized = str_replace('\\', '/', $resolved);
|
||||
$debug['normalized_realpath'] = $normalized;
|
||||
|
||||
$match = array();
|
||||
if (!preg_match('#/nc-webdav-source/connection-([0-9]+)/root-([0-9]+)/(.*)$#', $normalized, $match))
|
||||
{
|
||||
$debug['stage'] = 'source-regex';
|
||||
$debug['error'] = 'realpath passt nicht zum erwarteten nc-webdav-source-Schema.';
|
||||
bratonien_tools_webdav_debug_out(200, $debug);
|
||||
}
|
||||
|
||||
$connection_id = (int)$match[1];
|
||||
$root_fileid = (int)$match[2];
|
||||
$relative_path = trim((string)$match[3], '/');
|
||||
$debug['connection_id'] = $connection_id;
|
||||
$debug['root_fileid'] = $root_fileid;
|
||||
$debug['relative_path'] = $relative_path;
|
||||
|
||||
$table = defined('BRATONIEN_TOOLS_NC_CONNECTIONS_TABLE')
|
||||
? BRATONIEN_TOOLS_NC_CONNECTIONS_TABLE
|
||||
: $GLOBALS['prefixeTable'].'bratonien_tools_nc_connections';
|
||||
$debug['connections_table'] = $table;
|
||||
|
||||
$connection_result = pwg_query('SELECT id, enabled, adapter, config_json FROM `'.$table.'` WHERE id='.$connection_id.' LIMIT 1');
|
||||
if (!pwg_db_num_rows($connection_result))
|
||||
{
|
||||
$debug['stage'] = 'connection-row';
|
||||
$debug['error'] = 'Erkannte WebDAV-Verbindung existiert nicht in der Connector-Tabelle.';
|
||||
bratonien_tools_webdav_debug_out(200, $debug);
|
||||
}
|
||||
$connection_row = pwg_db_fetch_assoc($connection_result);
|
||||
$debug['connection_enabled'] = (int)($connection_row['enabled'] ?? 0);
|
||||
$debug['connection_adapter'] = (string)($connection_row['adapter'] ?? '');
|
||||
|
||||
$config = json_decode((string)($connection_row['config_json'] ?? ''), true);
|
||||
if (!is_array($config))
|
||||
{
|
||||
$debug['stage'] = 'config-json';
|
||||
$debug['error'] = 'config_json ist kein gueltiges JSON-Objekt.';
|
||||
bratonien_tools_webdav_debug_out(200, $debug);
|
||||
}
|
||||
|
||||
$debug['source_mode'] = (string)($config['source_mode'] ?? '');
|
||||
$debug['state_dir'] = (string)($config['state_dir'] ?? '');
|
||||
$debug['parallel_gallery_root'] = (string)($config['parallel_gallery_root'] ?? '');
|
||||
|
||||
$roots = isset($config['roots']) && is_array($config['roots']) ? $config['roots'] : array();
|
||||
$debug['roots'] = array();
|
||||
$root_path = '';
|
||||
foreach ($roots as $root)
|
||||
{
|
||||
$entry = array(
|
||||
'fileid'=>(int)($root['fileid'] ?? 0),
|
||||
'webdav_path'=>(string)($root['webdav_path'] ?? ''),
|
||||
'display_name'=>(string)($root['display_name'] ?? ''),
|
||||
);
|
||||
$debug['roots'][] = $entry;
|
||||
if ($entry['fileid'] === $root_fileid)
|
||||
{
|
||||
$root_path = trim($entry['webdav_path'], '/');
|
||||
}
|
||||
}
|
||||
$debug['matched_root_path'] = $root_path;
|
||||
if ($debug['source_mode'] !== 'webdav-placeholder')
|
||||
{
|
||||
$debug['stage'] = 'source-mode';
|
||||
$debug['error'] = 'source_mode ist nicht webdav-placeholder.';
|
||||
bratonien_tools_webdav_debug_out(200, $debug);
|
||||
}
|
||||
if ($root_path === '')
|
||||
{
|
||||
$debug['stage'] = 'root-match';
|
||||
$debug['error'] = 'root_fileid aus dem Dateipfad kommt in config.roots nicht vor.';
|
||||
bratonien_tools_webdav_debug_out(200, $debug);
|
||||
}
|
||||
|
||||
$mapping_file = rtrim((string)($config['state_dir'] ?? ''), '/').'/webdav-map.json';
|
||||
$debug['mapping_file'] = $mapping_file;
|
||||
$debug['mapping_readable'] = is_readable($mapping_file);
|
||||
$debug['mapping_has_resolved_key'] = false;
|
||||
$debug['mapping_has_normalized_key'] = false;
|
||||
$debug['mapping_entry'] = null;
|
||||
|
||||
if (is_readable($mapping_file))
|
||||
{
|
||||
$mapping = json_decode((string)file_get_contents($mapping_file), true);
|
||||
if (is_array($mapping) && isset($mapping['files']) && is_array($mapping['files']))
|
||||
{
|
||||
$debug['mapping_has_resolved_key'] = array_key_exists($resolved, $mapping['files']);
|
||||
$debug['mapping_has_normalized_key'] = array_key_exists($normalized, $mapping['files']);
|
||||
$entry = $mapping['files'][$resolved] ?? $mapping['files'][$normalized] ?? null;
|
||||
if (is_array($entry))
|
||||
{
|
||||
$debug['mapping_entry'] = $entry;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$debug['mapping_error'] = 'Mapping-Datei ist ungueltig oder enthaelt kein files-Objekt.';
|
||||
}
|
||||
}
|
||||
|
||||
$debug['runtime_source_info'] = bratonien_tools_webdav_image_source_info($image_id);
|
||||
$debug['stage'] = $debug['runtime_source_info'] ? 'ok' : 'runtime-source-info-null';
|
||||
$debug['ok'] = (bool)$debug['runtime_source_info'];
|
||||
if (!$debug['ok'])
|
||||
{
|
||||
$debug['error'] = 'Die produktive Zuordnungsfunktion liefert trotz der obigen Zwischenergebnisse null.';
|
||||
}
|
||||
|
||||
bratonien_tools_webdav_debug_out(200, $debug);
|
||||
494
webdav-derivative-test.php
Normal file
494
webdav-derivative-test.php
Normal file
@@ -0,0 +1,494 @@
|
||||
<?php
|
||||
define('PHPWG_ROOT_PATH', '../../');
|
||||
include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
|
||||
require_once(PHPWG_ROOT_PATH.'include/derivative.inc.php');
|
||||
|
||||
function bratonien_tools_webdav_derivative_test_abort($status, $message)
|
||||
{
|
||||
http_response_code((int)$status);
|
||||
header('Content-Type: text/plain; charset=utf-8');
|
||||
header('Cache-Control: no-store');
|
||||
echo $message;
|
||||
exit;
|
||||
}
|
||||
|
||||
function bratonien_tools_webdav_derivative_test_decrypt_secret($blob, $hex_key)
|
||||
{
|
||||
$hex_key = trim((string)$hex_key);
|
||||
if (!preg_match('/^[a-f0-9]{64}$/', $hex_key)) return null;
|
||||
|
||||
$outer = base64_decode(trim((string)$blob), true);
|
||||
$payload = is_string($outer) ? json_decode($outer, true) : null;
|
||||
if (!is_array($payload) || (int)($payload['v'] ?? 0) !== 1) return null;
|
||||
|
||||
$iv = base64_decode((string)($payload['iv'] ?? ''), true);
|
||||
$tag = base64_decode((string)($payload['tag'] ?? ''), true);
|
||||
$cipher = base64_decode((string)($payload['data'] ?? ''), true);
|
||||
if (!is_string($iv) || !is_string($tag) || !is_string($cipher)) return null;
|
||||
|
||||
$plain = openssl_decrypt($cipher, 'aes-256-gcm', hex2bin($hex_key), OPENSSL_RAW_DATA, $iv, $tag);
|
||||
if ($plain === false) return null;
|
||||
|
||||
$decoded = json_decode((string)$plain, true);
|
||||
return is_array($decoded) ? $decoded : null;
|
||||
}
|
||||
|
||||
function bratonien_tools_webdav_derivative_test_quote_path($path)
|
||||
{
|
||||
$parts = array_values(array_filter(explode('/', trim((string)$path, '/')), 'strlen'));
|
||||
return implode('/', array_map('rawurlencode', $parts));
|
||||
}
|
||||
|
||||
function bratonien_tools_webdav_derivative_test_source_info($image_id, array $image_row)
|
||||
{
|
||||
$image_id = (int)$image_id;
|
||||
$path = (string)($image_row['path'] ?? '');
|
||||
if ($image_id < 1 || $path === '') return null;
|
||||
|
||||
$absolute = $path;
|
||||
if (strpos($absolute, '/') !== 0)
|
||||
{
|
||||
$absolute = PHPWG_ROOT_PATH.ltrim(preg_replace('#^\\./#', '', $absolute), '/');
|
||||
}
|
||||
|
||||
$resolved = realpath($absolute);
|
||||
if ($resolved === false) return null;
|
||||
|
||||
$normalized = str_replace('\\', '/', $resolved);
|
||||
if (!preg_match('#/nc-webdav-source/connection-([0-9]+)/root-([0-9]+)/(.*)$#', $normalized, $match))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
$connection_id = (int)$match[1];
|
||||
$root_fileid = (int)$match[2];
|
||||
$relative_path = trim((string)$match[3], '/');
|
||||
if ($connection_id < 1 || $root_fileid < 1 || $relative_path === '') return null;
|
||||
|
||||
$table = $GLOBALS['prefixeTable'].'bratonien_tools_nc_connections';
|
||||
$connection_result = pwg_query('SELECT config_json FROM `'.$table.'` WHERE id='.$connection_id.' LIMIT 1');
|
||||
if (!pwg_db_num_rows($connection_result)) return null;
|
||||
|
||||
$connection_row = pwg_db_fetch_assoc($connection_result);
|
||||
$config = json_decode((string)$connection_row['config_json'], true);
|
||||
if (!is_array($config) || (string)($config['source_mode'] ?? '') !== 'webdav-placeholder') return null;
|
||||
|
||||
$root_found = false;
|
||||
$root_path = '';
|
||||
$roots = isset($config['roots']) && is_array($config['roots']) ? $config['roots'] : array();
|
||||
foreach ($roots as $root)
|
||||
{
|
||||
if ((int)($root['fileid'] ?? 0) === $root_fileid)
|
||||
{
|
||||
$root_found = true;
|
||||
$root_path = trim((string)($root['webdav_path'] ?? ''), '/');
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$root_found) return null;
|
||||
|
||||
$webdav_path = trim($root_path === '' ? $relative_path : $root_path.'/'.$relative_path, '/');
|
||||
if ($webdav_path === '') return null;
|
||||
|
||||
$content_type = '';
|
||||
$size = 0;
|
||||
$etag = '';
|
||||
$state_dir = rtrim((string)($config['state_dir'] ?? ''), '/');
|
||||
if ($state_dir !== '')
|
||||
{
|
||||
$mapping_file = $state_dir.'/webdav-map.json';
|
||||
if (is_readable($mapping_file))
|
||||
{
|
||||
$mapping = json_decode((string)file_get_contents($mapping_file), true);
|
||||
if (is_array($mapping) && isset($mapping['files']) && is_array($mapping['files']))
|
||||
{
|
||||
$entry = $mapping['files'][$resolved] ?? $mapping['files'][$normalized] ?? null;
|
||||
if (is_array($entry) && (string)($entry['kind'] ?? '') === 'file')
|
||||
{
|
||||
$webdav_path = trim((string)($entry['webdav_path'] ?? $webdav_path), '/');
|
||||
$content_type = (string)($entry['content_type'] ?? '');
|
||||
$size = (int)($entry['size'] ?? 0);
|
||||
$etag = (string)($entry['etag'] ?? '');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return array(
|
||||
'image_id'=>$image_id,
|
||||
'connection_id'=>$connection_id,
|
||||
'webdav_path'=>$webdav_path,
|
||||
'content_type'=>$content_type,
|
||||
'size'=>$size,
|
||||
'etag'=>$etag,
|
||||
'coi'=>$image_row['coi'] ?? null,
|
||||
);
|
||||
}
|
||||
|
||||
function bratonien_tools_webdav_derivative_test_download(array $source, $destination, &$detail=null)
|
||||
{
|
||||
$detail = '';
|
||||
$table = $GLOBALS['prefixeTable'].'bratonien_tools_nc_connections';
|
||||
$result = pwg_query('SELECT config_json, secret_blob FROM `'.$table.'` WHERE id='.(int)$source['connection_id'].' LIMIT 1');
|
||||
if (!pwg_db_num_rows($result))
|
||||
{
|
||||
$detail = 'WebDAV-Verbindung nicht gefunden.';
|
||||
return false;
|
||||
}
|
||||
|
||||
$row = pwg_db_fetch_assoc($result);
|
||||
$config = json_decode((string)$row['config_json'], true);
|
||||
if (!is_array($config))
|
||||
{
|
||||
$detail = 'WebDAV-Konfiguration ist ungueltig.';
|
||||
return false;
|
||||
}
|
||||
|
||||
$key_result = pwg_query("SELECT value FROM ".$GLOBALS['prefixeTable']."config WHERE param='bratonien_nc_connector_secret' LIMIT 1");
|
||||
if (!pwg_db_num_rows($key_result))
|
||||
{
|
||||
$detail = 'Connector-Schluessel fehlt.';
|
||||
return false;
|
||||
}
|
||||
|
||||
$key_row = pwg_db_fetch_assoc($key_result);
|
||||
$credentials = bratonien_tools_webdav_derivative_test_decrypt_secret((string)$row['secret_blob'], (string)$key_row['value']);
|
||||
if (!is_array($credentials))
|
||||
{
|
||||
$detail = 'WebDAV-Zugangsdaten konnten nicht gelesen werden.';
|
||||
return false;
|
||||
}
|
||||
|
||||
$base_url = rtrim((string)($config['nextcloud_url'] ?? ''), '/');
|
||||
$user = trim((string)($credentials['nextcloud_user'] ?? ''));
|
||||
$password = (string)($credentials['nextcloud_password'] ?? '');
|
||||
$webdav_path = trim((string)($source['webdav_path'] ?? ''), '/');
|
||||
if ($base_url === '' || $user === '' || $password === '' || $webdav_path === '')
|
||||
{
|
||||
$detail = 'WebDAV-Bildquelle ist unvollstaendig.';
|
||||
return false;
|
||||
}
|
||||
|
||||
$fp = @fopen($destination, 'xb');
|
||||
if (!$fp)
|
||||
{
|
||||
$detail = 'Temporaere Quelldatei konnte nicht angelegt werden.';
|
||||
return false;
|
||||
}
|
||||
|
||||
$url = $base_url.'/remote.php/dav/files/'.rawurlencode($user).'/'.bratonien_tools_webdav_derivative_test_quote_path($webdav_path);
|
||||
$ch = curl_init($url);
|
||||
curl_setopt_array($ch, array(
|
||||
CURLOPT_FOLLOWLOCATION => false,
|
||||
CURLOPT_CONNECTTIMEOUT => 10,
|
||||
CURLOPT_TIMEOUT => 180,
|
||||
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
|
||||
CURLOPT_USERPWD => $user.':'.$password,
|
||||
CURLOPT_RETURNTRANSFER => false,
|
||||
CURLOPT_FAILONERROR => false,
|
||||
CURLOPT_FILE => $fp,
|
||||
CURLOPT_USERAGENT => 'Bratonien-Tools-WebDAV-Derivative-Parallel-Test',
|
||||
));
|
||||
|
||||
$ok = curl_exec($ch);
|
||||
$errno = curl_errno($ch);
|
||||
$error = curl_error($ch);
|
||||
$http = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
fclose($fp);
|
||||
|
||||
if ($ok === false || $errno !== 0 || $http < 200 || $http >= 300)
|
||||
{
|
||||
@unlink($destination);
|
||||
$detail = 'Nextcloud-Download fehlgeschlagen (HTTP '.$http.', cURL '.$errno.($error !== '' ? ': '.$error : '').').';
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!is_file($destination) || filesize($destination) < 1 || @getimagesize($destination) === false)
|
||||
{
|
||||
@unlink($destination);
|
||||
$detail = 'Nextcloud-Datei ist kein lesbares Bild.';
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function bratonien_tools_webdav_derivative_test_inner_url($derivative_path)
|
||||
{
|
||||
$derivative_root = PHPWG_ROOT_PATH.PWG_DERIVATIVE_DIR;
|
||||
if (strpos($derivative_path, $derivative_root) !== 0) return null;
|
||||
|
||||
$location = ltrim(substr($derivative_path, strlen($derivative_root)), '/');
|
||||
if ($location === '') return null;
|
||||
|
||||
$segments = array_map('rawurlencode', explode('/', $location));
|
||||
return rtrim(get_absolute_root_url(), '/').'/i.php?/'.implode('/', $segments);
|
||||
}
|
||||
|
||||
function bratonien_tools_webdav_derivative_test_call_i($url, &$response_body, &$response_type, &$response_status, &$detail=null)
|
||||
{
|
||||
$detail = '';
|
||||
$response_body = '';
|
||||
$response_type = 'application/octet-stream';
|
||||
$response_status = 500;
|
||||
|
||||
$ch = curl_init($url);
|
||||
curl_setopt_array($ch, array(
|
||||
CURLOPT_FOLLOWLOCATION => false,
|
||||
CURLOPT_CONNECTTIMEOUT => 10,
|
||||
CURLOPT_TIMEOUT => 180,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_FAILONERROR => false,
|
||||
CURLOPT_USERAGENT => 'Bratonien-Tools-Derivative-Parallel-Gate',
|
||||
));
|
||||
|
||||
$body = curl_exec($ch);
|
||||
$errno = curl_errno($ch);
|
||||
$error = curl_error($ch);
|
||||
$status = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$content_type = (string)curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
|
||||
curl_close($ch);
|
||||
|
||||
$response_status = $status;
|
||||
if ($content_type !== '') $response_type = $content_type;
|
||||
if (is_string($body)) $response_body = $body;
|
||||
|
||||
if ($body === false || $errno !== 0)
|
||||
{
|
||||
$detail = 'Interner i.php-Aufruf fehlgeschlagen (cURL '.$errno.($error !== '' ? ': '.$error : '').').';
|
||||
return false;
|
||||
}
|
||||
if ($status < 200 || $status >= 400)
|
||||
{
|
||||
$detail = 'i.php antwortete mit HTTP '.$status.'.';
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
global $user;
|
||||
if (!isset($user['status']) || !in_array($user['status'], array('admin', 'webmaster'), true))
|
||||
{
|
||||
bratonien_tools_webdav_derivative_test_abort(403, 'Dieser Test-Endpunkt ist nur fuer Administratoren verfuegbar.');
|
||||
}
|
||||
|
||||
$metrics_requested = isset($_GET['metrics']) && (string)$_GET['metrics'] === '1';
|
||||
$metrics_started_at = microtime(true);
|
||||
$memory_start = memory_get_usage(true);
|
||||
if (function_exists('memory_reset_peak_usage'))
|
||||
{
|
||||
memory_reset_peak_usage();
|
||||
}
|
||||
|
||||
$image_id = (int)($_GET['id'] ?? 0);
|
||||
if ($image_id < 1)
|
||||
{
|
||||
bratonien_tools_webdav_derivative_test_abort(400, 'Bild-ID fehlt.');
|
||||
}
|
||||
|
||||
$type = trim((string)($_GET['type'] ?? IMG_THUMB));
|
||||
$defined = ImageStdParams::get_defined_type_map();
|
||||
if (!isset($defined[$type]))
|
||||
{
|
||||
bratonien_tools_webdav_derivative_test_abort(400, 'Unbekannter oder deaktivierter Derivat-Typ: '.$type);
|
||||
}
|
||||
|
||||
$result = pwg_query('SELECT * FROM '.IMAGES_TABLE.' WHERE id='.$image_id.' LIMIT 1');
|
||||
if (!pwg_db_num_rows($result))
|
||||
{
|
||||
bratonien_tools_webdav_derivative_test_abort(404, 'Bild nicht gefunden.');
|
||||
}
|
||||
$image_row = pwg_db_fetch_assoc($result);
|
||||
|
||||
$source = bratonien_tools_webdav_derivative_test_source_info($image_id, $image_row);
|
||||
if (!$source)
|
||||
{
|
||||
bratonien_tools_webdav_derivative_test_abort(404, 'Keine WebDAV-Quelle fuer dieses Bild gefunden.');
|
||||
}
|
||||
|
||||
$upload_dir = rtrim((string)($GLOBALS['conf']['upload_dir'] ?? './upload'), '/');
|
||||
$test_rel_dir = $upload_dir.'/bratonien-webdav-test';
|
||||
$test_root = PHPWG_ROOT_PATH.$test_rel_dir;
|
||||
if (!is_dir($test_root) && !@mkdir($test_root, 0755, true))
|
||||
{
|
||||
bratonien_tools_webdav_derivative_test_abort(500, 'Paralleler Testbereich konnte nicht angelegt werden: '.$test_root);
|
||||
}
|
||||
if (!is_writable($test_root))
|
||||
{
|
||||
bratonien_tools_webdav_derivative_test_abort(500, 'Paralleler Testbereich ist fuer PHP nicht beschreibbar: '.$test_root);
|
||||
}
|
||||
|
||||
$lock_path = $test_root.'/image-'.$image_id.'.lock';
|
||||
$lock = @fopen($lock_path, 'c');
|
||||
if (!$lock || !flock($lock, LOCK_EX))
|
||||
{
|
||||
if ($lock) fclose($lock);
|
||||
bratonien_tools_webdav_derivative_test_abort(503, 'Paralleler Test-Lock konnte nicht gesetzt werden.');
|
||||
}
|
||||
|
||||
$extension = strtolower(pathinfo((string)($image_row['path'] ?? ''), PATHINFO_EXTENSION));
|
||||
if (!in_array($extension, array('jpg', 'jpeg', 'png', 'gif', 'webp'), true))
|
||||
{
|
||||
$extension = 'jpg';
|
||||
}
|
||||
|
||||
$token = getmypid().'-'.bin2hex(random_bytes(6));
|
||||
$source_filename = 'source-'.$image_id.'-'.$token.'.'.$extension;
|
||||
$source_path = $test_root.'/'.$source_filename;
|
||||
$source_rel = $test_rel_dir.'/'.$source_filename;
|
||||
$temp_image_id = 0;
|
||||
$derivative_path = '';
|
||||
$cleaned = false;
|
||||
|
||||
$cleanup = function() use (&$cleaned, &$temp_image_id, &$derivative_path, $source_path, $lock)
|
||||
{
|
||||
if ($cleaned) return;
|
||||
$cleaned = true;
|
||||
|
||||
if ($temp_image_id > 0)
|
||||
{
|
||||
@pwg_query('DELETE FROM '.IMAGES_TABLE.' WHERE id='.(int)$temp_image_id.' LIMIT 1');
|
||||
$temp_image_id = 0;
|
||||
}
|
||||
|
||||
if ($derivative_path !== '' && is_file($derivative_path))
|
||||
{
|
||||
@unlink($derivative_path);
|
||||
}
|
||||
|
||||
if (is_file($source_path))
|
||||
{
|
||||
@unlink($source_path);
|
||||
}
|
||||
|
||||
@flock($lock, LOCK_UN);
|
||||
@fclose($lock);
|
||||
};
|
||||
register_shutdown_function($cleanup);
|
||||
|
||||
try
|
||||
{
|
||||
$detail = '';
|
||||
if (!bratonien_tools_webdav_derivative_test_download($source, $source_path, $detail))
|
||||
{
|
||||
bratonien_tools_webdav_derivative_test_abort(502, $detail);
|
||||
}
|
||||
|
||||
$source_bytes = (int)filesize($source_path);
|
||||
|
||||
$dimensions = @getimagesize($source_path);
|
||||
if (!is_array($dimensions) || empty($dimensions[0]) || empty($dimensions[1]))
|
||||
{
|
||||
bratonien_tools_webdav_derivative_test_abort(500, 'Paralleles Original besitzt keine gueltigen Bildabmessungen.');
|
||||
}
|
||||
|
||||
$test_row = $image_row;
|
||||
unset($test_row['id']);
|
||||
$test_row['path'] = $source_rel;
|
||||
$test_row['file'] = $source_filename;
|
||||
if (array_key_exists('width', $test_row)) $test_row['width'] = (int)$dimensions[0];
|
||||
if (array_key_exists('height', $test_row)) $test_row['height'] = (int)$dimensions[1];
|
||||
|
||||
single_insert(IMAGES_TABLE, $test_row);
|
||||
$temp_image_id = (int)pwg_db_insert_id();
|
||||
if ($temp_image_id < 1)
|
||||
{
|
||||
bratonien_tools_webdav_derivative_test_abort(500, 'Temporaerer Piwigo-Testeintrag konnte nicht angelegt werden.');
|
||||
}
|
||||
|
||||
$test_row['id'] = $temp_image_id;
|
||||
$test_src = new SrcImage($test_row);
|
||||
$derivative = new DerivativeImage($defined[$type], $test_src);
|
||||
if ($derivative->same_as_source())
|
||||
{
|
||||
bratonien_tools_webdav_derivative_test_abort(409, 'Dieser Typ ist fuer das parallele Testbild identisch mit der Quelle.');
|
||||
}
|
||||
|
||||
$derivative_path = $derivative->get_path();
|
||||
if ($derivative_path === '')
|
||||
{
|
||||
bratonien_tools_webdav_derivative_test_abort(500, 'Piwigo konnte keinen Derivat-Zielpfad fuer den parallelen Test bestimmen.');
|
||||
}
|
||||
|
||||
if (is_file($derivative_path))
|
||||
{
|
||||
@unlink($derivative_path);
|
||||
clearstatcache(true, $derivative_path);
|
||||
}
|
||||
|
||||
$inner_url = bratonien_tools_webdav_derivative_test_inner_url($derivative_path);
|
||||
if ($inner_url === null)
|
||||
{
|
||||
bratonien_tools_webdav_derivative_test_abort(500, 'Piwigo-i.php-URL konnte fuer den parallelen Test nicht bestimmt werden.');
|
||||
}
|
||||
|
||||
$body = '';
|
||||
$content_type = 'application/octet-stream';
|
||||
$status = 500;
|
||||
if (!bratonien_tools_webdav_derivative_test_call_i($inner_url, $body, $content_type, $status, $detail))
|
||||
{
|
||||
bratonien_tools_webdav_derivative_test_abort(502, $detail."\n".$body);
|
||||
}
|
||||
|
||||
clearstatcache(true, $derivative_path);
|
||||
if (!is_file($derivative_path) || !is_readable($derivative_path))
|
||||
{
|
||||
bratonien_tools_webdav_derivative_test_abort(
|
||||
500,
|
||||
"i.php antwortete, aber das parallele Piwigo-Derivat wurde nicht erzeugt.\n"
|
||||
.'Testquelle: '.$source_rel."\n"
|
||||
.'i.php: '.$inner_url
|
||||
);
|
||||
}
|
||||
|
||||
$derivative_size = @getimagesize($derivative_path);
|
||||
if (!is_array($derivative_size) || empty($derivative_size[0]) || empty($derivative_size[1]))
|
||||
{
|
||||
bratonien_tools_webdav_derivative_test_abort(500, 'Das von i.php erzeugte parallele Derivat ist keine lesbare Bilddatei.');
|
||||
}
|
||||
|
||||
$generated_width = (int)$derivative_size[0];
|
||||
$generated_height = (int)$derivative_size[1];
|
||||
$derivative_bytes = (int)filesize($derivative_path);
|
||||
|
||||
$cleanup();
|
||||
|
||||
$elapsed_ms = round((microtime(true) - $metrics_started_at) * 1000, 2);
|
||||
$memory_peak = memory_get_peak_usage(true);
|
||||
$memory_delta_peak = max(0, $memory_peak - $memory_start);
|
||||
|
||||
if ($metrics_requested)
|
||||
{
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('Cache-Control: no-store');
|
||||
echo json_encode(array(
|
||||
'source_bytes'=>$source_bytes,
|
||||
'derivative_bytes'=>$derivative_bytes,
|
||||
'elapsed_ms'=>$elapsed_ms,
|
||||
'memory_start_bytes'=>$memory_start,
|
||||
'memory_peak_bytes'=>$memory_peak,
|
||||
'memory_delta_peak_bytes'=>$memory_delta_peak,
|
||||
), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
||||
exit;
|
||||
}
|
||||
|
||||
header('X-Bratonien-WebDAV-Test: parallel-i.php-success');
|
||||
header('X-Bratonien-Resource-Source-Bytes: '.$source_bytes);
|
||||
header('X-Bratonien-Resource-Derivative-Bytes: '.$derivative_bytes);
|
||||
header('X-Bratonien-Resource-Elapsed-Ms: '.$elapsed_ms);
|
||||
header('X-Bratonien-Resource-Memory-Start-Bytes: '.$memory_start);
|
||||
header('X-Bratonien-Resource-Memory-Peak-Bytes: '.$memory_peak);
|
||||
header('X-Bratonien-Resource-Memory-Delta-Peak-Bytes: '.$memory_delta_peak);
|
||||
header('X-Bratonien-WebDAV-Test-Size: '.$generated_width.'x'.$generated_height);
|
||||
header('Content-Type: '.$content_type);
|
||||
header('Content-Length: '.strlen($body));
|
||||
header('Cache-Control: no-store');
|
||||
http_response_code($status);
|
||||
echo $body;
|
||||
}
|
||||
finally
|
||||
{
|
||||
$cleanup();
|
||||
}
|
||||
296
webdav-download-test.php
Normal file
296
webdav-download-test.php
Normal file
@@ -0,0 +1,296 @@
|
||||
<?php
|
||||
define('PHPWG_ROOT_PATH', '../../');
|
||||
include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
|
||||
|
||||
function bratonien_tools_webdav_download_test_abort($status, $message)
|
||||
{
|
||||
http_response_code((int)$status);
|
||||
header('Content-Type: text/plain; charset=utf-8');
|
||||
header('Cache-Control: no-store');
|
||||
echo $message;
|
||||
exit;
|
||||
}
|
||||
|
||||
function bratonien_tools_webdav_download_test_decrypt_secret($blob, $hex_key)
|
||||
{
|
||||
$hex_key = trim((string)$hex_key);
|
||||
if (!preg_match('/^[a-f0-9]{64}$/', $hex_key)) return null;
|
||||
|
||||
$outer = base64_decode(trim((string)$blob), true);
|
||||
$payload = is_string($outer) ? json_decode($outer, true) : null;
|
||||
if (!is_array($payload) || (int)($payload['v'] ?? 0) !== 1) return null;
|
||||
|
||||
$iv = base64_decode((string)($payload['iv'] ?? ''), true);
|
||||
$tag = base64_decode((string)($payload['tag'] ?? ''), true);
|
||||
$cipher = base64_decode((string)($payload['data'] ?? ''), true);
|
||||
if (!is_string($iv) || !is_string($tag) || !is_string($cipher)) return null;
|
||||
|
||||
$plain = openssl_decrypt($cipher, 'aes-256-gcm', hex2bin($hex_key), OPENSSL_RAW_DATA, $iv, $tag);
|
||||
if ($plain === false) return null;
|
||||
|
||||
$decoded = json_decode((string)$plain, true);
|
||||
return is_array($decoded) ? $decoded : null;
|
||||
}
|
||||
|
||||
function bratonien_tools_webdav_download_test_quote_path($path)
|
||||
{
|
||||
$parts = array_values(array_filter(explode('/', trim((string)$path, '/')), 'strlen'));
|
||||
return implode('/', array_map('rawurlencode', $parts));
|
||||
}
|
||||
|
||||
function bratonien_tools_webdav_download_test_source_info($image_id, array $image_row)
|
||||
{
|
||||
$path = (string)($image_row['path'] ?? '');
|
||||
if ($image_id < 1 || $path === '') return null;
|
||||
|
||||
$absolute = $path;
|
||||
if (strpos($absolute, '/') !== 0)
|
||||
{
|
||||
$absolute = PHPWG_ROOT_PATH.ltrim(preg_replace('#^\\./#', '', $absolute), '/');
|
||||
}
|
||||
|
||||
$resolved = realpath($absolute);
|
||||
if ($resolved === false) return null;
|
||||
|
||||
$normalized = str_replace('\\', '/', $resolved);
|
||||
if (!preg_match('#/nc-webdav-source/connection-([0-9]+)/root-([0-9]+)/(.*)$#', $normalized, $match))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
$connection_id = (int)$match[1];
|
||||
$root_fileid = (int)$match[2];
|
||||
$relative_path = trim((string)$match[3], '/');
|
||||
if ($connection_id < 1 || $root_fileid < 1 || $relative_path === '') return null;
|
||||
|
||||
$table = $GLOBALS['prefixeTable'].'bratonien_tools_nc_connections';
|
||||
$result = pwg_query('SELECT config_json FROM `'.$table.'` WHERE id='.$connection_id.' LIMIT 1');
|
||||
if (!pwg_db_num_rows($result)) return null;
|
||||
|
||||
$row = pwg_db_fetch_assoc($result);
|
||||
$config = json_decode((string)$row['config_json'], true);
|
||||
if (!is_array($config) || (string)($config['source_mode'] ?? '') !== 'webdav-placeholder') return null;
|
||||
|
||||
$root_found = false;
|
||||
$root_path = '';
|
||||
foreach ((array)($config['roots'] ?? array()) as $root)
|
||||
{
|
||||
if ((int)($root['fileid'] ?? 0) === $root_fileid)
|
||||
{
|
||||
$root_found = true;
|
||||
$root_path = trim((string)($root['webdav_path'] ?? ''), '/');
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$root_found) return null;
|
||||
|
||||
$webdav_path = trim($root_path === '' ? $relative_path : $root_path.'/'.$relative_path, '/');
|
||||
if ($webdav_path === '') return null;
|
||||
|
||||
$size = 0;
|
||||
$state_dir = rtrim((string)($config['state_dir'] ?? ''), '/');
|
||||
if ($state_dir !== '')
|
||||
{
|
||||
$mapping_file = $state_dir.'/webdav-map.json';
|
||||
if (is_readable($mapping_file))
|
||||
{
|
||||
$mapping = json_decode((string)file_get_contents($mapping_file), true);
|
||||
if (is_array($mapping) && isset($mapping['files']) && is_array($mapping['files']))
|
||||
{
|
||||
$entry = $mapping['files'][$resolved] ?? $mapping['files'][$normalized] ?? null;
|
||||
if (is_array($entry) && (string)($entry['kind'] ?? '') === 'file')
|
||||
{
|
||||
$webdav_path = trim((string)($entry['webdav_path'] ?? $webdav_path), '/');
|
||||
$size = (int)($entry['size'] ?? 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return array(
|
||||
'connection_id'=>$connection_id,
|
||||
'webdav_path'=>$webdav_path,
|
||||
'size'=>$size,
|
||||
);
|
||||
}
|
||||
|
||||
function bratonien_tools_webdav_download_test_connection(array $source)
|
||||
{
|
||||
$table = $GLOBALS['prefixeTable'].'bratonien_tools_nc_connections';
|
||||
$result = pwg_query('SELECT config_json, secret_blob FROM `'.$table.'` WHERE id='.(int)$source['connection_id'].' LIMIT 1');
|
||||
if (!pwg_db_num_rows($result)) return null;
|
||||
|
||||
$row = pwg_db_fetch_assoc($result);
|
||||
$config = json_decode((string)$row['config_json'], true);
|
||||
if (!is_array($config)) return null;
|
||||
|
||||
$key_result = pwg_query("SELECT value FROM ".$GLOBALS['prefixeTable']."config WHERE param='bratonien_nc_connector_secret' LIMIT 1");
|
||||
if (!pwg_db_num_rows($key_result)) return null;
|
||||
$key_row = pwg_db_fetch_assoc($key_result);
|
||||
|
||||
$credentials = bratonien_tools_webdav_download_test_decrypt_secret((string)$row['secret_blob'], (string)$key_row['value']);
|
||||
if (!is_array($credentials)) return null;
|
||||
|
||||
$base_url = rtrim((string)($config['nextcloud_url'] ?? ''), '/');
|
||||
$user = trim((string)($credentials['nextcloud_user'] ?? ''));
|
||||
$password = (string)($credentials['nextcloud_password'] ?? '');
|
||||
if ($base_url === '' || $user === '' || $password === '') return null;
|
||||
|
||||
return array('base_url'=>$base_url, 'user'=>$user, 'password'=>$password);
|
||||
}
|
||||
|
||||
global $user;
|
||||
if (!isset($user['status']) || !in_array($user['status'], array('admin', 'webmaster'), true))
|
||||
{
|
||||
bratonien_tools_webdav_download_test_abort(403, 'Dieser Test-Endpunkt ist nur fuer Administratoren verfuegbar.');
|
||||
}
|
||||
|
||||
$image_id = (int)($_GET['id'] ?? 0);
|
||||
$parallel = (int)($_GET['parallel'] ?? 1);
|
||||
if ($image_id < 1)
|
||||
{
|
||||
bratonien_tools_webdav_download_test_abort(400, 'Bild-ID fehlt.');
|
||||
}
|
||||
if ($parallel < 1 || $parallel > 20)
|
||||
{
|
||||
bratonien_tools_webdav_download_test_abort(400, 'parallel muss zwischen 1 und 20 liegen.');
|
||||
}
|
||||
|
||||
$result = pwg_query('SELECT * FROM '.IMAGES_TABLE.' WHERE id='.$image_id.' LIMIT 1');
|
||||
if (!pwg_db_num_rows($result))
|
||||
{
|
||||
bratonien_tools_webdav_download_test_abort(404, 'Bild nicht gefunden.');
|
||||
}
|
||||
$image_row = pwg_db_fetch_assoc($result);
|
||||
|
||||
$source = bratonien_tools_webdav_download_test_source_info($image_id, $image_row);
|
||||
if (!$source)
|
||||
{
|
||||
bratonien_tools_webdav_download_test_abort(404, 'Keine WebDAV-Quelle fuer dieses Bild gefunden.');
|
||||
}
|
||||
|
||||
$connection = bratonien_tools_webdav_download_test_connection($source);
|
||||
if (!$connection)
|
||||
{
|
||||
bratonien_tools_webdav_download_test_abort(500, 'WebDAV-Verbindung konnte nicht geladen werden.');
|
||||
}
|
||||
|
||||
$upload_dir = rtrim((string)($GLOBALS['conf']['upload_dir'] ?? './upload'), '/');
|
||||
$test_rel_dir = $upload_dir.'/bratonien-webdav-download-test';
|
||||
$test_root = PHPWG_ROOT_PATH.$test_rel_dir;
|
||||
if (!is_dir($test_root) && !@mkdir($test_root, 0755, true))
|
||||
{
|
||||
bratonien_tools_webdav_download_test_abort(500, 'Download-Testbereich konnte nicht angelegt werden: '.$test_root);
|
||||
}
|
||||
if (!is_writable($test_root))
|
||||
{
|
||||
bratonien_tools_webdav_download_test_abort(500, 'Download-Testbereich ist fuer PHP nicht beschreibbar: '.$test_root);
|
||||
}
|
||||
|
||||
$url = $connection['base_url'].'/remote.php/dav/files/'.rawurlencode($connection['user']).'/'.bratonien_tools_webdav_download_test_quote_path($source['webdav_path']);
|
||||
$token = getmypid().'-'.bin2hex(random_bytes(6));
|
||||
$multi = curl_multi_init();
|
||||
$items = array();
|
||||
|
||||
for ($i = 0; $i < $parallel; $i++)
|
||||
{
|
||||
$path = $test_root.'/download-'.$image_id.'-'.$token.'-'.$i.'.tmp';
|
||||
$fp = @fopen($path, 'xb');
|
||||
if (!$fp)
|
||||
{
|
||||
foreach ($items as $item)
|
||||
{
|
||||
@fclose($item['fp']);
|
||||
@unlink($item['path']);
|
||||
curl_multi_remove_handle($multi, $item['ch']);
|
||||
curl_close($item['ch']);
|
||||
}
|
||||
curl_multi_close($multi);
|
||||
bratonien_tools_webdav_download_test_abort(500, 'Temporaere Download-Datei konnte nicht angelegt werden.');
|
||||
}
|
||||
|
||||
$ch = curl_init($url);
|
||||
curl_setopt_array($ch, array(
|
||||
CURLOPT_FOLLOWLOCATION => false,
|
||||
CURLOPT_CONNECTTIMEOUT => 10,
|
||||
CURLOPT_TIMEOUT => 180,
|
||||
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
|
||||
CURLOPT_USERPWD => $connection['user'].':'.$connection['password'],
|
||||
CURLOPT_RETURNTRANSFER => false,
|
||||
CURLOPT_FAILONERROR => false,
|
||||
CURLOPT_FILE => $fp,
|
||||
CURLOPT_USERAGENT => 'Bratonien-Tools-WebDAV-Download-Stress-Test',
|
||||
));
|
||||
curl_multi_add_handle($multi, $ch);
|
||||
$items[] = array('ch'=>$ch, 'fp'=>$fp, 'path'=>$path);
|
||||
}
|
||||
|
||||
$started_at = microtime(true);
|
||||
do
|
||||
{
|
||||
$status = curl_multi_exec($multi, $running);
|
||||
if ($status !== CURLM_OK) break;
|
||||
if ($running > 0)
|
||||
{
|
||||
$selected = curl_multi_select($multi, 1.0);
|
||||
if ($selected === -1) usleep(10000);
|
||||
}
|
||||
}
|
||||
while ($running > 0);
|
||||
$elapsed_seconds = microtime(true) - $started_at;
|
||||
|
||||
$downloads = array();
|
||||
$total_bytes = 0;
|
||||
$successful = 0;
|
||||
$failed = 0;
|
||||
|
||||
foreach ($items as $index => $item)
|
||||
{
|
||||
@fflush($item['fp']);
|
||||
@fclose($item['fp']);
|
||||
|
||||
$http = (int)curl_getinfo($item['ch'], CURLINFO_HTTP_CODE);
|
||||
$time_ms = round((float)curl_getinfo($item['ch'], CURLINFO_TOTAL_TIME) * 1000, 2);
|
||||
$errno = curl_errno($item['ch']);
|
||||
$error = curl_error($item['ch']);
|
||||
$bytes = is_file($item['path']) ? (int)filesize($item['path']) : 0;
|
||||
$ok = ($errno === 0 && $http >= 200 && $http < 300 && $bytes > 0);
|
||||
|
||||
if ($ok) $successful++; else $failed++;
|
||||
$total_bytes += $bytes;
|
||||
|
||||
$downloads[] = array(
|
||||
'index'=>$index + 1,
|
||||
'ok'=>$ok,
|
||||
'http'=>$http,
|
||||
'bytes'=>$bytes,
|
||||
'elapsed_ms'=>$time_ms,
|
||||
'mbps'=>$time_ms > 0 ? round(($bytes * 8) / ($time_ms / 1000) / 1000000, 2) : 0,
|
||||
'curl_errno'=>$errno,
|
||||
'curl_error'=>$error,
|
||||
);
|
||||
|
||||
@unlink($item['path']);
|
||||
curl_multi_remove_handle($multi, $item['ch']);
|
||||
curl_close($item['ch']);
|
||||
}
|
||||
curl_multi_close($multi);
|
||||
|
||||
$elapsed_ms = round($elapsed_seconds * 1000, 2);
|
||||
$aggregate_mbps = $elapsed_seconds > 0 ? round(($total_bytes * 8) / $elapsed_seconds / 1000000, 2) : 0;
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('Cache-Control: no-store');
|
||||
echo json_encode(array(
|
||||
'image_id'=>$image_id,
|
||||
'parallel'=>$parallel,
|
||||
'same_source_repeated'=>true,
|
||||
'expected_source_bytes'=>(int)$source['size'],
|
||||
'successful'=>$successful,
|
||||
'failed'=>$failed,
|
||||
'total_bytes'=>$total_bytes,
|
||||
'peak_temp_disk_bytes'=>$total_bytes,
|
||||
'elapsed_ms'=>$elapsed_ms,
|
||||
'aggregate_mbps'=>$aggregate_mbps,
|
||||
'downloads'=>$downloads,
|
||||
), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
||||
Reference in New Issue
Block a user