]> Raphaƫl G. Git Repositories - bbcode/blob - bbcode.c
22780ae349c1890624db0f864abb018d38282459
[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 zval flag;
21 zend_object std;
22 } bbcode_object;
23
24 /* True global resources - no need for thread safety here */
25 zend_object_handlers bbcode_handlers;
26
27 /* BBCode object */
28 zend_class_entry *bbcode_ce;
29
30 /* BBCode exception object */
31 zend_class_entry *bbcode_exception_ce;
32
33 /* Define all constants */
34 #define BBCODE_DEF(GEN) \
35 /* Types */ \
36 GEN(TYPE_ARG) /*BBCODE_TYPE_ARG*/\
37 GEN(TYPE_NOARG) /*BBCODE_TYPE_NOARG*/\
38 GEN(TYPE_SINGLE) /*BBCODE_TYPE_SINGLE*/\
39 GEN(TYPE_OPTARG) /*BBCODE_TYPE_OPTARG*/\
40 GEN(TYPE_ROOT) /*BBCODE_TYPE_ROOT*/\
41 \
42 /* Quotes */ \
43 GEN(QUOTE_DOUBLE) /*BBCODE_ARG_DOUBLE_QUOTE*/\
44 GEN(QUOTE_SIMPLE) /*BBCODE_ARG_SINGLE_QUOTE*/\
45 GEN(QUOTE_HTML) /*BBCODE_ARG_HTML_QUOTE*/\
46 GEN(QUOTE_ESCAPE) /*BBCODE_ARG_QUOTE_ESCAPING*/\
47 \
48 /* Corrections */ \
49 GEN(CORRECT_AUTO) /*BBCODE_AUTO_CORRECT*/\
50 GEN(CORRECT_REOPEN) /*BBCODE_CORRECT_REOPEN_TAGS*/\
51 GEN(CORRECT_NOTREE) /*BBCODE_DISABLE_TREE_BUILD*/\
52 \
53 /* Smileys */ \
54 GEN(SMILEY_ON) /*BBCODE_DEFAULT_SMILEYS_ON*/\
55 GEN(SMILEY_CI) /*BBCODE_SMILEYS_CASE_INSENSITIVE*/\
56 \
57 /* Flags */ \
58 GEN(CLOSE_AUTO) /*BBCODE_FLAGS_ONE_OPEN_PER_LEVEL*/\
59 GEN(REMOVE_EMPTY) /*BBCODE_FLAGS_REMOVE_IF_EMPTY*/\
60 GEN(REMOVE_CONTENT) /*BBCODE_FLAGS_CDATA_NOT_ALLOWED*/\
61 GEN(REMOVE_REOPEN) /*BBCODE_FLAGS_DENY_REOPEN_CHILD*/
62
63
64 /* Generate enum version */
65 #define BBCODE_GEN_ENUM(NAME) BBCODE_ ## NAME,
66
67 /* Generate zend const version */
68 #define BBCODE_GEN_CONST(NAME) zend_declare_class_constant_long(bbcode_ce, #NAME, sizeof(#NAME) - 1, BBCODE_ ## NAME TSRMLS_CC);
69
70 /* All flags enum set */
71 enum {
72 BBCODE_DEF(BBCODE_GEN_ENUM)
73 };
74
75 /* Set default flag
76 * TODO: Set it in an ini ? */
77 #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
78
79 /*static inline bbcode_object *bbcode_object_from_zend_object(zend_object *obj) {
80 return (bbcode_object *)((char*)(obj) - XtOffsetOf(bbcode_object, std));
81 }*/
82
83 static inline bbcode_object* bbcode_fetch(zend_object *obj) {
84 return (bbcode_object *)((char *)obj - XtOffsetOf(bbcode_object, std));
85 }
86
87 #define Z_BBCODE_P(zv) bbcode_fetch(Z_OBJ_P((zv)))
88
89 static void bbcode_destroy(bbcode_object *obj TSRMLS_DC);
90 static void bbcode_free(bbcode_object *obj TSRMLS_DC);
91 static zend_object *bbcode_clone(zval *obj TSRMLS_DC);
92 static zend_object *bbcode_create(zend_class_entry *ce TSRMLS_DC);
93
94 /* BBCode object destroy call */
95 static void bbcode_destroy(bbcode_object *obj TSRMLS_DC) {
96 //zend_objects_destroy_object(&obj->std);
97 zend_objects_destroy_object((zend_object *)obj);
98
99 //zend_object_std_dtor(&obj->std TSRMLS_CC);
100
101 /*if (obj->flag) {
102 efree(obj->flag);
103 }*/
104
105 //efree(obj);
106 }
107
108 /* BBCode object free call */
109 static void bbcode_free(bbcode_object *obj TSRMLS_DC) {
110
111 /*if (obj->flag) {
112 efree(obj->flag);
113 }*/
114
115 //efree(obj);
116 zend_object_std_dtor((zend_object *)obj);
117 }
118
119 static zend_object *bbcode_clone(zval *obj) {
120
121 zend_object *oldobj = Z_OBJ_P(obj);
122 zend_object *newobj = bbcode_create(oldobj->ce);
123
124 zend_objects_clone_members(newobj, oldobj);
125
126 /*Retrieve from zend object old bbcode*/
127 bbcode_object *bboldobj = Z_BBCODE_P(obj);
128 /*Retrieve from zend object new bbcode*/
129 bbcode_object *bbnewobj = bbcode_fetch(newobj);
130
131 /*Restore object member values*/
132 /*TODO: deal with the other one*/
133 Z_LVAL(bbnewobj->flag) = Z_LVAL(bboldobj->flag);
134
135 return newobj;
136 }
137
138 static zend_object *bbcode_create(zend_class_entry *ce TSRMLS_DC) {
139 bbcode_object *obj = (bbcode_object *)ecalloc(1, sizeof(bbcode_object) + zend_object_properties_size(ce));//emalloc(sizeof(bbcode_object));
140
141 zend_object_std_init(&obj->std, ce TSRMLS_CC);
142 object_properties_init(&obj->std, ce TSRMLS_CC);
143
144 memcpy(&bbcode_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
145 bbcode_handlers.offset = XtOffsetOf(bbcode_object, std);
146 bbcode_handlers.dtor_obj = (zend_object_dtor_obj_t) bbcode_destroy;
147 bbcode_handlers.free_obj = (zend_object_free_obj_t) bbcode_free;
148 bbcode_handlers.clone_obj = (zend_object_clone_obj_t) bbcode_clone;
149
150 obj->std.handlers = &bbcode_handlers;
151
152 Z_LVAL(obj->flag) = 0;
153
154 return &obj->std;
155 }
156
157 /* {{{ PHP_METHOD(BBCode::__construct) {
158 */
159 ZEND_BEGIN_ARG_INFO_EX(bbcode_construct_arginfo, 0, 0, 1)
160 ZEND_ARG_ARRAY_INFO(0, tag, 1)
161 ZEND_ARG_ARRAY_INFO(0, smiley, 1)
162 ZEND_ARG_INFO(0, flag)
163 ZEND_END_ARG_INFO()
164
165
166 PHP_METHOD(BBCode, __construct) {
167 zend_array *tag;
168 zend_array *smiley;
169 long flag = BBCODE_DEFAULT_FLAG;
170
171 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|al", &tag, &smiley, &flag) == FAILURE) {
172 return;
173 }
174
175 bbcode_object *obj = bbcode_fetch(Z_OBJ_P(getThis()));
176 assert(obj != NULL);
177
178 //TODO: set tag and smiley
179
180 if (flag) {
181 Z_LVAL(obj->flag) = flag;
182 }
183
184 //obj = zend_object_store_get_object(getThis() TSRMLS_CC);
185 //if (error) {
186 //ZVAL_NULL(this);
187 //}
188 }
189 /* }}} */
190
191 /* {{{ PHP_METHOD(BBCode, __destruct) {
192 */
193 PHP_METHOD(BBCode, __destruct) {
194 }
195 /* }}} */
196
197 /* {{{ PHP_METHOD(BBCode, __toString) {
198 */
199 PHP_METHOD(BBCode, __toString) {
200 //TODO:
201 }
202 /* }}} */
203
204 /* {{{ bbcode_methods[]
205 *
206 * Every BBCode visible function must have an entry in bbcode_methods[].
207 */
208 const zend_function_entry bbcode_methods[] = {
209 PHP_ME(BBCode, __construct, bbcode_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
210 PHP_ME(BBCode, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
211 PHP_ME(BBCode, __toString, NULL, ZEND_ACC_PUBLIC)
212 PHP_FE_END
213 };
214 /* }}} */
215
216 /* {{{ PHP_MINIT_FUNCTION
217 */
218 PHP_MINIT_FUNCTION(bbcode)
219 {
220 /* If you have INI entries, uncomment these lines
221 REGISTER_INI_ENTRIES();
222 */
223
224 /* Tmp zend class entry*/
225 zend_class_entry tmp_ce;
226
227 /* Init BBCode class entry */
228 INIT_CLASS_ENTRY(tmp_ce, "BBCode", bbcode_methods);
229
230 /* Register BBCode class entry */
231 bbcode_ce = zend_register_internal_class(&tmp_ce TSRMLS_CC);
232 bbcode_ce->create_object = bbcode_create;
233
234 /* Generate const */
235 BBCODE_DEF(BBCODE_GEN_CONST)
236
237 /* Init BBCodeException class entry */
238 INIT_CLASS_ENTRY(tmp_ce, "BBCodeException", NULL);
239
240 /* Register BBCodeException class entry */
241 bbcode_exception_ce = zend_register_internal_class_ex(&tmp_ce, zend_exception_get_default(TSRMLS_C) TSRMLS_CC);
242
243 return SUCCESS;
244 }
245 /* }}} */
246
247 /* {{{ PHP_MSHUTDOWN_FUNCTION
248 */
249 PHP_MSHUTDOWN_FUNCTION(bbcode)
250 {
251 /* uncomment this line if you have INI entries
252 UNREGISTER_INI_ENTRIES();
253 */
254 return SUCCESS;
255 }
256 /* }}} */
257
258 /* {{{ PHP_MINFO_FUNCTION
259 */
260 PHP_MINFO_FUNCTION(bbcode)
261 {
262 php_info_print_table_start();
263 php_info_print_table_header(2, PHP_BBCODE_NAME " support", "enabled");
264 php_info_print_table_row(2, "Extension version", PHP_BBCODE_VERSION);
265 php_info_print_table_end();
266
267 /* Remove comments if you have entries in php.ini
268 DISPLAY_INI_ENTRIES();
269 */
270 }
271 /* }}} */
272
273 /* {{{ bbcode_module_entry
274 */
275 zend_module_entry bbcode_module_entry = {
276 STANDARD_MODULE_HEADER,
277 PHP_BBCODE_NAME,
278 NULL, /*bbcode_functions,*/
279 PHP_MINIT(bbcode),
280 PHP_MSHUTDOWN(bbcode),
281 NULL,
282 NULL,
283 PHP_MINFO(bbcode),
284 PHP_BBCODE_VERSION,
285 STANDARD_MODULE_PROPERTIES
286 };
287 /* }}} */
288
289 #ifdef COMPILE_DL_BBCODE
290 #ifdef ZTS
291 ZEND_TSRMLS_CACHE_DEFINE()
292 #endif
293 ZEND_GET_MODULE(bbcode)
294 #endif
295
296 /*
297 * Local variables:
298 * tab-width: 4
299 * c-basic-offset: 4
300 * End:
301 * vim600: noet sw=4 ts=4 fdm=marker
302 * vim<600: noet sw=4 ts=4
303 */