php-src/ext/dba/dba.c

1332 lines
33 KiB
C
Raw Normal View History

1999-07-21 15:12:32 +00:00
/*
+----------------------------------------------------------------------+
2014-09-19 16:33:14 +00:00
| PHP Version 7 |
1999-07-21 15:12:32 +00:00
+----------------------------------------------------------------------+
2017-01-02 15:30:12 +00:00
| Copyright (c) 1997-2017 The PHP Group |
1999-07-21 15:12:32 +00:00
+----------------------------------------------------------------------+
2006-01-01 12:51:34 +00:00
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
2006-01-01 12:51:34 +00:00
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
1999-07-21 15:12:32 +00:00
+----------------------------------------------------------------------+
| Authors: Sascha Schumann <sascha@schumann.cx> |
| Marcus Boerger <helly@php.net> |
1999-07-21 15:12:32 +00:00
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
1999-07-21 15:12:32 +00:00
#include "php.h"
#if HAVE_DBA
#include "php_ini.h"
2015-01-03 09:22:58 +00:00
#include <stdio.h>
#include <fcntl.h>
#ifdef HAVE_SYS_FILE_H
#include <sys/file.h>
#endif
2015-01-03 09:22:58 +00:00
#include "php_dba.h"
#include "ext/standard/info.h"
2002-11-14 21:09:41 +00:00
#include "ext/standard/php_string.h"
2003-06-16 09:11:31 +00:00
#include "ext/standard/flock_compat.h"
1999-07-21 15:12:32 +00:00
#include "php_gdbm.h"
#include "php_ndbm.h"
#include "php_dbm.h"
#include "php_cdb.h"
#include "php_db1.h"
#include "php_db2.h"
#include "php_db3.h"
2002-11-26 12:05:59 +00:00
#include "php_db4.h"
#include "php_flatfile.h"
#include "php_inifile.h"
#include "php_qdbm.h"
#include "php_tcadb.h"
1999-07-21 15:12:32 +00:00
2006-06-11 01:42:17 +00:00
/* {{{ arginfo */
ZEND_BEGIN_ARG_INFO_EX(arginfo_dba_popen, 0, 0, 2)
ZEND_ARG_INFO(0, path)
ZEND_ARG_INFO(0, mode)
ZEND_ARG_INFO(0, handlername)
ZEND_ARG_VARIADIC_INFO(0, handler_parameters)
2006-06-11 01:42:17 +00:00
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_dba_open, 0, 0, 2)
ZEND_ARG_INFO(0, path)
ZEND_ARG_INFO(0, mode)
ZEND_ARG_INFO(0, handlername)
ZEND_ARG_VARIADIC_INFO(0, handler_parameters)
2006-06-11 01:42:17 +00:00
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_dba_close, 0)
ZEND_ARG_INFO(0, handle)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_dba_exists, 0)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, handle)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_dba_fetch, 0, 0, 2)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, skip)
ZEND_ARG_INFO(0, handle)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_dba_key_split, 0)
ZEND_ARG_INFO(0, key)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_dba_firstkey, 0)
ZEND_ARG_INFO(0, handle)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_dba_nextkey, 0)
ZEND_ARG_INFO(0, handle)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_dba_delete, 0)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, handle)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_dba_insert, 0)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, value)
ZEND_ARG_INFO(0, handle)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_dba_replace, 0)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, value)
ZEND_ARG_INFO(0, handle)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_dba_optimize, 0)
ZEND_ARG_INFO(0, handle)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_dba_sync, 0)
ZEND_ARG_INFO(0, handle)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_dba_handlers, 0, 0, 0)
ZEND_ARG_INFO(0, full_info)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_dba_list, 0)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ dba_functions[]
*/
const zend_function_entry dba_functions[] = {
2006-06-11 01:42:17 +00:00
PHP_FE(dba_open, arginfo_dba_open)
PHP_FE(dba_popen, arginfo_dba_popen)
PHP_FE(dba_close, arginfo_dba_close)
PHP_FE(dba_delete, arginfo_dba_delete)
PHP_FE(dba_exists, arginfo_dba_exists)
PHP_FE(dba_fetch, arginfo_dba_fetch)
PHP_FE(dba_insert, arginfo_dba_insert)
PHP_FE(dba_replace, arginfo_dba_replace)
PHP_FE(dba_firstkey, arginfo_dba_firstkey)
PHP_FE(dba_nextkey, arginfo_dba_nextkey)
PHP_FE(dba_optimize, arginfo_dba_optimize)
PHP_FE(dba_sync, arginfo_dba_sync)
PHP_FE(dba_handlers, arginfo_dba_handlers)
PHP_FE(dba_list, arginfo_dba_list)
PHP_FE(dba_key_split, arginfo_dba_key_split)
2011-07-25 11:35:02 +00:00
PHP_FE_END
1999-07-21 15:12:32 +00:00
};
/* }}} */
1999-07-21 15:12:32 +00:00
PHP_MINIT_FUNCTION(dba);
PHP_MSHUTDOWN_FUNCTION(dba);
PHP_MINFO_FUNCTION(dba);
1999-07-21 15:12:32 +00:00
ZEND_BEGIN_MODULE_GLOBALS(dba)
char *default_handler;
dba_handler *default_hptr;
2015-01-03 09:22:58 +00:00
ZEND_END_MODULE_GLOBALS(dba)
ZEND_DECLARE_MODULE_GLOBALS(dba)
#ifdef ZTS
#define DBA_G(v) TSRMG(dba_globals_id, zend_dba_globals *, v)
#else
#define DBA_G(v) (dba_globals.v)
2015-01-03 09:22:58 +00:00
#endif
static PHP_GINIT_FUNCTION(dba);
zend_module_entry dba_module_entry = {
STANDARD_MODULE_HEADER,
"dba",
2015-01-03 09:22:58 +00:00
dba_functions,
PHP_MINIT(dba),
PHP_MSHUTDOWN(dba),
NULL,
NULL,
PHP_MINFO(dba),
2015-03-23 19:13:59 +00:00
PHP_DBA_VERSION,
PHP_MODULE_GLOBALS(dba),
PHP_GINIT(dba),
NULL,
NULL,
STANDARD_MODULE_PROPERTIES_EX
1999-07-21 15:12:32 +00:00
};
2000-05-02 03:38:26 +00:00
#ifdef COMPILE_DL_DBA
ZEND_GET_MODULE(dba)
#endif
1999-07-21 15:12:32 +00:00
/* {{{ macromania */
#define DBA_ID_PARS \
2008-11-07 12:39:11 +00:00
zval *id; \
dba_info *info = NULL; \
int ac = ZEND_NUM_ARGS()
1999-07-21 15:12:32 +00:00
/* these are used to get the standard arguments */
/* {{{ php_dba_myke_key */
2014-12-13 22:06:14 +00:00
static size_t php_dba_make_key(zval *key, char **key_str, char **key_free)
{
2008-11-07 12:39:11 +00:00
if (Z_TYPE_P(key) == IS_ARRAY) {
zval *group, *name;
HashPosition pos;
size_t len;
2015-01-03 09:22:58 +00:00
2008-11-07 12:39:11 +00:00
if (zend_hash_num_elements(Z_ARRVAL_P(key)) != 2) {
2016-06-14 16:32:33 +00:00
zend_throw_error(NULL, "Key does not have exactly two elements: (key, name)");
2015-12-10 09:35:35 +00:00
return 0;
}
2008-11-07 12:39:11 +00:00
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(key), &pos);
group = zend_hash_get_current_data_ex(Z_ARRVAL_P(key), &pos);
2008-11-07 12:39:11 +00:00
zend_hash_move_forward_ex(Z_ARRVAL_P(key), &pos);
name = zend_hash_get_current_data_ex(Z_ARRVAL_P(key), &pos);
convert_to_string_ex(group);
convert_to_string_ex(name);
2014-08-25 17:24:55 +00:00
if (Z_STRLEN_P(group) == 0) {
*key_str = Z_STRVAL_P(name);
*key_free = NULL;
2014-08-25 17:24:55 +00:00
return Z_STRLEN_P(name);
}
len = spprintf(key_str, 0, "[%s]%s", Z_STRVAL_P(group), Z_STRVAL_P(name));
*key_free = *key_str;
return len;
} else {
zval tmp;
int len;
2008-11-07 12:39:11 +00:00
ZVAL_COPY(&tmp, key);
convert_to_string(&tmp);
2008-11-07 12:39:11 +00:00
2014-08-25 17:24:55 +00:00
len = Z_STRLEN(tmp);
2015-12-10 09:35:35 +00:00
if (len) {
2015-12-10 07:02:01 +00:00
*key_free = *key_str = estrndup(Z_STRVAL(tmp), Z_STRLEN(tmp));
}
zval_ptr_dtor(&tmp);
return len;
}
}
/* }}} */
#define DBA_GET2 \
2008-11-07 12:39:11 +00:00
zval *key; \
char *key_str, *key_free; \
size_t key_len; \
2014-12-13 22:06:14 +00:00
if (zend_parse_parameters(ac, "zr", &key, &id) == FAILURE) { \
2008-11-07 12:39:11 +00:00
return; \
} \
2014-12-13 22:06:14 +00:00
if ((key_len = php_dba_make_key(key, &key_str, &key_free)) == 0) {\
RETURN_FALSE; \
}
1999-07-21 15:12:32 +00:00
#define DBA_GET2_3 \
2008-11-07 12:39:11 +00:00
zval *key; \
char *key_str, *key_free; \
size_t key_len; \
2014-08-25 17:24:55 +00:00
zend_long skip = 0; \
switch(ac) { \
case 2: \
2014-12-13 22:06:14 +00:00
if (zend_parse_parameters(ac, "zr", &key, &id) == FAILURE) { \
2008-11-07 12:39:11 +00:00
return; \
} \
break; \
case 3: \
2014-12-13 22:06:14 +00:00
if (zend_parse_parameters(ac, "zlr", &key, &skip, &id) == FAILURE) { \
2008-11-07 12:39:11 +00:00
return; \
} \
break; \
default: \
WRONG_PARAM_COUNT; \
} \
2014-12-13 22:06:14 +00:00
if ((key_len = php_dba_make_key(key, &key_str, &key_free)) == 0) {\
RETURN_FALSE; \
}
2008-11-07 12:39:11 +00:00
#define DBA_FETCH_RESOURCE(info, id) \
2015-02-03 04:07:36 +00:00
if ((info = (dba_info *)zend_fetch_resource2(Z_RES_P(id), "DBA identifier", le_db, le_pdb)) == NULL) { \
RETURN_FALSE; \
}
2008-11-07 12:39:11 +00:00
2015-12-10 07:02:01 +00:00
#define DBA_FETCH_RESOURCE_WITH_ID(info, id) \
if ((info = (dba_info *)zend_fetch_resource2(Z_RES_P(id), "DBA identifier", le_db, le_pdb)) == NULL) { \
DBA_ID_DONE; \
RETURN_FALSE; \
}
#define DBA_ID_GET2 DBA_ID_PARS; DBA_GET2; DBA_FETCH_RESOURCE_WITH_ID(info, id)
#define DBA_ID_GET2_3 DBA_ID_PARS; DBA_GET2_3; DBA_FETCH_RESOURCE_WITH_ID(info, id)
1999-07-21 15:12:32 +00:00
#define DBA_ID_DONE \
if (key_free) efree(key_free)
1999-07-21 15:12:32 +00:00
/* a DBA handler must have specific routines */
#define DBA_NAMED_HND(alias, name, flags) \
1999-07-21 15:12:32 +00:00
{\
2002-12-30 11:43:18 +00:00
#alias, flags, dba_open_##name, dba_close_##name, dba_fetch_##name, dba_update_##name, \
dba_exists_##name, dba_delete_##name, dba_firstkey_##name, dba_nextkey_##name, \
dba_optimize_##name, dba_sync_##name, dba_info_##name \
1999-07-21 15:12:32 +00:00
},
#define DBA_HND(name, flags) DBA_NAMED_HND(name, name, flags)
1999-07-21 15:12:32 +00:00
/* check whether the user has write access */
#define DBA_WRITE_CHECK \
if(info->mode != DBA_WRITER && info->mode != DBA_TRUNC && info->mode != DBA_CREAT) { \
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "You cannot perform a modification to a database without proper access"); \
1999-07-21 15:12:32 +00:00
RETURN_FALSE; \
}
/* the same check, but with a call to DBA_ID_DONE before returning */
#define DBA_WRITE_CHECK_WITH_ID \
if(info->mode != DBA_WRITER && info->mode != DBA_TRUNC && info->mode != DBA_CREAT) { \
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "You cannot perform a modification to a database without proper access"); \
DBA_ID_DONE; \
RETURN_FALSE; \
}
1999-07-21 15:12:32 +00:00
/* }}} */
/* {{{ globals */
static dba_handler handler[] = {
#if DBA_GDBM
DBA_HND(gdbm, DBA_LOCK_EXT) /* Locking done in library if set */
1999-07-21 15:12:32 +00:00
#endif
#if DBA_DBM
DBA_HND(dbm, DBA_LOCK_ALL) /* No lock in lib */
1999-07-21 15:12:32 +00:00
#endif
#if DBA_NDBM
DBA_HND(ndbm, DBA_LOCK_ALL) /* Could be done in library: filemode = 0644 + S_ENFMT */
1999-07-21 15:12:32 +00:00
#endif
#if DBA_CDB
DBA_HND(cdb, DBA_STREAM_OPEN|DBA_LOCK_ALL) /* No lock in lib */
1999-07-21 15:12:32 +00:00
#endif
#if DBA_CDB_BUILTIN
DBA_NAMED_HND(cdb_make, cdb, DBA_STREAM_OPEN|DBA_LOCK_ALL) /* No lock in lib */
#endif
#if DBA_DB1
DBA_HND(db1, DBA_LOCK_ALL) /* No lock in lib */
#endif
1999-07-21 15:12:32 +00:00
#if DBA_DB2
DBA_HND(db2, DBA_LOCK_ALL) /* No lock in lib */
#endif
#if DBA_DB3
DBA_HND(db3, DBA_LOCK_ALL) /* No lock in lib */
#endif
2002-11-26 12:05:59 +00:00
#if DBA_DB4
DBA_HND(db4, DBA_LOCK_ALL) /* No lock in lib */
2002-11-26 12:05:59 +00:00
#endif
#if DBA_INIFILE
DBA_HND(inifile, DBA_STREAM_OPEN|DBA_LOCK_ALL|DBA_CAST_AS_FD) /* No lock in lib */
#endif
#if DBA_FLATFILE
DBA_HND(flatfile, DBA_STREAM_OPEN|DBA_LOCK_ALL|DBA_NO_APPEND) /* No lock in lib */
#endif
#if DBA_QDBM
DBA_HND(qdbm, DBA_LOCK_EXT)
#endif
#if DBA_TCADB
DBA_HND(tcadb, DBA_LOCK_ALL)
1999-07-21 15:12:32 +00:00
#endif
2016-06-21 21:40:50 +00:00
{ NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1999-07-21 15:12:32 +00:00
};
#if DBA_FLATFILE
#define DBA_DEFAULT "flatfile"
#elif DBA_DB4
#define DBA_DEFAULT "db4"
#elif DBA_DB3
#define DBA_DEFAULT "db3"
#elif DBA_DB2
#define DBA_DEFAULT "db2"
#elif DBA_DB1
#define DBA_DEFAULT "db1"
#elif DBA_GDBM
#define DBA_DEFAULT "gdbm"
#elif DBA_NBBM
#define DBA_DEFAULT "ndbm"
#elif DBA_DBM
#define DBA_DEFAULT "dbm"
#elif DBA_QDBM
#define DBA_DEFAULT "qdbm"
#elif DBA_TCADB
#define DBA_DEFAULT "tcadb"
#else
#define DBA_DEFAULT ""
#endif
/* cdb/cdb_make and ini are no option here */
1999-07-21 15:12:32 +00:00
static int le_db;
static int le_pdb;
2006-06-11 01:42:17 +00:00
/* }}} */
/* {{{ dba_fetch_resource
2014-12-13 22:06:14 +00:00
PHPAPI void dba_fetch_resource(dba_info **pinfo, zval **id)
{
dba_info *info;
DBA_ID_FETCH
*pinfo = info;
}
*/
/* }}} */
/* {{{ dba_get_handler
PHPAPI dba_handler *dba_get_handler(const char* handler_name)
{
dba_handler *hptr;
for (hptr = handler; hptr->name && strcasecmp(hptr->name, handler_name); hptr++);
return hptr;
}
*/
1999-07-21 15:12:32 +00:00
/* }}} */
2015-01-03 09:22:58 +00:00
/* {{{ dba_close
*/
2014-12-13 22:06:14 +00:00
static void dba_close(dba_info *info)
1999-07-21 15:12:32 +00:00
{
2003-03-20 19:54:22 +00:00
if (info->hnd) {
2014-12-13 22:06:14 +00:00
info->hnd->close(info);
2003-03-20 19:54:22 +00:00
}
if (info->path) {
pefree(info->path, info->flags&DBA_PERSISTENT);
}
if (info->fp && info->fp != info->lock.fp) {
2014-05-21 04:22:59 +00:00
if (info->flags & DBA_PERSISTENT) {
php_stream_pclose(info->fp);
} else {
php_stream_close(info->fp);
}
2003-03-20 19:54:22 +00:00
}
if (info->lock.fp) {
2014-05-21 04:22:59 +00:00
if (info->flags & DBA_PERSISTENT) {
php_stream_pclose(info->lock.fp);
} else {
php_stream_close(info->lock.fp);
}
2003-03-20 19:54:22 +00:00
}
if (info->lock.name) {
pefree(info->lock.name, info->flags&DBA_PERSISTENT);
}
pefree(info, info->flags&DBA_PERSISTENT);
1999-07-21 15:12:32 +00:00
}
/* }}} */
/* {{{ dba_close_rsrc
*/
2014-12-13 22:06:14 +00:00
static void dba_close_rsrc(zend_resource *rsrc)
2000-10-26 17:38:01 +00:00
{
2015-01-03 09:22:58 +00:00
dba_info *info = (dba_info *)rsrc->ptr;
2003-04-13 19:50:28 +00:00
2014-12-13 22:06:14 +00:00
dba_close(info);
2000-10-26 17:38:01 +00:00
}
/* }}} */
2000-10-26 17:38:01 +00:00
2003-04-13 19:50:28 +00:00
/* {{{ dba_close_pe_rsrc_deleter */
2014-12-13 22:06:14 +00:00
int dba_close_pe_rsrc_deleter(zval *el, void *pDba)
2003-04-13 19:50:28 +00:00
{
2015-04-08 12:47:31 +00:00
if (Z_RES_P(el)->ptr == pDba) {
if (Z_DELREF_P(el) == 0) {
return ZEND_HASH_APPLY_REMOVE;
} else {
return ZEND_HASH_APPLY_KEEP | ZEND_HASH_APPLY_STOP;
}
} else {
return ZEND_HASH_APPLY_KEEP;
}
2003-04-13 19:50:28 +00:00
}
/* }}} */
/* {{{ dba_close_pe_rsrc */
2014-12-13 22:06:14 +00:00
static void dba_close_pe_rsrc(zend_resource *rsrc)
2003-04-13 19:50:28 +00:00
{
2015-01-03 09:22:58 +00:00
dba_info *info = (dba_info *)rsrc->ptr;
2003-04-13 19:50:28 +00:00
/* closes the resource by calling dba_close_rsrc() */
2014-12-13 22:06:14 +00:00
zend_hash_apply_with_argument(&EG(persistent_list), dba_close_pe_rsrc_deleter, info);
2003-04-13 19:50:28 +00:00
}
/* }}} */
/* {{{ PHP_INI
*/
2003-01-06 01:08:59 +00:00
ZEND_INI_MH(OnUpdateDefaultHandler)
{
dba_handler *hptr;
if (!ZSTR_LEN(new_value)) {
DBA_G(default_hptr) = NULL;
2014-12-13 22:06:14 +00:00
return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
}
for (hptr = handler; hptr->name && strcasecmp(hptr->name, ZSTR_VAL(new_value)); hptr++);
if (!hptr->name) {
php_error_docref(NULL, E_WARNING, "No such handler: %s", ZSTR_VAL(new_value));
return FAILURE;
}
DBA_G(default_hptr) = hptr;
2014-12-13 22:06:14 +00:00
return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
}
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("dba.default_handler", DBA_DEFAULT, PHP_INI_ALL, OnUpdateDefaultHandler, default_handler, zend_dba_globals, dba_globals)
PHP_INI_END()
/* }}} */
2015-01-03 09:22:58 +00:00
/* {{{ PHP_GINIT_FUNCTION
*/
static PHP_GINIT_FUNCTION(dba)
{
dba_globals->default_handler = "";
dba_globals->default_hptr = NULL;
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(dba)
1999-07-21 15:12:32 +00:00
{
REGISTER_INI_ENTRIES();
le_db = zend_register_list_destructors_ex(dba_close_rsrc, NULL, "dba", module_number);
2003-04-13 19:50:28 +00:00
le_pdb = zend_register_list_destructors_ex(dba_close_pe_rsrc, dba_close_rsrc, "dba persistent", module_number);
1999-07-21 15:12:32 +00:00
return SUCCESS;
}
/* }}} */
1999-07-21 15:12:32 +00:00
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(dba)
1999-07-21 15:12:32 +00:00
{
UNREGISTER_INI_ENTRIES();
1999-07-21 15:12:32 +00:00
return SUCCESS;
}
/* }}} */
1999-07-21 15:12:32 +00:00
#include "zend_smart_str.h"
2000-12-01 01:25:36 +00:00
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(dba)
1999-07-21 15:12:32 +00:00
{
dba_handler *hptr;
smart_str handlers = {0};
2000-12-01 01:25:36 +00:00
1999-07-21 15:12:32 +00:00
for(hptr = handler; hptr->name; hptr++) {
smart_str_appends(&handlers, hptr->name);
smart_str_appendc(&handlers, ' ');
2000-12-01 01:25:36 +00:00
}
php_info_print_table_start();
php_info_print_table_row(2, "DBA support", "enabled");
if (handlers.s) {
smart_str_0(&handlers);
php_info_print_table_row(2, "Supported handlers", ZSTR_VAL(handlers.s));
smart_str_free(&handlers);
} else {
php_info_print_table_row(2, "Supported handlers", "none");
}
2000-12-01 01:25:36 +00:00
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
1999-07-21 15:12:32 +00:00
}
/* }}} */
/* {{{ php_dba_update
*/
1999-12-17 21:34:28 +00:00
static void php_dba_update(INTERNAL_FUNCTION_PARAMETERS, int mode)
1999-07-21 15:12:32 +00:00
{
size_t val_len;
2008-11-07 12:39:11 +00:00
zval *id;
dba_info *info = NULL;
int ac = ZEND_NUM_ARGS();
zval *key;
char *val;
char *key_str, *key_free;
size_t key_len;
2014-12-13 22:06:14 +00:00
if (zend_parse_parameters(ac, "zsr", &key, &val, &val_len, &id) == FAILURE) {
2008-11-07 12:39:11 +00:00
return;
}
2014-12-13 22:06:14 +00:00
if ((key_len = php_dba_make_key(key, &key_str, &key_free)) == 0) {
2008-11-07 12:39:11 +00:00
RETURN_FALSE;
}
2015-12-10 07:02:01 +00:00
DBA_FETCH_RESOURCE_WITH_ID(info, id);
1999-07-21 15:12:32 +00:00
DBA_WRITE_CHECK_WITH_ID;
2008-11-07 12:39:11 +00:00
2014-12-13 22:06:14 +00:00
if (info->hnd->update(info, key_str, key_len, val, val_len, mode) == SUCCESS) {
DBA_ID_DONE;
RETURN_TRUE;
}
2008-11-07 12:39:11 +00:00
DBA_ID_DONE;
1999-07-21 15:12:32 +00:00
RETURN_FALSE;
}
/* }}} */
1999-07-21 15:12:32 +00:00
#define FREENOW if(args) {int i; for (i=0; i<ac; i++) { zval_ptr_dtor(&args[i]); } efree(args);} if(key) efree(key)
1999-07-21 15:12:32 +00:00
/* {{{ php_find_dbm
*/
2014-12-13 22:06:14 +00:00
dba_info *php_dba_find(const char* path)
{
zend_resource *le;
dba_info *info;
int numitems, i;
numitems = zend_hash_next_free_element(&EG(regular_list));
for (i=1; i<numitems; i++) {
if ((le = zend_hash_index_find_ptr(&EG(regular_list), i)) == NULL) {
continue;
}
if (le->type == le_db || le->type == le_pdb) {
info = (dba_info *)(le->ptr);
if (!strcmp(info->path, path)) {
return (dba_info *)(le->ptr);
}
}
}
return NULL;
}
/* }}} */
/* {{{ php_dba_open
*/
1999-12-17 21:34:28 +00:00
static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, int persistent)
1999-07-21 15:12:32 +00:00
{
zval *args = NULL;
int ac = ZEND_NUM_ARGS();
1999-07-21 15:12:32 +00:00
dba_mode_t modenr;
dba_info *info, *other;
1999-07-21 15:12:32 +00:00
dba_handler *hptr;
2002-11-05 14:46:36 +00:00
char *key = NULL, *error = NULL;
1999-07-21 15:12:32 +00:00
int keylen = 0;
int i;
int lock_mode, lock_flag, lock_dbf = 0;
char *file_mode;
char mode[4], *pmode, *lock_file_mode = NULL;
2003-04-13 19:50:28 +00:00
int persistent_flag = persistent ? STREAM_OPEN_PERSISTENT : 0;
zend_string *opened_path = NULL;
char *lock_name;
#ifdef PHP_WIN32
zend_bool restarted = 0;
#endif
2015-01-03 09:22:58 +00:00
if (ac < 2) {
1999-07-21 15:12:32 +00:00
WRONG_PARAM_COUNT;
}
2015-01-03 09:22:58 +00:00
1999-07-21 15:12:32 +00:00
/* we pass additional args to the respective handler */
args = safe_emalloc(ac, sizeof(zval), 0);
if (zend_get_parameters_array_ex(ac, args) != SUCCESS) {
efree(args);
1999-07-21 15:12:32 +00:00
WRONG_PARAM_COUNT;
}
2015-01-03 09:22:58 +00:00
1999-07-21 15:12:32 +00:00
/* we only take string arguments */
for (i = 0; i < ac; i++) {
ZVAL_STR(&args[i], zval_get_string(&args[i]));
2014-08-25 17:24:55 +00:00
keylen += Z_STRLEN(args[i]);
1999-07-21 15:12:32 +00:00
}
if (persistent) {
zend_resource *le;
2015-01-03 09:22:58 +00:00
1999-07-21 15:12:32 +00:00
/* calculate hash */
2003-04-24 20:54:43 +00:00
key = safe_emalloc(keylen, 1, 1);
key[keylen] = '\0';
1999-07-21 15:12:32 +00:00
keylen = 0;
2015-01-03 09:22:58 +00:00
1999-07-21 15:12:32 +00:00
for(i = 0; i < ac; i++) {
2014-08-25 17:24:55 +00:00
memcpy(key+keylen, Z_STRVAL(args[i]), Z_STRLEN(args[i]));
keylen += Z_STRLEN(args[i]);
1999-07-21 15:12:32 +00:00
}
/* try to find if we already have this link in our persistent list */
if ((le = zend_hash_str_find_ptr(&EG(persistent_list), key, keylen)) != NULL) {
1999-07-21 15:12:32 +00:00
FREENOW;
2015-01-03 09:22:58 +00:00
if (le->type != le_pdb) {
RETURN_FALSE;
}
2015-01-03 09:22:58 +00:00
info = (dba_info *)le->ptr;
2014-05-21 04:22:59 +00:00
GC_REFCOUNT(le)++;
2015-04-08 12:47:31 +00:00
RETURN_RES(zend_register_resource(info, le_pdb));
return;
1999-07-21 15:12:32 +00:00
}
}
2015-01-03 09:22:58 +00:00
if (ac==2) {
hptr = DBA_G(default_hptr);
if (!hptr) {
2014-12-13 22:06:14 +00:00
php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "No default handler selected");
FREENOW;
RETURN_FALSE;
}
} else {
for (hptr = handler; hptr->name && strcasecmp(hptr->name, Z_STRVAL(args[2])); hptr++);
}
1999-07-21 15:12:32 +00:00
if (!hptr->name) {
2014-12-13 22:06:14 +00:00
php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "No such handler: %s", Z_STRVAL(args[2]));
1999-07-21 15:12:32 +00:00
FREENOW;
RETURN_FALSE;
}
/* Check mode: [rwnc][fl]?t?
* r: Read
* w: Write
* n: Create/Truncate
* c: Create
*
* d: force lock on database file
* l: force lock on lck file
2002-12-10 22:51:09 +00:00
* -: ignore locking
*
* t: test open database, warning if locked
*/
strlcpy(mode, Z_STRVAL(args[1]), sizeof(mode));
pmode = &mode[0];
if (pmode[0] && (pmode[1]=='d' || pmode[1]=='l' || pmode[1]=='-')) { /* force lock on db file or lck file or disable locking */
switch (pmode[1]) {
case 'd':
lock_dbf = 1;
if ((hptr->flags & DBA_LOCK_ALL) == 0) {
lock_flag = (hptr->flags & DBA_LOCK_ALL);
break;
}
/* no break */
case 'l':
lock_flag = DBA_LOCK_ALL;
if ((hptr->flags & DBA_LOCK_ALL) == 0) {
2014-12-13 22:06:14 +00:00
php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_NOTICE, "Handler %s does locking internally", hptr->name);
}
break;
default:
case '-':
if ((hptr->flags & DBA_LOCK_ALL) == 0) {
2014-12-13 22:06:14 +00:00
php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Locking cannot be disabled for handler %s", hptr->name);
FREENOW;
RETURN_FALSE;
}
lock_flag = 0;
break;
}
} else {
lock_flag = (hptr->flags&DBA_LOCK_ALL);
lock_dbf = 1;
}
switch (*pmode++) {
2015-01-03 09:22:58 +00:00
case 'r':
modenr = DBA_READER;
lock_mode = (lock_flag & DBA_LOCK_READER) ? LOCK_SH : 0;
file_mode = "r";
1999-07-21 15:12:32 +00:00
break;
2015-01-03 09:22:58 +00:00
case 'w':
modenr = DBA_WRITER;
lock_mode = (lock_flag & DBA_LOCK_WRITER) ? LOCK_EX : 0;
file_mode = "r+b";
1999-07-21 15:12:32 +00:00
break;
case 'c': {
#ifdef PHP_WIN32
php_stream_statbuf ssb;
zend_bool need_creation = 0;
if (hptr->flags & (DBA_NO_APPEND|DBA_CAST_AS_FD)) {
need_creation = (SUCCESS != php_stream_stat_path(Z_STRVAL(args[0]), &ssb));
}
#endif
2015-01-03 09:22:58 +00:00
modenr = DBA_CREAT;
lock_mode = (lock_flag & DBA_LOCK_CREAT) ? LOCK_EX : 0;
if (lock_mode) {
if (lock_dbf) {
/* the create/append check will be done on the lock
* when the lib opens the file it is already created
*/
file_mode = "r+b"; /* read & write, seek 0 */
#ifdef PHP_WIN32
if (!need_creation) {
lock_file_mode = "r+b";
} else
#endif
lock_file_mode = "a+b"; /* append */
} else {
#ifdef PHP_WIN32
if (!need_creation) {
file_mode = "r+b";
} else
#endif
file_mode = "a+b"; /* append */
lock_file_mode = "w+b"; /* create/truncate */
}
} else {
#ifdef PHP_WIN32
if (!need_creation) {
file_mode = "r+b";
} else
#endif
file_mode = "a+b";
}
2015-01-03 09:22:58 +00:00
/* In case of the 'a+b' append mode, the handler is responsible
* to handle any rewind problems (see flatfile handler).
*/
break;
}
case 'n':
modenr = DBA_TRUNC;
lock_mode = (lock_flag & DBA_LOCK_TRUNC) ? LOCK_EX : 0;
file_mode = "w+b";
1999-07-21 15:12:32 +00:00
break;
default:
2014-12-13 22:06:14 +00:00
php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Illegal DBA mode");
2003-08-17 17:14:11 +00:00
FREENOW;
RETURN_FALSE;
}
if (!lock_file_mode) {
lock_file_mode = file_mode;
}
if (*pmode=='d' || *pmode=='l' || *pmode=='-') {
pmode++; /* done already - skip here */
}
if (*pmode=='t') {
pmode++;
if (!lock_flag) {
2014-12-13 22:06:14 +00:00
php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "You cannot combine modifiers - (no lock) and t (test lock)");
FREENOW;
RETURN_FALSE;
}
if (!lock_mode) {
if ((hptr->flags & DBA_LOCK_ALL) == 0) {
2014-12-13 22:06:14 +00:00
php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Handler %s uses its own locking which doesn't support mode modifier t (test lock)", hptr->name);
FREENOW;
RETURN_FALSE;
} else {
2014-12-13 22:06:14 +00:00
php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Handler %s doesn't uses locking for this mode which makes modifier t (test lock) obsolete", hptr->name);
FREENOW;
RETURN_FALSE;
}
} else {
lock_mode |= LOCK_NB; /* test =: non blocking */
}
}
2003-08-17 17:14:11 +00:00
if (*pmode) {
2014-12-13 22:06:14 +00:00
php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Illegal DBA mode");
FREENOW;
RETURN_FALSE;
1999-07-21 15:12:32 +00:00
}
2015-01-03 09:22:58 +00:00
info = pemalloc(sizeof(dba_info), persistent);
memset(info, 0, sizeof(dba_info));
info->path = pestrdup(Z_STRVAL(args[0]), persistent);
1999-07-21 15:12:32 +00:00
info->mode = modenr;
info->argc = ac - 3;
info->argv = args + 3;
info->flags = (hptr->flags & ~DBA_LOCK_ALL) | (lock_flag & DBA_LOCK_ALL) | (persistent ? DBA_PERSISTENT : 0);
info->lock.mode = lock_mode;
/* if any open call is a locking call:
* check if we already habe a locking call open that should block this call
* the problem is some systems would allow read during write
*/
if (hptr->flags & DBA_LOCK_ALL) {
2014-12-13 22:06:14 +00:00
if ((other = php_dba_find(info->path)) != NULL) {
if ( ( (lock_mode&LOCK_EX) && (other->lock.mode&(LOCK_EX|LOCK_SH)) )
|| ( (other->lock.mode&LOCK_EX) && (lock_mode&(LOCK_EX|LOCK_SH)) )
) {
error = "Unable to establish lock (database file already open)"; /* force failure exit */
}
}
}
1999-07-21 15:12:32 +00:00
#ifdef PHP_WIN32
restart:
#endif
if (!error && lock_mode) {
if (lock_dbf) {
lock_name = Z_STRVAL(args[0]);
} else {
spprintf(&lock_name, 0, "%s.lck", info->path);
if (!strcmp(file_mode, "r")) {
/* when in read only mode try to use existing .lck file first */
/* do not log errors for .lck file while in read ony mode on .lck file */
lock_file_mode = "rb";
info->lock.fp = php_stream_open_wrapper(lock_name, lock_file_mode, STREAM_MUST_SEEK|IGNORE_PATH|persistent_flag, &opened_path);
}
if (!info->lock.fp) {
/* when not in read mode or failed to open .lck file read only. now try again in create(write) mode and log errors */
lock_file_mode = "a+b";
} else {
if (opened_path) {
info->lock.name = pestrndup(ZSTR_VAL(opened_path), ZSTR_LEN(opened_path), persistent);
zend_string_release(opened_path);
}
}
}
if (!info->lock.fp) {
info->lock.fp = php_stream_open_wrapper(lock_name, lock_file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|persistent_flag, &opened_path);
if (info->lock.fp) {
if (lock_dbf) {
/* replace the path info with the real path of the opened file */
pefree(info->path, persistent);
info->path = pestrndup(ZSTR_VAL(opened_path), ZSTR_LEN(opened_path), persistent);
}
/* now store the name of the lock */
info->lock.name = pestrndup(ZSTR_VAL(opened_path), ZSTR_LEN(opened_path), persistent);
zend_string_release(opened_path);
}
}
if (!lock_dbf) {
efree(lock_name);
}
if (!info->lock.fp) {
2014-12-13 22:06:14 +00:00
dba_close(info);
/* stream operation already wrote an error message */
FREENOW;
RETURN_FALSE;
}
if (!php_stream_supports_lock(info->lock.fp)) {
error = "Stream does not support locking";
}
if (php_stream_lock(info->lock.fp, lock_mode)) {
error = "Unable to establish lock"; /* force failure exit */
}
}
/* centralised open stream for builtin */
if (!error && (hptr->flags&DBA_STREAM_OPEN)==DBA_STREAM_OPEN) {
if (info->lock.fp && lock_dbf) {
info->fp = info->lock.fp; /* use the same stream for locking and database access */
} else {
info->fp = php_stream_open_wrapper(info->path, file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|persistent_flag, NULL);
}
if (!info->fp) {
2014-12-13 22:06:14 +00:00
dba_close(info);
/* stream operation already wrote an error message */
FREENOW;
RETURN_FALSE;
}
if (hptr->flags & (DBA_NO_APPEND|DBA_CAST_AS_FD)) {
2015-01-03 09:22:58 +00:00
/* Needed because some systems do not allow to write to the original
* file contents with O_APPEND being set.
*/
if (SUCCESS != php_stream_cast(info->fp, PHP_STREAM_AS_FD, (void*)&info->fd, 1)) {
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "Could not cast stream");
dba_close(info);
FREENOW;
RETURN_FALSE;
#ifdef F_SETFL
} else if (modenr == DBA_CREAT) {
2015-04-08 12:53:28 +00:00
int flags = fcntl(info->fd, F_GETFL);
fcntl(info->fd, F_SETFL, flags & ~O_APPEND);
#elif defined(PHP_WIN32)
} else if (modenr == DBA_CREAT && !restarted) {
zend_bool close_both;
close_both = (info->fp != info->lock.fp);
php_stream_close(info->lock.fp);
if (close_both) {
php_stream_close(info->fp);
}
info->fp = NULL;
info->lock.fp = NULL;
info->fd = -1;
pefree(info->lock.name, persistent);
lock_file_mode = "r+b";
restarted = 1;
goto restart;
#endif
}
2015-01-03 09:22:58 +00:00
}
}
2014-12-13 22:06:14 +00:00
if (error || hptr->open(info, &error) != SUCCESS) {
dba_close(info);
php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Driver initialization failed for handler: %s%s%s", hptr->name, error?": ":"", error?error:"");
1999-07-21 15:12:32 +00:00
FREENOW;
RETURN_FALSE;
}
1999-07-21 15:12:32 +00:00
info->hnd = hptr;
info->argc = 0;
info->argv = NULL;
if (persistent) {
2015-04-08 12:47:31 +00:00
zval new_le;
2015-04-08 12:47:31 +00:00
ZVAL_NEW_PERSISTENT_RES(&new_le, -1, info, le_pdb);
if (zend_hash_str_update(&EG(persistent_list), key, keylen, &new_le) == NULL) {
2014-12-13 22:06:14 +00:00
dba_close(info);
php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Could not register persistent resource");
FREENOW;
RETURN_FALSE;
}
1999-07-21 15:12:32 +00:00
}
2015-02-03 04:07:36 +00:00
RETVAL_RES(zend_register_resource(info, (persistent ? le_pdb : le_db)));
1999-07-21 15:12:32 +00:00
FREENOW;
}
/* }}} */
#undef FREENOW
1999-07-21 15:12:32 +00:00
2003-06-13 14:59:01 +00:00
/* {{{ proto resource dba_popen(string path, string mode [, string handlername, string ...])
2000-02-24 16:30:42 +00:00
Opens path using the specified handler in mode persistently */
1999-07-21 15:12:32 +00:00
PHP_FUNCTION(dba_popen)
{
1999-12-17 21:34:28 +00:00
php_dba_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1999-07-21 15:12:32 +00:00
}
/* }}} */
2003-06-13 14:59:01 +00:00
/* {{{ proto resource dba_open(string path, string mode [, string handlername, string ...])
2000-02-24 16:30:42 +00:00
Opens path using the specified handler in mode*/
1999-07-21 15:12:32 +00:00
PHP_FUNCTION(dba_open)
{
1999-12-17 21:34:28 +00:00
php_dba_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1999-07-21 15:12:32 +00:00
}
/* }}} */
/* {{{ proto void dba_close(resource handle)
2000-02-24 16:30:42 +00:00
Closes database */
1999-07-21 15:12:32 +00:00
PHP_FUNCTION(dba_close)
{
2008-11-07 12:39:11 +00:00
zval *id;
dba_info *info = NULL;
2014-12-13 22:06:14 +00:00
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &id) == FAILURE) {
2008-11-07 12:39:11 +00:00
return;
}
DBA_FETCH_RESOURCE(info, id);
2008-11-07 12:39:11 +00:00
zend_list_close(Z_RES_P(id));
1999-07-21 15:12:32 +00:00
}
/* }}} */
/* {{{ proto bool dba_exists(string key, resource handle)
2000-02-24 16:30:42 +00:00
Checks, if the specified key exists */
1999-07-21 15:12:32 +00:00
PHP_FUNCTION(dba_exists)
{
DBA_ID_GET2;
2014-12-13 22:06:14 +00:00
if(info->hnd->exists(info, key_str, key_len) == SUCCESS) {
DBA_ID_DONE;
1999-07-21 15:12:32 +00:00
RETURN_TRUE;
}
DBA_ID_DONE;
1999-07-21 15:12:32 +00:00
RETURN_FALSE;
}
/* }}} */
/* {{{ proto string dba_fetch(string key, [int skip ,] resource handle)
2000-02-24 16:30:42 +00:00
Fetches the data associated with key */
1999-07-21 15:12:32 +00:00
PHP_FUNCTION(dba_fetch)
{
char *val;
int len = 0;
DBA_ID_GET2_3;
1999-07-21 15:12:32 +00:00
if (ac==3) {
if (!strcmp(info->hnd->name, "cdb")) {
if (skip < 0) {
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_NOTICE, "Handler %s accepts only skip values greater than or equal to zero, using skip=0", info->hnd->name);
skip = 0;
}
} else if (!strcmp(info->hnd->name, "inifile")) {
2015-01-03 09:22:58 +00:00
/* "-1" is compareable to 0 but allows a non restrictive
* access which is fater. For example 'inifile' uses this
* to allow faster access when the key was already found
* using firstkey/nextkey. However explicitly setting the
2015-01-03 09:22:58 +00:00
* value to 0 ensures the first value.
*/
if (skip < -1) {
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_NOTICE, "Handler %s accepts only skip value -1 and greater, using skip=0", info->hnd->name);
skip = 0;
}
} else {
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_NOTICE, "Handler %s does not support optional skip parameter, the value will be ignored", info->hnd->name);
2015-01-03 09:22:58 +00:00
skip = 0;
}
} else {
2015-01-03 09:22:58 +00:00
skip = 0;
}
2014-12-13 22:06:14 +00:00
if((val = info->hnd->fetch(info, key_str, key_len, skip, &len)) != NULL) {
DBA_ID_DONE;
RETVAL_STRINGL(val, len);
efree(val);
return;
2015-01-03 09:22:58 +00:00
}
DBA_ID_DONE;
1999-07-21 15:12:32 +00:00
RETURN_FALSE;
}
/* }}} */
2003-11-13 07:53:15 +00:00
/* {{{ proto array|false dba_key_split(string key)
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)
{
2003-11-13 07:53:15 +00:00
zval *zkey;
char *key, *name;
size_t key_len;
2003-11-13 07:53:15 +00:00
if (ZEND_NUM_ARGS() != 1) {
WRONG_PARAM_COUNT;
}
2014-12-13 22:06:14 +00:00
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "z", &zkey) == SUCCESS) {
if (Z_TYPE_P(zkey) == IS_NULL || (Z_TYPE_P(zkey) == IS_FALSE)) {
2003-11-13 07:53:15 +00:00
RETURN_BOOL(0);
}
}
2014-12-13 22:06:14 +00:00
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &key, &key_len) == FAILURE) {
2003-11-13 07:53:15 +00:00
RETURN_BOOL(0);
}
array_init(return_value);
if (key[0] == '[' && (name = strchr(key, ']')) != NULL) {
2014-04-15 11:40:40 +00:00
add_next_index_stringl(return_value, key+1, name - (key + 1));
add_next_index_stringl(return_value, name+1, key_len - (name - key + 1));
} else {
2014-04-15 11:40:40 +00:00
add_next_index_stringl(return_value, "", 0);
add_next_index_stringl(return_value, key, key_len);
}
}
/* }}} */
/* {{{ proto string dba_firstkey(resource handle)
2000-02-24 16:30:42 +00:00
Resets the internal key pointer and returns the first key */
1999-07-21 15:12:32 +00:00
PHP_FUNCTION(dba_firstkey)
{
char *fkey;
int len;
2008-11-07 12:39:11 +00:00
zval *id;
dba_info *info = NULL;
2014-12-13 22:06:14 +00:00
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &id) == FAILURE) {
2008-11-07 12:39:11 +00:00
return;
}
DBA_FETCH_RESOURCE(info, id);
1999-07-21 15:12:32 +00:00
2014-12-13 22:06:14 +00:00
fkey = info->hnd->firstkey(info, &len);
2008-11-07 12:39:11 +00:00
if (fkey) {
RETVAL_STRINGL(fkey, len);
efree(fkey);
return;
}
2008-11-07 12:39:11 +00:00
1999-07-21 15:12:32 +00:00
RETURN_FALSE;
}
/* }}} */
/* {{{ proto string dba_nextkey(resource handle)
2000-02-24 16:30:42 +00:00
Returns the next key */
1999-07-21 15:12:32 +00:00
PHP_FUNCTION(dba_nextkey)
{
char *nkey;
int len;
2008-11-07 12:39:11 +00:00
zval *id;
dba_info *info = NULL;
2014-12-13 22:06:14 +00:00
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &id) == FAILURE) {
2008-11-07 12:39:11 +00:00
return;
}
DBA_FETCH_RESOURCE(info, id);
1999-07-21 15:12:32 +00:00
2014-12-13 22:06:14 +00:00
nkey = info->hnd->nextkey(info, &len);
2008-11-07 12:39:11 +00:00
if (nkey) {
RETVAL_STRINGL(nkey, len);
efree(nkey);
return;
}
2008-11-07 12:39:11 +00:00
1999-07-21 15:12:32 +00:00
RETURN_FALSE;
}
/* }}} */
/* {{{ proto bool dba_delete(string key, resource handle)
Deletes the entry associated with key
If inifile: remove all other key lines */
1999-07-21 15:12:32 +00:00
PHP_FUNCTION(dba_delete)
{
DBA_ID_GET2;
2015-01-03 09:22:58 +00:00
DBA_WRITE_CHECK_WITH_ID;
2015-01-03 09:22:58 +00:00
2014-12-13 22:06:14 +00:00
if(info->hnd->delete(info, key_str, key_len) == SUCCESS)
{
DBA_ID_DONE;
1999-07-21 15:12:32 +00:00
RETURN_TRUE;
}
DBA_ID_DONE;
1999-07-21 15:12:32 +00:00
RETURN_FALSE;
}
/* }}} */
/* {{{ proto bool dba_insert(string key, string value, resource handle)
2015-01-03 09:22:58 +00:00
If not inifile: Insert value as key, return false, if key exists already
If inifile: Add vakue as key (next instance of key) */
1999-07-21 15:12:32 +00:00
PHP_FUNCTION(dba_insert)
{
1999-12-17 21:34:28 +00:00
php_dba_update(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1999-07-21 15:12:32 +00:00
}
/* }}} */
/* {{{ proto bool dba_replace(string key, string value, resource handle)
Inserts value as key, replaces key, if key exists already
If inifile: remove all other key lines */
1999-07-21 15:12:32 +00:00
PHP_FUNCTION(dba_replace)
{
1999-12-17 21:34:28 +00:00
php_dba_update(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1999-07-21 15:12:32 +00:00
}
/* }}} */
/* {{{ proto bool dba_optimize(resource handle)
2000-02-24 16:30:42 +00:00
Optimizes (e.g. clean up, vacuum) database */
1999-07-21 15:12:32 +00:00
PHP_FUNCTION(dba_optimize)
{
2008-11-07 12:39:11 +00:00
zval *id;
dba_info *info = NULL;
2014-12-13 22:06:14 +00:00
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &id) == FAILURE) {
2008-11-07 12:39:11 +00:00
return;
}
DBA_FETCH_RESOURCE(info, id);
2008-11-07 12:39:11 +00:00
1999-07-21 15:12:32 +00:00
DBA_WRITE_CHECK;
2008-11-07 12:39:11 +00:00
2014-12-13 22:06:14 +00:00
if (info->hnd->optimize(info) == SUCCESS) {
1999-07-21 15:12:32 +00:00
RETURN_TRUE;
}
2008-11-07 12:39:11 +00:00
1999-07-21 15:12:32 +00:00
RETURN_FALSE;
}
/* }}} */
/* {{{ proto bool dba_sync(resource handle)
2000-02-24 16:30:42 +00:00
Synchronizes database */
1999-07-21 15:12:32 +00:00
PHP_FUNCTION(dba_sync)
{
2008-11-07 12:39:11 +00:00
zval *id;
dba_info *info = NULL;
2014-12-13 22:06:14 +00:00
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &id) == FAILURE) {
2008-11-07 12:39:11 +00:00
return;
}
DBA_FETCH_RESOURCE(info, id);
2008-11-07 12:39:11 +00:00
2014-12-13 22:06:14 +00:00
if (info->hnd->sync(info) == SUCCESS) {
1999-07-21 15:12:32 +00:00
RETURN_TRUE;
}
2008-11-07 12:39:11 +00:00
1999-07-21 15:12:32 +00:00
RETURN_FALSE;
}
/* }}} */
/* {{{ proto array dba_handlers([bool full_info])
2003-05-19 22:52:27 +00:00
List configured database handlers */
PHP_FUNCTION(dba_handlers)
{
dba_handler *hptr;
zend_bool full_info = 0;
2014-12-13 22:06:14 +00:00
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &full_info) == FAILURE) {
RETURN_FALSE;
}
array_init(return_value);
for(hptr = handler; hptr->name; hptr++) {
if (full_info) {
2014-04-15 11:40:40 +00:00
// TODO: avoid reallocation ???
2014-12-13 22:06:14 +00:00
char *str = hptr->info(hptr, NULL);
2014-04-15 11:40:40 +00:00
add_assoc_string(return_value, hptr->name, str);
efree(str);
} else {
2014-04-15 11:40:40 +00:00
add_next_index_string(return_value, hptr->name);
}
}
}
/* }}} */
/* {{{ proto array dba_list()
2003-05-19 22:52:27 +00:00
List opened databases */
PHP_FUNCTION(dba_list)
{
2014-08-25 17:24:55 +00:00
zend_ulong numitems, i;
zend_resource *le;
dba_info *info;
2008-03-10 22:15:36 +00:00
if (zend_parse_parameters_none() == FAILURE) {
RETURN_FALSE;
}
array_init(return_value);
numitems = zend_hash_next_free_element(&EG(regular_list));
for (i=1; i<numitems; i++) {
if ((le = zend_hash_index_find_ptr(&EG(regular_list), i)) == NULL) {
continue;
}
if (le->type == le_db || le->type == le_pdb) {
info = (dba_info *)(le->ptr);
2014-04-15 11:40:40 +00:00
add_index_string(return_value, i, info->path);
}
}
}
/* }}} */
#endif /* HAVE_DBA */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/