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