From a292abc2c50a2dd50737634170c465c65077ee29 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Thu, 5 Jun 2025 15:52:43 +0900 Subject: [PATCH] phpstan updates --- www/admin/class_test.deprecated.helper.php | 3 +++ www/lib/CoreLibs/Basic.php | 4 ++-- www/lib/CoreLibs/Check/Encoding.php | 12 +++++++++++- www/lib/CoreLibs/Convert/Encoding.php | 4 ++-- www/lib/CoreLibs/Create/Email.php | 13 +++++++++++++ www/lib/CoreLibs/UrlRequests/Curl.php | 2 +- 6 files changed, 32 insertions(+), 6 deletions(-) diff --git a/www/admin/class_test.deprecated.helper.php b/www/admin/class_test.deprecated.helper.php index b629ff05..2eb1d83d 100644 --- a/www/admin/class_test.deprecated.helper.php +++ b/www/admin/class_test.deprecated.helper.php @@ -83,6 +83,9 @@ function mtParseCSV( 'UTF-8', $encoding ); + if ($string === false) { + return $lines; + } } if ($flag == 'INTERN') { // split with PHP function diff --git a/www/lib/CoreLibs/Basic.php b/www/lib/CoreLibs/Basic.php index b7f900d5..ef93875a 100644 --- a/www/lib/CoreLibs/Basic.php +++ b/www/lib/CoreLibs/Basic.php @@ -989,10 +989,10 @@ class Basic * @param bool $auto_check default true, if source encoding is set * check that the source is actually matching * to what we sav the source is - * @return string encoding converted string + * @return string|false encoding converted string * @deprecated use \CoreLibs\Convert\Encoding::convertEncoding() instead */ - public static function convertEncoding(string $string, string $to_encoding, string $source_encoding = '', bool $auto_check = true): string + public static function convertEncoding(string $string, string $to_encoding, string $source_encoding = '', bool $auto_check = true): string|false { trigger_error('Method ' . __METHOD__ . ' is deprecated, use \CoreLibs\Convert\Encoding::convertEncoding()', E_USER_DEPRECATED); return \CoreLibs\Convert\Encoding::convertEncoding($string, $to_encoding, $source_encoding, $auto_check); diff --git a/www/lib/CoreLibs/Check/Encoding.php b/www/lib/CoreLibs/Check/Encoding.php index 577cf9b9..08cb9dfb 100644 --- a/www/lib/CoreLibs/Check/Encoding.php +++ b/www/lib/CoreLibs/Check/Encoding.php @@ -56,7 +56,11 @@ class Encoding { // return mb_substitute_character(); if ($return_substitute_func === true) { - return mb_substitute_character(); + // if false abort with error + if (($return = mb_substitute_character()) === false) { + return self::$mb_error_char; + } + return $return; } else { return self::$mb_error_char; } @@ -88,7 +92,13 @@ class Encoding ): array|false { // convert to target encoding and convert back $temp = mb_convert_encoding($string, $to_encoding, $from_encoding); + if ($temp === false) { + return false; + } $compare = mb_convert_encoding($temp, $from_encoding, $to_encoding); + if ($compare === false) { + return false; + } // if string does not match anymore we have a convert problem if ($string == $compare) { return false; diff --git a/www/lib/CoreLibs/Convert/Encoding.php b/www/lib/CoreLibs/Convert/Encoding.php index 26e1a9de..a0a80bf9 100644 --- a/www/lib/CoreLibs/Convert/Encoding.php +++ b/www/lib/CoreLibs/Convert/Encoding.php @@ -23,14 +23,14 @@ class Encoding * @param bool $auto_check default true, if source encoding is set * check that the source is actually matching * to what we sav the source is - * @return string encoding converted string + * @return string|false encoding converted string or false on error */ public static function convertEncoding( string $string, string $to_encoding, string $source_encoding = '', bool $auto_check = true - ): string { + ): string|false { // set if not given if (!$source_encoding) { $source_encoding = mb_detect_encoding($string); diff --git a/www/lib/CoreLibs/Create/Email.php b/www/lib/CoreLibs/Create/Email.php index 5e0c6f72..2f549bbf 100644 --- a/www/lib/CoreLibs/Create/Email.php +++ b/www/lib/CoreLibs/Create/Email.php @@ -38,6 +38,7 @@ class Email * @param string $encoding Encoding, if not set UTF-8 * @param bool $kv_folding If set to true and a valid encoding, do KV folding * @return string Correctly encoded and build email string + * @throws \IntlException if email name cannot be converted to UTF-8 */ public static function encodeEmailName( string $email, @@ -52,6 +53,10 @@ class Email if ($encoding != 'UTF-8') { $email_name = mb_convert_encoding($email_name, $encoding, 'UTF-8'); } + // if we cannot transcode the name, return just the email + if ($email_name === false) { + throw new \IntlException('Cannot convert email_name to UTF-8'); + } $email_name = mb_encode_mimeheader( in_array($encoding, self::$encoding_kv_allowed) && $kv_folding ? @@ -77,6 +82,8 @@ class Email * @param bool $kv_folding If set to true and a valid encoding, * do KV folding * @return array Pos 0: Subject, Pos 1: Body + * @throws \IntlException if subject cannot be converted to UTF-8 + * @throws \IntlException if body cannot be converted to UTF-8 */ private static function replaceContent( string $subject, @@ -102,6 +109,12 @@ class Email $subject = mb_convert_encoding($subject, $encoding, 'UTF-8'); $body = mb_convert_encoding($body, $encoding, 'UTF-8'); } + if ($subject === false) { + throw new \IntlException('Cannot convert subject to UTF-8'); + } + if ($body === false) { + throw new \IntlException('Cannot convert body to UTF-8'); + } // we need to encodde the subject $subject = mb_encode_mimeheader( in_array($encoding, self::$encoding_kv_allowed) && $kv_folding ? diff --git a/www/lib/CoreLibs/UrlRequests/Curl.php b/www/lib/CoreLibs/UrlRequests/Curl.php index ced5129f..40b306f1 100644 --- a/www/lib/CoreLibs/UrlRequests/Curl.php +++ b/www/lib/CoreLibs/UrlRequests/Curl.php @@ -599,7 +599,7 @@ class Curl implements Interface\RequestsInterface // for post we set POST option if ($type == "post") { curl_setopt($handle, CURLOPT_POST, true); - } elseif (!empty($type) && in_array($type, self::CUSTOM_REQUESTS)) { + } elseif (in_array($type, self::CUSTOM_REQUESTS)) { curl_setopt($handle, CURLOPT_CUSTOMREQUEST, strtoupper($type)); } // set body data if not null, will send empty [] for empty data