1 /* Copyright(C) 2007 Brazil
3 This library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License as published by the Free Software Foundation; either
6 version 2.1 of the License, or (at your option) any later version.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Lesser General Public License for more details.
13 You should have received a copy of the GNU Lesser General Public
14 License along with this library; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "php_sen_ctx.h"
25 void sen_ctx_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
27 sen_ctx * resource = (sen_ctx *)(rsrc->ptr);
28 sen_ctx_close(resource);
32 void sen_pctx_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
34 sen_ctx * resource = (sen_ctx *)(rsrc->ptr);
35 sen_ctx_close(resource);
39 void sen_db_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
41 sen_db * resource = (sen_db *)(rsrc->ptr);
42 sen_db_close(resource);
46 function_entry sen_ctx_functions[] = {
47 PHP_FE(sen_db_create , NULL)
48 PHP_FE(sen_db_open , NULL)
49 PHP_FE(sen_db_close , NULL)
50 PHP_FE(sen_ctx_open , NULL)
51 PHP_FE(sen_ctx_connect , NULL)
52 PHP_FE(sen_ctx_pconnect , NULL)
53 PHP_FE(sen_ctx_load , NULL)
54 PHP_FE(sen_ctx_send , NULL)
55 PHP_FE(sen_ctx_recv , NULL)
56 PHP_FE(sen_ctx_close , NULL)
57 PHP_FE(sen_ctx_info_get , NULL)
62 zend_module_entry sen_ctx_module_entry = {
63 STANDARD_MODULE_HEADER,
66 PHP_MINIT(sen_ctx), /* Replace with NULL if there is nothing to do at php startup */
67 PHP_MSHUTDOWN(sen_ctx), /* Replace with NULL if there is nothing to do at php shutdown */
68 PHP_RINIT(sen_ctx), /* Replace with NULL if there is nothing to do at request start */
69 PHP_RSHUTDOWN(sen_ctx), /* Replace with NULL if there is nothing to do at request end */
72 STANDARD_MODULE_PROPERTIES
75 #ifdef COMPILE_DL_SEN_CTX
76 ZEND_GET_MODULE(sen_ctx)
80 PHP_MINIT_FUNCTION(sen_ctx)
82 REGISTER_LONG_CONSTANT("SEN_SYM_WITH_SIS", SEN_SYM_WITH_SIS, CONST_PERSISTENT | CONST_CS);
83 REGISTER_LONG_CONSTANT("SEN_CTX_MORE", SEN_CTX_MORE, CONST_PERSISTENT | CONST_CS);
84 REGISTER_LONG_CONSTANT("SEN_CTX_USEQL", SEN_CTX_USEQL, CONST_PERSISTENT | CONST_CS);
85 REGISTER_LONG_CONSTANT("SEN_CTX_BATCHMODE", SEN_CTX_BATCHMODE, CONST_PERSISTENT | CONST_CS);
86 REGISTER_LONG_CONSTANT("SEN_ENC_DEFAULT", 0, CONST_PERSISTENT | CONST_CS);
87 REGISTER_LONG_CONSTANT("SEN_ENC_NONE", 1, CONST_PERSISTENT | CONST_CS);
88 REGISTER_LONG_CONSTANT("SEN_ENC_EUC_JP", 2, CONST_PERSISTENT | CONST_CS);
89 REGISTER_LONG_CONSTANT("SEN_ENC_UTF8", 3, CONST_PERSISTENT | CONST_CS);
90 REGISTER_LONG_CONSTANT("SEN_ENC_SJIS", 4, CONST_PERSISTENT | CONST_CS);
91 REGISTER_LONG_CONSTANT("SEN_ENC_LATIN1", 5, CONST_PERSISTENT | CONST_CS);
92 REGISTER_LONG_CONSTANT("SEN_ENC_KOI8R", 6, CONST_PERSISTENT | CONST_CS);
93 le_sen_ctx = zend_register_list_destructors_ex(sen_ctx_dtor,
94 NULL, "sen_ctx", module_number);
95 le_sen_pctx = zend_register_list_destructors_ex(NULL,
96 sen_pctx_dtor, "sen_pctx", module_number);
97 le_sen_db = zend_register_list_destructors_ex(sen_db_dtor,
98 NULL, "sen_db", module_number);
104 PHP_MSHUTDOWN_FUNCTION(sen_ctx)
111 PHP_RINIT_FUNCTION(sen_ctx)
117 PHP_RSHUTDOWN_FUNCTION(sen_ctx)
123 PHP_MINFO_FUNCTION(sen_ctx)
125 php_info_print_box_start(0);
126 php_printf("<p>senna binding for PHP.</p>\n");
127 php_printf("<p>Version 0.1-devel (2007-08-16)</p>\n");
128 php_info_print_box_end();
132 PHP_FUNCTION(sen_db_create)
135 long return_res_id = -1;
136 const char * path = NULL;
141 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sll", &path, &path_len, &flags, &encoding) == FAILURE) {
145 return_res = sen_db_create(path,flags,encoding);
146 if(!return_res) RETURN_FALSE;
148 return_res_id = ZEND_REGISTER_RESOURCE(return_value, return_res, le_sen_db);
152 PHP_FUNCTION(sen_db_open)
155 long return_res_id = -1;
156 const char * path = NULL;
159 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &path, &path_len) == FAILURE) {
162 return_res = sen_db_open(path);
163 if(!return_res) RETURN_FALSE;
164 return_res_id = ZEND_REGISTER_RESOURCE(return_value, return_res, le_sen_db);
168 PHP_FUNCTION(sen_db_close)
174 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &res) == FAILURE) {
177 ZEND_FETCH_RESOURCE(res_res, sen_db *, &res, res_id, "sen_db", le_sen_db);
183 PHP_FUNCTION(sen_ctx_open)
185 sen_ctx * return_res;
186 long return_res_id = -1;
192 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &res, &flags) == FAILURE) {
195 ZEND_FETCH_RESOURCE(res_res, sen_db *, &res, res_id, "sen_db", le_sen_db);
197 return_res = sen_ctx_open(res_res,flags);
198 if(!return_res) RETURN_FALSE;
200 return_res_id = ZEND_REGISTER_RESOURCE(return_value, return_res, le_sen_ctx);
204 PHP_FUNCTION(sen_ctx_connect)
206 sen_ctx * return_res;
207 long return_res_id = -1;
208 const char * host = NULL;
213 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sll", &host, &host_len, &port, &flags) == FAILURE) {
216 return_res = sen_ctx_connect(host,port,flags);
217 if(!return_res) RETURN_FALSE;
218 return_res_id = ZEND_REGISTER_RESOURCE(return_value, return_res, le_sen_ctx);
222 PHP_FUNCTION(sen_ctx_pconnect)
224 sen_ctx * return_res;
225 long return_res_id = -1;
226 const char * host = NULL;
227 char *hash_key = NULL;
232 zend_rsrc_list_entry *le;
234 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sll", &host, &host_len, &port, &flags) == FAILURE) {
238 hash_key_len = spprintf(&hash_key, 0, "ctx_%s_%d_%d", host, port, flags);
240 if (zend_hash_find(&EG(persistent_list), hash_key, hash_key_len+1, (void **) &le)==FAILURE) {
241 zend_rsrc_list_entry new_le;
242 return_res = sen_ctx_connect(host,port,flags);
243 Z_TYPE(new_le) = le_sen_pctx;
244 new_le.ptr = return_res;
245 if (zend_hash_update(&EG(persistent_list), hash_key, hash_key_len+1, (void *) &new_le, sizeof(zend_rsrc_list_entry), NULL)==FAILURE) {
246 sen_ctx_close(return_res);
251 if (Z_TYPE_P(le) != le_sen_pctx) {
254 return_res = (sen_ctx *) le->ptr;
256 if(!return_res) RETURN_FALSE;
257 return_res_id = ZEND_REGISTER_RESOURCE(return_value, return_res, le_sen_pctx);
261 PHP_FUNCTION(sen_ctx_load)
266 const char * path = NULL;
269 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &res, &path, &path_len) == FAILURE) {
272 ZEND_FETCH_RESOURCE2(res_res, sen_ctx *, &res, res_id, "sen_ctx", le_sen_ctx, le_sen_pctx);
275 rc = sen_ctx_load(res_res,path);
280 PHP_FUNCTION(sen_ctx_send)
289 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsl", &res, &query, &query_len, &flags) == FAILURE) {
292 ZEND_FETCH_RESOURCE2(res_res, sen_ctx *, &res, res_id, "sen_ctx", le_sen_ctx, le_sen_pctx);
295 rc = sen_ctx_send(res_res, query, query_len, flags);
300 PHP_FUNCTION(sen_ctx_recv)
306 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &res) == FAILURE) {
309 ZEND_FETCH_RESOURCE2(res_res, sen_ctx *, &res, res_id, "sen_ctx", le_sen_ctx, le_sen_pctx);
312 array_init(return_value);
315 int rc,str_len,flags;
316 rc=sen_ctx_recv(res_res, &str, &str_len, &flags);
317 add_next_index_long(return_value, rc);
318 add_next_index_stringl(return_value, str, str_len, 1);
319 add_next_index_long(return_value, flags);
323 PHP_FUNCTION(sen_ctx_close)
329 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &res) == FAILURE) {
332 ZEND_FETCH_RESOURCE2(res_res, sen_ctx *, &res, res_id, "sen_ctx", le_sen_ctx, le_sen_pctx);
339 PHP_FUNCTION(sen_ctx_info_get)
345 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &res) == FAILURE) {
348 ZEND_FETCH_RESOURCE2(res_res, sen_ctx *, &res, res_id, "sen_ctx", le_sen_ctx, le_sen_pctx);
351 array_init(return_value);
355 rc=sen_ctx_info_get(res_res, &info);
356 add_assoc_long(return_value,"rc",rc);
357 add_assoc_long(return_value,"fd",info.fd);
358 add_assoc_long(return_value,"status",info.com_status);
359 add_assoc_long(return_value,"info",info.com_info);
362 #endif /* HAVE_SEN_CTX */