xref: /PHP-7.4/ext/dba/dba.c (revision f448b0e6)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) The PHP Group                                          |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Sascha Schumann <sascha@schumann.cx>                        |
16    |          Marcus Boerger <helly@php.net>                              |
17    +----------------------------------------------------------------------+
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include "php.h"
25 
26 #if HAVE_DBA
27 
28 #include "php_ini.h"
29 #include <stdio.h>
30 #include <fcntl.h>
31 #ifdef HAVE_SYS_FILE_H
32 #include <sys/file.h>
33 #endif
34 
35 #include "php_dba.h"
36 #include "ext/standard/info.h"
37 #include "ext/standard/php_string.h"
38 #include "ext/standard/flock_compat.h"
39 
40 #include "php_gdbm.h"
41 #include "php_ndbm.h"
42 #include "php_dbm.h"
43 #include "php_cdb.h"
44 #include "php_db1.h"
45 #include "php_db2.h"
46 #include "php_db3.h"
47 #include "php_db4.h"
48 #include "php_flatfile.h"
49 #include "php_inifile.h"
50 #include "php_qdbm.h"
51 #include "php_tcadb.h"
52 #include "php_lmdb.h"
53 
54 /* {{{ arginfo */
55 ZEND_BEGIN_ARG_INFO_EX(arginfo_dba_popen, 0, 0, 2)
56 	ZEND_ARG_INFO(0, path)
57 	ZEND_ARG_INFO(0, mode)
58 	ZEND_ARG_INFO(0, handlername)
59 	ZEND_ARG_VARIADIC_INFO(0, handler_parameters)
60 ZEND_END_ARG_INFO()
61 
62 ZEND_BEGIN_ARG_INFO_EX(arginfo_dba_open, 0, 0, 2)
63 	ZEND_ARG_INFO(0, path)
64 	ZEND_ARG_INFO(0, mode)
65 	ZEND_ARG_INFO(0, handlername)
66 	ZEND_ARG_VARIADIC_INFO(0, handler_parameters)
67 ZEND_END_ARG_INFO()
68 
69 ZEND_BEGIN_ARG_INFO(arginfo_dba_close, 0)
70 	ZEND_ARG_INFO(0, handle)
71 ZEND_END_ARG_INFO()
72 
73 ZEND_BEGIN_ARG_INFO(arginfo_dba_exists, 0)
74 	ZEND_ARG_INFO(0, key)
75 	ZEND_ARG_INFO(0, handle)
76 ZEND_END_ARG_INFO()
77 
78 ZEND_BEGIN_ARG_INFO_EX(arginfo_dba_fetch, 0, 0, 2)
79 	ZEND_ARG_INFO(0, key)
80 	ZEND_ARG_INFO(0, skip)
81 	ZEND_ARG_INFO(0, handle)
82 ZEND_END_ARG_INFO()
83 
84 ZEND_BEGIN_ARG_INFO(arginfo_dba_key_split, 0)
85 	ZEND_ARG_INFO(0, key)
86 ZEND_END_ARG_INFO()
87 
88 ZEND_BEGIN_ARG_INFO(arginfo_dba_firstkey, 0)
89 	ZEND_ARG_INFO(0, handle)
90 ZEND_END_ARG_INFO()
91 
92 ZEND_BEGIN_ARG_INFO(arginfo_dba_nextkey, 0)
93 	ZEND_ARG_INFO(0, handle)
94 ZEND_END_ARG_INFO()
95 
96 ZEND_BEGIN_ARG_INFO(arginfo_dba_delete, 0)
97 	ZEND_ARG_INFO(0, key)
98 	ZEND_ARG_INFO(0, handle)
99 ZEND_END_ARG_INFO()
100 
101 ZEND_BEGIN_ARG_INFO(arginfo_dba_insert, 0)
102 	ZEND_ARG_INFO(0, key)
103 	ZEND_ARG_INFO(0, value)
104 	ZEND_ARG_INFO(0, handle)
105 ZEND_END_ARG_INFO()
106 
107 ZEND_BEGIN_ARG_INFO(arginfo_dba_replace, 0)
108 	ZEND_ARG_INFO(0, key)
109 	ZEND_ARG_INFO(0, value)
110 	ZEND_ARG_INFO(0, handle)
111 ZEND_END_ARG_INFO()
112 
113 ZEND_BEGIN_ARG_INFO(arginfo_dba_optimize, 0)
114 	ZEND_ARG_INFO(0, handle)
115 ZEND_END_ARG_INFO()
116 
117 ZEND_BEGIN_ARG_INFO(arginfo_dba_sync, 0)
118 	ZEND_ARG_INFO(0, handle)
119 ZEND_END_ARG_INFO()
120 
121 ZEND_BEGIN_ARG_INFO_EX(arginfo_dba_handlers, 0, 0, 0)
122 	ZEND_ARG_INFO(0, full_info)
123 ZEND_END_ARG_INFO()
124 
125 ZEND_BEGIN_ARG_INFO(arginfo_dba_list, 0)
126 ZEND_END_ARG_INFO()
127 
128 /* }}} */
129 
130 /* {{{ dba_functions[]
131  */
132 static const zend_function_entry dba_functions[] = {
133 	PHP_FE(dba_open, arginfo_dba_open)
134 	PHP_FE(dba_popen, arginfo_dba_popen)
135 	PHP_FE(dba_close, arginfo_dba_close)
136 	PHP_FE(dba_delete, arginfo_dba_delete)
137 	PHP_FE(dba_exists, arginfo_dba_exists)
138 	PHP_FE(dba_fetch, arginfo_dba_fetch)
139 	PHP_FE(dba_insert, arginfo_dba_insert)
140 	PHP_FE(dba_replace, arginfo_dba_replace)
141 	PHP_FE(dba_firstkey, arginfo_dba_firstkey)
142 	PHP_FE(dba_nextkey, arginfo_dba_nextkey)
143 	PHP_FE(dba_optimize, arginfo_dba_optimize)
144 	PHP_FE(dba_sync, arginfo_dba_sync)
145 	PHP_FE(dba_handlers, arginfo_dba_handlers)
146 	PHP_FE(dba_list, arginfo_dba_list)
147 	PHP_FE(dba_key_split, arginfo_dba_key_split)
148 	PHP_FE_END
149 };
150 /* }}} */
151 
152 PHP_MINIT_FUNCTION(dba);
153 PHP_MSHUTDOWN_FUNCTION(dba);
154 PHP_MINFO_FUNCTION(dba);
155 
156 ZEND_BEGIN_MODULE_GLOBALS(dba)
157 	char *default_handler;
158 	dba_handler *default_hptr;
159 ZEND_END_MODULE_GLOBALS(dba)
160 
161 ZEND_DECLARE_MODULE_GLOBALS(dba)
162 
163 #define DBA_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(dba, v)
164 
165 static PHP_GINIT_FUNCTION(dba);
166 
167 zend_module_entry dba_module_entry = {
168 	STANDARD_MODULE_HEADER,
169 	"dba",
170 	dba_functions,
171 	PHP_MINIT(dba),
172 	PHP_MSHUTDOWN(dba),
173 	NULL,
174 	NULL,
175 	PHP_MINFO(dba),
176 	PHP_DBA_VERSION,
177 	PHP_MODULE_GLOBALS(dba),
178 	PHP_GINIT(dba),
179 	NULL,
180 	NULL,
181 	STANDARD_MODULE_PROPERTIES_EX
182 };
183 
184 #ifdef COMPILE_DL_DBA
185 #ifdef ZTS
186 ZEND_TSRMLS_CACHE_DEFINE()
187 #endif
ZEND_GET_MODULE(dba)188 ZEND_GET_MODULE(dba)
189 #endif
190 
191 /* {{{ macromania */
192 
193 #define DBA_ID_PARS 											\
194 	zval *id; 													\
195 	dba_info *info = NULL; 										\
196 	int ac = ZEND_NUM_ARGS()
197 
198 /* these are used to get the standard arguments */
199 
200 /* {{{ php_dba_myke_key */
201 static size_t php_dba_make_key(zval *key, char **key_str, char **key_free)
202 {
203 	if (Z_TYPE_P(key) == IS_ARRAY) {
204 		zval *group, *name;
205 		HashPosition pos;
206 		size_t len;
207 
208 		if (zend_hash_num_elements(Z_ARRVAL_P(key)) != 2) {
209 			zend_throw_error(NULL, "Key does not have exactly two elements: (key, name)");
210 			return 0;
211 		}
212 		zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(key), &pos);
213 		group = zend_hash_get_current_data_ex(Z_ARRVAL_P(key), &pos);
214 		zend_hash_move_forward_ex(Z_ARRVAL_P(key), &pos);
215 		name = zend_hash_get_current_data_ex(Z_ARRVAL_P(key), &pos);
216 		convert_to_string_ex(group);
217 		convert_to_string_ex(name);
218 		if (Z_STRLEN_P(group) == 0) {
219 			*key_str = Z_STRVAL_P(name);
220 			*key_free = NULL;
221 			return Z_STRLEN_P(name);
222 		}
223 		len = spprintf(key_str, 0, "[%s]%s", Z_STRVAL_P(group), Z_STRVAL_P(name));
224 		*key_free = *key_str;
225 		return len;
226 	} else {
227 		zval tmp;
228 		size_t len;
229 
230 		ZVAL_COPY(&tmp, key);
231 		convert_to_string(&tmp);
232 
233 		len = Z_STRLEN(tmp);
234 		if (len) {
235 			*key_free = *key_str = estrndup(Z_STRVAL(tmp), Z_STRLEN(tmp));
236 		}
237 		zval_ptr_dtor(&tmp);
238 		return len;
239 	}
240 }
241 /* }}} */
242 
243 #define DBA_GET2 												\
244 	zval *key;													\
245 	char *key_str, *key_free;									\
246 	size_t key_len; 											\
247 	if (zend_parse_parameters(ac, "zr", &key, &id) == FAILURE) { 	\
248 		return; 												\
249 	} 															\
250 	if ((key_len = php_dba_make_key(key, &key_str, &key_free)) == 0) {\
251 		RETURN_FALSE;											\
252 	}
253 
254 #define DBA_GET2_3												\
255 	zval *key;													\
256 	char *key_str, *key_free;									\
257 	size_t key_len; 											\
258 	zend_long skip = 0;  											\
259 	switch(ac) {												\
260 	case 2: 													\
261 		if (zend_parse_parameters(ac, "zr", &key, &id) == FAILURE) { \
262 			return;												\
263 		} 														\
264 		break;  												\
265 	case 3: 													\
266 		if (zend_parse_parameters(ac, "zlr", &key, &skip, &id) == FAILURE) { \
267 			return;												\
268 		} 														\
269 		break;  												\
270 	default:													\
271 		WRONG_PARAM_COUNT; 										\
272 	} 															\
273 	if ((key_len = php_dba_make_key(key, &key_str, &key_free)) == 0) {\
274 		RETURN_FALSE;											\
275 	}
276 
277 
278 #define DBA_FETCH_RESOURCE(info, id)	\
279 	if ((info = (dba_info *)zend_fetch_resource2(Z_RES_P(id), "DBA identifier", le_db, le_pdb)) == NULL) { \
280 		RETURN_FALSE; \
281 	}
282 
283 #define DBA_FETCH_RESOURCE_WITH_ID(info, id)	\
284 	if ((info = (dba_info *)zend_fetch_resource2(Z_RES_P(id), "DBA identifier", le_db, le_pdb)) == NULL) { \
285 		DBA_ID_DONE; \
286 		RETURN_FALSE; \
287 	}
288 
289 #define DBA_ID_GET2   DBA_ID_PARS; DBA_GET2;   DBA_FETCH_RESOURCE_WITH_ID(info, id)
290 #define DBA_ID_GET2_3 DBA_ID_PARS; DBA_GET2_3; DBA_FETCH_RESOURCE_WITH_ID(info, id)
291 
292 #define DBA_ID_DONE												\
293 	if (key_free) efree(key_free)
294 /* a DBA handler must have specific routines */
295 
296 #define DBA_NAMED_HND(alias, name, flags) \
297 {\
298 	#alias, flags, dba_open_##name, dba_close_##name, dba_fetch_##name, dba_update_##name, \
299 	dba_exists_##name, dba_delete_##name, dba_firstkey_##name, dba_nextkey_##name, \
300 	dba_optimize_##name, dba_sync_##name, dba_info_##name \
301 },
302 
303 #define DBA_HND(name, flags) DBA_NAMED_HND(name, name, flags)
304 
305 /* check whether the user has write access */
306 #define DBA_WRITE_CHECK \
307 	if(info->mode != DBA_WRITER && info->mode != DBA_TRUNC && info->mode != DBA_CREAT) { \
308 		php_error_docref(NULL, E_WARNING, "You cannot perform a modification to a database without proper access"); \
309 		RETURN_FALSE; \
310 	}
311 
312 /* the same check, but with a call to DBA_ID_DONE before returning */
313 #define DBA_WRITE_CHECK_WITH_ID \
314 	if(info->mode != DBA_WRITER && info->mode != DBA_TRUNC && info->mode != DBA_CREAT) { \
315 		php_error_docref(NULL, E_WARNING, "You cannot perform a modification to a database without proper access"); \
316 		DBA_ID_DONE; \
317 		RETURN_FALSE; \
318 	}
319 
320 /* }}} */
321 
322 /* {{{ globals */
323 
324 static dba_handler handler[] = {
325 #if DBA_GDBM
326 	DBA_HND(gdbm, DBA_LOCK_EXT) /* Locking done in library if set */
327 #endif
328 #if DBA_DBM
329 	DBA_HND(dbm, DBA_LOCK_ALL) /* No lock in lib */
330 #endif
331 #if DBA_NDBM
332 	DBA_HND(ndbm, DBA_LOCK_ALL) /* Could be done in library: filemode = 0644 + S_ENFMT */
333 #endif
334 #if DBA_CDB
335 	DBA_HND(cdb, DBA_STREAM_OPEN|DBA_LOCK_ALL) /* No lock in lib */
336 #endif
337 #if DBA_CDB_BUILTIN
338     DBA_NAMED_HND(cdb_make, cdb, DBA_STREAM_OPEN|DBA_LOCK_ALL) /* No lock in lib */
339 #endif
340 #if DBA_DB1
341 	DBA_HND(db1, DBA_LOCK_ALL) /* No lock in lib */
342 #endif
343 #if DBA_DB2
344 	DBA_HND(db2, DBA_LOCK_ALL) /* No lock in lib */
345 #endif
346 #if DBA_DB3
347 	DBA_HND(db3, DBA_LOCK_ALL) /* No lock in lib */
348 #endif
349 #if DBA_DB4
350 	DBA_HND(db4, DBA_LOCK_ALL) /* No lock in lib */
351 #endif
352 #if DBA_INIFILE
353 	DBA_HND(inifile, DBA_STREAM_OPEN|DBA_LOCK_ALL|DBA_CAST_AS_FD) /* No lock in lib */
354 #endif
355 #if DBA_FLATFILE
356 	DBA_HND(flatfile, DBA_STREAM_OPEN|DBA_LOCK_ALL|DBA_NO_APPEND) /* No lock in lib */
357 #endif
358 #if DBA_QDBM
359 	DBA_HND(qdbm, DBA_LOCK_EXT)
360 #endif
361 #if DBA_TCADB
362 	DBA_HND(tcadb, DBA_LOCK_ALL)
363 #endif
364 #if DBA_LMDB
365 	DBA_HND(lmdb, DBA_LOCK_EXT)
366 #endif
367 	{ NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
368 };
369 
370 #if DBA_FLATFILE
371 #define DBA_DEFAULT "flatfile"
372 #elif DBA_DB4
373 #define DBA_DEFAULT "db4"
374 #elif DBA_DB3
375 #define DBA_DEFAULT "db3"
376 #elif DBA_DB2
377 #define DBA_DEFAULT "db2"
378 #elif DBA_DB1
379 #define DBA_DEFAULT "db1"
380 #elif DBA_GDBM
381 #define DBA_DEFAULT "gdbm"
382 #elif DBA_NBBM
383 #define DBA_DEFAULT "ndbm"
384 #elif DBA_DBM
385 #define DBA_DEFAULT "dbm"
386 #elif DBA_QDBM
387 #define DBA_DEFAULT "qdbm"
388 #elif DBA_TCADB
389 #define DBA_DEFAULT "tcadb"
390 #elif DBA_LMDB
391 #define DBA_DEFAULT "lmdb"
392 #else
393 #define DBA_DEFAULT ""
394 #endif
395 /* cdb/cdb_make and ini are no option here */
396 
397 static int le_db;
398 static int le_pdb;
399 /* }}} */
400 
401 /* {{{ dba_fetch_resource
402 PHPAPI void dba_fetch_resource(dba_info **pinfo, zval **id)
403 {
404 	dba_info *info;
405 	DBA_ID_FETCH
406 	*pinfo = info;
407 }
408 */
409 /* }}} */
410 
411 /* {{{ dba_get_handler
412 PHPAPI dba_handler *dba_get_handler(const char* handler_name)
413 {
414 	dba_handler *hptr;
415 	for (hptr = handler; hptr->name && strcasecmp(hptr->name, handler_name); hptr++);
416 	return hptr;
417 }
418 */
419 /* }}} */
420 
421 /* {{{ dba_close
422  */
dba_close(dba_info * info)423 static void dba_close(dba_info *info)
424 {
425 	if (info->hnd) {
426 		info->hnd->close(info);
427 	}
428 	if (info->path) {
429 		pefree(info->path, info->flags&DBA_PERSISTENT);
430 	}
431 	if (info->fp && info->fp != info->lock.fp) {
432 		if (info->flags & DBA_PERSISTENT) {
433 			php_stream_pclose(info->fp);
434 		} else {
435 			php_stream_close(info->fp);
436 		}
437 	}
438 	if (info->lock.fp) {
439 		if (info->flags & DBA_PERSISTENT) {
440 			php_stream_pclose(info->lock.fp);
441 		} else {
442 			php_stream_close(info->lock.fp);
443 		}
444 	}
445 	if (info->lock.name) {
446 		pefree(info->lock.name, info->flags&DBA_PERSISTENT);
447 	}
448 	pefree(info, info->flags&DBA_PERSISTENT);
449 }
450 /* }}} */
451 
452 /* {{{ dba_close_rsrc
453  */
dba_close_rsrc(zend_resource * rsrc)454 static void dba_close_rsrc(zend_resource *rsrc)
455 {
456 	dba_info *info = (dba_info *)rsrc->ptr;
457 
458 	dba_close(info);
459 }
460 /* }}} */
461 
462 /* {{{ dba_close_pe_rsrc_deleter */
dba_close_pe_rsrc_deleter(zval * el,void * pDba)463 int dba_close_pe_rsrc_deleter(zval *el, void *pDba)
464 {
465 	if (Z_RES_P(el)->ptr == pDba) {
466 		if (Z_DELREF_P(el) == 0) {
467 			return ZEND_HASH_APPLY_REMOVE;
468 		} else {
469 			return ZEND_HASH_APPLY_KEEP | ZEND_HASH_APPLY_STOP;
470 		}
471 	} else {
472 		return ZEND_HASH_APPLY_KEEP;
473 	}
474 }
475 /* }}} */
476 
477 /* {{{ dba_close_pe_rsrc */
dba_close_pe_rsrc(zend_resource * rsrc)478 static void dba_close_pe_rsrc(zend_resource *rsrc)
479 {
480 	dba_info *info = (dba_info *)rsrc->ptr;
481 
482 	/* closes the resource by calling dba_close_rsrc() */
483 	zend_hash_apply_with_argument(&EG(persistent_list), dba_close_pe_rsrc_deleter, info);
484 }
485 /* }}} */
486 
487 /* {{{ PHP_INI
488  */
ZEND_INI_MH(OnUpdateDefaultHandler)489 ZEND_INI_MH(OnUpdateDefaultHandler)
490 {
491 	dba_handler *hptr;
492 
493 	if (!ZSTR_LEN(new_value)) {
494 		DBA_G(default_hptr) = NULL;
495 		return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
496 	}
497 
498 	for (hptr = handler; hptr->name && strcasecmp(hptr->name, ZSTR_VAL(new_value)); hptr++);
499 
500 	if (!hptr->name) {
501 		php_error_docref(NULL, E_WARNING, "No such handler: %s", ZSTR_VAL(new_value));
502 		return FAILURE;
503 	}
504 	DBA_G(default_hptr) = hptr;
505 	return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
506 }
507 
508 PHP_INI_BEGIN()
509     STD_PHP_INI_ENTRY("dba.default_handler", DBA_DEFAULT, PHP_INI_ALL, OnUpdateDefaultHandler, default_handler,    zend_dba_globals, dba_globals)
PHP_INI_END()510 PHP_INI_END()
511 /* }}} */
512 
513 /* {{{ PHP_GINIT_FUNCTION
514  */
515 static PHP_GINIT_FUNCTION(dba)
516 {
517 #if defined(COMPILE_DL_DBA) && defined(ZTS)
518 	ZEND_TSRMLS_CACHE_UPDATE();
519 #endif
520 	dba_globals->default_handler = "";
521 	dba_globals->default_hptr    = NULL;
522 }
523 /* }}} */
524 
525 /* {{{ PHP_MINIT_FUNCTION
526  */
PHP_MINIT_FUNCTION(dba)527 PHP_MINIT_FUNCTION(dba)
528 {
529 	REGISTER_INI_ENTRIES();
530 	le_db = zend_register_list_destructors_ex(dba_close_rsrc, NULL, "dba", module_number);
531 	le_pdb = zend_register_list_destructors_ex(dba_close_pe_rsrc, dba_close_rsrc, "dba persistent", module_number);
532 	return SUCCESS;
533 }
534 /* }}} */
535 
536 /* {{{ PHP_MSHUTDOWN_FUNCTION
537  */
PHP_MSHUTDOWN_FUNCTION(dba)538 PHP_MSHUTDOWN_FUNCTION(dba)
539 {
540 	UNREGISTER_INI_ENTRIES();
541 	return SUCCESS;
542 }
543 /* }}} */
544 
545 #include "zend_smart_str.h"
546 
547 /* {{{ PHP_MINFO_FUNCTION
548  */
PHP_MINFO_FUNCTION(dba)549 PHP_MINFO_FUNCTION(dba)
550 {
551 	dba_handler *hptr;
552 	smart_str handlers = {0};
553 
554 	for(hptr = handler; hptr->name; hptr++) {
555 		smart_str_appends(&handlers, hptr->name);
556 		smart_str_appendc(&handlers, ' ');
557  	}
558 
559 	php_info_print_table_start();
560  	php_info_print_table_row(2, "DBA support", "enabled");
561 	if (handlers.s) {
562 		smart_str_0(&handlers);
563 		php_info_print_table_row(2, "Supported handlers", ZSTR_VAL(handlers.s));
564 		smart_str_free(&handlers);
565 	} else {
566 		php_info_print_table_row(2, "Supported handlers", "none");
567 	}
568 	php_info_print_table_end();
569 	DISPLAY_INI_ENTRIES();
570 }
571 /* }}} */
572 
573 /* {{{ php_dba_update
574  */
php_dba_update(INTERNAL_FUNCTION_PARAMETERS,int mode)575 static void php_dba_update(INTERNAL_FUNCTION_PARAMETERS, int mode)
576 {
577 	size_t val_len;
578 	zval *id;
579 	dba_info *info = NULL;
580 	int ac = ZEND_NUM_ARGS();
581 	zval *key;
582 	char *val;
583 	char *key_str, *key_free;
584 	size_t key_len;
585 
586 	if (zend_parse_parameters(ac, "zsr", &key, &val, &val_len, &id) == FAILURE) {
587 		return;
588 	}
589 
590 	if ((key_len = php_dba_make_key(key, &key_str, &key_free)) == 0) {
591 		RETURN_FALSE;
592 	}
593 
594 	DBA_FETCH_RESOURCE_WITH_ID(info, id);
595 
596 	DBA_WRITE_CHECK_WITH_ID;
597 
598 	if (info->hnd->update(info, key_str, key_len, val, val_len, mode) == SUCCESS) {
599 		DBA_ID_DONE;
600 		RETURN_TRUE;
601 	}
602 
603 	DBA_ID_DONE;
604 	RETURN_FALSE;
605 }
606 /* }}} */
607 
608 #define FREENOW if(args) {int i; for (i=0; i<ac; i++) { zval_ptr_dtor(&args[i]); } efree(args);} if(key) efree(key)
609 
610 /* {{{ php_find_dbm
611  */
php_dba_find(const char * path)612 dba_info *php_dba_find(const char* path)
613 {
614 	zend_resource *le;
615 	dba_info *info;
616 	zend_long numitems, i;
617 
618 	numitems = zend_hash_next_free_element(&EG(regular_list));
619 	for (i=1; i<numitems; i++) {
620 		if ((le = zend_hash_index_find_ptr(&EG(regular_list), i)) == NULL) {
621 			continue;
622 		}
623 		if (le->type == le_db || le->type == le_pdb) {
624 			info = (dba_info *)(le->ptr);
625 			if (!strcmp(info->path, path)) {
626 				return (dba_info *)(le->ptr);
627 			}
628 		}
629 	}
630 
631 	return NULL;
632 }
633 /* }}} */
634 
635 /* {{{ php_dba_open
636  */
php_dba_open(INTERNAL_FUNCTION_PARAMETERS,int persistent)637 static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, int persistent)
638 {
639 	zval *args = NULL;
640 	int ac = ZEND_NUM_ARGS();
641 	dba_mode_t modenr;
642 	dba_info *info, *other;
643 	dba_handler *hptr;
644 	char *key = NULL, *error = NULL;
645 	size_t keylen = 0;
646 	int i;
647 	int lock_mode, lock_flag, lock_dbf = 0;
648 	char *file_mode;
649 	char mode[4], *pmode, *lock_file_mode = NULL;
650 	int persistent_flag = persistent ? STREAM_OPEN_PERSISTENT : 0;
651 	zend_string *opened_path = NULL;
652 	char *lock_name;
653 #ifdef PHP_WIN32
654 	zend_bool restarted = 0;
655 	zend_bool need_creation = 0;
656 #endif
657 
658 	if (ac < 2) {
659 		WRONG_PARAM_COUNT;
660 	}
661 
662 	/* we pass additional args to the respective handler */
663 	args = safe_emalloc(ac, sizeof(zval), 0);
664 	if (zend_get_parameters_array_ex(ac, args) != SUCCESS) {
665 		efree(args);
666 		WRONG_PARAM_COUNT;
667 	}
668 
669 	/* we only take string arguments */
670 	for (i = 0; i < ac; i++) {
671 		ZVAL_STR(&args[i], zval_get_string(&args[i]));
672 		keylen += Z_STRLEN(args[i]);
673 	}
674 
675 	/* Exception during string conversion */
676 	if (EG(exception)) {
677 		FREENOW;
678 		return;
679 	}
680 
681 	if (persistent) {
682 		zend_resource *le;
683 
684 		/* calculate hash */
685 		key = safe_emalloc(keylen, 1, 1);
686 		key[keylen] = '\0';
687 		keylen = 0;
688 
689 		for(i = 0; i < ac; i++) {
690 			memcpy(key+keylen, Z_STRVAL(args[i]), Z_STRLEN(args[i]));
691 			keylen += Z_STRLEN(args[i]);
692 		}
693 
694 		/* try to find if we already have this link in our persistent list */
695 		if ((le = zend_hash_str_find_ptr(&EG(persistent_list), key, keylen)) != NULL) {
696 			FREENOW;
697 
698 			if (le->type != le_pdb) {
699 				RETURN_FALSE;
700 			}
701 
702 			info = (dba_info *)le->ptr;
703 
704 			GC_ADDREF(le);
705 			RETURN_RES(zend_register_resource(info, le_pdb));
706 			return;
707 		}
708 	}
709 
710 	if (ac==2) {
711 		hptr = DBA_G(default_hptr);
712 		if (!hptr) {
713 			php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "No default handler selected");
714 			FREENOW;
715 			RETURN_FALSE;
716 		}
717 	} else {
718 		for (hptr = handler; hptr->name && strcasecmp(hptr->name, Z_STRVAL(args[2])); hptr++);
719 	}
720 
721 	if (!hptr->name) {
722 		php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "No such handler: %s", Z_STRVAL(args[2]));
723 		FREENOW;
724 		RETURN_FALSE;
725 	}
726 
727 	/* Check mode: [rwnc][fl]?t?
728 	 * r: Read
729 	 * w: Write
730 	 * n: Create/Truncate
731 	 * c: Create
732 	 *
733 	 * d: force lock on database file
734 	 * l: force lock on lck file
735 	 * -: ignore locking
736 	 *
737 	 * t: test open database, warning if locked
738 	 */
739 	strlcpy(mode, Z_STRVAL(args[1]), sizeof(mode));
740 	pmode = &mode[0];
741 	if (pmode[0] && (pmode[1]=='d' || pmode[1]=='l' || pmode[1]=='-')) { /* force lock on db file or lck file or disable locking */
742 		switch (pmode[1]) {
743 		case 'd':
744 			lock_dbf = 1;
745 			if ((hptr->flags & DBA_LOCK_ALL) == 0) {
746 				lock_flag = (hptr->flags & DBA_LOCK_ALL);
747 				break;
748 			}
749 			/* no break */
750 		case 'l':
751 			lock_flag = DBA_LOCK_ALL;
752 			if ((hptr->flags & DBA_LOCK_ALL) == 0) {
753 				php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_NOTICE, "Handler %s does locking internally", hptr->name);
754 			}
755 			break;
756 		default:
757 		case '-':
758 			if ((hptr->flags & DBA_LOCK_ALL) == 0) {
759 				php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Locking cannot be disabled for handler %s", hptr->name);
760 				FREENOW;
761 				RETURN_FALSE;
762 			}
763 			lock_flag = 0;
764 			break;
765 		}
766 	} else {
767 		lock_flag = (hptr->flags&DBA_LOCK_ALL);
768 		lock_dbf = 1;
769 	}
770 	switch (*pmode++) {
771 		case 'r':
772 			modenr = DBA_READER;
773 			lock_mode = (lock_flag & DBA_LOCK_READER) ? LOCK_SH : 0;
774 			file_mode = "r";
775 			break;
776 		case 'w':
777 			modenr = DBA_WRITER;
778 			lock_mode = (lock_flag & DBA_LOCK_WRITER) ? LOCK_EX : 0;
779 			file_mode = "r+b";
780 			break;
781 		case 'c': {
782 #ifdef PHP_WIN32
783 			if (hptr->flags & (DBA_NO_APPEND|DBA_CAST_AS_FD)) {
784 				php_stream_statbuf ssb;
785 				need_creation = (SUCCESS != php_stream_stat_path(Z_STRVAL(args[0]), &ssb));
786 			}
787 #endif
788 			modenr = DBA_CREAT;
789 			lock_mode = (lock_flag & DBA_LOCK_CREAT) ? LOCK_EX : 0;
790 			if (lock_mode) {
791 				if (lock_dbf) {
792 					/* the create/append check will be done on the lock
793 					 * when the lib opens the file it is already created
794 					 */
795 					file_mode = "r+b";       /* read & write, seek 0 */
796 #ifdef PHP_WIN32
797 					if (!need_creation) {
798 						lock_file_mode = "r+b";
799 					} else
800 #endif
801 					lock_file_mode = "a+b";  /* append */
802 				} else {
803 #ifdef PHP_WIN32
804 					if (!need_creation) {
805 						file_mode = "r+b";
806 					} else
807 #endif
808 					file_mode = "a+b";       /* append */
809 					lock_file_mode = "w+b";  /* create/truncate */
810 				}
811 			} else {
812 #ifdef PHP_WIN32
813 				if (!need_creation) {
814 					file_mode = "r+b";
815 				} else
816 #endif
817 				file_mode = "a+b";
818 			}
819 			/* In case of the 'a+b' append mode, the handler is responsible
820 			 * to handle any rewind problems (see flatfile handler).
821 			 */
822 			break;
823 		}
824 		case 'n':
825 			modenr = DBA_TRUNC;
826 			lock_mode = (lock_flag & DBA_LOCK_TRUNC) ? LOCK_EX : 0;
827 			file_mode = "w+b";
828 			break;
829 		default:
830 			php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Illegal DBA mode");
831 			FREENOW;
832 			RETURN_FALSE;
833 	}
834 	if (!lock_file_mode) {
835 		lock_file_mode = file_mode;
836 	}
837 	if (*pmode=='d' || *pmode=='l' || *pmode=='-') {
838 		pmode++; /* done already - skip here */
839 	}
840 	if (*pmode=='t') {
841 		pmode++;
842 		if (!lock_flag) {
843 			php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "You cannot combine modifiers - (no lock) and t (test lock)");
844 			FREENOW;
845 			RETURN_FALSE;
846 		}
847 		if (!lock_mode) {
848 			if ((hptr->flags & DBA_LOCK_ALL) == 0) {
849 				php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Handler %s uses its own locking which doesn't support mode modifier t (test lock)", hptr->name);
850 				FREENOW;
851 				RETURN_FALSE;
852 			} else {
853 				php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Handler %s doesn't uses locking for this mode which makes modifier t (test lock) obsolete", hptr->name);
854 				FREENOW;
855 				RETURN_FALSE;
856 			}
857 		} else {
858 			lock_mode |= LOCK_NB; /* test =: non blocking */
859 		}
860 	}
861 	if (*pmode) {
862 		php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Illegal DBA mode");
863 		FREENOW;
864 		RETURN_FALSE;
865 	}
866 
867 	info = pemalloc(sizeof(dba_info), persistent);
868 	memset(info, 0, sizeof(dba_info));
869 	info->path = pestrdup(Z_STRVAL(args[0]), persistent);
870 	info->mode = modenr;
871 	info->argc = ac - 3;
872 	info->argv = args + 3;
873 	info->flags = (hptr->flags & ~DBA_LOCK_ALL) | (lock_flag & DBA_LOCK_ALL) | (persistent ? DBA_PERSISTENT : 0);
874 	info->lock.mode = lock_mode;
875 
876 	/* if any open call is a locking call:
877 	 * check if we already habe a locking call open that should block this call
878 	 * the problem is some systems would allow read during write
879 	 */
880 	if (hptr->flags & DBA_LOCK_ALL) {
881 		if ((other = php_dba_find(info->path)) != NULL) {
882 			if (   ( (lock_mode&LOCK_EX)        && (other->lock.mode&(LOCK_EX|LOCK_SH)) )
883 			    || ( (other->lock.mode&LOCK_EX) && (lock_mode&(LOCK_EX|LOCK_SH))        )
884 			   ) {
885 				error = "Unable to establish lock (database file already open)"; /* force failure exit */
886 			}
887 		}
888 	}
889 
890 #ifdef PHP_WIN32
891 restart:
892 #endif
893 	if (!error && lock_mode) {
894 		if (lock_dbf) {
895 			lock_name = Z_STRVAL(args[0]);
896 		} else {
897 			spprintf(&lock_name, 0, "%s.lck", info->path);
898 			if (!strcmp(file_mode, "r")) {
899 				/* when in read only mode try to use existing .lck file first */
900 				/* do not log errors for .lck file while in read only mode on .lck file */
901 				lock_file_mode = "rb";
902 				info->lock.fp = php_stream_open_wrapper(lock_name, lock_file_mode, STREAM_MUST_SEEK|IGNORE_PATH|persistent_flag, &opened_path);
903 			}
904 			if (!info->lock.fp) {
905 				/* when not in read mode or failed to open .lck file read only. now try again in create(write) mode and log errors */
906 				lock_file_mode = "a+b";
907 			} else {
908 				if (opened_path) {
909 					info->lock.name = pestrndup(ZSTR_VAL(opened_path), ZSTR_LEN(opened_path), persistent);
910 					zend_string_release_ex(opened_path, 0);
911 				}
912 			}
913 		}
914 		if (!info->lock.fp) {
915 			info->lock.fp = php_stream_open_wrapper(lock_name, lock_file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|persistent_flag, &opened_path);
916 			if (info->lock.fp) {
917 				if (lock_dbf) {
918 					/* replace the path info with the real path of the opened file */
919 					pefree(info->path, persistent);
920 					info->path = pestrndup(ZSTR_VAL(opened_path), ZSTR_LEN(opened_path), persistent);
921 				}
922 				/* now store the name of the lock */
923 				info->lock.name = pestrndup(ZSTR_VAL(opened_path), ZSTR_LEN(opened_path), persistent);
924 				zend_string_release_ex(opened_path, 0);
925 			}
926 		}
927 		if (!lock_dbf) {
928 			efree(lock_name);
929 		}
930 		if (!info->lock.fp) {
931 			dba_close(info);
932 			/* stream operation already wrote an error message */
933 			FREENOW;
934 			RETURN_FALSE;
935 		}
936 		if (!php_stream_supports_lock(info->lock.fp)) {
937 			error = "Stream does not support locking";
938 		}
939 		if (php_stream_lock(info->lock.fp, lock_mode)) {
940 			error = "Unable to establish lock"; /* force failure exit */
941 		}
942 	}
943 
944 	/* centralised open stream for builtin */
945 	if (!error && (hptr->flags&DBA_STREAM_OPEN)==DBA_STREAM_OPEN) {
946 		if (info->lock.fp && lock_dbf) {
947 			info->fp = info->lock.fp; /* use the same stream for locking and database access */
948 		} else {
949 			info->fp = php_stream_open_wrapper(info->path, file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|persistent_flag, NULL);
950 		}
951 		if (!info->fp) {
952 			dba_close(info);
953 			/* stream operation already wrote an error message */
954 			FREENOW;
955 			RETURN_FALSE;
956 		}
957 		if (hptr->flags & (DBA_NO_APPEND|DBA_CAST_AS_FD)) {
958 			/* Needed because some systems do not allow to write to the original
959 			 * file contents with O_APPEND being set.
960 			 */
961 			if (SUCCESS != php_stream_cast(info->fp, PHP_STREAM_AS_FD, (void*)&info->fd, 1)) {
962 				php_error_docref(NULL, E_WARNING, "Could not cast stream");
963 				dba_close(info);
964 				FREENOW;
965 				RETURN_FALSE;
966 #ifdef F_SETFL
967 			} else if (modenr == DBA_CREAT) {
968 				int flags = fcntl(info->fd, F_GETFL);
969 				fcntl(info->fd, F_SETFL, flags & ~O_APPEND);
970 #elif defined(PHP_WIN32)
971 			} else if (modenr == DBA_CREAT && need_creation && !restarted) {
972 				zend_bool close_both;
973 
974 				close_both = (info->fp != info->lock.fp);
975 				php_stream_free(info->lock.fp, persistent ? PHP_STREAM_FREE_CLOSE_PERSISTENT : PHP_STREAM_FREE_CLOSE);
976 				if (close_both) {
977 					php_stream_free(info->fp, persistent ? PHP_STREAM_FREE_CLOSE_PERSISTENT : PHP_STREAM_FREE_CLOSE);
978 				}
979 				info->fp = NULL;
980 				info->lock.fp = NULL;
981 				info->fd = -1;
982 
983 				pefree(info->lock.name, persistent);
984 
985 				lock_file_mode = "r+b";
986 
987 				restarted = 1;
988 				goto restart;
989 #endif
990 			}
991 		}
992 	}
993 
994 	if (error || hptr->open(info, &error) != SUCCESS) {
995 		dba_close(info);
996 		php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Driver initialization failed for handler: %s%s%s", hptr->name, error?": ":"", error?error:"");
997 		FREENOW;
998 		RETURN_FALSE;
999 	}
1000 
1001 	info->hnd = hptr;
1002 	info->argc = 0;
1003 	info->argv = NULL;
1004 
1005 	if (persistent) {
1006 		if (zend_register_persistent_resource(key, keylen, info, le_pdb) == NULL) {
1007 			dba_close(info);
1008 			php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Could not register persistent resource");
1009 			FREENOW;
1010 			RETURN_FALSE;
1011 		}
1012 	}
1013 
1014 	RETVAL_RES(zend_register_resource(info, (persistent ? le_pdb : le_db)));
1015 	FREENOW;
1016 }
1017 /* }}} */
1018 #undef FREENOW
1019 
1020 /* {{{ proto resource dba_popen(string path, string mode [, string handlername, string ...])
1021    Opens path using the specified handler in mode persistently */
PHP_FUNCTION(dba_popen)1022 PHP_FUNCTION(dba_popen)
1023 {
1024 	php_dba_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1025 }
1026 /* }}} */
1027 
1028 /* {{{ proto resource dba_open(string path, string mode [, string handlername, string ...])
1029    Opens path using the specified handler in mode*/
PHP_FUNCTION(dba_open)1030 PHP_FUNCTION(dba_open)
1031 {
1032 	php_dba_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1033 }
1034 /* }}} */
1035 
1036 /* {{{ proto void dba_close(resource handle)
1037    Closes database */
PHP_FUNCTION(dba_close)1038 PHP_FUNCTION(dba_close)
1039 {
1040 	zval *id;
1041 	dba_info *info = NULL;
1042 
1043 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &id) == FAILURE) {
1044 		return;
1045 	}
1046 
1047 	DBA_FETCH_RESOURCE(info, id);
1048 
1049 	zend_list_close(Z_RES_P(id));
1050 }
1051 /* }}} */
1052 
1053 /* {{{ proto bool dba_exists(string key, resource handle)
1054    Checks, if the specified key exists */
PHP_FUNCTION(dba_exists)1055 PHP_FUNCTION(dba_exists)
1056 {
1057 	DBA_ID_GET2;
1058 
1059 	if(info->hnd->exists(info, key_str, key_len) == SUCCESS) {
1060 		DBA_ID_DONE;
1061 		RETURN_TRUE;
1062 	}
1063 	DBA_ID_DONE;
1064 	RETURN_FALSE;
1065 }
1066 /* }}} */
1067 
1068 /* {{{ proto string dba_fetch(string key, [int skip ,] resource handle)
1069    Fetches the data associated with key */
PHP_FUNCTION(dba_fetch)1070 PHP_FUNCTION(dba_fetch)
1071 {
1072 	char *val;
1073 	size_t len = 0;
1074 	DBA_ID_GET2_3;
1075 
1076 	if (ac==3) {
1077 		if (!strcmp(info->hnd->name, "cdb")) {
1078 			if (skip < 0) {
1079 				php_error_docref(NULL, E_NOTICE, "Handler %s accepts only skip values greater than or equal to zero, using skip=0", info->hnd->name);
1080 				skip = 0;
1081 			}
1082 		} else if (!strcmp(info->hnd->name, "inifile")) {
1083 			/* "-1" is compareable to 0 but allows a non restrictive
1084 			 * access which is fater. For example 'inifile' uses this
1085 			 * to allow faster access when the key was already found
1086 			 * using firstkey/nextkey. However explicitly setting the
1087 			 * value to 0 ensures the first value.
1088 			 */
1089 			if (skip < -1) {
1090 				php_error_docref(NULL, E_NOTICE, "Handler %s accepts only skip value -1 and greater, using skip=0", info->hnd->name);
1091 				skip = 0;
1092 			}
1093 		} else {
1094 			php_error_docref(NULL, E_NOTICE, "Handler %s does not support optional skip parameter, the value will be ignored", info->hnd->name);
1095 			skip = 0;
1096 		}
1097 	} else {
1098 		skip = 0;
1099 	}
1100 	if((val = info->hnd->fetch(info, key_str, key_len, skip, &len)) != NULL) {
1101 		DBA_ID_DONE;
1102 		RETVAL_STRINGL(val, len);
1103 		efree(val);
1104 		return;
1105 	}
1106 	DBA_ID_DONE;
1107 	RETURN_FALSE;
1108 }
1109 /* }}} */
1110 
1111 /* {{{ proto array|false dba_key_split(string key)
1112    Splits an inifile key into an array of the form array(0=>group,1=>value_name) but returns false if input is false or null */
PHP_FUNCTION(dba_key_split)1113 PHP_FUNCTION(dba_key_split)
1114 {
1115 	zval *zkey;
1116 	char *key, *name;
1117 	size_t key_len;
1118 
1119 	if (ZEND_NUM_ARGS() != 1) {
1120 		WRONG_PARAM_COUNT;
1121 	}
1122 	if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "z", &zkey) == SUCCESS) {
1123 		if (Z_TYPE_P(zkey) == IS_NULL || (Z_TYPE_P(zkey) == IS_FALSE)) {
1124 			RETURN_BOOL(0);
1125 		}
1126 	}
1127 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &key, &key_len) == FAILURE) {
1128 		RETURN_BOOL(0);
1129 	}
1130 	array_init(return_value);
1131 	if (key[0] == '[' && (name = strchr(key, ']')) != NULL) {
1132 		add_next_index_stringl(return_value, key+1, name - (key + 1));
1133 		add_next_index_stringl(return_value, name+1, key_len - (name - key + 1));
1134 	} else {
1135 		add_next_index_stringl(return_value, "", 0);
1136 		add_next_index_stringl(return_value, key, key_len);
1137 	}
1138 }
1139 /* }}} */
1140 
1141 /* {{{ proto string dba_firstkey(resource handle)
1142    Resets the internal key pointer and returns the first key */
PHP_FUNCTION(dba_firstkey)1143 PHP_FUNCTION(dba_firstkey)
1144 {
1145 	char *fkey;
1146 	size_t len;
1147 	zval *id;
1148 	dba_info *info = NULL;
1149 
1150 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &id) == FAILURE) {
1151 		return;
1152 	}
1153 
1154 	DBA_FETCH_RESOURCE(info, id);
1155 
1156 	fkey = info->hnd->firstkey(info, &len);
1157 
1158 	if (fkey) {
1159 		RETVAL_STRINGL(fkey, len);
1160 		efree(fkey);
1161 		return;
1162 	}
1163 
1164 	RETURN_FALSE;
1165 }
1166 /* }}} */
1167 
1168 /* {{{ proto string dba_nextkey(resource handle)
1169    Returns the next key */
PHP_FUNCTION(dba_nextkey)1170 PHP_FUNCTION(dba_nextkey)
1171 {
1172 	char *nkey;
1173 	size_t len;
1174 	zval *id;
1175 	dba_info *info = NULL;
1176 
1177 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &id) == FAILURE) {
1178 		return;
1179 	}
1180 
1181 	DBA_FETCH_RESOURCE(info, id);
1182 
1183 	nkey = info->hnd->nextkey(info, &len);
1184 
1185 	if (nkey) {
1186 		RETVAL_STRINGL(nkey, len);
1187 		efree(nkey);
1188 		return;
1189 	}
1190 
1191 	RETURN_FALSE;
1192 }
1193 /* }}} */
1194 
1195 /* {{{ proto bool dba_delete(string key, resource handle)
1196    Deletes the entry associated with key
1197    If inifile: remove all other key lines */
PHP_FUNCTION(dba_delete)1198 PHP_FUNCTION(dba_delete)
1199 {
1200 	DBA_ID_GET2;
1201 
1202 	DBA_WRITE_CHECK_WITH_ID;
1203 
1204 	if(info->hnd->delete(info, key_str, key_len) == SUCCESS)
1205 	{
1206 		DBA_ID_DONE;
1207 		RETURN_TRUE;
1208 	}
1209 	DBA_ID_DONE;
1210 	RETURN_FALSE;
1211 }
1212 /* }}} */
1213 
1214 /* {{{ proto bool dba_insert(string key, string value, resource handle)
1215    If not inifile: Insert value as key, return false, if key exists already
1216    If inifile: Add vakue as key (next instance of key) */
PHP_FUNCTION(dba_insert)1217 PHP_FUNCTION(dba_insert)
1218 {
1219 	php_dba_update(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1220 }
1221 /* }}} */
1222 
1223 /* {{{ proto bool dba_replace(string key, string value, resource handle)
1224    Inserts value as key, replaces key, if key exists already
1225    If inifile: remove all other key lines */
PHP_FUNCTION(dba_replace)1226 PHP_FUNCTION(dba_replace)
1227 {
1228 	php_dba_update(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1229 }
1230 /* }}} */
1231 
1232 /* {{{ proto bool dba_optimize(resource handle)
1233    Optimizes (e.g. clean up, vacuum) database */
PHP_FUNCTION(dba_optimize)1234 PHP_FUNCTION(dba_optimize)
1235 {
1236 	zval *id;
1237 	dba_info *info = NULL;
1238 
1239 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &id) == FAILURE) {
1240 		return;
1241 	}
1242 
1243 	DBA_FETCH_RESOURCE(info, id);
1244 
1245 	DBA_WRITE_CHECK;
1246 
1247 	if (info->hnd->optimize(info) == SUCCESS) {
1248 		RETURN_TRUE;
1249 	}
1250 
1251 	RETURN_FALSE;
1252 }
1253 /* }}} */
1254 
1255 /* {{{ proto bool dba_sync(resource handle)
1256    Synchronizes database */
PHP_FUNCTION(dba_sync)1257 PHP_FUNCTION(dba_sync)
1258 {
1259 	zval *id;
1260 	dba_info *info = NULL;
1261 
1262 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &id) == FAILURE) {
1263 		return;
1264 	}
1265 
1266 	DBA_FETCH_RESOURCE(info, id);
1267 
1268 	if (info->hnd->sync(info) == SUCCESS) {
1269 		RETURN_TRUE;
1270 	}
1271 
1272 	RETURN_FALSE;
1273 }
1274 /* }}} */
1275 
1276 /* {{{ proto array dba_handlers([bool full_info])
1277    List configured database handlers */
PHP_FUNCTION(dba_handlers)1278 PHP_FUNCTION(dba_handlers)
1279 {
1280 	dba_handler *hptr;
1281 	zend_bool full_info = 0;
1282 
1283 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &full_info) == FAILURE) {
1284 		RETURN_FALSE;
1285 	}
1286 
1287 	array_init(return_value);
1288 
1289 	for(hptr = handler; hptr->name; hptr++) {
1290 		if (full_info) {
1291 			// TODO: avoid reallocation ???
1292 			char *str = hptr->info(hptr, NULL);
1293 			add_assoc_string(return_value, hptr->name, str);
1294 			efree(str);
1295 		} else {
1296 			add_next_index_string(return_value, hptr->name);
1297 		}
1298  	}
1299 }
1300 /* }}} */
1301 
1302 /* {{{ proto array dba_list()
1303    List opened databases */
PHP_FUNCTION(dba_list)1304 PHP_FUNCTION(dba_list)
1305 {
1306 	zend_ulong numitems, i;
1307 	zend_resource *le;
1308 	dba_info *info;
1309 
1310 	if (zend_parse_parameters_none() == FAILURE) {
1311 		RETURN_FALSE;
1312 	}
1313 
1314 	array_init(return_value);
1315 
1316 	numitems = zend_hash_next_free_element(&EG(regular_list));
1317 	for (i=1; i<numitems; i++) {
1318 		if ((le = zend_hash_index_find_ptr(&EG(regular_list), i)) == NULL) {
1319 			continue;
1320 		}
1321 		if (le->type == le_db || le->type == le_pdb) {
1322 			info = (dba_info *)(le->ptr);
1323 			add_index_string(return_value, i, info->path);
1324 		}
1325 	}
1326 }
1327 /* }}} */
1328 
1329 #endif /* HAVE_DBA */
1330