-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathphp_badwords.c
396 lines (336 loc) · 11.1 KB
/
php_badwords.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/** Copyright 2011 HoopCHINA, Co., Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "ext/standard/php_string.h"
#include "ext/standard/php_var.h"
#include "ext/standard/php_smart_str.h"
#include "ext/standard/info.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include "php_badwords.h"
#include "_mbsupport.h"
/* True global resources - no need for thread safety here */
static int le_badwords_compiler, le_badwords_trie;
/* {{{ badwords_functions[]
*
* Every user visible function must have an entry in badwords_functions[].
*/
zend_function_entry badwords_functions[] = {
PHP_FE(badwords_compiler_create, NULL)
PHP_FE(badwords_compiler_append, NULL)
PHP_FE(badwords_compiler_compile, NULL)
PHP_FE(badwords_create, NULL)
PHP_FE(badwords_match, NULL)
PHP_FE(badwords_replace, NULL)
PHP_FE(badwords_version, NULL)
{NULL, NULL, NULL} /* Must be the last line in badwords_functions[] */
};
/* }}} */
/* {{{ badwords_module_entry
*/
zend_module_entry badwords_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"badwords",
badwords_functions,
PHP_MINIT(badwords),
PHP_MSHUTDOWN(badwords),
NULL,
NULL,
PHP_MINFO(badwords),
#if ZEND_MODULE_API_NO >= 20010901
PHP_BADWORDS_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_BADWORDS
ZEND_GET_MODULE(badwords)
#endif
static void php_badwords_compiler_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
struct bw_trie_compiler_t *compiler = (struct bw_trie_compiler_t *) rsrc->ptr;
bw_trie_compiler_free(compiler);
}
static void php_badwords_trie_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
struct bw_trie_mmap_t *mmi = (struct bw_trie_mmap_t *) rsrc->ptr;
if (mmi && --mmi->refcount <= 0) {
munmap(mmi->trie, mmi->mlen);
free(mmi);
}
}
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(badwords)
{
le_badwords_compiler = zend_register_list_destructors_ex(php_badwords_compiler_dtor, NULL, PHP_BADWORDS_COMPILER_RES_NAME, module_number);
le_badwords_trie = zend_register_list_destructors_ex(php_badwords_trie_dtor, php_badwords_trie_dtor, PHP_BADWORDS_TRIE_RES_NAME, module_number);
REGISTER_LONG_CONSTANT("BADWORDS_ENCODING_UTF8", BW_ENC_UTF8, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("BADWORDS_ENCODING_GBK", BW_ENC_GBK, CONST_CS | CONST_PERSISTENT);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(badwords)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(badwords)
{
php_info_print_table_start();
php_info_print_table_header(2, "badwords support", "enabled");
php_info_print_table_row(2, "extension version", PHP_BADWORDS_VERSION);
php_info_print_table_end();
}
/* }}} */
/* {{{ proto resource badwords_compiler_create([encoding = utf8, [case_insensitive = False]])
*/
PHP_FUNCTION(badwords_compiler_create)
{
struct bw_trie_compiler_t *compiler;
int trie_encoding = BW_ENC_UTF8;
zend_bool case_sensitive = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lb", &trie_encoding, &case_sensitive) == FAILURE) {
return;
}
compiler = bw_trie_compiler_create(trie_encoding, case_sensitive);
ZEND_REGISTER_RESOURCE(return_value, compiler, le_badwords_compiler);
}
/* }}} */
/* {{{ proto int badwords_compiler_append(resource compiler, string from, string to)
proto int badwords_compiler_append(resource compiler, array replace)
*/
PHP_FUNCTION(badwords_compiler_append)
{
struct bw_trie_compiler_t *compiler;
zval *zcompiler;
zval **from;
char *to = NULL;
int to_len = 0;
int ac = ZEND_NUM_ARGS();
long added, total_added = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rZ|s", &zcompiler, &from, &to, &to_len) == FAILURE) {
return;
}
if (ac == 2 && Z_TYPE_PP(from) != IS_ARRAY) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The second argument is not an array when only 2 arguments");
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE(compiler, struct bw_trie_compiler_t *, &zcompiler, -1, PHP_BADWORDS_COMPILER_RES_NAME, le_badwords_compiler);
if (Z_TYPE_PP(from) != IS_ARRAY) {
convert_to_string_ex(from);
added = bw_trie_compiler_add_word(compiler, Z_STRVAL_PP(from), Z_STRLEN_PP(from), to, to_len);
if (added >= 0) {
RETURN_LONG(added);
} else {
RETURN_FALSE;
}
}
/* HASH */
HashTable *hash = HASH_OF(*from);
HashPosition hpos;
zval **entry;
int key_len, elen;
char *key, *eval;
ulong num_key;
int keytype;
zval ktmp, etmp;
zend_hash_internal_pointer_reset_ex(hash, &hpos);
while (zend_hash_get_current_data_ex(hash, (void **)&entry, &hpos) == SUCCESS) {
/* KEY */
keytype = zend_hash_get_current_key_ex(hash, &key, &key_len, &num_key, 0, &hpos);
if (keytype == HASH_KEY_IS_LONG) {
ZVAL_LONG(&ktmp, num_key);
convert_to_string(&ktmp);
key = Z_STRVAL(ktmp);
key_len = Z_STRLEN(ktmp);
} else {
key_len--;
}
/* VALUE */
if (Z_TYPE_PP(entry) != IS_STRING) {
etmp = **entry;
zval_copy_ctor(&etmp);
convert_to_string(&etmp);
eval = Z_STRVAL(etmp);
elen = Z_STRLEN(etmp);
} else {
eval = Z_STRVAL_PP(entry);
elen = Z_STRLEN_PP(entry);
}
/* ADD... */
added = bw_trie_compiler_add_word(compiler, key, key_len, eval, elen);
if (Z_TYPE_PP(entry) != IS_STRING)
zval_dtor(&etmp);
if (keytype == HASH_KEY_IS_LONG)
zval_dtor(&ktmp);
/* CHECK... */
if (added > 0)
total_added += added;
/*
if (added < 0)
break;
*/
zend_hash_move_forward_ex(hash, &hpos);
}
RETURN_LONG(total_added);
}
/* }}} */
/* {{{ proto string badwords_compiler_compile(resource compiler)
*/
PHP_FUNCTION(badwords_compiler_compile)
{
struct bw_trie_compiler_t *compiler;
zval *zcompiler;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zcompiler) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(compiler, struct bw_trie_compiler_t *, &zcompiler, -1, PHP_BADWORDS_COMPILER_RES_NAME, le_badwords_compiler);
bw_trie_compiler_compile(compiler, return_value);
}
/* }}} */
/* {{{ proto resource badwords_create(string filename, [string persist_key])
*/
PHP_FUNCTION(badwords_create)
{
char *filename, *persistkey = NULL;
int flen, klen = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &filename, &flen, &persistkey, &klen) == FAILURE) {
return;
}
int fd = open(filename, O_RDONLY);
if (fd < 0) {
if (persistkey) {
zend_hash_del(&EG(persistent_list), persistkey, klen+1);
}
RETURN_FALSE;
}
struct stat stat;
fstat(fd, &stat);
if (persistkey) {
struct bw_trie_mmap_t *existing_mmi;
zend_rsrc_list_entry *existing_mmi_le;
if (zend_hash_find(&EG(persistent_list), persistkey, klen+1, (void **)&existing_mmi_le) == SUCCESS) {
existing_mmi = (struct bw_trie_mmap_t *) existing_mmi_le->ptr;
if (existing_mmi->trie_tim == stat.st_mtime
&& existing_mmi->trie_ino == stat.st_ino
&& existing_mmi->trie_dev == stat.st_dev
&& existing_mmi->mlen == stat.st_size) {
existing_mmi->refcount++;
close(fd);
ZEND_REGISTER_RESOURCE(return_value, existing_mmi, le_badwords_trie);
return;
} else {
zend_hash_del(&EG(persistent_list), persistkey, klen+1);
}
}
}
uint8_t *addr = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);
/* MMAP_CHECK */
if (addr == MAP_FAILED) RETURN_FALSE;
struct bw_trie_mmap_t *mmi;
mmi = (struct bw_trie_mmap_t *) malloc(sizeof(struct bw_trie_mmap_t));
if (!mmi) {
munmap(addr, stat.st_size);
RETURN_FALSE;
}
mmi->refcount = 1;
mmi->trie_dev = stat.st_dev;
mmi->trie_ino = stat.st_ino;
mmi->trie_tim = stat.st_mtime;
mmi->trie = addr;
mmi->mlen = stat.st_size;
ZEND_REGISTER_RESOURCE(return_value, mmi, le_badwords_trie);
if (persistkey) {
zend_rsrc_list_entry le;
le.type = le_badwords_trie;
le.ptr = mmi;
if (zend_hash_update(&EG(persistent_list), persistkey, klen+1, (void*)&le, sizeof(le), NULL) == SUCCESS)
mmi->refcount++;
}
}
/* }}} */
/* {{{ proto string badwords_match(resource trie, string text)
proto string badwords_match(string trie, string text)
*/
PHP_FUNCTION(badwords_match)
{
zval **trie;
char *text;
int text_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Zs", &trie, &text, &text_len) == FAILURE) {
return;
}
if (Z_TYPE_PP(trie) == IS_STRING) {
bw_trie_match(*trie, return_value, text, text_len);
}
else if (Z_TYPE_PP(trie) == IS_RESOURCE) {
struct bw_trie_mmap_t *mmi;
zval trie_;
ZEND_FETCH_RESOURCE(mmi, struct bw_trie_mmap_t *, trie, -1, PHP_BADWORDS_TRIE_RES_NAME, le_badwords_trie);
ZVAL_STRINGL(&trie_, mmi->trie, mmi->mlen, 0);
bw_trie_match(&trie_, return_value, text, text_len);
}
}
/* }}} */
/* {{{ proto string badwords_replace(resource trie, string text)
proto string badwords_replace(string trie, string text)
*/
PHP_FUNCTION(badwords_replace)
{
zval **trie;
char *text;
int text_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Zs", &trie, &text, &text_len) == FAILURE) {
return;
}
if (Z_TYPE_PP(trie) == IS_STRING) {
bw_trie_replace(*trie, return_value, text, text_len);
}
else if (Z_TYPE_PP(trie) == IS_RESOURCE) {
struct bw_trie_mmap_t *mmi;
zval trie_;
ZEND_FETCH_RESOURCE(mmi, struct bw_trie_mmap_t *, trie, -1, PHP_BADWORDS_TRIE_RES_NAME, le_badwords_trie);
ZVAL_STRINGL(&trie_, mmi->trie, mmi->mlen, 0);
bw_trie_replace(&trie_, return_value, text, text_len);
}
}
/* }}} */
/* {{{ proto string badwords_version()
*/
PHP_FUNCTION(badwords_version)
{
RETURN_STRING(PHP_BADWORDS_VERSION, 1);
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/