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    | https://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(Z_REFVAL_P(error_code), ret);
47 		Z_TRY_ADDREF_P(error_code);
48 	}
49 	RETVAL_BOOL(ret != 0);
50 }
51 /* }}} */
52 
53 /* {{{ Checks if a given text contains any confusable characters */
PHP_METHOD(Spoofchecker,areConfusable)54 PHP_METHOD(Spoofchecker, areConfusable)
55 {
56 	int ret;
57 	char *s1, *s2;
58 	size_t s1_len, s2_len;
59 	zval *error_code = NULL;
60 	SPOOFCHECKER_METHOD_INIT_VARS;
61 
62 	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "ss|z", &s1, &s1_len,
63 										 &s2, &s2_len, &error_code)) {
64 		RETURN_THROWS();
65 	}
66 
67 	SPOOFCHECKER_METHOD_FETCH_OBJECT;
68 	if(s1_len > INT32_MAX || s2_len > INT32_MAX) {
69 		SPOOFCHECKER_ERROR_CODE(co) = U_BUFFER_OVERFLOW_ERROR;
70 	} else {
71 		ret = uspoof_areConfusableUTF8(co->uspoof, s1, (int32_t)s1_len, s2, (int32_t)s2_len, SPOOFCHECKER_ERROR_CODE_P(co));
72 	}
73 	if (U_FAILURE(SPOOFCHECKER_ERROR_CODE(co))) {
74 		php_error_docref(NULL, E_WARNING, "(%d) %s", SPOOFCHECKER_ERROR_CODE(co), u_errorName(SPOOFCHECKER_ERROR_CODE(co)));
75 		RETURN_TRUE;
76 	}
77 
78 	if (error_code) {
79 		zval_ptr_dtor(error_code);
80 		ZVAL_LONG(Z_REFVAL_P(error_code), ret);
81 		Z_TRY_ADDREF_P(error_code);
82 	}
83 	RETVAL_BOOL(ret != 0);
84 }
85 /* }}} */
86 
87 /* {{{ Locales to use when running checks */
PHP_METHOD(Spoofchecker,setAllowedLocales)88 PHP_METHOD(Spoofchecker, setAllowedLocales)
89 {
90 	char *locales;
91 	size_t locales_len;
92 	SPOOFCHECKER_METHOD_INIT_VARS;
93 
94 	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &locales, &locales_len)) {
95 		RETURN_THROWS();
96 	}
97 
98 	SPOOFCHECKER_METHOD_FETCH_OBJECT;
99 
100 	uspoof_setAllowedLocales(co->uspoof, locales, SPOOFCHECKER_ERROR_CODE_P(co));
101 
102 	if (U_FAILURE(SPOOFCHECKER_ERROR_CODE(co))) {
103 		php_error_docref(NULL, E_WARNING, "(%d) %s", SPOOFCHECKER_ERROR_CODE(co), u_errorName(SPOOFCHECKER_ERROR_CODE(co)));
104 		return;
105 	}
106 }
107 /* }}} */
108 
109 /* {{{ Set the checks to run */
PHP_METHOD(Spoofchecker,setChecks)110 PHP_METHOD(Spoofchecker, setChecks)
111 {
112 	zend_long checks;
113 	SPOOFCHECKER_METHOD_INIT_VARS;
114 
115 	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "l", &checks)) {
116 		RETURN_THROWS();
117 	}
118 
119 	SPOOFCHECKER_METHOD_FETCH_OBJECT;
120 
121 	uspoof_setChecks(co->uspoof, checks, SPOOFCHECKER_ERROR_CODE_P(co));
122 
123 	if (U_FAILURE(SPOOFCHECKER_ERROR_CODE(co))) {
124 		php_error_docref(NULL, E_WARNING, "(%d) %s", SPOOFCHECKER_ERROR_CODE(co), u_errorName(SPOOFCHECKER_ERROR_CODE(co)));
125 	}
126 }
127 /* }}} */
128 
129 #if U_ICU_VERSION_MAJOR_NUM >= 58
130 /* TODO Document this method on PHP.net */
131 /* {{{ Set the loosest restriction level allowed for strings. */
PHP_METHOD(Spoofchecker,setRestrictionLevel)132 PHP_METHOD(Spoofchecker, setRestrictionLevel)
133 {
134 	zend_long level;
135 	SPOOFCHECKER_METHOD_INIT_VARS;
136 
137 	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "l", &level)) {
138 		RETURN_THROWS();
139 	}
140 
141 	SPOOFCHECKER_METHOD_FETCH_OBJECT;
142 
143 	if (USPOOF_ASCII != level &&
144 			USPOOF_SINGLE_SCRIPT_RESTRICTIVE != level &&
145 			USPOOF_HIGHLY_RESTRICTIVE != level &&
146 			USPOOF_MODERATELY_RESTRICTIVE != level &&
147 			USPOOF_MINIMALLY_RESTRICTIVE != level &&
148 			USPOOF_UNRESTRICTIVE != level) {
149 		zend_argument_value_error(1, "must be one of Spoofchecker::ASCII, Spoofchecker::SINGLE_SCRIPT_RESTRICTIVE, "
150 			"Spoofchecker::SINGLE_HIGHLY_RESTRICTIVE, Spoofchecker::SINGLE_MODERATELY_RESTRICTIVE, "
151 			"Spoofchecker::SINGLE_MINIMALLY_RESTRICTIVE, or Spoofchecker::UNRESTRICTIVE");
152 		RETURN_THROWS();
153 	}
154 
155 	uspoof_setRestrictionLevel(co->uspoof, (URestrictionLevel)level);
156 }
157 /* }}} */
158 #endif
159