diff --git a/NEWS b/NEWS index 36ab60a871e0..0aa39742403b 100644 --- a/NEWS +++ b/NEWS @@ -45,6 +45,8 @@ PHP NEWS - Intl: . Added IntlNumberFormatter::toSkeleton(). (Weilin Du) + . Added Spoofchecker::getChecks() and Spoofchecker::getAllowedLocales(). + (Weilin Du) . Added Spoofchecker::getRestrictionLevel(). (Weilin Du) . Added Collator::getRules() and collator_get_rules(). (Weilin Du) . Added IntlRelativeDateTimeFormatter for locale-aware relative date and time diff --git a/UPGRADING b/UPGRADING index 7c36597353bd..74153f4cc2b5 100644 --- a/UPGRADING +++ b/UPGRADING @@ -162,6 +162,8 @@ PHP 8.7 UPGRADE NOTES the rules cannot be converted to UTF-8. . Added IntlNumberFormatter::toSkeleton() to export the formatter settings as a normalized, locale-independent ICU number skeleton. + . Added Spoofchecker::getChecks() to retrieve the enabled checks as a bitmask + and Spoofchecker::getAllowedLocales() to retrieve the configured locale list. . Added Spoofchecker::getRestrictionLevel() to retrieve the configured restriction level as one of the Spoofchecker restriction level constants. diff --git a/ext/intl/spoofchecker/spoofchecker.stub.php b/ext/intl/spoofchecker/spoofchecker.stub.php index 0232f5ccbe4a..f5e957513de8 100644 --- a/ext/intl/spoofchecker/spoofchecker.stub.php +++ b/ext/intl/spoofchecker/spoofchecker.stub.php @@ -73,9 +73,13 @@ public function areConfusable(string $string1, string $string2, &$errorCode = nu /** @tentative-return-type */ public function setAllowedLocales(string $locales): void {} + public function getAllowedLocales(): string {} + /** @tentative-return-type */ public function setChecks(int $checks): void {} + public function getChecks(): int {} + /** @tentative-return-type */ public function setRestrictionLevel(int $level): void {} diff --git a/ext/intl/spoofchecker/spoofchecker_arginfo.h b/ext/intl/spoofchecker/spoofchecker_arginfo.h index 550096605ea8..f76bf73d54b0 100644 --- a/ext/intl/spoofchecker/spoofchecker_arginfo.h +++ b/ext/intl/spoofchecker/spoofchecker_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit spoofchecker.stub.php instead. - * Stub hash: f836555c3a7d642b88c0856e0366447f0a70a9c7 */ + * Stub hash: 02a441d6c5c3345e96fba475ce39e30cbc21343f */ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Spoofchecker___construct, 0, 0, 0) ZEND_END_ARG_INFO() @@ -19,16 +19,21 @@ ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_Spoofchecker_set ZEND_ARG_TYPE_INFO(0, locales, IS_STRING, 0) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Spoofchecker_getAllowedLocales, 0, 0, IS_STRING, 0) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_Spoofchecker_setChecks, 0, 1, IS_VOID, 0) ZEND_ARG_TYPE_INFO(0, checks, IS_LONG, 0) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Spoofchecker_getChecks, 0, 0, IS_LONG, 0) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_Spoofchecker_setRestrictionLevel, 0, 1, IS_VOID, 0) ZEND_ARG_TYPE_INFO(0, level, IS_LONG, 0) ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Spoofchecker_getRestrictionLevel, 0, 0, IS_LONG, 0) -ZEND_END_ARG_INFO() +#define arginfo_class_Spoofchecker_getRestrictionLevel arginfo_class_Spoofchecker_getChecks ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Spoofchecker_setAllowedChars, 0, 1, IS_VOID, 0) ZEND_ARG_TYPE_INFO(0, pattern, IS_STRING, 0) @@ -57,7 +62,9 @@ ZEND_METHOD(Spoofchecker, __construct); ZEND_METHOD(Spoofchecker, isSuspicious); ZEND_METHOD(Spoofchecker, areConfusable); ZEND_METHOD(Spoofchecker, setAllowedLocales); +ZEND_METHOD(Spoofchecker, getAllowedLocales); ZEND_METHOD(Spoofchecker, setChecks); +ZEND_METHOD(Spoofchecker, getChecks); ZEND_METHOD(Spoofchecker, setRestrictionLevel); ZEND_METHOD(Spoofchecker, getRestrictionLevel); ZEND_METHOD(Spoofchecker, setAllowedChars); @@ -72,7 +79,9 @@ static const zend_function_entry class_Spoofchecker_methods[] = { ZEND_ME(Spoofchecker, isSuspicious, arginfo_class_Spoofchecker_isSuspicious, ZEND_ACC_PUBLIC) ZEND_ME(Spoofchecker, areConfusable, arginfo_class_Spoofchecker_areConfusable, ZEND_ACC_PUBLIC) ZEND_ME(Spoofchecker, setAllowedLocales, arginfo_class_Spoofchecker_setAllowedLocales, ZEND_ACC_PUBLIC) + ZEND_ME(Spoofchecker, getAllowedLocales, arginfo_class_Spoofchecker_getAllowedLocales, ZEND_ACC_PUBLIC) ZEND_ME(Spoofchecker, setChecks, arginfo_class_Spoofchecker_setChecks, ZEND_ACC_PUBLIC) + ZEND_ME(Spoofchecker, getChecks, arginfo_class_Spoofchecker_getChecks, ZEND_ACC_PUBLIC) ZEND_ME(Spoofchecker, setRestrictionLevel, arginfo_class_Spoofchecker_setRestrictionLevel, ZEND_ACC_PUBLIC) ZEND_ME(Spoofchecker, getRestrictionLevel, arginfo_class_Spoofchecker_getRestrictionLevel, ZEND_ACC_PUBLIC) ZEND_ME(Spoofchecker, setAllowedChars, arginfo_class_Spoofchecker_setAllowedChars, ZEND_ACC_PUBLIC) diff --git a/ext/intl/spoofchecker/spoofchecker_main.cpp b/ext/intl/spoofchecker/spoofchecker_main.cpp index cbadf6f3d1be..f7a165462fd4 100644 --- a/ext/intl/spoofchecker/spoofchecker_main.cpp +++ b/ext/intl/spoofchecker/spoofchecker_main.cpp @@ -116,6 +116,25 @@ PHP_METHOD(Spoofchecker, setAllowedLocales) } /* }}} */ +/* {{{ Get the configured list of allowed locales. */ +PHP_METHOD(Spoofchecker, getAllowedLocales) +{ + SPOOFCHECKER_METHOD_INIT_VARS; + + ZEND_PARSE_PARAMETERS_NONE(); + + SPOOFCHECKER_METHOD_FETCH_OBJECT; + + const char *locales = uspoof_getAllowedLocales(co->uspoof, SPOOFCHECKER_ERROR_CODE_P(co)); + if (UNEXPECTED(locales == nullptr)) { + intl_error_set_code(nullptr, SPOOFCHECKER_ERROR_CODE(co)); + zend_throw_exception(IntlException_ce_ptr, "Failed to retrieve allowed locales", 0); + RETURN_THROWS(); + } + RETURN_STRING(locales); +} +/* }}} */ + /* {{{ Set the checks to run */ PHP_METHOD(Spoofchecker, setChecks) { @@ -137,6 +156,25 @@ PHP_METHOD(Spoofchecker, setChecks) } /* }}} */ +/* {{{ Get the enabled checks as a bitmask. */ +PHP_METHOD(Spoofchecker, getChecks) +{ + SPOOFCHECKER_METHOD_INIT_VARS; + + ZEND_PARSE_PARAMETERS_NONE(); + + SPOOFCHECKER_METHOD_FETCH_OBJECT; + + int32_t checks = uspoof_getChecks(co->uspoof, SPOOFCHECKER_ERROR_CODE_P(co)); + if (U_FAILURE(SPOOFCHECKER_ERROR_CODE(co))) { + intl_error_set_code(nullptr, SPOOFCHECKER_ERROR_CODE(co)); + zend_throw_exception(IntlException_ce_ptr, "Failed to retrieve enabled checks", 0); + RETURN_THROWS(); + } + RETURN_LONG(checks); +} +/* }}} */ + /* TODO Document this method on PHP.net */ /* {{{ Set the loosest restriction level allowed for strings. */ PHP_METHOD(Spoofchecker, setRestrictionLevel) diff --git a/ext/intl/tests/spoofchecker_get_checks_and_allowed_locales.phpt b/ext/intl/tests/spoofchecker_get_checks_and_allowed_locales.phpt new file mode 100644 index 000000000000..5ad5adc237ac --- /dev/null +++ b/ext/intl/tests/spoofchecker_get_checks_and_allowed_locales.phpt @@ -0,0 +1,34 @@ +--TEST-- +Spoofchecker::getChecks() and getAllowedLocales() reflect changes to the checker +--EXTENSIONS-- +intl +--FILE-- +getAllowedLocales()); + +$checker->setChecks(Spoofchecker::INVISIBLE); +var_dump($checker->getChecks() === Spoofchecker::INVISIBLE); + +$checker->setAllowedLocales('en'); +$locales = $checker->getAllowedLocales(); +var_dump($checker->getChecks() === (Spoofchecker::INVISIBLE | Spoofchecker::CHAR_LIMIT)); + +$checker->setAllowedLocales(''); +var_dump($checker->getAllowedLocales(), $locales); +var_dump($checker->getChecks() === Spoofchecker::INVISIBLE); + +$checker->setChecks(0); +var_dump($checker->getChecks()); +$checker->setAllowedChars('[a-z]'); +var_dump($checker->getChecks() === Spoofchecker::CHAR_LIMIT); +?> +--EXPECT-- +string(0) "" +bool(true) +bool(true) +string(0) "" +string(2) "en" +bool(true) +int(0) +bool(true) diff --git a/ext/intl/tests/spoofchecker_get_settings_unconstructed.phpt b/ext/intl/tests/spoofchecker_get_settings_unconstructed.phpt new file mode 100644 index 000000000000..8bbb255ac62f --- /dev/null +++ b/ext/intl/tests/spoofchecker_get_settings_unconstructed.phpt @@ -0,0 +1,21 @@ +--TEST-- +Spoofchecker::getChecks() and getAllowedLocales() reject an unconstructed object +--EXTENSIONS-- +intl +--FILE-- +$method(); + } catch (Error $e) { + echo $e->getMessage(), "\n"; + } +} +?> +--EXPECT-- +Found unconstructed Spoofchecker +Found unconstructed Spoofchecker