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