mirror of
https://github.com/Terranom674/Piwigo_Bratonien_Tools.git
synced 2026-09-20 16:53:22 +00:00
Compare commits
10 Commits
3a15c2d03d
...
fix/09728-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
af129c8202 | ||
|
|
e8ef0a50e8 | ||
|
|
8641b37eeb | ||
|
|
ee40d2f159 | ||
|
|
2e8f733646 | ||
|
|
de39d5849a | ||
|
|
73707433c5 | ||
|
|
dbc5e27693 | ||
|
|
91f0d0cac1 | ||
|
|
2c1c7ff499 |
@@ -18,7 +18,7 @@ function bratonien_tools_register_nc_productive_ws_methods($arr)
|
|||||||
'info' => 'Piwigo storage site to synchronize. Default: 1.',
|
'info' => 'Piwigo storage site to synchronize. Default: 1.',
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
'Runs the approved Piwigo filesystem synchronization for the Bratonien NC Connector.',
|
'Runs the approved direct Bratonien filesystem synchronization for the NC Connector.',
|
||||||
null,
|
null,
|
||||||
array(
|
array(
|
||||||
'admin_only' => true,
|
'admin_only' => true,
|
||||||
@@ -27,9 +27,17 @@ function bratonien_tools_register_nc_productive_ws_methods($arr)
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function bratonien_tools_nc_productive_error(&$errors, $path, $type)
|
||||||
|
{
|
||||||
|
$errors[] = array(
|
||||||
|
'path' => (string)$path,
|
||||||
|
'type' => (string)$type,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function bratonien_tools_ws_nc_sync_productive($params, &$service)
|
function bratonien_tools_ws_nc_sync_productive($params, &$service)
|
||||||
{
|
{
|
||||||
global $conf, $user, $template, $page;
|
global $conf, $user;
|
||||||
|
|
||||||
$piwigo_version = defined('PHPWG_VERSION') ? (string)PHPWG_VERSION : '';
|
$piwigo_version = defined('PHPWG_VERSION') ? (string)PHPWG_VERSION : '';
|
||||||
if ($piwigo_version !== '16.4.0')
|
if ($piwigo_version !== '16.4.0')
|
||||||
@@ -51,92 +59,385 @@ function bratonien_tools_ws_nc_sync_productive($params, &$service)
|
|||||||
return new PwgError(400, 'Invalid site_id.');
|
return new PwgError(400, 'Invalid site_id.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$saved_get = $_GET;
|
include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
|
||||||
$saved_post = $_POST;
|
include_once(PHPWG_ROOT_PATH.'admin/site_reader_local.php');
|
||||||
$saved_request_method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : null;
|
|
||||||
$saved_default_status = isset($conf['newcat_default_status']) ? $conf['newcat_default_status'] : null;
|
$query = 'SELECT galleries_url FROM '.SITES_TABLE.' WHERE id = '.$site_id.' LIMIT 1';
|
||||||
$saved_template_dirs = null;
|
$result = pwg_query($query);
|
||||||
$output = '';
|
if (!pwg_db_num_rows($result))
|
||||||
$counts = array();
|
{
|
||||||
|
return new PwgError(404, 'Piwigo site does not exist.');
|
||||||
|
}
|
||||||
|
|
||||||
|
list($site_url) = pwg_db_fetch_row($result);
|
||||||
|
if (url_is_remote($site_url))
|
||||||
|
{
|
||||||
|
return new PwgError(400, 'Remote Piwigo sites are not supported by this synchronization method.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$site_reader = new LocalSiteReader($site_url);
|
||||||
|
if (!$site_reader->open())
|
||||||
|
{
|
||||||
|
return new PwgError(500, 'Piwigo could not open the configured local site.');
|
||||||
|
}
|
||||||
|
|
||||||
|
list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW()'));
|
||||||
|
$errors = array();
|
||||||
|
$counts = array(
|
||||||
|
'new_categories' => 0,
|
||||||
|
'del_categories' => 0,
|
||||||
|
'new_elements' => 0,
|
||||||
|
'del_elements' => 0,
|
||||||
|
'upd_elements' => 0,
|
||||||
|
'new_formats' => 0,
|
||||||
|
'del_formats' => 0,
|
||||||
|
'metadata_candidates' => 0,
|
||||||
|
'metadata_updated' => 0,
|
||||||
|
);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (!defined('IN_ADMIN'))
|
$query = 'SELECT id, id_uppercat, uppercats, global_rank, status, visible FROM '.CATEGORIES_TABLE.' WHERE dir IS NOT NULL AND site_id = '.$site_id;
|
||||||
|
$db_categories = hash_from_query($query, 'id');
|
||||||
|
$db_fulldirs = get_fulldirs(array_keys($db_categories));
|
||||||
|
$basedir = preg_replace('#/*$#', '', $site_url);
|
||||||
|
$db_fulldirs = array_flip($db_fulldirs);
|
||||||
|
$fs_fulldirs = $site_reader->get_full_directories($basedir);
|
||||||
|
|
||||||
|
$next_rank = array('NULL'=>1);
|
||||||
|
$result = pwg_query('SELECT id FROM '.CATEGORIES_TABLE);
|
||||||
|
while ($row = pwg_db_fetch_assoc($result))
|
||||||
{
|
{
|
||||||
define('IN_ADMIN', true);
|
$next_rank[$row['id']] = 1;
|
||||||
|
}
|
||||||
|
$result = pwg_query('SELECT id_uppercat, MAX(`rank`)+1 AS next_rank FROM '.CATEGORIES_TABLE.' GROUP BY id_uppercat');
|
||||||
|
while ($row = pwg_db_fetch_assoc($result))
|
||||||
|
{
|
||||||
|
$key = empty($row['id_uppercat']) ? 'NULL' : $row['id_uppercat'];
|
||||||
|
$next_rank[$key] = (int)$row['next_rank'];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_object($template) && isset($template->smarty) && method_exists($template, 'set_template_dir'))
|
$next_id = pwg_db_nextval('id', CATEGORIES_TABLE);
|
||||||
|
$category_inserts = array();
|
||||||
|
|
||||||
|
foreach (array_diff($fs_fulldirs, array_keys($db_fulldirs)) as $fulldir)
|
||||||
{
|
{
|
||||||
$saved_template_dirs = $template->smarty->getTemplateDir();
|
$dir = basename($fulldir);
|
||||||
$template->set_template_dir(PHPWG_ROOT_PATH.'admin/themes/default/template');
|
if (!preg_match($conf['sync_chars_regex'], $dir))
|
||||||
|
{
|
||||||
|
bratonien_tools_nc_productive_error($errors, $fulldir, 'PWG-UPDATE-1');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$insert = array(
|
||||||
|
'id' => $next_id++,
|
||||||
|
'dir' => $dir,
|
||||||
|
'name' => str_replace('_', ' ', $dir),
|
||||||
|
'site_id' => $site_id,
|
||||||
|
'commentable' => boolean_to_string($conf['newcat_default_commentable']),
|
||||||
|
'status' => 'private',
|
||||||
|
'visible' => boolean_to_string($conf['newcat_default_visible']),
|
||||||
|
);
|
||||||
|
|
||||||
|
$parent_path = dirname($fulldir);
|
||||||
|
if (isset($db_fulldirs[$parent_path]))
|
||||||
|
{
|
||||||
|
$parent = $db_fulldirs[$parent_path];
|
||||||
|
$insert['id_uppercat'] = $parent;
|
||||||
|
$insert['uppercats'] = $db_categories[$parent]['uppercats'].','.$insert['id'];
|
||||||
|
$insert['rank'] = $next_rank[$parent]++;
|
||||||
|
$insert['global_rank'] = $db_categories[$parent]['global_rank'].'.'.$insert['rank'];
|
||||||
|
if ((string)$db_categories[$parent]['visible'] === 'false')
|
||||||
|
{
|
||||||
|
$insert['visible'] = 'false';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$insert['uppercats'] = (string)$insert['id'];
|
||||||
|
$insert['rank'] = $next_rank['NULL']++;
|
||||||
|
$insert['global_rank'] = (string)$insert['rank'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$category_inserts[] = $insert;
|
||||||
|
$db_categories[$insert['id']] = array(
|
||||||
|
'id' => $insert['id'],
|
||||||
|
'id_uppercat' => $insert['id_uppercat'] ?? null,
|
||||||
|
'uppercats' => $insert['uppercats'],
|
||||||
|
'global_rank' => $insert['global_rank'],
|
||||||
|
'status' => 'private',
|
||||||
|
'visible' => $insert['visible'],
|
||||||
|
);
|
||||||
|
$db_fulldirs[$fulldir] = $insert['id'];
|
||||||
|
$next_rank[$insert['id']] = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
$conf['newcat_default_status'] = 'private';
|
if ($category_inserts)
|
||||||
$_SERVER['REQUEST_METHOD'] = 'POST';
|
{
|
||||||
$_GET = array(
|
mass_inserts(
|
||||||
'page' => 'site_update',
|
CATEGORIES_TABLE,
|
||||||
'site' => $site_id,
|
array('id','dir','name','site_id','id_uppercat','uppercats','commentable','visible','status','rank','global_rank'),
|
||||||
);
|
$category_inserts
|
||||||
$_POST = array(
|
);
|
||||||
'sync' => 'files',
|
$category_ids = array_map(function ($row) { return (int)$row['id']; }, $category_inserts);
|
||||||
'display_info' => 1,
|
pwg_activity('album', $category_ids, 'add', array('sync'=>true));
|
||||||
'privacy_level' => 0,
|
add_permission_on_category($category_ids, get_admins());
|
||||||
'sync_meta' => 1,
|
$counts['new_categories'] = count($category_ids);
|
||||||
'simulate' => 0,
|
}
|
||||||
'subcats-included' => 1,
|
|
||||||
'bratonien_connector' => 1,
|
|
||||||
'submit' => 1,
|
|
||||||
);
|
|
||||||
|
|
||||||
ob_start();
|
$to_delete_categories = array();
|
||||||
include(PHPWG_ROOT_PATH.'admin/site_update.php');
|
foreach (array_diff(array_keys($db_fulldirs), $fs_fulldirs) as $fulldir)
|
||||||
$output = (string)ob_get_clean();
|
{
|
||||||
|
$to_delete_categories[] = (int)$db_fulldirs[$fulldir];
|
||||||
|
unset($db_fulldirs[$fulldir]);
|
||||||
|
}
|
||||||
|
if ($to_delete_categories)
|
||||||
|
{
|
||||||
|
delete_categories($to_delete_categories);
|
||||||
|
$counts['del_categories'] = count($to_delete_categories);
|
||||||
|
}
|
||||||
|
|
||||||
|
$fs = $site_reader->get_elements($basedir);
|
||||||
|
$cat_ids = array_diff(array_keys($db_categories), $to_delete_categories);
|
||||||
|
$db_elements = array();
|
||||||
|
if ($cat_ids)
|
||||||
|
{
|
||||||
|
$query = 'SELECT id, path FROM '.IMAGES_TABLE.' WHERE storage_category_id IN ('.implode(',', array_map('intval', $cat_ids)).')';
|
||||||
|
$db_elements = simple_hash_from_query($query, 'id', 'path');
|
||||||
|
}
|
||||||
|
|
||||||
|
$next_element_id = pwg_db_nextval('id', IMAGES_TABLE);
|
||||||
|
$image_inserts = array();
|
||||||
|
$image_links = array();
|
||||||
|
$format_inserts = array();
|
||||||
|
$new_image_ids = array();
|
||||||
|
|
||||||
|
foreach (array_diff(array_keys($fs), $db_elements) as $path)
|
||||||
|
{
|
||||||
|
$dirname = dirname($path);
|
||||||
|
if (!isset($db_fulldirs[$dirname]))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$filename = basename($path);
|
||||||
|
if (!preg_match($conf['sync_chars_regex'], $filename))
|
||||||
|
{
|
||||||
|
bratonien_tools_nc_productive_error($errors, $path, 'PWG-UPDATE-1');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$id = $next_element_id++;
|
||||||
|
$image_inserts[] = array(
|
||||||
|
'id' => $id,
|
||||||
|
'file' => $filename,
|
||||||
|
'name' => get_name_from_file($filename),
|
||||||
|
'date_available' => $dbnow,
|
||||||
|
'path' => $path,
|
||||||
|
'representative_ext' => $fs[$path]['representative_ext'],
|
||||||
|
'storage_category_id' => $db_fulldirs[$dirname],
|
||||||
|
'added_by' => (int)$user['id'],
|
||||||
|
);
|
||||||
|
$image_links[] = array('image_id'=>$id, 'category_id'=>$db_fulldirs[$dirname]);
|
||||||
|
$new_image_ids[] = $id;
|
||||||
|
|
||||||
|
if (!empty($conf['enable_formats']) && !empty($fs[$path]['formats']))
|
||||||
|
{
|
||||||
|
foreach ($fs[$path]['formats'] as $ext => $filesize)
|
||||||
|
{
|
||||||
|
$format_inserts[] = array('image_id'=>$id, 'ext'=>$ext, 'filesize'=>$filesize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($image_inserts)
|
||||||
|
{
|
||||||
|
mass_inserts(IMAGES_TABLE, array_keys($image_inserts[0]), $image_inserts);
|
||||||
|
mass_inserts(IMAGE_CATEGORY_TABLE, array_keys($image_links[0]), $image_links);
|
||||||
|
pwg_activity('photo', $new_image_ids, 'add', array('sync'=>true));
|
||||||
|
$counts['new_elements'] = count($image_inserts);
|
||||||
|
}
|
||||||
|
if ($format_inserts)
|
||||||
|
{
|
||||||
|
mass_inserts(IMAGE_FORMAT_TABLE, array_keys($format_inserts[0]), $format_inserts);
|
||||||
|
$counts['new_formats'] += count($format_inserts);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($conf['enable_formats']) && $db_elements)
|
||||||
|
{
|
||||||
|
$db_elements_flip = array_flip($db_elements);
|
||||||
|
$existing_ids = array();
|
||||||
|
foreach (array_intersect_key($fs, $db_elements_flip) as $path => $unused)
|
||||||
|
{
|
||||||
|
$existing_ids[] = (int)$db_elements_flip[$path];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($existing_ids)
|
||||||
|
{
|
||||||
|
$db_formats = array();
|
||||||
|
$result = pwg_query('SELECT * FROM '.IMAGE_FORMAT_TABLE.' WHERE image_id IN ('.implode(',', $existing_ids).')');
|
||||||
|
while ($row = pwg_db_fetch_assoc($result))
|
||||||
|
{
|
||||||
|
$db_formats[$row['image_id']][$row['ext']] = $row['format_id'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$formats_to_delete = array();
|
||||||
|
$formats_to_insert = array();
|
||||||
|
foreach ($existing_ids as $image_id)
|
||||||
|
{
|
||||||
|
$path = $db_elements[$image_id];
|
||||||
|
$known = $db_formats[$image_id] ?? array();
|
||||||
|
$present = $fs[$path]['formats'] ?? array();
|
||||||
|
foreach (array_diff_key($known, $present) as $format_id)
|
||||||
|
{
|
||||||
|
$formats_to_delete[] = (int)$format_id;
|
||||||
|
}
|
||||||
|
foreach (array_diff_key($present, $known) as $ext => $filesize)
|
||||||
|
{
|
||||||
|
$formats_to_insert[] = array('image_id'=>$image_id, 'ext'=>$ext, 'filesize'=>$filesize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($formats_to_delete)
|
||||||
|
{
|
||||||
|
pwg_query('DELETE FROM '.IMAGE_FORMAT_TABLE.' WHERE format_id IN ('.implode(',', $formats_to_delete).')');
|
||||||
|
$counts['del_formats'] = count($formats_to_delete);
|
||||||
|
}
|
||||||
|
if ($formats_to_insert)
|
||||||
|
{
|
||||||
|
mass_inserts(IMAGE_FORMAT_TABLE, array_keys($formats_to_insert[0]), $formats_to_insert);
|
||||||
|
$counts['new_formats'] += count($formats_to_insert);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$to_delete_elements = array();
|
||||||
|
foreach (array_diff($db_elements, array_keys($fs)) as $path)
|
||||||
|
{
|
||||||
|
$id = array_search($path, $db_elements, true);
|
||||||
|
if ($id !== false)
|
||||||
|
{
|
||||||
|
$to_delete_elements[] = (int)$id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($to_delete_elements)
|
||||||
|
{
|
||||||
|
delete_elements($to_delete_elements);
|
||||||
|
$counts['del_elements'] = count($to_delete_elements);
|
||||||
|
}
|
||||||
|
|
||||||
|
update_category('all');
|
||||||
|
update_global_rank();
|
||||||
|
|
||||||
|
$files = get_filelist('', $site_id, true, false);
|
||||||
|
$updates = array();
|
||||||
|
foreach ($files as $id => $file)
|
||||||
|
{
|
||||||
|
$data = $site_reader->get_element_update_attributes($file['path']);
|
||||||
|
if (!is_array($data))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$data['id'] = $id;
|
||||||
|
$updates[] = $data;
|
||||||
|
}
|
||||||
|
if ($updates)
|
||||||
|
{
|
||||||
|
mass_updates(
|
||||||
|
IMAGES_TABLE,
|
||||||
|
array('primary'=>array('id'), 'update'=>$site_reader->get_update_attributes()),
|
||||||
|
$updates
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$counts['upd_elements'] = count($updates);
|
||||||
|
|
||||||
|
$metadata_files = get_filelist('', $site_id, true, true);
|
||||||
|
$counts['metadata_candidates'] = count($metadata_files);
|
||||||
|
$metadata_updates = array();
|
||||||
|
$tags_of = array();
|
||||||
|
|
||||||
|
foreach ($metadata_files as $id => $element_infos)
|
||||||
|
{
|
||||||
|
$data = $site_reader->get_element_metadata($element_infos);
|
||||||
|
if (!is_array($data))
|
||||||
|
{
|
||||||
|
bratonien_tools_nc_productive_error($errors, $element_infos['path'], 'PWG-ERROR-NO-FS');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$data['date_metadata_update'] = $dbnow;
|
||||||
|
$data['id'] = $id;
|
||||||
|
$metadata_updates[] = $data;
|
||||||
|
|
||||||
|
foreach (array('keywords','tags') as $key)
|
||||||
|
{
|
||||||
|
if (!isset($data[$key]))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$tags_of[$id] = $tags_of[$id] ?? array();
|
||||||
|
foreach (explode(',', $data[$key]) as $tag_name)
|
||||||
|
{
|
||||||
|
$tags_of[$id][] = tag_id_from_tag_name($tag_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($metadata_updates)
|
||||||
|
{
|
||||||
|
mass_updates(
|
||||||
|
IMAGES_TABLE,
|
||||||
|
array(
|
||||||
|
'primary'=>array('id'),
|
||||||
|
'update'=>array_unique(array_merge(
|
||||||
|
array_diff($site_reader->get_metadata_attributes(), array('keywords','tags')),
|
||||||
|
array('date_metadata_update')
|
||||||
|
)),
|
||||||
|
),
|
||||||
|
$metadata_updates,
|
||||||
|
MASS_UPDATES_SKIP_EMPTY
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if ($tags_of)
|
||||||
|
{
|
||||||
|
set_tags_of($tags_of);
|
||||||
|
}
|
||||||
|
$counts['metadata_updated'] = count($metadata_updates);
|
||||||
|
|
||||||
|
// Mirror Piwigo 16.4.0 Maintenance -> "Update albums informations".
|
||||||
|
// This repairs the derived album hierarchy and counters that the direct
|
||||||
|
// API sync otherwise bypasses when no admin maintenance page is invoked.
|
||||||
|
images_integrity();
|
||||||
|
categories_integrity();
|
||||||
|
update_uppercats();
|
||||||
|
update_category('all');
|
||||||
|
update_global_rank();
|
||||||
|
invalidate_user_cache(true);
|
||||||
|
|
||||||
|
// Mirror Piwigo 16.4.0 Maintenance -> "Update photos information".
|
||||||
|
// This finalizes physical paths, ratings and derived photo information.
|
||||||
|
images_integrity();
|
||||||
|
update_path();
|
||||||
|
include_once(PHPWG_ROOT_PATH.'include/functions_rate.inc.php');
|
||||||
|
update_rating_score();
|
||||||
|
invalidate_user_cache();
|
||||||
}
|
}
|
||||||
catch (Throwable $e)
|
catch (Throwable $e)
|
||||||
{
|
{
|
||||||
if (ob_get_level() > 0)
|
return new PwgError(500, 'Bratonien direct synchronization failed: '.$e->getMessage());
|
||||||
{
|
|
||||||
ob_end_clean();
|
|
||||||
}
|
|
||||||
return new PwgError(500, 'Piwigo core synchronization failed: '.$e->getMessage());
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
$_GET = $saved_get;
|
|
||||||
$_POST = $saved_post;
|
|
||||||
|
|
||||||
if ($saved_template_dirs !== null && is_object($template) && isset($template->smarty))
|
|
||||||
{
|
|
||||||
$template->smarty->setTemplateDir($saved_template_dirs);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($saved_default_status === null)
|
|
||||||
{
|
|
||||||
unset($conf['newcat_default_status']);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$conf['newcat_default_status'] = $saved_default_status;
|
|
||||||
}
|
|
||||||
if ($saved_request_method === null)
|
|
||||||
{
|
|
||||||
unset($_SERVER['REQUEST_METHOD']);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$_SERVER['REQUEST_METHOD'] = $saved_request_method;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
'mode' => 'productive',
|
'mode' => 'productive',
|
||||||
|
'engine' => 'bratonien-direct',
|
||||||
'approved_piwigo_version' => '16.4.0',
|
'approved_piwigo_version' => '16.4.0',
|
||||||
'piwigo_version' => $piwigo_version,
|
'piwigo_version' => $piwigo_version,
|
||||||
'site_id' => $site_id,
|
'site_id' => $site_id,
|
||||||
'counts' => is_array($counts) ? $counts : array(),
|
'site_url' => $site_url,
|
||||||
|
'counts' => $counts,
|
||||||
|
'errors' => $errors,
|
||||||
|
'error_count' => count($errors),
|
||||||
'database_writes' => true,
|
'database_writes' => true,
|
||||||
'core_output_bytes' => strlen($output),
|
|
||||||
'username' => isset($user['username']) ? (string)$user['username'] : '',
|
'username' => isset($user['username']) ? (string)$user['username'] : '',
|
||||||
'status' => isset($user['status']) ? (string)$user['status'] : '',
|
'status' => isset($user['status']) ? (string)$user['status'] : '',
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
/*
|
/*
|
||||||
Plugin Name: Bratonien Tools
|
Plugin Name: Bratonien Tools
|
||||||
Version: 0.9.7.25
|
Version: 0.9.7.28
|
||||||
Description: Erweiterbare Administrationswerkzeuge fuer die Bratonien-Piwigo-Installation.
|
Description: Erweiterbare Administrationswerkzeuge fuer die Bratonien-Piwigo-Installation.
|
||||||
Plugin URI: https://github.com/Terranom674/Piwigo_Bratonien_Tools
|
Plugin URI: https://github.com/Terranom674/Piwigo_Bratonien_Tools
|
||||||
Author: Bratonien
|
Author: Bratonien
|
||||||
@@ -22,6 +22,7 @@ require_once(BRATONIEN_TOOLS_PATH . 'include/picture_navigation.inc.php');
|
|||||||
require_once(BRATONIEN_TOOLS_PATH . 'include/batch_titles.inc.php');
|
require_once(BRATONIEN_TOOLS_PATH . 'include/batch_titles.inc.php');
|
||||||
require_once(BRATONIEN_TOOLS_PATH . 'include/album_shares.inc.php');
|
require_once(BRATONIEN_TOOLS_PATH . 'include/album_shares.inc.php');
|
||||||
require_once(BRATONIEN_TOOLS_PATH . 'include/nc_connector_ws.inc.php');
|
require_once(BRATONIEN_TOOLS_PATH . 'include/nc_connector_ws.inc.php');
|
||||||
|
require_once(BRATONIEN_TOOLS_PATH . 'include/nc_orphan_ws.inc.php');
|
||||||
require_once(BRATONIEN_TOOLS_PATH . 'include/nc_productive_ws.inc.php');
|
require_once(BRATONIEN_TOOLS_PATH . 'include/nc_productive_ws.inc.php');
|
||||||
require_once(BRATONIEN_TOOLS_PATH . 'include/nc_connector_scheduler.inc.php');
|
require_once(BRATONIEN_TOOLS_PATH . 'include/nc_connector_scheduler.inc.php');
|
||||||
|
|
||||||
@@ -37,6 +38,7 @@ add_event_handler('init', 'bratonien_tools_preserve_private_album_access', EVENT
|
|||||||
add_event_handler('init', 'bratonien_tools_album_shares_init');
|
add_event_handler('init', 'bratonien_tools_album_shares_init');
|
||||||
add_event_handler('delete_categories', 'bratonien_tools_album_shares_on_delete_categories');
|
add_event_handler('delete_categories', 'bratonien_tools_album_shares_on_delete_categories');
|
||||||
add_event_handler('ws_add_methods', 'bratonien_tools_register_ws_methods');
|
add_event_handler('ws_add_methods', 'bratonien_tools_register_ws_methods');
|
||||||
|
add_event_handler('ws_add_methods', 'bratonien_tools_register_nc_orphan_ws_methods');
|
||||||
add_event_handler('ws_add_methods', 'bratonien_tools_register_nc_productive_ws_methods');
|
add_event_handler('ws_add_methods', 'bratonien_tools_register_nc_productive_ws_methods');
|
||||||
|
|
||||||
function bratonien_tools_prepare_connector_private_import()
|
function bratonien_tools_prepare_connector_private_import()
|
||||||
|
|||||||
201
runtime/lib/piwigo-sync.php
Executable file → Normal file
201
runtime/lib/piwigo-sync.php
Executable file → Normal file
@@ -15,12 +15,27 @@ function http_error_detail($body)
|
|||||||
{
|
{
|
||||||
$body = trim((string)$body);
|
$body = trim((string)$body);
|
||||||
if ($body === '') return '';
|
if ($body === '') return '';
|
||||||
|
|
||||||
$decoded = json_decode($body, true);
|
$decoded = json_decode($body, true);
|
||||||
if (is_array($decoded))
|
if (is_array($decoded))
|
||||||
{
|
{
|
||||||
$detail = (string)($decoded['message'] ?? $decoded['err'] ?? '');
|
$detail = (string)($decoded['message'] ?? $decoded['err'] ?? '');
|
||||||
if ($detail !== '') return $detail;
|
if ($detail !== '') return $detail;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (function_exists('simplexml_load_string'))
|
||||||
|
{
|
||||||
|
$previous = libxml_use_internal_errors(true);
|
||||||
|
$xml = simplexml_load_string($body);
|
||||||
|
libxml_clear_errors();
|
||||||
|
libxml_use_internal_errors($previous);
|
||||||
|
if ($xml !== false)
|
||||||
|
{
|
||||||
|
$detail = trim((string)($xml->message ?? $xml->err ?? ''));
|
||||||
|
if ($detail !== '') return $detail;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$plain = trim(preg_replace('/\s+/', ' ', strip_tags($body)));
|
$plain = trim(preg_replace('/\s+/', ' ', strip_tags($body)));
|
||||||
return $plain === '' ? '' : mb_substr($plain, 0, 500);
|
return $plain === '' ? '' : mb_substr($plain, 0, 500);
|
||||||
}
|
}
|
||||||
@@ -59,15 +74,55 @@ function http_request($url, array $fields, array $headers = array(), $cookie_fil
|
|||||||
return (string)$body;
|
return (string)$body;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function xml_value_sync(SimpleXMLElement $node)
|
||||||
|
{
|
||||||
|
$children = $node->children();
|
||||||
|
if (count($children) === 0) return (string)$node;
|
||||||
|
$result = array();
|
||||||
|
foreach ($children as $name => $child)
|
||||||
|
{
|
||||||
|
$value = xml_value_sync($child);
|
||||||
|
if (array_key_exists($name, $result))
|
||||||
|
{
|
||||||
|
if (!is_array($result[$name]) || !array_is_list($result[$name])) $result[$name] = array($result[$name]);
|
||||||
|
$result[$name][] = $value;
|
||||||
|
}
|
||||||
|
else $result[$name] = $value;
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
function decode_ws($body)
|
function decode_ws($body)
|
||||||
{
|
{
|
||||||
$decoded = json_decode((string)$body, true);
|
$decoded = json_decode((string)$body, true);
|
||||||
if (!is_array($decoded)) fail_sync('Piwigo lieferte keine gueltige JSON-Webservice-Antwort.');
|
if (!is_array($decoded))
|
||||||
|
{
|
||||||
|
if (!function_exists('simplexml_load_string')) fail_sync('Piwigo lieferte XML, aber SimpleXML ist nicht verfuegbar.');
|
||||||
|
$previous = libxml_use_internal_errors(true);
|
||||||
|
$xml = simplexml_load_string((string)$body);
|
||||||
|
libxml_clear_errors();
|
||||||
|
libxml_use_internal_errors($previous);
|
||||||
|
if ($xml === false || $xml->getName() !== 'rsp') fail_sync('Piwigo lieferte weder gueltiges JSON noch eine gueltige XML-Webservice-Antwort.');
|
||||||
|
$decoded = array('stat'=>(string)$xml['stat']);
|
||||||
|
foreach ($xml->children() as $name => $child)
|
||||||
|
{
|
||||||
|
$value = xml_value_sync($child);
|
||||||
|
if (array_key_exists($name, $decoded))
|
||||||
|
{
|
||||||
|
if (!is_array($decoded[$name]) || !array_is_list($decoded[$name])) $decoded[$name] = array($decoded[$name]);
|
||||||
|
$decoded[$name][] = $value;
|
||||||
|
}
|
||||||
|
else $decoded[$name] = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (($decoded['stat'] ?? '') !== 'ok')
|
if (($decoded['stat'] ?? '') !== 'ok')
|
||||||
{
|
{
|
||||||
fail_sync((string)($decoded['message'] ?? $decoded['err'] ?? 'Piwigo-Aufruf wurde abgelehnt.'));
|
$message = (string)($decoded['message'] ?? $decoded['err'] ?? 'Piwigo-Aufruf wurde abgelehnt.');
|
||||||
|
fail_sync($message);
|
||||||
}
|
}
|
||||||
return $decoded['result'] ?? array();
|
if (array_key_exists('result', $decoded)) return $decoded['result'];
|
||||||
|
unset($decoded['stat']);
|
||||||
|
return $decoded;
|
||||||
}
|
}
|
||||||
|
|
||||||
function decrypt_blob($blob, $hex_key)
|
function decrypt_blob($blob, $hex_key)
|
||||||
@@ -79,28 +134,34 @@ function decrypt_blob($blob, $hex_key)
|
|||||||
$iv = base64_decode((string)($payload['iv'] ?? ''), true);
|
$iv = base64_decode((string)($payload['iv'] ?? ''), true);
|
||||||
$tag = base64_decode((string)($payload['tag'] ?? ''), true);
|
$tag = base64_decode((string)($payload['tag'] ?? ''), true);
|
||||||
$cipher = base64_decode((string)($payload['data'] ?? ''), true);
|
$cipher = base64_decode((string)($payload['data'] ?? ''), true);
|
||||||
if (!is_string($iv) || !is_string($tag) || !is_string($cipher)) fail_sync('Gespeicherte Zugangsdaten sind unvollstaendig.');
|
|
||||||
$plain = openssl_decrypt($cipher, 'aes-256-gcm', hex2bin($hex_key), OPENSSL_RAW_DATA, $iv, $tag);
|
$plain = openssl_decrypt($cipher, 'aes-256-gcm', hex2bin($hex_key), OPENSSL_RAW_DATA, $iv, $tag);
|
||||||
if ($plain === false) fail_sync('Gespeicherte Zugangsdaten konnten nicht entschluesselt werden.');
|
if ($plain === false) fail_sync('Gespeicherte Zugangsdaten konnten nicht entschluesselt werden.');
|
||||||
$decoded = json_decode((string)$plain, true);
|
return (string)$plain;
|
||||||
return is_array($decoded) ? $decoded : array();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function ensure_webdav_site(mysqli $db, $prefixeTable, $piwigo_root, array $connection_config, $connection_id)
|
function ensure_webdav_site(mysqli $db, $prefixeTable, $piwigo_root, array $connection_config, $connection_id)
|
||||||
{
|
{
|
||||||
|
if ((string)($connection_config['source_mode'] ?? '') !== 'webdav-placeholder') return 1;
|
||||||
|
|
||||||
$gallery_root = rtrim((string)($connection_config['parallel_gallery_root'] ?? ''), '/');
|
$gallery_root = rtrim((string)($connection_config['parallel_gallery_root'] ?? ''), '/');
|
||||||
if ($gallery_root === '') fail_sync('WebDAV-Galeriewurzel fehlt in der Verbindungskonfiguration.');
|
if ($gallery_root === '') fail_sync('WebDAV-Galeriewurzel fehlt in der Verbindungskonfiguration.');
|
||||||
|
|
||||||
$piwigo_root = rtrim((string)$piwigo_root, '/');
|
$piwigo_root = rtrim((string)$piwigo_root, '/');
|
||||||
if (strpos($gallery_root, $piwigo_root.'/') !== 0) fail_sync('WebDAV-Galeriewurzel liegt ausserhalb der Piwigo-Installation.');
|
if (strpos($gallery_root, $piwigo_root.'/') !== 0)
|
||||||
|
{
|
||||||
|
fail_sync('WebDAV-Galeriewurzel liegt ausserhalb der Piwigo-Installation.');
|
||||||
|
}
|
||||||
if (!is_dir($gallery_root)) fail_sync('WebDAV-Galeriewurzel existiert nicht: '.$gallery_root);
|
if (!is_dir($gallery_root)) fail_sync('WebDAV-Galeriewurzel existiert nicht: '.$gallery_root);
|
||||||
|
|
||||||
$relative = ltrim(substr($gallery_root, strlen($piwigo_root)), '/');
|
$relative = ltrim(substr($gallery_root, strlen($piwigo_root)), '/');
|
||||||
|
if ($relative === '') fail_sync('WebDAV-Galeriewurzel darf nicht dem Piwigo-Hauptverzeichnis entsprechen.');
|
||||||
$site_url = './'.rtrim($relative, '/').'/';
|
$site_url = './'.rtrim($relative, '/').'/';
|
||||||
$escaped = $db->real_escape_string($site_url);
|
$escaped = $db->real_escape_string($site_url);
|
||||||
$result = $db->query("SELECT id FROM `{$prefixeTable}sites` WHERE galleries_url='{$escaped}' LIMIT 1");
|
$result = $db->query("SELECT id FROM `{$prefixeTable}sites` WHERE galleries_url='{$escaped}' LIMIT 1");
|
||||||
if (!$result) fail_sync('Piwigo-Site konnte nicht gelesen werden: '.$db->error);
|
if (!$result) fail_sync('Piwigo-Site konnte nicht gelesen werden: '.$db->error);
|
||||||
if ($result->num_rows) return (int)$result->fetch_assoc()['id'];
|
if ($result->num_rows)
|
||||||
|
{
|
||||||
|
return (int)$result->fetch_assoc()['id'];
|
||||||
|
}
|
||||||
|
|
||||||
if (!$db->query("INSERT INTO `{$prefixeTable}sites` (galleries_url) VALUES ('{$escaped}')"))
|
if (!$db->query("INSERT INTO `{$prefixeTable}sites` (galleries_url) VALUES ('{$escaped}')"))
|
||||||
{
|
{
|
||||||
@@ -125,6 +186,7 @@ try
|
|||||||
$conf = array();
|
$conf = array();
|
||||||
$prefixeTable = 'piwigo_';
|
$prefixeTable = 'piwigo_';
|
||||||
require $db_config;
|
require $db_config;
|
||||||
|
foreach (array('db_host','db_user','db_password','db_base') as $key) if (!isset($conf[$key])) fail_sync('Piwigo-Datenbankkonfiguration ist unvollstaendig: '.$key);
|
||||||
|
|
||||||
$db = new mysqli($conf['db_host'], $conf['db_user'], $conf['db_password'], $conf['db_base']);
|
$db = new mysqli($conf['db_host'], $conf['db_user'], $conf['db_password'], $conf['db_base']);
|
||||||
if ($db->connect_errno) fail_sync('Piwigo-Datenbank ist nicht erreichbar: '.$db->connect_error);
|
if ($db->connect_errno) fail_sync('Piwigo-Datenbank ist nicht erreichbar: '.$db->connect_error);
|
||||||
@@ -140,43 +202,80 @@ try
|
|||||||
$connection_row = $connection_result->fetch_assoc();
|
$connection_row = $connection_result->fetch_assoc();
|
||||||
$connection_config = json_decode((string)$connection_row['config_json'], true);
|
$connection_config = json_decode((string)$connection_row['config_json'], true);
|
||||||
if (!is_array($connection_config)) $connection_config = array();
|
if (!is_array($connection_config)) $connection_config = array();
|
||||||
$credentials = decrypt_blob((string)$connection_row['secret_blob'], $hex_key);
|
$connection_plain = decrypt_blob((string)$connection_row['secret_blob'], $hex_key);
|
||||||
|
$connection_credentials = json_decode($connection_plain, true);
|
||||||
|
if (!is_array($connection_credentials)) $connection_credentials = array();
|
||||||
|
|
||||||
$site_id = ensure_webdav_site($db, $prefixeTable, $piwigo_root, $connection_config, $connection_id);
|
$site_id = ensure_webdav_site($db, $prefixeTable, $piwigo_root, $connection_config, $connection_id);
|
||||||
|
|
||||||
$api_enabled = !empty($connection_config['api_enabled']);
|
$api = array('key_id'=>'', 'key_secret'=>'');
|
||||||
$api_key_id = $api_enabled ? trim((string)($credentials['api_key_id'] ?? '')) : '';
|
$connection_scoped = (string)($connection_config['piwigo_auth'] ?? '') === 'connection-scoped' || array_key_exists('api_enabled', $connection_config);
|
||||||
$api_key_secret = $api_enabled ? trim((string)($credentials['api_key_secret'] ?? '')) : '';
|
if ($connection_scoped)
|
||||||
|
|
||||||
if ($api_key_id !== '' && $api_key_secret !== '')
|
|
||||||
{
|
{
|
||||||
try
|
if (!empty($connection_config['api_enabled']))
|
||||||
{
|
{
|
||||||
$headers = array(
|
$api['key_id'] = trim((string)($connection_credentials['api_key_id'] ?? ''));
|
||||||
'X-PIWIGO-API: '.$api_key_id.':'.$api_key_secret,
|
$api['key_secret'] = trim((string)($connection_credentials['api_key_secret'] ?? ''));
|
||||||
'Accept: application/json',
|
|
||||||
'Content-Type: application/x-www-form-urlencoded',
|
|
||||||
);
|
|
||||||
decode_ws(http_request(
|
|
||||||
$base_url.'/ws.php?format=json',
|
|
||||||
array('method'=>'bratonien.nc.syncProductive', 'site_id'=>$site_id),
|
|
||||||
$headers
|
|
||||||
));
|
|
||||||
echo "Piwigo-Synchronisierung per API erfolgreich (Piwigo-Core, Site $site_id)\n";
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
catch (Throwable $e)
|
|
||||||
{
|
|
||||||
fwrite(STDERR, 'Piwigo-API nicht nutzbar: '.$e->getMessage()."\n");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fwrite(STDERR, "Piwigo-API nicht nutzbar: Fuer diese Verbindung ist keine API konfiguriert.\n");
|
$api_result = $db->query("SELECT value FROM `{$prefixeTable}config` WHERE param='bratonien_nc_piwigo_api' LIMIT 1");
|
||||||
|
if ($api_result && $api_result->num_rows)
|
||||||
|
{
|
||||||
|
$api_blob = (string)$api_result->fetch_assoc()['value'];
|
||||||
|
if ($api_blob !== '')
|
||||||
|
{
|
||||||
|
$api_plain = decrypt_blob($api_blob, $hex_key);
|
||||||
|
$api_decoded = json_decode($api_plain, true);
|
||||||
|
if (is_array($api_decoded))
|
||||||
|
{
|
||||||
|
$api['key_id'] = (string)($api_decoded['key_id'] ?? '');
|
||||||
|
$api['key_secret'] = (string)($api_decoded['key_secret'] ?? '');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$fallback_user = (string)($connection_credentials['piwigo_user'] ?? '');
|
||||||
|
$fallback_password = (string)($connection_credentials['piwigo_password'] ?? '');
|
||||||
|
|
||||||
|
$api_error = null;
|
||||||
|
if ($api['key_id'] !== '' && $api['key_secret'] !== '')
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
$headers = array(
|
||||||
|
'X-PIWIGO-API: '.$api['key_id'].':'.$api['key_secret'],
|
||||||
|
'Accept: application/json, text/xml;q=0.9',
|
||||||
|
'Content-Type: application/x-www-form-urlencoded',
|
||||||
|
);
|
||||||
|
decode_ws(http_request($base_url.'/ws.php?format=json', array('method'=>'bratonien.nc.syncProductive', 'site_id'=>$site_id), $headers));
|
||||||
|
$orphan = decode_ws(http_request($base_url.'/ws.php?format=json', array('method'=>'bratonien.nc.syncOrphans', 'site_id'=>$site_id, 'simulate'=>0), $headers));
|
||||||
|
// Entfernt alte technische bratonien-webdav-N Wrapper aus Site 1,
|
||||||
|
// nachdem deren generierte Verzeichnisse beim Reconcile entfernt wurden.
|
||||||
|
if ($site_id !== 1)
|
||||||
|
{
|
||||||
|
decode_ws(http_request($base_url.'/ws.php?format=json', array('method'=>'bratonien.nc.syncOrphans', 'site_id'=>1, 'simulate'=>0), $headers));
|
||||||
|
}
|
||||||
|
$added = (int)($orphan['added_orphans'] ?? 0);
|
||||||
|
$deleted = (int)($orphan['deleted_orphans'] ?? 0);
|
||||||
|
echo "Piwigo-Synchronisierung per API erfolgreich (Site $site_id)\n";
|
||||||
|
echo "Piwigo-Orphans synchronisiert: +$added / -$deleted\n";
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
catch (Throwable $e)
|
||||||
|
{
|
||||||
|
$api_error = $e->getMessage();
|
||||||
|
fwrite(STDERR, "Piwigo-API nicht nutzbar: ".$api_error."\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$api_error = $connection_scoped ? 'Fuer diese Verbindung ist keine API konfiguriert.' : 'Keine gespeicherten API-Zugangsdaten.';
|
||||||
|
fwrite(STDERR, "Piwigo-API nicht nutzbar: ".$api_error."\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
$fallback_user = trim((string)($credentials['piwigo_user'] ?? ''));
|
|
||||||
$fallback_password = (string)($credentials['piwigo_password'] ?? '');
|
|
||||||
if ($fallback_user === '' || $fallback_password === '') fail_sync('Kein gespeicherter Benutzername/Passwort-Fallback fuer diese Verbindung vorhanden.');
|
if ($fallback_user === '' || $fallback_password === '') fail_sync('Kein gespeicherter Benutzername/Passwort-Fallback fuer diese Verbindung vorhanden.');
|
||||||
|
|
||||||
$cookie_file = tempnam(sys_get_temp_dir(), 'br-nc-sync-');
|
$cookie_file = tempnam(sys_get_temp_dir(), 'br-nc-sync-');
|
||||||
@@ -184,30 +283,22 @@ try
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
decode_ws(http_request(
|
decode_ws(http_request($base_url.'/ws.php?format=json', array('method'=>'pwg.session.login', 'username'=>$fallback_user, 'password'=>$fallback_password), array(), $cookie_file));
|
||||||
$base_url.'/ws.php?format=json',
|
|
||||||
array('method'=>'pwg.session.login', 'username'=>$fallback_user, 'password'=>$fallback_password),
|
|
||||||
array(),
|
|
||||||
$cookie_file
|
|
||||||
));
|
|
||||||
|
|
||||||
http_request(
|
http_request(
|
||||||
$base_url.'/admin.php?page=site_update&site='.$site_id,
|
$base_url.'/admin.php?page=site_update&site='.$site_id,
|
||||||
array(
|
array('sync'=>'files','display_info'=>1,'privacy_level'=>0,'sync_meta'=>1,'simulate'=>0,'subcats-included'=>1,'bratonien_connector'=>1,'submit'=>1),
|
||||||
'sync'=>'files',
|
|
||||||
'display_info'=>1,
|
|
||||||
'privacy_level'=>0,
|
|
||||||
'sync_meta'=>1,
|
|
||||||
'simulate'=>0,
|
|
||||||
'subcats-included'=>1,
|
|
||||||
'bratonien_connector'=>1,
|
|
||||||
'submit'=>1,
|
|
||||||
),
|
|
||||||
array(),
|
array(),
|
||||||
$cookie_file
|
$cookie_file
|
||||||
);
|
);
|
||||||
|
$orphan = decode_ws(http_request($base_url.'/ws.php?format=json', array('method'=>'bratonien.nc.syncOrphans', 'site_id'=>$site_id, 'simulate'=>0), array(), $cookie_file));
|
||||||
echo "Piwigo-Datenbanksynchronisierung per Benutzername/Passwort-Fallback erfolgreich (Piwigo-Core, Site $site_id)\n";
|
if ($site_id !== 1)
|
||||||
|
{
|
||||||
|
decode_ws(http_request($base_url.'/ws.php?format=json', array('method'=>'bratonien.nc.syncOrphans', 'site_id'=>1, 'simulate'=>0), array(), $cookie_file));
|
||||||
|
}
|
||||||
|
$added = (int)($orphan['added_orphans'] ?? 0);
|
||||||
|
$deleted = (int)($orphan['deleted_orphans'] ?? 0);
|
||||||
|
echo "Piwigo-Datenbanksynchronisierung per Benutzername/Passwort-Fallback erfolgreich (Site $site_id)\n";
|
||||||
|
echo "Piwigo-Orphans synchronisiert: +$added / -$deleted\n";
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -216,6 +307,6 @@ try
|
|||||||
}
|
}
|
||||||
catch (Throwable $e)
|
catch (Throwable $e)
|
||||||
{
|
{
|
||||||
fwrite(STDERR, 'Piwigo-Synchronisierung fehlgeschlagen: '.$e->getMessage()."\n");
|
fwrite(STDERR, $e->getMessage()."\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ set -Eeuo pipefail
|
|||||||
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
|
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
PIWIGO_ROOT_DEFAULT="${BRATONIEN_NC_PIWIGO_ROOT:-$(cd -- "$SCRIPT_DIR/../../.." && pwd)}"
|
PIWIGO_ROOT_DEFAULT="${BRATONIEN_NC_PIWIGO_ROOT:-$(cd -- "$SCRIPT_DIR/../../.." && pwd)}"
|
||||||
CONFIG_DIR="${BRATONIEN_NC_CONFIG_DIR:-/etc/bratonien-tools/nc-connector}"
|
CONFIG_DIR="${BRATONIEN_NC_CONFIG_DIR:-/etc/bratonien-tools/nc-connector}"
|
||||||
NATIVE_MODE="${BRATONIEN_NC_NATIVE:-0}"
|
|
||||||
TARGET_CONNECTION_ID="${BRATONIEN_NC_CONNECTION_ID:-0}"
|
TARGET_CONNECTION_ID="${BRATONIEN_NC_CONNECTION_ID:-0}"
|
||||||
GLOBAL_LOCK_DIR="${PIWIGO_ROOT_DEFAULT%/}/_data/bratonien-tools/nc-connector-scheduler"
|
GLOBAL_LOCK_DIR="${PIWIGO_ROOT_DEFAULT%/}/_data/bratonien-tools/nc-connector-scheduler"
|
||||||
GLOBAL_LOCK_FILE="$GLOBAL_LOCK_DIR/worker.lock"
|
GLOBAL_LOCK_FILE="$GLOBAL_LOCK_DIR/worker.lock"
|
||||||
@@ -55,25 +54,17 @@ write_route_status() {
|
|||||||
' "$ROUTE_STATUS_FILE" "$route" "$label" "$detail" "$success"
|
' "$ROUTE_STATUS_FILE" "$route" "$label" "$detail" "$success"
|
||||||
}
|
}
|
||||||
|
|
||||||
if [[ "$NATIVE_MODE" != "1" ]]; then
|
# Legacy ist beendet. Der Runner bereitet ausschließlich die aktuelle WebDAV-Runtime vor.
|
||||||
php "$SCRIPT_DIR/reconcile.php"
|
|
||||||
fi
|
|
||||||
php "$SCRIPT_DIR/reconcile-webdav.php"
|
php "$SCRIPT_DIR/reconcile-webdav.php"
|
||||||
php "$SCRIPT_DIR/cleanup-webdav-piwigo.php"
|
|
||||||
if [[ "$NATIVE_MODE" != "1" ]]; then
|
|
||||||
php "$SCRIPT_DIR/cleanup-stale.php"
|
|
||||||
fi
|
|
||||||
|
|
||||||
configs=("$CONFIG_DIR"/connection-*.conf)
|
|
||||||
webdav_configs=("$CONFIG_DIR"/webdav-connection-*.conf)
|
webdav_configs=("$CONFIG_DIR"/webdav-connection-*.conf)
|
||||||
|
if [[ ${#webdav_configs[@]} -eq 0 ]]; then
|
||||||
if [[ ${#configs[@]} -eq 0 && ${#webdav_configs[@]} -eq 0 ]]; then
|
echo "Keine WebDAV-NC-Connector-Verbindungen konfiguriert."
|
||||||
echo "Keine NC-Connector-Verbindungen konfiguriert."
|
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
route_piwigo_root=""
|
route_piwigo_root=""
|
||||||
for candidate in "${webdav_configs[@]}" "${configs[@]}"; do
|
for candidate in "${webdav_configs[@]}"; do
|
||||||
[[ -f "$candidate" ]] || continue
|
[[ -f "$candidate" ]] || continue
|
||||||
candidate_id="$(read_config_value CONNECTION_ID "$candidate")"
|
candidate_id="$(read_config_value CONNECTION_ID "$candidate")"
|
||||||
if [[ "$TARGET_CONNECTION_ID" -gt 0 && "$candidate_id" != "$TARGET_CONNECTION_ID" ]]; then
|
if [[ "$TARGET_CONNECTION_ID" -gt 0 && "$candidate_id" != "$TARGET_CONNECTION_ID" ]]; then
|
||||||
@@ -86,10 +77,8 @@ done
|
|||||||
ROUTE_STATUS_FILE="${route_piwigo_root%/}/_data/bratonien-tools/nc-connector-status/route-status.json"
|
ROUTE_STATUS_FILE="${route_piwigo_root%/}/_data/bratonien-tools/nc-connector-status/route-status.json"
|
||||||
|
|
||||||
failure_count=0
|
failure_count=0
|
||||||
webdav_count=0
|
|
||||||
local_count=0
|
|
||||||
summary_parts=()
|
|
||||||
matched_count=0
|
matched_count=0
|
||||||
|
summary_parts=()
|
||||||
|
|
||||||
for config in "${webdav_configs[@]}"; do
|
for config in "${webdav_configs[@]}"; do
|
||||||
[[ -f "$config" ]] || continue
|
[[ -f "$config" ]] || continue
|
||||||
@@ -106,7 +95,6 @@ for config in "${webdav_configs[@]}"; do
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
matched_count=$((matched_count + 1))
|
matched_count=$((matched_count + 1))
|
||||||
webdav_count=$((webdav_count + 1))
|
|
||||||
echo "NC Connector WebDAV #$connection_id: $name"
|
echo "NC Connector WebDAV #$connection_id: $name"
|
||||||
output=""
|
output=""
|
||||||
if output="$(env PIWIGO_CONFIG="$config" bash "$SCRIPT_DIR/sync-webdav.sh" 2>&1)"; then
|
if output="$(env PIWIGO_CONFIG="$config" bash "$SCRIPT_DIR/sync-webdav.sh" 2>&1)"; then
|
||||||
@@ -120,75 +108,21 @@ for config in "${webdav_configs[@]}"; do
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
if [[ "$NATIVE_MODE" != "1" ]]; then
|
|
||||||
for config in "${configs[@]}"; do
|
|
||||||
[[ -f "$config" ]] || continue
|
|
||||||
name="$(basename "$config")"
|
|
||||||
connection_id="0"
|
|
||||||
if [[ "$name" =~ ^connection-([0-9]+)\.conf$ ]]; then
|
|
||||||
connection_id="${BASH_REMATCH[1]}"
|
|
||||||
fi
|
|
||||||
if [[ "$connection_id" -lt 1 ]]; then
|
|
||||||
echo "NC Connector: $name besitzt keine gueltige Verbindungs-ID." >&2
|
|
||||||
failure_count=$((failure_count + 1))
|
|
||||||
summary_parts+=("$name: ungueltige Verbindungs-ID")
|
|
||||||
continue
|
|
||||||
fi
|
|
||||||
if [[ "$TARGET_CONNECTION_ID" -gt 0 && "$connection_id" != "$TARGET_CONNECTION_ID" ]]; then
|
|
||||||
continue
|
|
||||||
fi
|
|
||||||
|
|
||||||
matched_count=$((matched_count + 1))
|
|
||||||
piwigo_root="$(read_config_value PIWIGO_ROOT "$config")"
|
|
||||||
[[ -n "$piwigo_root" ]] || piwigo_root="$PIWIGO_ROOT_DEFAULT"
|
|
||||||
tombstone_dir="${piwigo_root%/}/_data/bratonien-tools/nc-connector-status"
|
|
||||||
if [[ -f "$tombstone_dir/deleted-$connection_id" ]]; then
|
|
||||||
echo "NC Connector: Verbindung $connection_id wurde geloescht; Laufzeitdateien werden entfernt."
|
|
||||||
rm -f -- "$CONFIG_DIR/connection-$connection_id.conf" \
|
|
||||||
"$CONFIG_DIR/connection-$connection_id.db-password" \
|
|
||||||
"$CONFIG_DIR/connection-$connection_id.piwigo-password" \
|
|
||||||
"$CONFIG_DIR/connection-$connection_id.storages.tsv" \
|
|
||||||
"$CONFIG_DIR/connection-$connection_id.roots.tsv"
|
|
||||||
rm -f -- "$tombstone_dir/deleted-$connection_id"
|
|
||||||
continue
|
|
||||||
fi
|
|
||||||
|
|
||||||
local_count=$((local_count + 1))
|
|
||||||
echo "NC Connector Local #$connection_id: $name"
|
|
||||||
if env PIWIGO_CONFIG="$config" bash "$SCRIPT_DIR/sync.sh"; then
|
|
||||||
summary_parts+=("Local #$connection_id erfolgreich")
|
|
||||||
else
|
|
||||||
failure_count=$((failure_count + 1))
|
|
||||||
summary_parts+=("Local #$connection_id fehlgeschlagen")
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "$TARGET_CONNECTION_ID" -gt 0 && "$matched_count" -eq 0 ]]; then
|
if [[ "$TARGET_CONNECTION_ID" -gt 0 && "$matched_count" -eq 0 ]]; then
|
||||||
write_route_status "failed" "FEHLER - Verbindung #$TARGET_CONNECTION_ID" "Keine Laufzeitkonfiguration für Verbindung #$TARGET_CONNECTION_ID gefunden." "0"
|
write_route_status "failed" "FEHLER - Verbindung #$TARGET_CONNECTION_ID" "Keine WebDAV-Laufzeitkonfiguration für Verbindung #$TARGET_CONNECTION_ID gefunden." "0"
|
||||||
echo "NC Connector: keine Laufzeitkonfiguration für Verbindung #$TARGET_CONNECTION_ID gefunden." >&2
|
echo "NC Connector: keine WebDAV-Laufzeitkonfiguration für Verbindung #$TARGET_CONNECTION_ID gefunden." >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
summary_detail="$(IFS='; '; printf '%s' "${summary_parts[*]}")"
|
summary_detail="$(IFS='; '; printf '%s' "${summary_parts[*]}")"
|
||||||
[[ -n "$summary_detail" ]] || summary_detail="Keine Verbindung wurde ausgefuehrt."
|
[[ -n "$summary_detail" ]] || summary_detail="Keine WebDAV-Verbindung wurde ausgefuehrt."
|
||||||
|
|
||||||
if [[ "$failure_count" -eq 0 ]]; then
|
if [[ "$failure_count" -eq 0 ]]; then
|
||||||
if [[ "$webdav_count" -gt 0 && "$local_count" -gt 0 ]]; then
|
write_route_status "webdav" "WebDAV" "$summary_detail" "1"
|
||||||
route="mixed"
|
echo "NC Connector: WebDAV-Verbindung wurde erfolgreich verarbeitet."
|
||||||
label="WebDAV + Local"
|
|
||||||
elif [[ "$webdav_count" -gt 0 ]]; then
|
|
||||||
route="webdav"
|
|
||||||
label="WebDAV"
|
|
||||||
else
|
|
||||||
route="local"
|
|
||||||
label="Local"
|
|
||||||
fi
|
|
||||||
write_route_status "$route" "$label" "$summary_detail" "1"
|
|
||||||
echo "NC Connector: angeforderte Verbindung wurde erfolgreich verarbeitet."
|
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
write_route_status "failed" "FEHLER - angeforderte Verbindung" "$summary_detail" "0"
|
write_route_status "failed" "FEHLER - WebDAV-Verbindung" "$summary_detail" "0"
|
||||||
echo "NC Connector: die angeforderte Verbindung ist fehlgeschlagen." >&2
|
echo "NC Connector: die angeforderte WebDAV-Verbindung ist fehlgeschlagen." >&2
|
||||||
exit 1
|
exit 1
|
||||||
|
|||||||
@@ -59,6 +59,10 @@ for target in (status_file, public_file):
|
|||||||
PY
|
PY
|
||||||
}
|
}
|
||||||
|
|
||||||
|
compact_output() {
|
||||||
|
tail -n 20 | tr '\n' ' ' | sed 's/[[:space:]]\+/ /g; s/^[[:space:]]//; s/[[:space:]]$//'
|
||||||
|
}
|
||||||
|
|
||||||
failure() {
|
failure() {
|
||||||
local code="$1" command="$2" line="$3"
|
local code="$1" command="$2" line="$3"
|
||||||
trap - ERR
|
trap - ERR
|
||||||
@@ -97,7 +101,7 @@ fi
|
|||||||
if [[ "$PLACEHOLDER_EXIT" -ne 0 ]]; then
|
if [[ "$PLACEHOLDER_EXIT" -ne 0 ]]; then
|
||||||
DETAIL="Exit-Code: $PLACEHOLDER_EXIT"
|
DETAIL="Exit-Code: $PLACEHOLDER_EXIT"
|
||||||
if [[ -n "$PLACEHOLDER_OUTPUT" ]]; then
|
if [[ -n "$PLACEHOLDER_OUTPUT" ]]; then
|
||||||
DETAIL+="; Ausgabe: $(printf '%s\n' "$PLACEHOLDER_OUTPUT" | tail -n 20 | tr '\n' ' ' | sed 's/[[:space:]]\+/ /g; s/^[[:space:]]//; s/[[:space:]]$//')"
|
DETAIL+="; Ausgabe: $(printf '%s\n' "$PLACEHOLDER_OUTPUT" | compact_output)"
|
||||||
fi
|
fi
|
||||||
trap - ERR
|
trap - ERR
|
||||||
write_status error "WebDAV-Shadow-Tree fehlgeschlagen" "$DETAIL"
|
write_status error "WebDAV-Shadow-Tree fehlgeschlagen" "$DETAIL"
|
||||||
@@ -118,7 +122,7 @@ fi
|
|||||||
if [[ "$SHADOW_EXIT" -ne 0 ]]; then
|
if [[ "$SHADOW_EXIT" -ne 0 ]]; then
|
||||||
DETAIL="Exit-Code: $SHADOW_EXIT"
|
DETAIL="Exit-Code: $SHADOW_EXIT"
|
||||||
if [[ -n "$SHADOW_OUTPUT" ]]; then
|
if [[ -n "$SHADOW_OUTPUT" ]]; then
|
||||||
DETAIL+="; Ausgabe: $(printf '%s\n' "$SHADOW_OUTPUT" | tail -n 20 | tr '\n' ' ' | sed 's/[[:space:]]\+/ /g; s/^[[:space:]]//; s/[[:space:]]$//')"
|
DETAIL+="; Ausgabe: $(printf '%s\n' "$SHADOW_OUTPUT" | compact_output)"
|
||||||
fi
|
fi
|
||||||
trap - ERR
|
trap - ERR
|
||||||
write_status error "WebDAV-Shadow-Tree fehlgeschlagen" "$DETAIL"
|
write_status error "WebDAV-Shadow-Tree fehlgeschlagen" "$DETAIL"
|
||||||
@@ -126,6 +130,28 @@ if [[ "$SHADOW_EXIT" -ne 0 ]]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
trap - ERR
|
trap - ERR
|
||||||
|
PREVIEW_CACHE="$PIWIGO_ROOT/_data/bratonien-tools/nc-webdav-preview/connection-$CONNECTION_ID"
|
||||||
|
PREVIEW_OUTPUT=""
|
||||||
|
PREVIEW_EXIT=0
|
||||||
|
if PREVIEW_OUTPUT="$(php "$SCRIPT_DIR/lib/precache-webdav-previews.php" \
|
||||||
|
--mapping="$WEBDAV_MAPPING_FILE" \
|
||||||
|
--base-url="$WEBDAV_BASE_URL" \
|
||||||
|
--user="$WEBDAV_USER" \
|
||||||
|
--password-file="$WEBDAV_PASSWORD_FILE" \
|
||||||
|
--cache-dir="$PREVIEW_CACHE" 2>&1)"; then
|
||||||
|
PREVIEW_EXIT=0
|
||||||
|
else
|
||||||
|
PREVIEW_EXIT=$?
|
||||||
|
fi
|
||||||
|
[[ -z "$PREVIEW_OUTPUT" ]] || printf '%s\n' "$PREVIEW_OUTPUT"
|
||||||
|
if [[ "$PREVIEW_EXIT" -ne 0 ]]; then
|
||||||
|
DETAIL="Exit-Code: $PREVIEW_EXIT"
|
||||||
|
if [[ -n "$PREVIEW_OUTPUT" ]]; then
|
||||||
|
DETAIL+="; Ausgabe: $(printf '%s\n' "$PREVIEW_OUTPUT" | compact_output)"
|
||||||
|
fi
|
||||||
|
write_status error "WebDAV-Vorschaubilder konnten beim Einlesen nicht erzeugt werden" "$DETAIL"
|
||||||
|
exit "$PREVIEW_EXIT"
|
||||||
|
fi
|
||||||
|
|
||||||
if [[ "${PIWIGO_SYNC_ENABLED:-0}" == "1" ]]; then
|
if [[ "${PIWIGO_SYNC_ENABLED:-0}" == "1" ]]; then
|
||||||
PIWIGO_OUTPUT=""
|
PIWIGO_OUTPUT=""
|
||||||
@@ -143,7 +169,7 @@ if [[ "${PIWIGO_SYNC_ENABLED:-0}" == "1" ]]; then
|
|||||||
if [[ "$PIWIGO_EXIT" -ne 0 ]]; then
|
if [[ "$PIWIGO_EXIT" -ne 0 ]]; then
|
||||||
DETAIL="Exit-Code: $PIWIGO_EXIT"
|
DETAIL="Exit-Code: $PIWIGO_EXIT"
|
||||||
if [[ -n "$PIWIGO_OUTPUT" ]]; then
|
if [[ -n "$PIWIGO_OUTPUT" ]]; then
|
||||||
DETAIL+="; Ausgabe: $(printf '%s\n' "$PIWIGO_OUTPUT" | tail -n 12 | tr '\n' ' ' | sed 's/[[:space:]]\+/ /g; s/^[[:space:]]//; s/[[:space:]]$//')"
|
DETAIL+="; Ausgabe: $(printf '%s\n' "$PIWIGO_OUTPUT" | compact_output)"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if grep -qi 'Invalid username/password' <<<"$PIWIGO_OUTPUT"; then
|
if grep -qi 'Invalid username/password' <<<"$PIWIGO_OUTPUT"; then
|
||||||
@@ -170,9 +196,28 @@ if [[ "${PIWIGO_SYNC_ENABLED:-0}" == "1" ]]; then
|
|||||||
exit "$PIWIGO_EXIT"
|
exit "$PIWIGO_EXIT"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
DERIVATIVE_OUTPUT=""
|
||||||
|
DERIVATIVE_EXIT=0
|
||||||
|
if DERIVATIVE_OUTPUT="$(php "$SCRIPT_DIR/lib/build-webdav-derivatives.php" \
|
||||||
|
--piwigo-root="$PIWIGO_ROOT" \
|
||||||
|
--connection-id="$CONNECTION_ID" 2>&1)"; then
|
||||||
|
DERIVATIVE_EXIT=0
|
||||||
|
else
|
||||||
|
DERIVATIVE_EXIT=$?
|
||||||
|
fi
|
||||||
|
[[ -z "$DERIVATIVE_OUTPUT" ]] || printf '%s\n' "$DERIVATIVE_OUTPUT"
|
||||||
|
if [[ "$DERIVATIVE_EXIT" -ne 0 ]]; then
|
||||||
|
DETAIL="Exit-Code: $DERIVATIVE_EXIT"
|
||||||
|
if [[ -n "$DERIVATIVE_OUTPUT" ]]; then
|
||||||
|
DETAIL+="; Ausgabe: $(printf '%s\n' "$DERIVATIVE_OUTPUT" | compact_output)"
|
||||||
|
fi
|
||||||
|
write_status error "Piwigo-Derivate für WebDAV-Bilder konnten nicht erzeugt werden" "$DETAIL"
|
||||||
|
exit "$DERIVATIVE_EXIT"
|
||||||
|
fi
|
||||||
|
|
||||||
if grep -q 'Piwigo-Synchronisierung per API erfolgreich' <<<"$PIWIGO_OUTPUT"; then
|
if grep -q 'Piwigo-Synchronisierung per API erfolgreich' <<<"$PIWIGO_OUTPUT"; then
|
||||||
write_status ok \
|
write_status ok \
|
||||||
"WebDAV eingelesen und Piwigo synchronisiert" \
|
"WebDAV eingelesen, Piwigo synchronisiert und Derivate erzeugt" \
|
||||||
"" \
|
"" \
|
||||||
"api" \
|
"api" \
|
||||||
"ok" \
|
"ok" \
|
||||||
@@ -181,7 +226,7 @@ if [[ "${PIWIGO_SYNC_ENABLED:-0}" == "1" ]]; then
|
|||||||
"Fallback wurde nicht benötigt"
|
"Fallback wurde nicht benötigt"
|
||||||
elif grep -q 'Piwigo-Datenbanksynchronisierung per Benutzername/Passwort-Fallback erfolgreich' <<<"$PIWIGO_OUTPUT"; then
|
elif grep -q 'Piwigo-Datenbanksynchronisierung per Benutzername/Passwort-Fallback erfolgreich' <<<"$PIWIGO_OUTPUT"; then
|
||||||
write_status ok \
|
write_status ok \
|
||||||
"WebDAV eingelesen und Piwigo über Fallback synchronisiert" \
|
"WebDAV eingelesen, Piwigo über Fallback synchronisiert und Derivate erzeugt" \
|
||||||
"" \
|
"" \
|
||||||
"fallback" \
|
"fallback" \
|
||||||
"not_used" \
|
"not_used" \
|
||||||
@@ -189,8 +234,8 @@ if [[ "${PIWIGO_SYNC_ENABLED:-0}" == "1" ]]; then
|
|||||||
"ok" \
|
"ok" \
|
||||||
"Benutzername/Passwort-Fallback erfolgreich"
|
"Benutzername/Passwort-Fallback erfolgreich"
|
||||||
else
|
else
|
||||||
write_status ok "WebDAV eingelesen und Piwigo synchronisiert"
|
write_status ok "WebDAV eingelesen, Piwigo synchronisiert und Derivate erzeugt"
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
write_status ok "WebDAV eingelesen; Piwigo-Synchronisierung ist für diese Verbindung deaktiviert"
|
write_status ok "WebDAV eingelesen und Vorschaubilder erzeugt; Piwigo-Synchronisierung ist für diese Verbindung deaktiviert"
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user