mirror of
https://github.com/Terranom674/Piwigo_Bratonien_Tools.git
synced 2026-09-19 14:04:34 +00:00
Remove legacy runtime status checks
This commit is contained in:
@@ -6,10 +6,7 @@ if (!defined('PHPWG_ROOT_PATH'))
|
|||||||
|
|
||||||
function bratonien_tools_nc_connector_systemctl_value(array $args)
|
function bratonien_tools_nc_connector_systemctl_value(array $args)
|
||||||
{
|
{
|
||||||
if (!function_exists('proc_open'))
|
if (!function_exists('proc_open')) return '';
|
||||||
{
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
$command = array_merge(array('/usr/bin/systemctl'), $args);
|
$command = array_merge(array('/usr/bin/systemctl'), $args);
|
||||||
$spec = array(
|
$spec = array(
|
||||||
@@ -19,10 +16,7 @@ function bratonien_tools_nc_connector_systemctl_value(array $args)
|
|||||||
);
|
);
|
||||||
$environment = array_merge($_ENV, array('LC_ALL'=>'C','LANG'=>'C'));
|
$environment = array_merge($_ENV, array('LC_ALL'=>'C','LANG'=>'C'));
|
||||||
$process = @proc_open($command, $spec, $pipes, null, $environment);
|
$process = @proc_open($command, $spec, $pipes, null, $environment);
|
||||||
if (!is_resource($process))
|
if (!is_resource($process)) return '';
|
||||||
{
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
$stdout = stream_get_contents($pipes[1]);
|
$stdout = stream_get_contents($pipes[1]);
|
||||||
stream_get_contents($pipes[2]);
|
stream_get_contents($pipes[2]);
|
||||||
fclose($pipes[1]);
|
fclose($pipes[1]);
|
||||||
@@ -34,10 +28,7 @@ function bratonien_tools_nc_connector_systemctl_value(array $args)
|
|||||||
function bratonien_tools_nc_connector_parse_systemd_time($value)
|
function bratonien_tools_nc_connector_parse_systemd_time($value)
|
||||||
{
|
{
|
||||||
$value = trim((string)$value);
|
$value = trim((string)$value);
|
||||||
if ($value === '' || strtolower($value) === 'n/a')
|
if ($value === '' || strtolower($value) === 'n/a') return 0;
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
$parsed = strtotime($value);
|
$parsed = strtotime($value);
|
||||||
return $parsed === false ? 0 : (int)$parsed;
|
return $parsed === false ? 0 : (int)$parsed;
|
||||||
}
|
}
|
||||||
@@ -45,106 +36,52 @@ function bratonien_tools_nc_connector_parse_systemd_time($value)
|
|||||||
function bratonien_tools_nc_connector_monotonic_to_timestamp($value)
|
function bratonien_tools_nc_connector_monotonic_to_timestamp($value)
|
||||||
{
|
{
|
||||||
$value = trim((string)$value);
|
$value = trim((string)$value);
|
||||||
if ($value === '' || $value === '0' || strtolower($value) === 'n/a')
|
if ($value === '' || $value === '0' || strtolower($value) === 'n/a') return 0;
|
||||||
{
|
if (!preg_match('/^([0-9]+)(?:us)?$/', $value, $matches)) return 0;
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (preg_match('/^([0-9]+)(?:us)?$/', $value, $matches))
|
|
||||||
{
|
|
||||||
$next_boot_seconds = ((float)$matches[1]) / 1000000;
|
$next_boot_seconds = ((float)$matches[1]) / 1000000;
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
$uptime_raw = @file_get_contents('/proc/uptime');
|
$uptime_raw = @file_get_contents('/proc/uptime');
|
||||||
if (!is_string($uptime_raw) || !preg_match('/^([0-9]+(?:\.[0-9]+)?)/', trim($uptime_raw), $uptime_match))
|
if (!is_string($uptime_raw) || !preg_match('/^([0-9]+(?:\.[0-9]+)?)/', trim($uptime_raw), $uptime_match)) return 0;
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
$remaining = $next_boot_seconds - (float)$uptime_match[1];
|
$remaining = $next_boot_seconds - (float)$uptime_match[1];
|
||||||
if ($remaining < -1)
|
if ($remaining < -1) return 0;
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (int)round(time() + max(0, $remaining));
|
return (int)round(time() + max(0, $remaining));
|
||||||
}
|
}
|
||||||
|
|
||||||
function bratonien_tools_nc_connector_next_from_timer_list($timer)
|
function bratonien_tools_nc_connector_next_from_timer_list($timer)
|
||||||
{
|
{
|
||||||
$line = bratonien_tools_nc_connector_systemctl_value(array(
|
$line = bratonien_tools_nc_connector_systemctl_value(array('list-timers','--all','--no-pager','--no-legend',$timer));
|
||||||
'list-timers', '--all', '--no-pager', '--no-legend', $timer,
|
if ($line === '') return 0;
|
||||||
));
|
|
||||||
if ($line === '')
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
$first_line = trim((string)strtok($line, "\n"));
|
$first_line = trim((string)strtok($line, "\n"));
|
||||||
if (preg_match('/^(\S+\s+\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}\s+\S+)/', $first_line, $matches))
|
if (!preg_match('/^(\S+\s+\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}\s+\S+)/', $first_line, $matches)) return 0;
|
||||||
{
|
|
||||||
$parsed = strtotime($matches[1]);
|
$parsed = strtotime($matches[1]);
|
||||||
return $parsed === false ? 0 : (int)$parsed;
|
return $parsed === false ? 0 : (int)$parsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
function bratonien_tools_nc_connector_connection_last_status(array $connection)
|
function bratonien_tools_nc_connector_connection_last_status(array $connection)
|
||||||
{
|
{
|
||||||
$empty = array(
|
$empty = array(
|
||||||
'timestamp'=>0,
|
'timestamp'=>0,'label'=>'Nicht verfügbar','state'=>'','message'=>'','auth_mode'=>'',
|
||||||
'label'=>'Nicht verfügbar',
|
'api_state'=>'','api_message'=>'','fallback_state'=>'','fallback_message'=>'','error_detail'=>'',
|
||||||
'state'=>'',
|
|
||||||
'message'=>'',
|
|
||||||
'auth_mode'=>'',
|
|
||||||
'api_state'=>'',
|
|
||||||
'api_message'=>'',
|
|
||||||
'fallback_state'=>'',
|
|
||||||
'fallback_message'=>'',
|
|
||||||
'error_detail'=>'',
|
|
||||||
);
|
);
|
||||||
|
|
||||||
$connection_id = (int)($connection['id'] ?? 0);
|
$connection_id = (int)($connection['id'] ?? 0);
|
||||||
$candidates = array();
|
$candidates = array();
|
||||||
if ($connection_id > 0)
|
if ($connection_id > 0) $candidates[] = rtrim(PHPWG_ROOT_PATH, '/').'/_data/bratonien-tools/nc-connector-status/connection-'.$connection_id.'.json';
|
||||||
{
|
|
||||||
$candidates[] = rtrim(PHPWG_ROOT_PATH, '/').'/_data/bratonien-tools/nc-connector-status/connection-'.$connection_id.'.json';
|
|
||||||
}
|
|
||||||
|
|
||||||
$config = isset($connection['config']) && is_array($connection['config']) ? $connection['config'] : array();
|
$config = isset($connection['config']) && is_array($connection['config']) ? $connection['config'] : array();
|
||||||
$state_dir = rtrim((string)($config['state_dir'] ?? ''), '/');
|
$state_dir = rtrim((string)($config['state_dir'] ?? ''), '/');
|
||||||
if ($state_dir === '' && $connection_id > 0)
|
if ($state_dir === '' && $connection_id > 0) $state_dir = '/var/lib/bratonien-tools/nc-connector/connection-'.$connection_id;
|
||||||
{
|
if ($state_dir !== '') $candidates[] = $state_dir.'/connector-status.json';
|
||||||
$state_dir = '/var/lib/bratonien-tools/nc-connector/connection-'.$connection_id;
|
|
||||||
}
|
|
||||||
if ($state_dir !== '')
|
|
||||||
{
|
|
||||||
$candidates[] = $state_dir.'/connector-status.json';
|
|
||||||
}
|
|
||||||
|
|
||||||
$decoded = null;
|
$decoded = null;
|
||||||
foreach ($candidates as $candidate)
|
foreach ($candidates as $candidate)
|
||||||
{
|
{
|
||||||
if (!is_readable($candidate))
|
if (!is_readable($candidate)) continue;
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$value = json_decode((string)@file_get_contents($candidate), true);
|
$value = json_decode((string)@file_get_contents($candidate), true);
|
||||||
if (is_array($value))
|
if (is_array($value)) { $decoded = $value; break; }
|
||||||
{
|
|
||||||
$decoded = $value;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!is_array($decoded))
|
|
||||||
{
|
|
||||||
return $empty;
|
|
||||||
}
|
}
|
||||||
|
if (!is_array($decoded)) return $empty;
|
||||||
|
|
||||||
$timestamp = (int)($decoded['timestamp'] ?? 0);
|
$timestamp = (int)($decoded['timestamp'] ?? 0);
|
||||||
$api = isset($decoded['api']) && is_array($decoded['api']) ? $decoded['api'] : array();
|
$api = isset($decoded['api']) && is_array($decoded['api']) ? $decoded['api'] : array();
|
||||||
@@ -167,21 +104,11 @@ function bratonien_tools_nc_connector_connection_last_status(array $connection)
|
|||||||
function bratonien_tools_nc_connector_last_status(array $connections)
|
function bratonien_tools_nc_connector_last_status(array $connections)
|
||||||
{
|
{
|
||||||
$latest = array('timestamp'=>0,'state'=>'','message'=>'','auth_mode'=>'','api_state'=>'','api_message'=>'','fallback_state'=>'','fallback_message'=>'','error_detail'=>'');
|
$latest = array('timestamp'=>0,'state'=>'','message'=>'','auth_mode'=>'','api_state'=>'','api_message'=>'','fallback_state'=>'','fallback_message'=>'','error_detail'=>'');
|
||||||
|
|
||||||
foreach ($connections as $connection)
|
foreach ($connections as $connection)
|
||||||
{
|
{
|
||||||
if (empty($connection['enabled']) || (string)($connection['takeover_state'] ?? '') !== 'active')
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$status = bratonien_tools_nc_connector_connection_last_status($connection);
|
$status = bratonien_tools_nc_connector_connection_last_status($connection);
|
||||||
if ((int)$status['timestamp'] >= (int)$latest['timestamp'])
|
if ((int)$status['timestamp'] >= (int)$latest['timestamp']) $latest = $status;
|
||||||
{
|
|
||||||
$latest = $status;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return $latest;
|
return $latest;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,17 +119,12 @@ function bratonien_tools_nc_connector_system_status(array $connections = array()
|
|||||||
|
|
||||||
$next_realtime_raw = bratonien_tools_nc_connector_systemctl_value(array('show',$timer,'--property=NextElapseUSecRealtime','--value'));
|
$next_realtime_raw = bratonien_tools_nc_connector_systemctl_value(array('show',$timer,'--property=NextElapseUSecRealtime','--value'));
|
||||||
$next_timestamp = bratonien_tools_nc_connector_parse_systemd_time($next_realtime_raw);
|
$next_timestamp = bratonien_tools_nc_connector_parse_systemd_time($next_realtime_raw);
|
||||||
|
|
||||||
if ($next_timestamp <= 0)
|
if ($next_timestamp <= 0)
|
||||||
{
|
{
|
||||||
$next_monotonic_raw = bratonien_tools_nc_connector_systemctl_value(array('show',$timer,'--property=NextElapseUSecMonotonic','--value'));
|
$next_monotonic_raw = bratonien_tools_nc_connector_systemctl_value(array('show',$timer,'--property=NextElapseUSecMonotonic','--value'));
|
||||||
$next_timestamp = bratonien_tools_nc_connector_monotonic_to_timestamp($next_monotonic_raw);
|
$next_timestamp = bratonien_tools_nc_connector_monotonic_to_timestamp($next_monotonic_raw);
|
||||||
}
|
}
|
||||||
|
if ($next_timestamp <= 0) $next_timestamp = bratonien_tools_nc_connector_next_from_timer_list($timer);
|
||||||
if ($next_timestamp <= 0)
|
|
||||||
{
|
|
||||||
$next_timestamp = bratonien_tools_nc_connector_next_from_timer_list($timer);
|
|
||||||
}
|
|
||||||
|
|
||||||
$active = bratonien_tools_nc_connector_systemctl_value(array('is-active',$timer));
|
$active = bratonien_tools_nc_connector_systemctl_value(array('is-active',$timer));
|
||||||
$enabled = bratonien_tools_nc_connector_systemctl_value(array('is-enabled',$timer));
|
$enabled = bratonien_tools_nc_connector_systemctl_value(array('is-enabled',$timer));
|
||||||
@@ -230,9 +152,5 @@ function bratonien_tools_nc_connector_system_status(array $connections = array()
|
|||||||
'last_run_error_detail'=>(string)$last['error_detail'],
|
'last_run_error_detail'=>(string)$last['error_detail'],
|
||||||
'next_run_timestamp'=>$next_timestamp,
|
'next_run_timestamp'=>$next_timestamp,
|
||||||
'next_run_label'=>$next_timestamp > 0 ? date('d.m.Y H:i:s', $next_timestamp) : 'Nicht verfügbar',
|
'next_run_label'=>$next_timestamp > 0 ? date('d.m.Y H:i:s', $next_timestamp) : 'Nicht verfügbar',
|
||||||
'legacy_runtime_exists' => is_dir('/opt/piwigo-sync'),
|
|
||||||
'legacy_config_exists' => is_dir('/etc/piwigo-sync'),
|
|
||||||
'legacy_service_exists' => is_file('/etc/systemd/system/piwigo-sync.service'),
|
|
||||||
'legacy_timer_exists' => is_file('/etc/systemd/system/piwigo-sync.timer'),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user