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 #include "spoofchecker_class.h"
18 #include "spoofchecker_main.h"
19 #include "spoofchecker_create.h"
20 #include "php_intl.h"
21 #include "intl_error.h"
22 
23 #include <unicode/uspoof.h>
24 
25 zend_class_entry *Spoofchecker_ce_ptr = NULL;
26 static zend_object_handlers Spoofchecker_handlers;
27 
28 /*
29  * Auxiliary functions needed by objects of 'Spoofchecker' class
30  */
31 
32 /* {{{ Spoofchecker_objects_free */
Spoofchecker_objects_free(zend_object * object)33 void Spoofchecker_objects_free(zend_object *object)
34 {
35 	Spoofchecker_object* co = php_intl_spoofchecker_fetch_object(object);
36 
37 	zend_object_std_dtor(&co->zo);
38 
39 	spoofchecker_object_destroy(co);
40 }
41 /* }}} */
42 
43 /* {{{ Spoofchecker_object_create */
Spoofchecker_object_create(zend_class_entry * ce)44 zend_object *Spoofchecker_object_create(zend_class_entry *ce)
45 {
46 	Spoofchecker_object*     intern;
47 
48 	intern = zend_object_alloc(sizeof(Spoofchecker_object), ce);
49 	intl_error_init(SPOOFCHECKER_ERROR_P(intern));
50 	zend_object_std_init(&intern->zo, ce);
51 	object_properties_init(&intern->zo, ce);
52 
53 	intern->zo.handlers = &Spoofchecker_handlers;
54 
55 	return &intern->zo;
56 }
57 /* }}} */
58 
59 /*
60  * 'Spoofchecker' class registration structures & functions
61  */
62 
63 /* {{{ Spoofchecker methods arguments info */
64 ZEND_BEGIN_ARG_INFO_EX(spoofchecker_0_args, 0, 0, 0)
65 ZEND_END_ARG_INFO()
66 
67 ZEND_BEGIN_ARG_INFO_EX(spoofchecker_set_checks, 0, 0, 1)
68 	ZEND_ARG_INFO(0, checks)
69 ZEND_END_ARG_INFO()
70 
71 ZEND_BEGIN_ARG_INFO_EX(spoofchecker_set_allowed_locales, 0, 0, 1)
72 	ZEND_ARG_INFO(0, locale_list)
73 ZEND_END_ARG_INFO()
74 
75 ZEND_BEGIN_ARG_INFO_EX(spoofchecker_is_suspicous, 0, 0, 1)
76 	ZEND_ARG_INFO(0, text)
77 	ZEND_ARG_INFO(1, error)
78 ZEND_END_ARG_INFO()
79 
80 ZEND_BEGIN_ARG_INFO_EX(spoofchecker_are_confusable, 0, 0, 2)
81 	ZEND_ARG_INFO(0, s1)
82 	ZEND_ARG_INFO(0, s2)
83 	ZEND_ARG_INFO(1, error)
84 ZEND_END_ARG_INFO()
85 
86 #if U_ICU_VERSION_MAJOR_NUM >= 58
87 ZEND_BEGIN_ARG_INFO_EX(spoofchecker_set_restriction_level, 0, 0, 1)
88 	ZEND_ARG_INFO(0, level)
89 ZEND_END_ARG_INFO()
90 #endif
91 
92 /* }}} */
93 
94 /* {{{ Spoofchecker_class_functions
95  * Every 'Spoofchecker' class method has an entry in this table
96  */
97 
98 static const zend_function_entry Spoofchecker_class_functions[] = {
99 	PHP_ME(Spoofchecker, __construct, spoofchecker_0_args, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
100 	PHP_ME(Spoofchecker, isSuspicious, spoofchecker_is_suspicous, ZEND_ACC_PUBLIC)
101 	PHP_ME(Spoofchecker, areConfusable, spoofchecker_are_confusable, ZEND_ACC_PUBLIC)
102 	PHP_ME(Spoofchecker, setAllowedLocales, spoofchecker_set_allowed_locales, ZEND_ACC_PUBLIC)
103 	PHP_ME(Spoofchecker, setChecks, spoofchecker_set_checks, ZEND_ACC_PUBLIC)
104 #if U_ICU_VERSION_MAJOR_NUM >= 58
105 	PHP_ME(Spoofchecker, setRestrictionLevel, spoofchecker_set_restriction_level, ZEND_ACC_PUBLIC)
106 #endif
107 	PHP_FE_END
108 };
109 /* }}} */
110 
spoofchecker_clone_obj(zval * object)111 static zend_object *spoofchecker_clone_obj(zval *object) /* {{{ */
112 {
113 	zend_object *new_obj_val;
114 	Spoofchecker_object *sfo, *new_sfo;
115 
116     sfo = Z_INTL_SPOOFCHECKER_P(object);
117     intl_error_reset(SPOOFCHECKER_ERROR_P(sfo));
118 
119 	new_obj_val = Spoofchecker_ce_ptr->create_object(Z_OBJCE_P(object));
120 	new_sfo = php_intl_spoofchecker_fetch_object(new_obj_val);
121 	/* clone standard parts */
122 	zend_objects_clone_members(&new_sfo->zo, &sfo->zo);
123 	/* clone internal object */
124 	new_sfo->uspoof = uspoof_clone(sfo->uspoof, SPOOFCHECKER_ERROR_CODE_P(new_sfo));
125 	if(U_FAILURE(SPOOFCHECKER_ERROR_CODE(new_sfo))) {
126 		/* set up error in case error handler is interested */
127 		intl_error_set( NULL, SPOOFCHECKER_ERROR_CODE(new_sfo), "Failed to clone SpoofChecker object", 0 );
128 		Spoofchecker_objects_free(&new_sfo->zo); /* free new object */
129 		zend_error(E_ERROR, "Failed to clone SpoofChecker object");
130 	}
131 	return new_obj_val;
132 }
133 /* }}} */
134 
135 /* {{{ spoofchecker_register_Spoofchecker_class
136  * Initialize 'Spoofchecker' class
137  */
spoofchecker_register_Spoofchecker_class(void)138 void spoofchecker_register_Spoofchecker_class(void)
139 {
140 	zend_class_entry ce;
141 
142 	/* Create and register 'Spoofchecker' class. */
143 	INIT_CLASS_ENTRY(ce, "Spoofchecker", Spoofchecker_class_functions);
144 	ce.create_object = Spoofchecker_object_create;
145 	Spoofchecker_ce_ptr = zend_register_internal_class(&ce);
146 
147 	memcpy(&Spoofchecker_handlers, &std_object_handlers,
148 		sizeof Spoofchecker_handlers);
149 	Spoofchecker_handlers.offset = XtOffsetOf(Spoofchecker_object, zo);
150 	Spoofchecker_handlers.clone_obj = spoofchecker_clone_obj;
151 	Spoofchecker_handlers.free_obj = Spoofchecker_objects_free;
152 }
153 /* }}} */
154 
155 /* {{{ void spoofchecker_object_init( Spoofchecker_object* co )
156  * Initialize internals of Spoofchecker_object.
157  * Must be called before any other call to 'spoofchecker_object_...' functions.
158  */
spoofchecker_object_init(Spoofchecker_object * co)159 void spoofchecker_object_init(Spoofchecker_object* co)
160 {
161 	if (!co) {
162 		return;
163 	}
164 
165 	intl_error_init(SPOOFCHECKER_ERROR_P(co));
166 }
167 /* }}} */
168 
169 /* {{{ void spoofchecker_object_destroy( Spoofchecker_object* co )
170  * Clean up mem allocted by internals of Spoofchecker_object
171  */
spoofchecker_object_destroy(Spoofchecker_object * co)172 void spoofchecker_object_destroy(Spoofchecker_object* co)
173 {
174 	if (!co) {
175 		return;
176 	}
177 
178 	if (co->uspoof) {
179 		uspoof_close(co->uspoof);
180 		co->uspoof = NULL;
181 	}
182 
183 	intl_error_reset(SPOOFCHECKER_ERROR_P(co));
184 }
185 /* }}} */
186 
187 /*
188  * Local variables:
189  * tab-width: 4
190  * c-basic-offset: 4
191  * End:
192  * vim600: noet sw=4 ts=4 fdm=marker
193  * vim<600: noet sw=4 ts=4
194  */
195