From 2ccf579125015f67cad2cdfc2eff2d2409003695 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Wed, 28 Feb 2024 18:47:09 +0100 Subject: [PATCH] LSattr_html_gpg_pub_key & LSformRule_gpg_pub_key: fix setting GnuPG home dir in PHP 7.3 --- .../class/class.LSformElement_gpg_pub_key.php | 12 ++++++++---- src/includes/class/class.LSformRule_gpg_pub_key.php | 12 ++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/includes/class/class.LSformElement_gpg_pub_key.php b/src/includes/class/class.LSformElement_gpg_pub_key.php index 88695571..49208f43 100644 --- a/src/includes/class/class.LSformElement_gpg_pub_key.php +++ b/src/includes/class/class.LSformElement_gpg_pub_key.php @@ -44,10 +44,14 @@ class LSformElement_gpg_pub_key extends LSformElement { public function parseValue($value, $details=true) { if (!$details) return $value; - if (function_exists('gnupg_init')) { - // @phpstan-ignore-next-line - $res = gnupg_init(["home_dir" => LS_TMP_DIR_PATH]); - $info = gnupg_import($res, $value); + if (class_exists('gnupg')) { + // The home_dir parameter passed to gnupg_init() is not correctly handled in PHP 7.3, also set + // the GNUPGHOME environment variable. + putenv('GNUPGHOME='.LS_TMP_DIR_PATH); + $gpg = new gnupg(["home_dir" => LS_TMP_DIR_PATH]); + // Don't warn about (GNUPG_ERROR_SILENT is currently the default but ensure it) + $gpg -> seterrormode(GNUPG_ERROR_SILENT); + $info = $gpg -> import($value); } else { LSerror :: addErrorCode('LSformElement_gpg_pub_key_01'); diff --git a/src/includes/class/class.LSformRule_gpg_pub_key.php b/src/includes/class/class.LSformRule_gpg_pub_key.php index 2b64b78a..e47304c6 100644 --- a/src/includes/class/class.LSformRule_gpg_pub_key.php +++ b/src/includes/class/class.LSformRule_gpg_pub_key.php @@ -37,13 +37,17 @@ class LSformRule_gpg_pub_key extends LSformRule { * @return boolean true if the value is valide, false if not */ public static function validate($value, $options, &$formElement) { - if (!function_exists('gnupg_init')) { + if (!class_exists('gnupg')) { LSerror :: addErrorCode('LSformRule_gpg_pub_key_01'); return false; } - // @phpstan-ignore-next-line - $res = gnupg_init(["home_dir" => LS_TMP_DIR_PATH]); - $info = gnupg_import($res, $value); + // The home_dir parameter passed to gnupg_init() is not correctly handled in PHP 7.3, also set + // the GNUPGHOME environment variable. + putenv('GNUPGHOME='.LS_TMP_DIR_PATH); + $gpg = new gnupg(["home_dir" => LS_TMP_DIR_PATH]); + // Don't warn about (GNUPG_ERROR_SILENT is currently the default but ensure it) + $gpg -> seterrormode(GNUPG_ERROR_SILENT); + $info = $gpg -> import($value); // @phpstan-ignore-next-line return is_array($info) && ($info['imported'] + $info['unchanged']) == 1 && $info['fingerprint']; }