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