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