1 /*
2 +----------------------------------------------------------------------+
3 | This source file is subject to version 3.01 of the PHP license, |
4 | that is bundled with this package in the file LICENSE, and is |
5 | available through the world-wide-web at the following url: |
6 | http://www.php.net/license/3_01.txt |
7 | If you did not receive a copy of the PHP license and are unable to |
8 | obtain it through the world-wide-web, please send a note to |
9 | license@php.net so we can mail you a copy immediately. |
10 +----------------------------------------------------------------------+
11 | Authors: Scott MacVicar <scottmac@php.net> |
12 +----------------------------------------------------------------------+
13 */
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif
18
19 #include "php_intl.h"
20 #include "spoofchecker_class.h"
21
22 /* {{{ Checks if a given text contains any suspicious characters */
PHP_METHOD(Spoofchecker,isSuspicious)23 PHP_METHOD(Spoofchecker, isSuspicious)
24 {
25 int ret;
26 char *text;
27 size_t text_len;
28 zval *error_code = NULL;
29 SPOOFCHECKER_METHOD_INIT_VARS;
30
31 if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s|z", &text, &text_len, &error_code)) {
32 RETURN_THROWS();
33 }
34
35 SPOOFCHECKER_METHOD_FETCH_OBJECT;
36
37 ret = uspoof_checkUTF8(co->uspoof, text, text_len, NULL, SPOOFCHECKER_ERROR_CODE_P(co));
38
39 if (U_FAILURE(SPOOFCHECKER_ERROR_CODE(co))) {
40 php_error_docref(NULL, E_WARNING, "(%d) %s", SPOOFCHECKER_ERROR_CODE(co), u_errorName(SPOOFCHECKER_ERROR_CODE(co)));
41 RETURN_TRUE;
42 }
43
44 if (error_code) {
45 zval_ptr_dtor(error_code);
46 ZVAL_LONG(error_code, ret);
47 }
48 RETVAL_BOOL(ret != 0);
49 }
50 /* }}} */
51
52 /* {{{ Checks if a given text contains any confusable characters */
PHP_METHOD(Spoofchecker,areConfusable)53 PHP_METHOD(Spoofchecker, areConfusable)
54 {
55 int ret;
56 char *s1, *s2;
57 size_t s1_len, s2_len;
58 zval *error_code = NULL;
59 SPOOFCHECKER_METHOD_INIT_VARS;
60
61 if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "ss|z", &s1, &s1_len,
62 &s2, &s2_len, &error_code)) {
63 RETURN_THROWS();
64 }
65
66 SPOOFCHECKER_METHOD_FETCH_OBJECT;
67 if(s1_len > INT32_MAX || s2_len > INT32_MAX) {
68 SPOOFCHECKER_ERROR_CODE(co) = U_BUFFER_OVERFLOW_ERROR;
69 } else {
70 ret = uspoof_areConfusableUTF8(co->uspoof, s1, (int32_t)s1_len, s2, (int32_t)s2_len, SPOOFCHECKER_ERROR_CODE_P(co));
71 }
72 if (U_FAILURE(SPOOFCHECKER_ERROR_CODE(co))) {
73 php_error_docref(NULL, E_WARNING, "(%d) %s", SPOOFCHECKER_ERROR_CODE(co), u_errorName(SPOOFCHECKER_ERROR_CODE(co)));
74 RETURN_TRUE;
75 }
76
77 if (error_code) {
78 zval_ptr_dtor(error_code);
79 ZVAL_LONG(error_code, ret);
80 }
81 RETVAL_BOOL(ret != 0);
82 }
83 /* }}} */
84
85 /* {{{ Locales to use when running checks */
PHP_METHOD(Spoofchecker,setAllowedLocales)86 PHP_METHOD(Spoofchecker, setAllowedLocales)
87 {
88 char *locales;
89 size_t locales_len;
90 SPOOFCHECKER_METHOD_INIT_VARS;
91
92 if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &locales, &locales_len)) {
93 RETURN_THROWS();
94 }
95
96 SPOOFCHECKER_METHOD_FETCH_OBJECT;
97
98 uspoof_setAllowedLocales(co->uspoof, locales, SPOOFCHECKER_ERROR_CODE_P(co));
99
100 if (U_FAILURE(SPOOFCHECKER_ERROR_CODE(co))) {
101 php_error_docref(NULL, E_WARNING, "(%d) %s", SPOOFCHECKER_ERROR_CODE(co), u_errorName(SPOOFCHECKER_ERROR_CODE(co)));
102 return;
103 }
104 }
105 /* }}} */
106
107 /* {{{ Set the checks to run */
PHP_METHOD(Spoofchecker,setChecks)108 PHP_METHOD(Spoofchecker, setChecks)
109 {
110 zend_long checks;
111 SPOOFCHECKER_METHOD_INIT_VARS;
112
113 if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "l", &checks)) {
114 RETURN_THROWS();
115 }
116
117 SPOOFCHECKER_METHOD_FETCH_OBJECT;
118
119 uspoof_setChecks(co->uspoof, checks, SPOOFCHECKER_ERROR_CODE_P(co));
120
121 if (U_FAILURE(SPOOFCHECKER_ERROR_CODE(co))) {
122 php_error_docref(NULL, E_WARNING, "(%d) %s", SPOOFCHECKER_ERROR_CODE(co), u_errorName(SPOOFCHECKER_ERROR_CODE(co)));
123 }
124 }
125 /* }}} */
126
127 #if U_ICU_VERSION_MAJOR_NUM >= 58
128 /* TODO Document this method on PHP.net */
129 /* {{{ Set the loosest restriction level allowed for strings. */
PHP_METHOD(Spoofchecker,setRestrictionLevel)130 PHP_METHOD(Spoofchecker, setRestrictionLevel)
131 {
132 zend_long level;
133 SPOOFCHECKER_METHOD_INIT_VARS;
134
135 if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "l", &level)) {
136 RETURN_THROWS();
137 }
138
139 SPOOFCHECKER_METHOD_FETCH_OBJECT;
140
141 if (USPOOF_ASCII != level &&
142 USPOOF_SINGLE_SCRIPT_RESTRICTIVE != level &&
143 USPOOF_HIGHLY_RESTRICTIVE != level &&
144 USPOOF_MODERATELY_RESTRICTIVE != level &&
145 USPOOF_MINIMALLY_RESTRICTIVE != level &&
146 USPOOF_UNRESTRICTIVE != level) {
147 zend_argument_value_error(1, "must be one of Spoofchecker::ASCII, Spoofchecker::SINGLE_SCRIPT_RESTRICTIVE, "
148 "Spoofchecker::SINGLE_HIGHLY_RESTRICTIVE, Spoofchecker::SINGLE_MODERATELY_RESTRICTIVE, "
149 "Spoofchecker::SINGLE_MINIMALLY_RESTRICTIVE, or Spoofchecker::UNRESTRICTIVE");
150 RETURN_THROWS();
151 }
152
153 uspoof_setRestrictionLevel(co->uspoof, (URestrictionLevel)level);
154 }
155 /* }}} */
156 #endif
157