First import
[bbcode] / bbcode.c
1
2 #ifdef HAVE_CONFIG_H
3 #include "config.h"
4 #endif
5
6 #include "php.h"
7 #include "php_ini.h"
8 #include "ext/standard/info.h"
9 #include "Zend/zend_exceptions.h"
10 #include "php_bbcode.h"
11
12 /* If you declare any globals in php_bbcode.h uncomment this:
13 ZEND_DECLARE_MODULE_GLOBALS(bbcode)
14 */
15
16 typedef struct _bbcode_object {
17 /*zval *tag;
18 zval *smiley;
19 zval *flag;*/
20 zend_long flag;
21 zend_object_handlers bbcode_handlers;
22 zend_object std;
23 } bbcode_object;
24
25 /* True global resources - no need for thread safety here */
26 /* BBCode object */
27 zend_class_entry *bbcode_ce;
28
29 /* BBCode exception object */
30 zend_class_entry *bbcode_exception_ce;
31
32 /* Define all constants */
33 #define BBCODE_DEF(GEN) \
34 /* Types */ \
35 GEN(TYPE_ARG) /*BBCODE_TYPE_ARG*/\
36 GEN(TYPE_NOARG) /*BBCODE_TYPE_NOARG*/\
37 GEN(TYPE_SINGLE) /*BBCODE_TYPE_SINGLE*/\
38 GEN(TYPE_OPTARG) /*BBCODE_TYPE_OPTARG*/\
39 GEN(TYPE_ROOT) /*BBCODE_TYPE_ROOT*/\
40 \
41 /* Quotes */ \
42 GEN(QUOTE_DOUBLE) /*BBCODE_ARG_DOUBLE_QUOTE*/\
43 GEN(QUOTE_SIMPLE) /*BBCODE_ARG_SINGLE_QUOTE*/\
44 GEN(QUOTE_HTML) /*BBCODE_ARG_HTML_QUOTE*/\
45 GEN(QUOTE_ESCAPE) /*BBCODE_ARG_QUOTE_ESCAPING*/\
46 \
47 /* Corrections */ \
48 GEN(CORRECT_AUTO) /*BBCODE_AUTO_CORRECT*/\
49 GEN(CORRECT_REOPEN) /*BBCODE_CORRECT_REOPEN_TAGS*/\
50 GEN(CORRECT_NOTREE) /*BBCODE_DISABLE_TREE_BUILD*/\
51 \
52 /* Smileys */ \
53 GEN(SMILEY_ON) /*BBCODE_DEFAULT_SMILEYS_ON*/\
54 GEN(SMILEY_CI) /*BBCODE_SMILEYS_CASE_INSENSITIVE*/\
55 \
56 /* Flags */ \
57 GEN(CLOSE_AUTO) /*BBCODE_FLAGS_ONE_OPEN_PER_LEVEL*/\
58 GEN(REMOVE_EMPTY) /*BBCODE_FLAGS_REMOVE_IF_EMPTY*/\
59 GEN(REMOVE_CONTENT) /*BBCODE_FLAGS_CDATA_NOT_ALLOWED*/\
60 GEN(REMOVE_REOPEN) /*BBCODE_FLAGS_DENY_REOPEN_CHILD*/
61
62
63 /* Generate enum version */
64 #define BBCODE_GEN_ENUM(NAME) BBCODE_ ## NAME,
65
66 /* Generate zend const version */
67 #define BBCODE_GEN_CONST(NAME) zend_declare_class_constant_long(bbcode_ce, #NAME, sizeof(#NAME) - 1, BBCODE_ ## NAME TSRMLS_CC);
68
69 /* All flags enum set */
70 enum {
71 BBCODE_DEF(BBCODE_GEN_ENUM)
72 };
73
74 /* Set default flag
75 * TODO: Set it in an ini ? */
76 #define BBCODE_DEFAULT_FLAG BBCODE_QUOTE_DOUBLE|BBCODE_QUOTE_SIMPLE|BBCODE_QUOTE_ESCAPE|BBCODE_CORRECT_AUTO|BBCODE_CORRECT_REOPEN|BBCODE_SMILEY_ON|BBCODE_CLOSE_AUTO|BBCODE_REMOVE_EMPTY
77
78 static void bbcode_destroy(bbcode_object *obj TSRMLS_DC);
79 static void bbcode_free(bbcode_object *obj TSRMLS_DC);
80 static zend_object *bbcode_clone(zval *obj TSRMLS_DC);
81 static zend_object *bbcode_create(zend_class_entry *ce TSRMLS_DC);
82
83 /* BBCode object destroy call */
84 static void bbcode_destroy(bbcode_object *obj TSRMLS_DC) {
85 //zend_objects_destroy_object(&obj->std);
86 zend_objects_destroy_object((zend_object *)obj);
87
88 //zend_object_std_dtor(&obj->std TSRMLS_CC);
89
90 /*if (obj->flag) {
91 efree(obj->flag);
92 }*/
93
94 //efree(obj);
95 }
96
97 /* BBCode object free call */
98 static void bbcode_free(bbcode_object *obj TSRMLS_DC) {
99
100 /*if (obj->flag) {
101 efree(obj->flag);
102 }*/
103
104 //efree(obj);
105 zend_object_std_dtor((zend_object *)obj);
106 }
107
108 static zend_object *bbcode_clone(zval *obj TSRMLS_DC) {
109
110 bbcode_object *oldobj = (bbcode_object *)((char *)obj - XtOffsetOf(bbcode_object, std));
111
112 bbcode_object *newobj = ecalloc(1, sizeof(bbcode_object) + zend_object_properties_size(Z_OBJCE_P(obj)));
113
114 zend_object_std_init(&newobj->std, Z_OBJCE_P(obj) TSRMLS_CC);
115 object_properties_init(&newobj->std, Z_OBJCE_P(obj) TSRMLS_CC);
116
117 memcpy(&newobj->bbcode_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
118 newobj->bbcode_handlers.offset = XtOffsetOf(bbcode_object, std);
119 newobj->bbcode_handlers.dtor_obj = (zend_object_dtor_obj_t) bbcode_destroy;
120 newobj->bbcode_handlers.free_obj = (zend_object_free_obj_t) bbcode_free;
121 newobj->bbcode_handlers.clone_obj = (zend_object_clone_obj_t) bbcode_clone;
122
123 newobj->std.handlers = &(newobj->bbcode_handlers);
124
125 zend_objects_clone_members(&newobj->std, &oldobj->std);
126
127 #if 0
128 bbcode_object *oldobj = (bbcode_object *)((char *)obj - XtOffsetOf(bbcode_object, std));
129 zend_object *newobj_val = bbcode_create(Z_OBJCE_P(obj) TSRMLS_CC);
130 bbcode_object *newobj = (bbcode_object *)((char *)newobj_val - XtOffsetOf(bbcode_object, std));
131
132 zend_objects_clone_members(&newobj->std, &oldobj->std);
133 #endif
134
135 /*XXX: clone flag member*/
136 newobj->flag = oldobj->flag;
137
138 /* TODO: clone the members of bbcode_object
139 newobj->buffer = oldobj->buffer;
140 newobj->length = oldobj->length;
141
142 if (oldobj->flag) {
143 newobj->buffer = emalloc(oldobj->length);
144 memcpy(newobj->buffer, oldobj->buffer, oldobj->length);
145 }*/
146
147 return &newobj->std;
148 }
149
150 #if 0
151 static zend_object_value bbcode_clone(zval *obj TSRMLS_DC)
152 {
153 bbcode_object *oldobj = zend_object_store_get_object(obj TSRMLS_CC);
154 zend_object_value newobj_val = array_buffer_create_object(Z_OBJCE_P(obj) TSRMLS_CC);
155 bbcode_object *newobj = zend_object_store_get_object_by_handle(newobj_val.handle TSRMLS_CC);
156
157 zend_objects_clone_members(&newobj->std, &oldobj->std);
158
159 /* TODO: clone the members of bbcode_object
160 newobj->buffer = oldobj->buffer;
161 newobj->length = oldobj->length;
162
163 if (oldobj->flag) {
164 newobj->buffer = emalloc(oldobj->length);
165 memcpy(newobj->buffer, oldobj->buffer, oldobj->length);
166 }*/
167
168 return newobj_val;
169 }
170 #endif
171
172 static zend_object *bbcode_create(zend_class_entry *ce TSRMLS_DC) {
173 bbcode_object *obj = ecalloc(1, sizeof(bbcode_object) + zend_object_properties_size(ce));//emalloc(sizeof(bbcode_object));
174 obj->flag = 0;
175 //memset(obj, 0, sizeof(bbcode_object));
176
177 zend_object_std_init(&obj->std, ce TSRMLS_CC);
178 object_properties_init(&obj->std, ce TSRMLS_CC);
179
180 /**/
181
182 memcpy(&obj->bbcode_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
183 obj->bbcode_handlers.offset = XtOffsetOf(bbcode_object, std);
184 obj->bbcode_handlers.dtor_obj = (zend_object_dtor_obj_t) bbcode_destroy;
185 obj->bbcode_handlers.free_obj = (zend_object_free_obj_t) bbcode_free;
186 obj->bbcode_handlers.clone_obj = (zend_object_clone_obj_t) bbcode_clone;
187 //bbcode_handlers.offset = XtOffsetOf(bbcode_object, std);
188 //bbcode_handlers.dtor_obj = (zend_object_dtor_obj_t) bbcode_destroy;
189 //bbcode_handlers.free_obj = (zend_object_free_obj_t) bbcode_free;
190 //bbcode_handlers.clone_obj = (zend_object_clone_obj_t) bbcode_clone;
191
192
193 /*retval.handle = zend_object_store_put(
194 obj,
195 (zend_objects_store_dtor_t) zend_objects_destroy_object,
196 (zend_objects_free_object_storage_t) bbcode_free,
197 NULL TSRMLS_CC
198 );
199 retval.handlers = &bbcode_handlers;
200
201 return retval;*/
202
203 obj->std.handlers = &(obj->bbcode_handlers);
204
205 return &obj->std;
206 }
207
208 static inline bbcode_object* bbcode_fetch(zend_object *obj) {
209 return (bbcode_object *)((char *)obj - XtOffsetOf(bbcode_object, std));
210 }
211
212 /* {{{ PHP_METHOD(BBCode::__construct) {
213 */
214 ZEND_BEGIN_ARG_INFO_EX(bbcode_construct_arginfo, 0, 0, 1)
215 ZEND_ARG_ARRAY_INFO(0, tag, 1)
216 ZEND_ARG_ARRAY_INFO(0, smiley, 1)
217 ZEND_ARG_INFO(0, flag)
218 ZEND_END_ARG_INFO()
219
220
221 PHP_METHOD(BBCode, __construct) {
222 zend_array *tag;
223 zend_array *smiley;
224 zend_long flag = BBCODE_DEFAULT_FLAG;
225
226 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|al", &tag, &smiley, &flag) == FAILURE) {
227 return;
228 }
229
230 bbcode_object *obj = bbcode_fetch(Z_OBJ_P(getThis()));
231 assert(obj != NULL);
232
233 if (flag) {
234 obj->flag = (zend_long) flag;
235 }
236
237 //obj = zend_object_store_get_object(getThis() TSRMLS_CC);
238 //if (error) {
239 //ZVAL_NULL(this);
240 //}
241 }
242 /* }}} */
243
244 /* {{{ PHP_METHOD(BBCode, __destruct) {
245 */
246 PHP_METHOD(BBCode, __destruct) {
247 }
248 /* }}} */
249
250 /* {{{ PHP_METHOD(BBCode, __toString) {
251 */
252 PHP_METHOD(BBCode, __toString) {
253 //TODO:
254 }
255 /* }}} */
256
257 /* {{{ bbcode_methods[]
258 *
259 * Every BBCode visible function must have an entry in bbcode_methods[].
260 */
261 const zend_function_entry bbcode_methods[] = {
262 PHP_ME(BBCode, __construct, bbcode_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
263 PHP_ME(BBCode, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
264 PHP_ME(BBCode, __toString, NULL, ZEND_ACC_PUBLIC)
265 PHP_FE_END
266 };
267 /* }}} */
268
269 /* {{{ PHP_MINIT_FUNCTION
270 */
271 PHP_MINIT_FUNCTION(bbcode)
272 {
273 /* If you have INI entries, uncomment these lines
274 REGISTER_INI_ENTRIES();
275 */
276
277 /* Tmp zend class entry*/
278 zend_class_entry tmp_ce;
279
280 /* Init BBCode class entry */
281 INIT_CLASS_ENTRY(tmp_ce, "BBCode", bbcode_methods);
282
283 /* Register BBCode class entry */
284 bbcode_ce = zend_register_internal_class(&tmp_ce TSRMLS_CC);
285 bbcode_ce->create_object = bbcode_create;
286
287 /* Generate const */
288 BBCODE_DEF(BBCODE_GEN_CONST)
289
290 /* Init BBCodeException class entry */
291 INIT_CLASS_ENTRY(tmp_ce, "BBCodeException", NULL);
292
293 /* Register BBCodeException class entry */
294 bbcode_exception_ce = zend_register_internal_class_ex(&tmp_ce, zend_exception_get_default(TSRMLS_C) TSRMLS_CC);
295
296 return SUCCESS;
297 }
298 /* }}} */
299
300 /* {{{ PHP_MSHUTDOWN_FUNCTION
301 */
302 PHP_MSHUTDOWN_FUNCTION(bbcode)
303 {
304 /* uncomment this line if you have INI entries
305 UNREGISTER_INI_ENTRIES();
306 */
307 return SUCCESS;
308 }
309 /* }}} */
310
311 /* {{{ PHP_MINFO_FUNCTION
312 */
313 PHP_MINFO_FUNCTION(bbcode)
314 {
315 php_info_print_table_start();
316 php_info_print_table_header(2, PHP_BBCODE_NAME " support", "enabled");
317 php_info_print_table_row(2, "Extension version", PHP_BBCODE_VERSION);
318 php_info_print_table_end();
319
320 /* Remove comments if you have entries in php.ini
321 DISPLAY_INI_ENTRIES();
322 */
323 }
324 /* }}} */
325
326 /* {{{ bbcode_module_entry
327 */
328 zend_module_entry bbcode_module_entry = {
329 STANDARD_MODULE_HEADER,
330 PHP_BBCODE_NAME,
331 NULL, /*bbcode_functions,*/
332 PHP_MINIT(bbcode),
333 PHP_MSHUTDOWN(bbcode),
334 NULL,
335 NULL,
336 PHP_MINFO(bbcode),
337 PHP_BBCODE_VERSION,
338 STANDARD_MODULE_PROPERTIES
339 };
340 /* }}} */
341
342 #ifdef COMPILE_DL_BBCODE
343 #ifdef ZTS
344 ZEND_TSRMLS_CACHE_DEFINE()
345 #endif
346 ZEND_GET_MODULE(bbcode)
347 #endif
348
349 /*
350 * Local variables:
351 * tab-width: 4
352 * c-basic-offset: 4
353 * End:
354 * vim600: noet sw=4 ts=4 fdm=marker
355 * vim<600: noet sw=4 ts=4
356 */