Split Piwigo API key diagnostic credentials

This commit is contained in:
Terranom674
2026-08-17 13:45:10 +02:00
parent 01ec3268e9
commit 892660d5d1

View File

@@ -4,13 +4,23 @@ if (!defined('PHPWG_ROOT_PATH'))
die('Hacking attempt!');
}
function bratonien_tools_nc_connector_piwigo_api_request($api_key, $method)
function bratonien_tools_nc_connector_piwigo_api_request($api_key_id, $api_key_secret, $method)
{
if (!function_exists('curl_init'))
{
throw new RuntimeException('cURL ist in PHP nicht verfuegbar. Der API-Key-Test kann nicht ausgefuehrt werden.');
}
$api_key_id = trim((string)$api_key_id);
$api_key_secret = trim((string)$api_key_secret);
if ($api_key_id === '' || $api_key_secret === '')
{
throw new RuntimeException('API-Schluessel-ID und Geheimnis muessen angegeben werden.');
}
// Piwigo expects the exact header value "public-id:secret". The separator is
// added here by the connector and is not submitted through a form field.
$api_key = $api_key_id.':'.$api_key_secret;
$url = rtrim(get_absolute_root_url(true), '/').'/ws.php';
$ch = curl_init($url);
curl_setopt_array($ch, array(
@@ -21,7 +31,7 @@ function bratonien_tools_nc_connector_piwigo_api_request($api_key, $method)
'format' => 'json',
)),
CURLOPT_HTTPHEADER => array(
'X-PIWIGO-API: '.(string)$api_key,
'X-PIWIGO-API: '.$api_key,
'Accept: application/json',
'Content-Type: application/x-www-form-urlencoded',
),
@@ -35,6 +45,7 @@ function bratonien_tools_nc_connector_piwigo_api_request($api_key, $method)
$errno = curl_errno($ch);
$error = curl_error($ch);
$http_code = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
$content_type = (string)curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch);
if ($body === false || $errno !== 0)
@@ -49,7 +60,8 @@ function bratonien_tools_nc_connector_piwigo_api_request($api_key, $method)
$decoded = json_decode((string)$body, true);
if (!is_array($decoded))
{
throw new RuntimeException('Piwigo-API lieferte keine gueltige JSON-Antwort.');
$detail = $content_type !== '' ? ' Antworttyp: '.$content_type.'.' : '';
throw new RuntimeException('Piwigo-API lieferte keine gueltige JSON-Antwort.'.$detail);
}
if (($decoded['stat'] ?? '') !== 'ok')
{
@@ -62,16 +74,18 @@ function bratonien_tools_nc_connector_piwigo_api_request($api_key, $method)
function bratonien_tools_nc_connector_piwigo_api_test()
{
$api_key = trim((string)($_POST['nc_piwigo_api_key'] ?? ''));
if ($api_key === '')
$api_key_id = trim((string)($_POST['nc_piwigo_api_key_id'] ?? ''));
$api_key_secret = trim((string)($_POST['nc_piwigo_api_key_secret'] ?? ''));
if ($api_key_id === '')
{
throw new RuntimeException('Piwigo-API-Key fehlt.');
throw new RuntimeException('Piwigo-API-Schluessel-ID fehlt.');
}
if ($api_key_secret === '')
{
throw new RuntimeException('Piwigo-API-Geheimnis fehlt.');
}
// Piwigo 16 API keys have a public pkid and a secret separated by a colon.
// Do not reject other formats outright so the checker remains useful if
// Piwigo changes the exact key syntax later.
$status = bratonien_tools_nc_connector_piwigo_api_request($api_key, 'pwg.session.getStatus');
$status = bratonien_tools_nc_connector_piwigo_api_request($api_key_id, $api_key_secret, 'pwg.session.getStatus');
if (!is_array($status))
{
throw new RuntimeException('Piwigo hat keinen auswertbaren Benutzerstatus geliefert.');
@@ -81,7 +95,7 @@ function bratonien_tools_nc_connector_piwigo_api_test()
$user_status = strtolower((string)($status['status'] ?? ''));
$is_admin = in_array($user_status, array('admin', 'webmaster'), true);
$method_result = bratonien_tools_nc_connector_piwigo_api_request($api_key, 'reflection.getMethodList');
$method_result = bratonien_tools_nc_connector_piwigo_api_request($api_key_id, $api_key_secret, 'reflection.getMethodList');
$methods = array();
if (is_array($method_result))
{