xref: /PHP-8.4/ext/dba/dba.c (revision 98dc77f6)
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 					ZEND_ASSERT(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 				}
852 			}
853 			if (opened_path) {
854 				zend_string_release_ex(opened_path, 0);
855 				opened_path = NULL;
856 			}
857 		}
858 		if (!is_db_lock) {
859 			efree(lock_name);
860 		}
861 		if (!connection->info->lock.fp) {
862 			/* stream operation already wrote an error message */
863 			efree(resource_key);
864 			zval_ptr_dtor(return_value);
865 			RETURN_FALSE;
866 		}
867 		if (!php_stream_supports_lock(connection->info->lock.fp)) {
868 			error = "Stream does not support locking";
869 		}
870 		if (php_stream_lock(connection->info->lock.fp, lock_mode)) {
871 			error = "Unable to establish lock"; /* force failure exit */
872 		}
873 	}
874 
875 	/* centralised open stream for builtin */
876 	if (!error && (hptr->flags&DBA_STREAM_OPEN)==DBA_STREAM_OPEN) {
877 		if (connection->info->lock.fp && is_db_lock) {
878 			connection->info->fp = connection->info->lock.fp; /* use the same stream for locking and database access */
879 		} else {
880 			connection->info->fp = php_stream_open_wrapper(ZSTR_VAL(connection->info->path), file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|persistent_flag, NULL);
881 		}
882 		if (!connection->info->fp) {
883 			/* stream operation already wrote an error message */
884 			efree(resource_key);
885 			zval_ptr_dtor(return_value);
886 			RETURN_FALSE;
887 		}
888 		if (hptr->flags & (DBA_NO_APPEND|DBA_CAST_AS_FD)) {
889 			/* Needed because some systems do not allow to write to the original
890 			 * file contents with O_APPEND being set.
891 			 */
892 			if (SUCCESS != php_stream_cast(connection->info->fp, PHP_STREAM_AS_FD, (void*)&connection->info->fd, 1)) {
893 				php_error_docref(NULL, E_WARNING, "Could not cast stream");
894 				efree(resource_key);
895 				zval_ptr_dtor(return_value);
896 				RETURN_FALSE;
897 #ifdef F_SETFL
898 			} else if (modenr == DBA_CREAT) {
899 				int flags = fcntl(connection->info->fd, F_GETFL);
900 				fcntl(connection->info->fd, F_SETFL, flags & ~O_APPEND);
901 #elif defined(PHP_WIN32)
902 			} else if (modenr == DBA_CREAT && need_creation && !restarted) {
903 				if (connection->info->lock.fp != NULL) {
904 					php_stream_free(connection->info->lock.fp, persistent ? PHP_STREAM_FREE_CLOSE_PERSISTENT : PHP_STREAM_FREE_CLOSE);
905 				}
906 				if (connection->info->fp != connection->info->lock.fp) {
907 					php_stream_free(connection->info->fp, persistent ? PHP_STREAM_FREE_CLOSE_PERSISTENT : PHP_STREAM_FREE_CLOSE);
908 				}
909 				connection->info->fp = NULL;
910 				connection->info->lock.fp = NULL;
911 				connection->info->fd = -1;
912 
913 				lock_file_mode = "r+b";
914 
915 				restarted = 1;
916 				goto restart;
917 #endif
918 			}
919 		}
920 	}
921 
922 	if (error || hptr->open(connection->info, &error) == FAILURE) {
923 		if (EXPECTED(!EG(exception))) {
924 			if (error) {
925 				php_error_docref(NULL, E_WARNING, "Driver initialization failed for handler: %s: %s", hptr->name, error);
926 			} else {
927 				php_error_docref(NULL, E_WARNING, "Driver initialization failed for handler: %s", hptr->name);
928 			}
929 		}
930 		efree(resource_key);
931 		zval_ptr_dtor(return_value);
932 		RETURN_FALSE;
933 	}
934 
935 	connection->info->hnd = hptr;
936 
937 	if (persistent) {
938 		if (zend_register_persistent_resource(resource_key, resource_key_len, connection->info, le_pdb) == NULL) {
939 			php_error_docref(NULL, E_WARNING, "Could not register persistent resource");
940 			efree(resource_key);
941 			zval_ptr_dtor(return_value);
942 			RETURN_FALSE;
943 		}
944 	}
945 
946 	zend_hash_add_new(&DBA_G(connections), connection->hash, return_value);
947 	efree(resource_key);
948 }
949 /* }}} */
950 
951 /* {{{ Opens path using the specified handler in mode persistently */
PHP_FUNCTION(dba_popen)952 PHP_FUNCTION(dba_popen)
953 {
954 	php_dba_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
955 }
956 /* }}} */
957 
958 /* {{{ Opens path using the specified handler in mode*/
PHP_FUNCTION(dba_open)959 PHP_FUNCTION(dba_open)
960 {
961 	php_dba_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
962 }
963 /* }}} */
964 
965 /* {{{ Closes database */
PHP_FUNCTION(dba_close)966 PHP_FUNCTION(dba_close)
967 {
968 	zval *id;
969 	dba_connection *connection = NULL;
970 
971 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &id, dba_connection_ce) == FAILURE) {
972 		RETURN_THROWS();
973 	}
974 
975 	connection = Z_DBA_CONNECTION_P(id);
976 	CHECK_DBA_CONNECTION(connection->info);
977 
978 	bool persistent = connection->info->flags & DBA_PERSISTENT;
979 
980 	dba_close_connection(connection);
981 
982 	if (persistent) {
983 		zend_hash_apply_with_argument(&EG(persistent_list), remove_pconnection_from_list, (void *) connection->info);
984 	}
985 }
986 /* }}} */
987 
988 /* {{{ Checks, if the specified key exists */
PHP_FUNCTION(dba_exists)989 PHP_FUNCTION(dba_exists)
990 {
991 	zval *id;
992 	dba_info *info = NULL;
993 	HashTable *key_ht = NULL;
994 	zend_string *key_str = NULL;
995 
996 	ZEND_PARSE_PARAMETERS_START(2, 2)
997 		Z_PARAM_ARRAY_HT_OR_STR(key_ht, key_str)
998 		Z_PARAM_OBJECT_OF_CLASS(id, dba_connection_ce);
999 	ZEND_PARSE_PARAMETERS_END();
1000 
1001 	info = Z_DBA_INFO_P(id);
1002 	CHECK_DBA_CONNECTION(info);
1003 
1004 	if (key_ht) {
1005 		key_str = php_dba_make_key(key_ht);
1006 		if (!key_str) {
1007 			// TODO ValueError?
1008 			RETURN_FALSE;
1009 		}
1010 	}
1011 
1012 	RETVAL_BOOL(info->hnd->exists(info, key_str) == SUCCESS);
1013 	DBA_RELEASE_HT_KEY_CREATION();
1014 }
1015 /* }}} */
1016 
1017 /* {{{ Fetches the data associated with key */
PHP_FUNCTION(dba_fetch)1018 PHP_FUNCTION(dba_fetch)
1019 {
1020 	zval *id;
1021 	dba_info *info = NULL;
1022 	HashTable *key_ht = NULL;
1023 	zend_string *key_str = NULL;
1024 	zend_long skip = 0;
1025 
1026 	/* Check for legacy signature */
1027 	if (ZEND_NUM_ARGS() == 3) {
1028 		ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_QUIET, 3, 3)
1029 			Z_PARAM_ARRAY_HT_OR_STR(key_ht, key_str)
1030 			Z_PARAM_LONG(skip)
1031 			Z_PARAM_OBJECT_OF_CLASS(id, dba_connection_ce);
1032 		ZEND_PARSE_PARAMETERS_END_EX(goto standard;);
1033 
1034 		zend_error(E_DEPRECATED, "Calling dba_fetch() with $dba at the 3rd parameter is deprecated");
1035 		if (UNEXPECTED(EG(exception))) {
1036 			RETURN_THROWS();
1037 		}
1038 	} else {
1039 		standard:
1040 		ZEND_PARSE_PARAMETERS_START(2, 3)
1041 			Z_PARAM_ARRAY_HT_OR_STR(key_ht, key_str)
1042 			Z_PARAM_OBJECT_OF_CLASS(id, dba_connection_ce);
1043 			Z_PARAM_OPTIONAL
1044 			Z_PARAM_LONG(skip)
1045 		ZEND_PARSE_PARAMETERS_END();
1046 	}
1047 
1048 	info = Z_DBA_INFO_P(id);
1049 	CHECK_DBA_CONNECTION(info);
1050 
1051 	if (key_ht) {
1052 		key_str = php_dba_make_key(key_ht);
1053 		if (!key_str) {
1054 			// TODO ValueError?
1055 			RETURN_FALSE;
1056 		}
1057 	}
1058 
1059 	if (skip != 0) {
1060 		if (!strcmp(info->hnd->name, "cdb")) {
1061 			// TODO ValueError?
1062 			if (skip < 0) {
1063 				php_error_docref(NULL, E_NOTICE, "Handler %s accepts only skip values greater than or equal to zero, using skip=0", info->hnd->name);
1064 				skip = 0;
1065 			}
1066 		} else if (!strcmp(info->hnd->name, "inifile")) {
1067 			/* "-1" is comparable to 0 but allows a non restrictive
1068 			 * access which is faster. For example 'inifile' uses this
1069 			 * to allow faster access when the key was already found
1070 			 * using firstkey/nextkey. However explicitly setting the
1071 			 * value to 0 ensures the first value.
1072 			 */
1073 			if (skip < -1) {
1074 				// TODO ValueError?
1075 				php_error_docref(NULL, E_NOTICE, "Handler %s accepts only skip value -1 and greater, using skip=0", info->hnd->name);
1076 				skip = 0;
1077 			}
1078 		} else {
1079 			php_error_docref(NULL, E_NOTICE, "Handler %s does not support optional skip parameter, the value will be ignored", info->hnd->name);
1080 			skip = 0;
1081 		}
1082 	}
1083 
1084 	zend_string *val;
1085 	if ((val = info->hnd->fetch(info, key_str, skip)) == NULL) {
1086 		DBA_RELEASE_HT_KEY_CREATION();
1087 		RETURN_FALSE;
1088 	}
1089 	DBA_RELEASE_HT_KEY_CREATION();
1090 	RETURN_STR(val);
1091 }
1092 /* }}} */
1093 
1094 /* {{{ 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)1095 PHP_FUNCTION(dba_key_split)
1096 {
1097 	zval *zkey;
1098 	char *key, *name;
1099 	size_t key_len;
1100 
1101 	if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "z", &zkey) == SUCCESS) {
1102 		if (Z_TYPE_P(zkey) == IS_NULL || (Z_TYPE_P(zkey) == IS_FALSE)) {
1103 			php_error_docref(NULL, E_DEPRECATED, "Passing false or null is deprecated since 8.4");
1104 			RETURN_FALSE;
1105 		}
1106 	}
1107 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &key, &key_len) == FAILURE) {
1108 		RETURN_THROWS();
1109 	}
1110 	array_init(return_value);
1111 	if (key[0] == '[' && (name = strchr(key, ']')) != NULL) {
1112 		add_next_index_stringl(return_value, key+1, name - (key + 1));
1113 		add_next_index_stringl(return_value, name+1, key_len - (name - key + 1));
1114 	} else {
1115 		add_next_index_stringl(return_value, "", 0);
1116 		add_next_index_stringl(return_value, key, key_len);
1117 	}
1118 }
1119 /* }}} */
1120 
1121 /* {{{ Resets the internal key pointer and returns the first key */
PHP_FUNCTION(dba_firstkey)1122 PHP_FUNCTION(dba_firstkey)
1123 {
1124 	zval *id;
1125 	dba_info *info = NULL;
1126 
1127 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &id, dba_connection_ce) == FAILURE) {
1128 		RETURN_THROWS();
1129 	}
1130 
1131 	info = Z_DBA_INFO_P(id);
1132 	CHECK_DBA_CONNECTION(info);
1133 
1134 	zend_string *fkey = info->hnd->firstkey(info);
1135 
1136 	if (fkey) {
1137 		RETURN_STR(fkey);
1138 	}
1139 
1140 	RETURN_FALSE;
1141 }
1142 /* }}} */
1143 
1144 /* {{{ Returns the next key */
PHP_FUNCTION(dba_nextkey)1145 PHP_FUNCTION(dba_nextkey)
1146 {
1147 	zval *id;
1148 	dba_info *info = NULL;
1149 
1150 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &id, dba_connection_ce) == FAILURE) {
1151 		RETURN_THROWS();
1152 	}
1153 
1154 	info = Z_DBA_INFO_P(id);
1155 	CHECK_DBA_CONNECTION(info);
1156 
1157 	zend_string *nkey = info->hnd->nextkey(info);
1158 
1159 	if (nkey) {
1160 		RETURN_STR(nkey);
1161 	}
1162 
1163 	RETURN_FALSE;
1164 }
1165 /* }}} */
1166 
1167 /* {{{ Deletes the entry associated with key
1168    If inifile: remove all other key lines */
PHP_FUNCTION(dba_delete)1169 PHP_FUNCTION(dba_delete)
1170 {
1171 	zval *id;
1172 	dba_info *info = NULL;
1173 	HashTable *key_ht = NULL;
1174 	zend_string *key_str = NULL;
1175 
1176 	ZEND_PARSE_PARAMETERS_START(2, 2)
1177 		Z_PARAM_ARRAY_HT_OR_STR(key_ht, key_str)
1178 		Z_PARAM_OBJECT_OF_CLASS(id, dba_connection_ce);
1179 	ZEND_PARSE_PARAMETERS_END();
1180 
1181 	info = Z_DBA_INFO_P(id);
1182 	CHECK_DBA_CONNECTION(info);
1183 	DBA_WRITE_CHECK(info);
1184 
1185 	if (key_ht) {
1186 		key_str = php_dba_make_key(key_ht);
1187 		if (!key_str) {
1188 			// TODO ValueError?
1189 			RETURN_FALSE;
1190 		}
1191 	}
1192 
1193 	RETVAL_BOOL(info->hnd->delete(info, key_str) == SUCCESS);
1194 	DBA_RELEASE_HT_KEY_CREATION();
1195 }
1196 /* }}} */
1197 
1198 /* {{{ If not inifile: Insert value as key, return false, if key exists already
1199    If inifile: Add vakue as key (next instance of key) */
PHP_FUNCTION(dba_insert)1200 PHP_FUNCTION(dba_insert)
1201 {
1202 	php_dba_update(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1203 }
1204 /* }}} */
1205 
1206 /* {{{ Inserts value as key, replaces key, if key exists already
1207    If inifile: remove all other key lines */
PHP_FUNCTION(dba_replace)1208 PHP_FUNCTION(dba_replace)
1209 {
1210 	php_dba_update(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1211 }
1212 /* }}} */
1213 
1214 /* {{{ Optimizes (e.g. clean up, vacuum) database */
PHP_FUNCTION(dba_optimize)1215 PHP_FUNCTION(dba_optimize)
1216 {
1217 	zval *id;
1218 	dba_info *info = NULL;
1219 
1220 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &id, dba_connection_ce) == FAILURE) {
1221 		RETURN_THROWS();
1222 	}
1223 
1224 	info = Z_DBA_INFO_P(id);
1225 	CHECK_DBA_CONNECTION(info);
1226 	DBA_WRITE_CHECK(info);
1227 
1228 	if (info->hnd->optimize(info) == SUCCESS) {
1229 		RETURN_TRUE;
1230 	}
1231 
1232 	RETURN_FALSE;
1233 }
1234 /* }}} */
1235 
1236 /* {{{ Synchronizes database */
PHP_FUNCTION(dba_sync)1237 PHP_FUNCTION(dba_sync)
1238 {
1239 	zval *id;
1240 	dba_info *info = NULL;
1241 
1242 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &id, dba_connection_ce) == FAILURE) {
1243 		RETURN_THROWS();
1244 	}
1245 
1246 	info = Z_DBA_INFO_P(id);
1247 	CHECK_DBA_CONNECTION(info);
1248 
1249 	if (info->hnd->sync(info) == SUCCESS) {
1250 		RETURN_TRUE;
1251 	}
1252 
1253 	RETURN_FALSE;
1254 }
1255 /* }}} */
1256 
1257 /* {{{ List configured database handlers */
PHP_FUNCTION(dba_handlers)1258 PHP_FUNCTION(dba_handlers)
1259 {
1260 	const dba_handler *hptr;
1261 	bool full_info = 0;
1262 
1263 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &full_info) == FAILURE) {
1264 		RETURN_THROWS();
1265 	}
1266 
1267 	array_init(return_value);
1268 
1269 	for(hptr = handler; hptr->name; hptr++) {
1270 		if (full_info) {
1271 			// TODO: avoid reallocation ???
1272 			char *str = hptr->info(hptr, NULL);
1273 			add_assoc_string(return_value, hptr->name, str);
1274 			efree(str);
1275 		} else {
1276 			add_next_index_string(return_value, hptr->name);
1277 		}
1278 	}
1279 }
1280 /* }}} */
1281 
1282 /* {{{ List opened databases */
PHP_FUNCTION(dba_list)1283 PHP_FUNCTION(dba_list)
1284 {
1285 	if (zend_parse_parameters_none() == FAILURE) {
1286 		RETURN_THROWS();
1287 	}
1288 
1289 	array_init(return_value);
1290 
1291 	zval *zv;
1292 	ZEND_HASH_MAP_FOREACH_VAL(&DBA_G(connections), zv) {
1293 		dba_info *info = Z_DBA_INFO_P(zv);
1294 		if (info) {
1295 			add_next_index_str(return_value, zend_string_copy(info->path));
1296 		}
1297 	} ZEND_HASH_FOREACH_END();
1298 }
1299 /* }}} */
1300 
1301 #endif /* HAVE_DBA */
1302