Skip to content

Commit 7bb7172

Browse files
authored
ext/intl: Add remaining getter to Spoofchecker (#26076)
I forget this one when I was doing #25995. This is a follow-up :) This add getAllowedChars to Spoofchecker which is the getter method of setAllowedChars. And is the only remaining one in Spoofchecker now.
1 parent 625424f commit 7bb7172

6 files changed

Lines changed: 96 additions & 2 deletions

File tree

‎NEWS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ PHP NEWS
5252

5353
- Intl:
5454
. Added IntlNumberFormatter::toSkeleton(). (Weilin Du)
55+
. Added Spoofchecker::getAllowedChars(). (Weilin Du)
5556
. Added Spoofchecker::getChecks(). (Weilin Du)
5657
. Added Spoofchecker::getAllowedLocales(). (Weilin Du)
5758
. Added Spoofchecker::getRestrictionLevel(). (Weilin Du)

‎UPGRADING‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ PHP 8.7 UPGRADE NOTES
181181
the rules cannot be converted to UTF-8.
182182
. Added IntlNumberFormatter::toSkeleton() to export the formatter settings
183183
as a normalized, locale-independent ICU number skeleton.
184+
. Added Spoofchecker::getAllowedChars() to export the allowed character set
185+
as a UnicodeSet pattern that can be passed to setAllowedChars().
184186
. Added Spoofchecker::getChecks() to retrieve the enabled checks as a bitmask.
185187
. Added Spoofchecker::getAllowedLocales() to retrieve the configured locale
186188
list.

‎ext/intl/spoofchecker/spoofchecker.stub.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public function getRestrictionLevel(): int {}
8787

8888
public function setAllowedChars(string $pattern, int $patternOptions = 0): void {}
8989

90+
public function getAllowedChars(): string {}
91+
9092
public function getSkeleton(string $string): string|false {}
9193

9294
#if U_ICU_VERSION_MAJOR_NUM >= 74

‎ext/intl/spoofchecker/spoofchecker_arginfo.h‎

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎ext/intl/spoofchecker/spoofchecker_main.cpp‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818

1919
#if __cplusplus >= 201703L
2020
#include <string_view>
21-
#include <unicode/unistr.h>
2221
#endif
2322

23+
#include <unicode/uniset.h>
24+
#include <unicode/uspoof.h>
25+
2426
#include "php_intl.h"
2527
#include "intl_convert.h"
2628
#include "spoofchecker_class.h"
@@ -278,6 +280,39 @@ PHP_METHOD(Spoofchecker, setAllowedChars)
278280
}
279281
}
280282

283+
/* {{{ Get the allowed character set as a UnicodeSet pattern. */
284+
PHP_METHOD(Spoofchecker, getAllowedChars)
285+
{
286+
SPOOFCHECKER_METHOD_INIT_VARS;
287+
288+
ZEND_PARSE_PARAMETERS_NONE();
289+
290+
SPOOFCHECKER_METHOD_FETCH_OBJECT;
291+
292+
UErrorCode status = U_ZERO_ERROR;
293+
const icu::UnicodeSet *allowed = uspoof_getAllowedUnicodeSet(co->uspoof, &status);
294+
if (U_SUCCESS(status)) {
295+
// Generate a pattern from the actual elements. ICU can cache a pattern
296+
// whose meaning depends on the options originally passed to setAllowedChars().
297+
icu::UnicodeSet chars;
298+
icu::UnicodeString pattern;
299+
chars.addAll(*allowed).toPattern(pattern, /* escapeUnprintable */ true);
300+
if (chars.isBogus() || pattern.isBogus()) {
301+
status = U_MEMORY_ALLOCATION_ERROR;
302+
} else {
303+
zend_string *result = intl_convert_utf16_to_utf8(pattern.getBuffer(), pattern.length(), &status);
304+
if (result != nullptr) {
305+
RETURN_STR(result);
306+
}
307+
}
308+
}
309+
310+
intl_errors_set_code(SPOOFCHECKER_ERROR_P(co), status);
311+
zend_throw_exception(IntlException_ce_ptr, "Failed to export allowed characters", 0);
312+
RETURN_THROWS();
313+
}
314+
/* }}} */
315+
281316
/* {{{ Runs an ICU skeleton generator over a UTF-8 string, preflighting the result buffer */
282317
template <typename F>
283318
static zend_string *spoofchecker_skeleton(Spoofchecker_object *co, zend_string *string, F&& skeletonfn)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
--TEST--
2+
Spoofchecker::getAllowedChars() exports a reusable UnicodeSet pattern
3+
--EXTENSIONS--
4+
intl
5+
--FILE--
6+
<?php
7+
$checker = new Spoofchecker();
8+
var_dump($checker->getAllowedChars());
9+
10+
$copy = new Spoofchecker();
11+
$copy->setChecks(Spoofchecker::CHAR_LIMIT);
12+
13+
$checker->setAllowedChars('[]');
14+
var_dump($checker->getAllowedChars());
15+
16+
$checker->setAllowedChars('[a-c{ch}]');
17+
var_dump($checker->getAllowedChars());
18+
19+
$checker->setAllowedChars('[\uD800]');
20+
var_dump($checker->getAllowedChars());
21+
22+
$checker->setAllowedChars('[[A-C]&[:upper:]]', Spoofchecker::IGNORE_SPACE | Spoofchecker::CASE_INSENSITIVE);
23+
$copy->setAllowedChars($checker->getAllowedChars());
24+
var_dump($copy->isSuspicious('aC'), $copy->isSuspicious('d'));
25+
26+
$checker->setAllowedLocales('ru');
27+
$pattern = $checker->getAllowedChars();
28+
unset($checker);
29+
$copy->setAllowedChars($pattern);
30+
var_dump($copy->isSuspicious("\u{0416}"), $copy->isSuspicious("\u{03A9}"));
31+
32+
$unconstructed = new class extends Spoofchecker {
33+
public function __construct() {}
34+
};
35+
try {
36+
$unconstructed->getAllowedChars();
37+
} catch (Error $e) {
38+
echo $e->getMessage(), "\n";
39+
}
40+
?>
41+
--EXPECT--
42+
string(19) "[\u0000-\U0010FFFF]"
43+
string(2) "[]"
44+
string(9) "[a-c{ch}]"
45+
string(8) "[\uD800]"
46+
bool(false)
47+
bool(true)
48+
bool(false)
49+
bool(true)
50+
Found unconstructed Spoofchecker

0 commit comments

Comments
 (0)