xref: /PHP-8.4/ext/dba/dba.c (revision d3b0efe9)
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 			// TODO ValueError?
482 			RETURN_FALSE;
483 		}
484 	}
485 
486 	RETVAL_BOOL(info->hnd->update(info, key_str, value, mode) == SUCCESS);
487 	DBA_RELEASE_HT_KEY_CREATION();
488 }
489 /* }}} */
490 
491 /* {{{ php_find_dbm */
php_dba_find(const zend_string * path)492 static dba_info *php_dba_find(const zend_string *path)
493 {
494 	zval *zv;
495 
496 	ZEND_HASH_MAP_FOREACH_VAL(&DBA_G(connections), zv) {
497 		dba_info *info = Z_DBA_INFO_P(zv);
498 		if (info && zend_string_equals(path, info->path)) {
499 			return info;
500 		}
501 	} ZEND_HASH_FOREACH_END();
502 
503 	return NULL;
504 }
505 /* }}} */
506 
php_dba_zend_string_dup_safe(zend_string * s,bool persistent)507 static zend_always_inline zend_string *php_dba_zend_string_dup_safe(zend_string *s, bool persistent)
508 {
509 	if (ZSTR_IS_INTERNED(s) && !persistent) {
510 		return s;
511 	} else {
512 		zend_string *duplicated_str = zend_string_init(ZSTR_VAL(s), ZSTR_LEN(s), persistent);
513 		if (persistent) {
514 			GC_MAKE_PERSISTENT_LOCAL(duplicated_str);
515 		}
516 		return duplicated_str;
517 	}
518 }
519 
520 /* {{{ php_dba_open */
php_dba_open(INTERNAL_FUNCTION_PARAMETERS,bool persistent)521 static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, bool persistent)
522 {
523 	dba_mode_t modenr;
524 	const dba_handler *hptr;
525 	const char *error = NULL;
526 	int lock_mode, lock_flag = 0;
527 	const char *file_mode;
528 	const char *lock_file_mode = NULL;
529 	int persistent_flag = persistent ? STREAM_OPEN_PERSISTENT : 0;
530 	char *lock_name;
531 #ifdef PHP_WIN32
532 	bool restarted = 0;
533 	bool need_creation = 0;
534 #endif
535 
536 	zend_string *path;
537 	zend_string *mode;
538 	zend_string *handler_str = NULL;
539 	zend_long permission = 0644;
540 	zend_long map_size = 0;
541 	zend_long driver_flags = DBA_DEFAULT_DRIVER_FLAGS;
542 	bool is_flags_null = true;
543 
544 	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "PS|S!lll!", &path, &mode, &handler_str,
545 			&permission, &map_size, &driver_flags, &is_flags_null)) {
546 		RETURN_THROWS();
547 	}
548 
549 	if (ZSTR_LEN(path) == 0) {
550 		zend_argument_must_not_be_empty_error(1);
551 		RETURN_THROWS();
552 	}
553 	if (ZSTR_LEN(mode) == 0) {
554 		zend_argument_must_not_be_empty_error(2);
555 		RETURN_THROWS();
556 	}
557 	if (handler_str && ZSTR_LEN(handler_str) == 0) {
558 		zend_argument_must_not_be_empty_error(3);
559 		RETURN_THROWS();
560 	}
561 	// TODO Check Value for permission
562 	if (map_size < 0) {
563 		zend_argument_value_error(5, "must be greater than or equal to 0");
564 		RETURN_THROWS();
565 	}
566 
567 	if (!is_flags_null && driver_flags < 0) {
568 		zend_argument_value_error(6, "must be greater than or equal to 0");
569 		RETURN_THROWS();
570 	}
571 
572 	char *resource_key;
573 	size_t resource_key_len = spprintf(&resource_key, 0,
574 		"dba_%d_%s_%s_%s", persistent, ZSTR_VAL(path), ZSTR_VAL(mode), handler_str ? ZSTR_VAL(handler_str) : ""
575 	);
576 
577 	if (persistent) {
578 		zend_resource *le;
579 
580 		/* try to find if we already have this link in our persistent list */
581 		if ((le = zend_hash_str_find_ptr(&EG(persistent_list), resource_key, resource_key_len)) != NULL) {
582 			if (le->type != le_pdb) {
583 				// TODO This should never happen
584 				efree(resource_key);
585 				RETURN_FALSE;
586 			}
587 
588 			object_init_ex(return_value, dba_connection_ce);
589 			dba_connection *connection = Z_DBA_CONNECTION_P(return_value);
590 			connection->info = (dba_info *)le->ptr;
591 			connection->hash = zend_string_init(resource_key, resource_key_len, persistent);
592 			if (persistent) {
593 				GC_MAKE_PERSISTENT_LOCAL(connection->hash);
594 			}
595 
596 			if (zend_hash_exists(&DBA_G(connections), connection->hash)) {
597 				zend_hash_del(&DBA_G(connections), connection->hash);
598 			}
599 
600 			zend_hash_add_new(&DBA_G(connections), connection->hash, return_value);
601 			efree(resource_key);
602 			return;
603 		}
604 	}
605 
606 	if (!handler_str) {
607 		hptr = DBA_G(default_hptr);
608 		if (!hptr) {
609 			php_error_docref(NULL, E_WARNING, "No default handler selected");
610 			efree(resource_key);
611 			RETURN_FALSE;
612 		}
613 		ZEND_ASSERT(hptr->name);
614 	} else {
615 		/* Loop through global static var handlers to see if such a handler exists */
616 		for (hptr = handler; hptr->name && strcasecmp(hptr->name, ZSTR_VAL(handler_str)); hptr++);
617 
618 		if (!hptr->name) {
619 			php_error_docref(NULL, E_WARNING, "Handler \"%s\" is not available", ZSTR_VAL(handler_str));
620 			efree(resource_key);
621 			RETURN_FALSE;
622 		}
623 	}
624 
625 	/* Check mode: [rwnc][dl-]?t?
626 	 * r: Read
627 	 * w: Write
628 	 * n: Create/Truncate
629 	 * c: Create
630 	 *
631 	 * d: force lock on database file
632 	 * l: force lock on lck file
633 	 * -: ignore locking
634 	 *
635 	 * t: test open database, warning if locked
636 	 */
637 	bool is_test_lock = false;
638 	bool is_db_lock = false;
639 	bool is_lock_ignored = false;
640 	// bool is_file_lock = false;
641 
642 	if (ZSTR_LEN(mode) > 3) {
643 		zend_argument_value_error(2, "must be at most 3 characters");
644 		efree(resource_key);
645 		RETURN_THROWS();
646 	}
647 	if (ZSTR_LEN(mode) == 3) {
648 		if (ZSTR_VAL(mode)[2] != 't') {
649 			zend_argument_value_error(2, "third character must be \"t\"");
650 			efree(resource_key);
651 			RETURN_THROWS();
652 		}
653 		is_test_lock = true;
654 	}
655 	if (ZSTR_LEN(mode) >= 2) {
656 		switch (ZSTR_VAL(mode)[1]) {
657 			case 't':
658 				is_test_lock = true;
659 				break;
660 			case '-':
661 				if ((hptr->flags & DBA_LOCK_ALL) == 0) {
662 					php_error_docref(NULL, E_WARNING, "Locking cannot be disabled for handler %s", hptr->name);
663 					efree(resource_key);
664 					RETURN_FALSE;
665 				}
666 				is_lock_ignored = true;
667 				lock_flag = 0;
668 			break;
669 			case 'd':
670 				is_db_lock = true;
671 				if ((hptr->flags & DBA_LOCK_ALL) == 0) {
672 					lock_flag = (hptr->flags & DBA_LOCK_ALL);
673 					break;
674 				}
675 				ZEND_FALLTHROUGH;
676 			case 'l':
677 				// is_file_lock = true;
678 				lock_flag = DBA_LOCK_ALL;
679 				if ((hptr->flags & DBA_LOCK_ALL) == 0) {
680 					php_error_docref(NULL, E_NOTICE, "Handler %s does locking internally", hptr->name);
681 				}
682 				break;
683 			default:
684 				zend_argument_value_error(2, "second character must be one of \"d\", \"l\", \"-\", or \"t\"");
685 				efree(resource_key);
686 				RETURN_THROWS();
687 		}
688 	} else {
689 		lock_flag = (hptr->flags&DBA_LOCK_ALL);
690 		is_db_lock = true;
691 	}
692 
693 	switch (ZSTR_VAL(mode)[0]) {
694 		case 'r':
695 			modenr = DBA_READER;
696 			lock_mode = (lock_flag & DBA_LOCK_READER) ? LOCK_SH : 0;
697 			file_mode = "r";
698 			break;
699 		case 'w':
700 			modenr = DBA_WRITER;
701 			lock_mode = (lock_flag & DBA_LOCK_WRITER) ? LOCK_EX : 0;
702 			file_mode = "r+b";
703 			break;
704 		case 'c': {
705 #ifdef PHP_WIN32
706 			if (hptr->flags & (DBA_NO_APPEND|DBA_CAST_AS_FD)) {
707 				php_stream_statbuf ssb;
708 				need_creation = (SUCCESS != php_stream_stat_path(ZSTR_VAL(path), &ssb));
709 			}
710 #endif
711 			modenr = DBA_CREAT;
712 			lock_mode = (lock_flag & DBA_LOCK_CREAT) ? LOCK_EX : 0;
713 			if (lock_mode) {
714 				if (is_db_lock) {
715 					/* the create/append check will be done on the lock
716 					 * when the lib opens the file it is already created
717 					 */
718 					file_mode = "r+b";       /* read & write, seek 0 */
719 #ifdef PHP_WIN32
720 					if (!need_creation) {
721 						lock_file_mode = "r+b";
722 					} else
723 #endif
724 					lock_file_mode = "a+b";  /* append */
725 				} else {
726 #ifdef PHP_WIN32
727 					if (!need_creation) {
728 						file_mode = "r+b";
729 					} else
730 #endif
731 					file_mode = "a+b";       /* append */
732 					lock_file_mode = "w+b";  /* create/truncate */
733 				}
734 			} else {
735 #ifdef PHP_WIN32
736 				if (!need_creation) {
737 					file_mode = "r+b";
738 				} else
739 #endif
740 				file_mode = "a+b";
741 			}
742 			/* In case of the 'a+b' append mode, the handler is responsible
743 			 * to handle any rewind problems (see flatfile handler).
744 			 */
745 			break;
746 		}
747 		case 'n':
748 			modenr = DBA_TRUNC;
749 			lock_mode = (lock_flag & DBA_LOCK_TRUNC) ? LOCK_EX : 0;
750 			file_mode = "w+b";
751 			break;
752 		default:
753 			zend_argument_value_error(2, "first character must be one of \"r\", \"w\", \"c\", or \"n\"");
754 			efree(resource_key);
755 			RETURN_THROWS();
756 	}
757 	if (!lock_file_mode) {
758 		lock_file_mode = file_mode;
759 	}
760 	if (is_test_lock) {
761 		if (is_lock_ignored) {
762 			zend_argument_value_error(2, "cannot combine mode \"-\" (no lock) and \"t\" (test lock)");
763 			efree(resource_key);
764 			RETURN_THROWS();
765 		}
766 		if (!lock_mode) {
767 			if ((hptr->flags & DBA_LOCK_ALL) == 0) {
768 				php_error_docref(NULL, E_WARNING, "Handler %s uses its own locking which doesn't support mode modifier t (test lock)", hptr->name);
769 				efree(resource_key);
770 				RETURN_FALSE;
771 			} else {
772 				php_error_docref(NULL, E_WARNING, "Handler %s doesn't uses locking for this mode which makes modifier t (test lock) obsolete", hptr->name);
773 				efree(resource_key);
774 				RETURN_FALSE;
775 			}
776 		} else {
777 			lock_mode |= LOCK_NB; /* test =: non blocking */
778 		}
779 	}
780 
781 	zval *connection_zval;
782 	dba_connection *connection;
783 	if ((connection_zval = zend_hash_str_find(&DBA_G(connections), resource_key, resource_key_len)) == NULL) {
784 		object_init_ex(return_value, dba_connection_ce);
785 		connection = Z_DBA_CONNECTION_P(return_value);
786 
787 		connection->info = pecalloc(1, sizeof(dba_info), persistent);
788 		connection->info->path = php_dba_zend_string_dup_safe(path, persistent);
789 		connection->info->mode = modenr;
790 		connection->info->file_permission = permission;
791 		connection->info->map_size = map_size;
792 		connection->info->driver_flags = driver_flags;
793 		connection->info->flags = (hptr->flags & ~DBA_LOCK_ALL) | (lock_flag & DBA_LOCK_ALL) | (persistent ? DBA_PERSISTENT : 0);
794 		connection->info->lock.mode = lock_mode;
795 		connection->hash = zend_string_init(resource_key, resource_key_len, persistent);
796 		if (persistent) {
797 			GC_MAKE_PERSISTENT_LOCAL(connection->hash);
798 		}
799 	} else {
800 		ZVAL_COPY(return_value, connection_zval);
801 		connection = Z_DBA_CONNECTION_P(return_value);
802 	}
803 
804 	/* if any open call is a locking call:
805 	 * check if we already have a locking call open that should block this call
806 	 * the problem is some systems would allow read during write
807 	 */
808 	if (hptr->flags & DBA_LOCK_ALL) {
809 		dba_info *other;
810 		if ((other = php_dba_find(connection->info->path)) != NULL) {
811 			if (   ( (lock_mode&LOCK_EX)        && (other->lock.mode&(LOCK_EX|LOCK_SH)) )
812 			    || ( (other->lock.mode&LOCK_EX) && (lock_mode&(LOCK_EX|LOCK_SH))        )
813 			   ) {
814 				error = "Unable to establish lock (database file already open)"; /* force failure exit */
815 			}
816 		}
817 	}
818 
819 #ifdef PHP_WIN32
820 restart:
821 #endif
822 	if (!error && lock_mode) {
823 		if (is_db_lock) {
824 			lock_name = ZSTR_VAL(path);
825 		} else {
826 			spprintf(&lock_name, 0, "%s.lck", ZSTR_VAL(connection->info->path));
827 			if (!strcmp(file_mode, "r")) {
828 				zend_string *opened_path = NULL;
829 				/* when in read only mode try to use existing .lck file first */
830 				/* do not log errors for .lck file while in read only mode on .lck file */
831 				lock_file_mode = "rb";
832 				connection->info->lock.fp = php_stream_open_wrapper(lock_name, lock_file_mode, STREAM_MUST_SEEK|IGNORE_PATH|persistent_flag, &opened_path);
833 				if (opened_path) {
834 					zend_string_release_ex(opened_path, 0);
835 				}
836 			}
837 			if (!connection->info->lock.fp) {
838 				/* when not in read mode or failed to open .lck file read only. now try again in create(write) mode and log errors */
839 				lock_file_mode = "a+b";
840 			}
841 		}
842 		if (!connection->info->lock.fp) {
843 			zend_string *opened_path = NULL;
844 			connection->info->lock.fp = php_stream_open_wrapper(lock_name, lock_file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|persistent_flag, &opened_path);
845 			if (connection->info->lock.fp) {
846 				if (is_db_lock) {
847 					if (opened_path) {
848 						/* replace the path info with the real path of the opened file */
849 						zend_string_release_ex(connection->info->path, persistent);
850 						connection->info->path = php_dba_zend_string_dup_safe(opened_path, persistent);
851 					} else {
852 						error = "Unable to determine path for locking";
853 					}
854 				}
855 			}
856 			if (opened_path) {
857 				zend_string_release_ex(opened_path, 0);
858 				opened_path = NULL;
859 			}
860 		}
861 		if (!is_db_lock) {
862 			efree(lock_name);
863 		}
864 		if (!connection->info->lock.fp) {
865 			/* stream operation already wrote an error message */
866 			efree(resource_key);
867 			zval_ptr_dtor(return_value);
868 			RETURN_FALSE;
869 		}
870 		if (!error && !php_stream_supports_lock(connection->info->lock.fp)) {
871 			error = "Stream does not support locking";
872 		}
873 		if (!error && php_stream_lock(connection->info->lock.fp, lock_mode)) {
874 			error = "Unable to establish lock"; /* force failure exit */
875 		}
876 	}
877 
878 	/* centralised open stream for builtin */
879 	if (!error && (hptr->flags&DBA_STREAM_OPEN)==DBA_STREAM_OPEN) {
880 		if (connection->info->lock.fp && is_db_lock) {
881 			connection->info->fp = connection->info->lock.fp; /* use the same stream for locking and database access */
882 		} else {
883 			connection->info->fp = php_stream_open_wrapper(ZSTR_VAL(connection->info->path), file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|persistent_flag, NULL);
884 		}
885 		if (!connection->info->fp) {
886 			/* stream operation already wrote an error message */
887 			efree(resource_key);
888 			zval_ptr_dtor(return_value);
889 			RETURN_FALSE;
890 		}
891 		if (hptr->flags & (DBA_NO_APPEND|DBA_CAST_AS_FD)) {
892 			/* Needed because some systems do not allow to write to the original
893 			 * file contents with O_APPEND being set.
894 			 */
895 			if (SUCCESS != php_stream_cast(connection->info->fp, PHP_STREAM_AS_FD, (void*)&connection->info->fd, 1)) {
896 				php_error_docref(NULL, E_WARNING, "Could not cast stream");
897 				efree(resource_key);
898 				zval_ptr_dtor(return_value);
899 				RETURN_FALSE;
900 #ifdef F_SETFL
901 			} else if (modenr == DBA_CREAT) {
902 				int flags = fcntl(connection->info->fd, F_GETFL);
903 				fcntl(connection->info->fd, F_SETFL, flags & ~O_APPEND);
904 #elif defined(PHP_WIN32)
905 			} else if (modenr == DBA_CREAT && need_creation && !restarted) {
906 				if (connection->info->lock.fp != NULL) {
907 					php_stream_free(connection->info->lock.fp, persistent ? PHP_STREAM_FREE_CLOSE_PERSISTENT : PHP_STREAM_FREE_CLOSE);
908 				}
909 				if (connection->info->fp != connection->info->lock.fp) {
910 					php_stream_free(connection->info->fp, persistent ? PHP_STREAM_FREE_CLOSE_PERSISTENT : PHP_STREAM_FREE_CLOSE);
911 				}
912 				connection->info->fp = NULL;
913 				connection->info->lock.fp = NULL;
914 				connection->info->fd = -1;
915 
916 				lock_file_mode = "r+b";
917 
918 				restarted = 1;
919 				goto restart;
920 #endif
921 			}
922 		}
923 	}
924 
925 	if (error || hptr->open(connection->info, &error) == FAILURE) {
926 		if (EXPECTED(!EG(exception))) {
927 			if (error) {
928 				php_error_docref(NULL, E_WARNING, "Driver initialization failed for handler: %s: %s", hptr->name, error);
929 			} else {
930 				php_error_docref(NULL, E_WARNING, "Driver initialization failed for handler: %s", hptr->name);
931 			}
932 		}
933 		efree(resource_key);
934 		zval_ptr_dtor(return_value);
935 		RETURN_FALSE;
936 	}
937 
938 	connection->info->hnd = hptr;
939 
940 	if (persistent) {
941 		if (zend_register_persistent_resource(resource_key, resource_key_len, connection->info, le_pdb) == NULL) {
942 			php_error_docref(NULL, E_WARNING, "Could not register persistent resource");
943 			efree(resource_key);
944 			zval_ptr_dtor(return_value);
945 			RETURN_FALSE;
946 		}
947 	}
948 
949 	zend_hash_add_new(&DBA_G(connections), connection->hash, return_value);
950 	efree(resource_key);
951 }
952 /* }}} */
953 
954 /* {{{ Opens path using the specified handler in mode persistently */
PHP_FUNCTION(dba_popen)955 PHP_FUNCTION(dba_popen)
956 {
957 	php_dba_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
958 }
959 /* }}} */
960 
961 /* {{{ Opens path using the specified handler in mode*/
PHP_FUNCTION(dba_open)962 PHP_FUNCTION(dba_open)
963 {
964 	php_dba_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
965 }
966 /* }}} */
967 
968 /* {{{ Closes database */
PHP_FUNCTION(dba_close)969 PHP_FUNCTION(dba_close)
970 {
971 	zval *id;
972 	dba_connection *connection = NULL;
973 
974 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &id, dba_connection_ce) == FAILURE) {
975 		RETURN_THROWS();
976 	}
977 
978 	connection = Z_DBA_CONNECTION_P(id);
979 	CHECK_DBA_CONNECTION(connection->info);
980 
981 	bool persistent = connection->info->flags & DBA_PERSISTENT;
982 
983 	dba_close_connection(connection);
984 
985 	if (persistent) {
986 		zend_hash_apply_with_argument(&EG(persistent_list), remove_pconnection_from_list, (void *) connection->info);
987 	}
988 }
989 /* }}} */
990 
991 /* {{{ Checks, if the specified key exists */
PHP_FUNCTION(dba_exists)992 PHP_FUNCTION(dba_exists)
993 {
994 	zval *id;
995 	dba_info *info = NULL;
996 	HashTable *key_ht = NULL;
997 	zend_string *key_str = NULL;
998 
999 	ZEND_PARSE_PARAMETERS_START(2, 2)
1000 		Z_PARAM_ARRAY_HT_OR_STR(key_ht, key_str)
1001 		Z_PARAM_OBJECT_OF_CLASS(id, dba_connection_ce);
1002 	ZEND_PARSE_PARAMETERS_END();
1003 
1004 	info = Z_DBA_INFO_P(id);
1005 	CHECK_DBA_CONNECTION(info);
1006 
1007 	if (key_ht) {
1008 		key_str = php_dba_make_key(key_ht);
1009 		if (!key_str) {
1010 			// TODO ValueError?
1011 			RETURN_FALSE;
1012 		}
1013 	}
1014 
1015 	RETVAL_BOOL(info->hnd->exists(info, key_str) == SUCCESS);
1016 	DBA_RELEASE_HT_KEY_CREATION();
1017 }
1018 /* }}} */
1019 
1020 /* {{{ Fetches the data associated with key */
PHP_FUNCTION(dba_fetch)1021 PHP_FUNCTION(dba_fetch)
1022 {
1023 	zval *id;
1024 	dba_info *info = NULL;
1025 	HashTable *key_ht = NULL;
1026 	zend_string *key_str = NULL;
1027 	zend_long skip = 0;
1028 
1029 	/* Check for legacy signature */
1030 	if (ZEND_NUM_ARGS() == 3) {
1031 		ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_QUIET, 3, 3)
1032 			Z_PARAM_ARRAY_HT_OR_STR(key_ht, key_str)
1033 			Z_PARAM_LONG(skip)
1034 			Z_PARAM_OBJECT_OF_CLASS(id, dba_connection_ce);
1035 		ZEND_PARSE_PARAMETERS_END_EX(goto standard;);
1036 
1037 		zend_error(E_DEPRECATED, "Calling dba_fetch() with $dba at the 3rd parameter is deprecated");
1038 		if (UNEXPECTED(EG(exception))) {
1039 			RETURN_THROWS();
1040 		}
1041 	} else {
1042 		standard:
1043 		ZEND_PARSE_PARAMETERS_START(2, 3)
1044 			Z_PARAM_ARRAY_HT_OR_STR(key_ht, key_str)
1045 			Z_PARAM_OBJECT_OF_CLASS(id, dba_connection_ce);
1046 			Z_PARAM_OPTIONAL
1047 			Z_PARAM_LONG(skip)
1048 		ZEND_PARSE_PARAMETERS_END();
1049 	}
1050 
1051 	info = Z_DBA_INFO_P(id);
1052 	CHECK_DBA_CONNECTION(info);
1053 
1054 	if (key_ht) {
1055 		key_str = php_dba_make_key(key_ht);
1056 		if (!key_str) {
1057 			// TODO ValueError?
1058 			RETURN_FALSE;
1059 		}
1060 	}
1061 
1062 	if (skip != 0) {
1063 		if (!strcmp(info->hnd->name, "cdb")) {
1064 			// TODO ValueError?
1065 			if (skip < 0) {
1066 				php_error_docref(NULL, E_NOTICE, "Handler %s accepts only skip values greater than or equal to zero, using skip=0", info->hnd->name);
1067 				skip = 0;
1068 			}
1069 		} else if (!strcmp(info->hnd->name, "inifile")) {
1070 			/* "-1" is comparable to 0 but allows a non restrictive
1071 			 * access which is faster. For example 'inifile' uses this
1072 			 * to allow faster access when the key was already found
1073 			 * using firstkey/nextkey. However explicitly setting the
1074 			 * value to 0 ensures the first value.
1075 			 */
1076 			if (skip < -1) {
1077 				// TODO ValueError?
1078 				php_error_docref(NULL, E_NOTICE, "Handler %s accepts only skip value -1 and greater, using skip=0", info->hnd->name);
1079 				skip = 0;
1080 			}
1081 		} else {
1082 			php_error_docref(NULL, E_NOTICE, "Handler %s does not support optional skip parameter, the value will be ignored", info->hnd->name);
1083 			skip = 0;
1084 		}
1085 	}
1086 
1087 	zend_string *val;
1088 	if ((val = info->hnd->fetch(info, key_str, skip)) == NULL) {
1089 		DBA_RELEASE_HT_KEY_CREATION();
1090 		RETURN_FALSE;
1091 	}
1092 	DBA_RELEASE_HT_KEY_CREATION();
1093 	RETURN_STR(val);
1094 }
1095 /* }}} */
1096 
1097 /* {{{ 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)1098 PHP_FUNCTION(dba_key_split)
1099 {
1100 	zval *zkey;
1101 	char *key, *name;
1102 	size_t key_len;
1103 
1104 	if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "z", &zkey) == SUCCESS) {
1105 		if (Z_TYPE_P(zkey) == IS_NULL || (Z_TYPE_P(zkey) == IS_FALSE)) {
1106 			php_error_docref(NULL, E_DEPRECATED, "Passing false or null is deprecated since 8.4");
1107 			RETURN_FALSE;
1108 		}
1109 	}
1110 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &key, &key_len) == FAILURE) {
1111 		RETURN_THROWS();
1112 	}
1113 	array_init(return_value);
1114 	if (key[0] == '[' && (name = strchr(key, ']')) != NULL) {
1115 		add_next_index_stringl(return_value, key+1, name - (key + 1));
1116 		add_next_index_stringl(return_value, name+1, key_len - (name - key + 1));
1117 	} else {
1118 		add_next_index_stringl(return_value, "", 0);
1119 		add_next_index_stringl(return_value, key, key_len);
1120 	}
1121 }
1122 /* }}} */
1123 
1124 /* {{{ Resets the internal key pointer and returns the first key */
PHP_FUNCTION(dba_firstkey)1125 PHP_FUNCTION(dba_firstkey)
1126 {
1127 	zval *id;
1128 	dba_info *info = NULL;
1129 
1130 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &id, dba_connection_ce) == FAILURE) {
1131 		RETURN_THROWS();
1132 	}
1133 
1134 	info = Z_DBA_INFO_P(id);
1135 	CHECK_DBA_CONNECTION(info);
1136 
1137 	zend_string *fkey = info->hnd->firstkey(info);
1138 
1139 	if (fkey) {
1140 		RETURN_STR(fkey);
1141 	}
1142 
1143 	RETURN_FALSE;
1144 }
1145 /* }}} */
1146 
1147 /* {{{ Returns the next key */
PHP_FUNCTION(dba_nextkey)1148 PHP_FUNCTION(dba_nextkey)
1149 {
1150 	zval *id;
1151 	dba_info *info = NULL;
1152 
1153 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &id, dba_connection_ce) == FAILURE) {
1154 		RETURN_THROWS();
1155 	}
1156 
1157 	info = Z_DBA_INFO_P(id);
1158 	CHECK_DBA_CONNECTION(info);
1159 
1160 	zend_string *nkey = info->hnd->nextkey(info);
1161 
1162 	if (nkey) {
1163 		RETURN_STR(nkey);
1164 	}
1165 
1166 	RETURN_FALSE;
1167 }
1168 /* }}} */
1169 
1170 /* {{{ Deletes the entry associated with key
1171    If inifile: remove all other key lines */
PHP_FUNCTION(dba_delete)1172 PHP_FUNCTION(dba_delete)
1173 {
1174 	zval *id;
1175 	dba_info *info = NULL;
1176 	HashTable *key_ht = NULL;
1177 	zend_string *key_str = NULL;
1178 
1179 	ZEND_PARSE_PARAMETERS_START(2, 2)
1180 		Z_PARAM_ARRAY_HT_OR_STR(key_ht, key_str)
1181 		Z_PARAM_OBJECT_OF_CLASS(id, dba_connection_ce);
1182 	ZEND_PARSE_PARAMETERS_END();
1183 
1184 	info = Z_DBA_INFO_P(id);
1185 	CHECK_DBA_CONNECTION(info);
1186 	DBA_WRITE_CHECK(info);
1187 
1188 	if (key_ht) {
1189 		key_str = php_dba_make_key(key_ht);
1190 		if (!key_str) {
1191 			// TODO ValueError?
1192 			RETURN_FALSE;
1193 		}
1194 	}
1195 
1196 	RETVAL_BOOL(info->hnd->delete(info, key_str) == SUCCESS);
1197 	DBA_RELEASE_HT_KEY_CREATION();
1198 }
1199 /* }}} */
1200 
1201 /* {{{ If not inifile: Insert value as key, return false, if key exists already
1202    If inifile: Add vakue as key (next instance of key) */
PHP_FUNCTION(dba_insert)1203 PHP_FUNCTION(dba_insert)
1204 {
1205 	php_dba_update(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1206 }
1207 /* }}} */
1208 
1209 /* {{{ Inserts value as key, replaces key, if key exists already
1210    If inifile: remove all other key lines */
PHP_FUNCTION(dba_replace)1211 PHP_FUNCTION(dba_replace)
1212 {
1213 	php_dba_update(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1214 }
1215 /* }}} */
1216 
1217 /* {{{ Optimizes (e.g. clean up, vacuum) database */
PHP_FUNCTION(dba_optimize)1218 PHP_FUNCTION(dba_optimize)
1219 {
1220 	zval *id;
1221 	dba_info *info = NULL;
1222 
1223 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &id, dba_connection_ce) == FAILURE) {
1224 		RETURN_THROWS();
1225 	}
1226 
1227 	info = Z_DBA_INFO_P(id);
1228 	CHECK_DBA_CONNECTION(info);
1229 	DBA_WRITE_CHECK(info);
1230 
1231 	if (info->hnd->optimize(info) == SUCCESS) {
1232 		RETURN_TRUE;
1233 	}
1234 
1235 	RETURN_FALSE;
1236 }
1237 /* }}} */
1238 
1239 /* {{{ Synchronizes database */
PHP_FUNCTION(dba_sync)1240 PHP_FUNCTION(dba_sync)
1241 {
1242 	zval *id;
1243 	dba_info *info = NULL;
1244 
1245 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &id, dba_connection_ce) == FAILURE) {
1246 		RETURN_THROWS();
1247 	}
1248 
1249 	info = Z_DBA_INFO_P(id);
1250 	CHECK_DBA_CONNECTION(info);
1251 
1252 	if (info->hnd->sync(info) == SUCCESS) {
1253 		RETURN_TRUE;
1254 	}
1255 
1256 	RETURN_FALSE;
1257 }
1258 /* }}} */
1259 
1260 /* {{{ List configured database handlers */
PHP_FUNCTION(dba_handlers)1261 PHP_FUNCTION(dba_handlers)
1262 {
1263 	const dba_handler *hptr;
1264 	bool full_info = 0;
1265 
1266 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &full_info) == FAILURE) {
1267 		RETURN_THROWS();
1268 	}
1269 
1270 	array_init(return_value);
1271 
1272 	for(hptr = handler; hptr->name; hptr++) {
1273 		if (full_info) {
1274 			// TODO: avoid reallocation ???
1275 			char *str = hptr->info(hptr, NULL);
1276 			add_assoc_string(return_value, hptr->name, str);
1277 			efree(str);
1278 		} else {
1279 			add_next_index_string(return_value, hptr->name);
1280 		}
1281 	}
1282 }
1283 /* }}} */
1284 
1285 /* {{{ List opened databases */
PHP_FUNCTION(dba_list)1286 PHP_FUNCTION(dba_list)
1287 {
1288 	if (zend_parse_parameters_none() == FAILURE) {
1289 		RETURN_THROWS();
1290 	}
1291 
1292 	array_init(return_value);
1293 
1294 	zval *zv;
1295 	ZEND_HASH_MAP_FOREACH_VAL(&DBA_G(connections), zv) {
1296 		dba_info *info = Z_DBA_INFO_P(zv);
1297 		if (info) {
1298 			add_next_index_str(return_value, zend_string_copy(info->path));
1299 		}
1300 	} ZEND_HASH_FOREACH_END();
1301 }
1302 /* }}} */
1303 
1304 #endif /* HAVE_DBA */
1305