12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262 |
- #ifdef HAVE_CONFIG_H
- #include "config.h"
- #endif
- #include "php.h"
- #if HAVE_DBA
- #include "php_ini.h"
- #include <stdio.h>
- #include <fcntl.h>
- #ifdef HAVE_SYS_FILE_H
- #include <sys/file.h>
- #endif
-
- #include "php_dba.h"
- #include "ext/standard/info.h"
- #include "ext/standard/php_string.h"
- #include "ext/standard/flock_compat.h"
- #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"
- #include "php_db4.h"
- #include "php_flatfile.h"
- #include "php_inifile.h"
- #include "php_qdbm.h"
- #include "php_tcadb.h"
- 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)
- 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)
- 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()
- const zend_function_entry dba_functions[] = {
- 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)
- PHP_FE_END
- };
- PHP_MINIT_FUNCTION(dba);
- PHP_MSHUTDOWN_FUNCTION(dba);
- PHP_MINFO_FUNCTION(dba);
- ZEND_BEGIN_MODULE_GLOBALS(dba)
- char *default_handler;
- dba_handler *default_hptr;
- 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)
- #endif
- static PHP_GINIT_FUNCTION(dba);
- zend_module_entry dba_module_entry = {
- STANDARD_MODULE_HEADER,
- "dba",
- dba_functions,
- PHP_MINIT(dba),
- PHP_MSHUTDOWN(dba),
- NULL,
- NULL,
- PHP_MINFO(dba),
- NO_VERSION_YET,
- PHP_MODULE_GLOBALS(dba),
- PHP_GINIT(dba),
- NULL,
- NULL,
- STANDARD_MODULE_PROPERTIES_EX
- };
- #ifdef COMPILE_DL_DBA
- ZEND_GET_MODULE(dba)
- #endif
- #define DBA_ID_PARS \
- zval *id; \
- dba_info *info = NULL; \
- int ac = ZEND_NUM_ARGS()
- static size_t php_dba_make_key(zval *key, char **key_str, char **key_free TSRMLS_DC)
- {
- if (Z_TYPE_P(key) == IS_ARRAY) {
- zval **group, **name;
- HashPosition pos;
- size_t len;
-
- if (zend_hash_num_elements(Z_ARRVAL_P(key)) != 2) {
- php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Key does not have exactly two elements: (key, name)");
- return -1;
- }
- zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(key), &pos);
- zend_hash_get_current_data_ex(Z_ARRVAL_P(key), (void **) &group, &pos);
- zend_hash_move_forward_ex(Z_ARRVAL_P(key), &pos);
- zend_hash_get_current_data_ex(Z_ARRVAL_P(key), (void **) &name, &pos);
- convert_to_string_ex(group);
- convert_to_string_ex(name);
- if (Z_STRLEN_PP(group) == 0) {
- *key_str = Z_STRVAL_PP(name);
- *key_free = NULL;
- return Z_STRLEN_PP(name);
- }
- len = spprintf(key_str, 0, "[%s]%s", Z_STRVAL_PP(group), Z_STRVAL_PP(name));
- *key_free = *key_str;
- return len;
- } else {
- zval tmp = *key;
- int len;
- zval_copy_ctor(&tmp);
- convert_to_string(&tmp);
- *key_free = *key_str = estrndup(Z_STRVAL(tmp), Z_STRLEN(tmp));
- len = Z_STRLEN(tmp);
- zval_dtor(&tmp);
- return len;
- }
- }
- #define DBA_GET2 \
- zval *key; \
- char *key_str, *key_free; \
- size_t key_len; \
- if (zend_parse_parameters(ac TSRMLS_CC, "zr", &key, &id) == FAILURE) { \
- return; \
- } \
- if ((key_len = php_dba_make_key(key, &key_str, &key_free TSRMLS_CC)) == 0) {\
- RETURN_FALSE; \
- }
- #define DBA_GET2_3 \
- zval *key; \
- char *key_str, *key_free; \
- size_t key_len; \
- long skip = 0; \
- switch(ac) { \
- case 2: \
- if (zend_parse_parameters(ac TSRMLS_CC, "zr", &key, &id) == FAILURE) { \
- return; \
- } \
- break; \
- case 3: \
- if (zend_parse_parameters(ac TSRMLS_CC, "zlr", &key, &skip, &id) == FAILURE) { \
- return; \
- } \
- break; \
- default: \
- WRONG_PARAM_COUNT; \
- } \
- if ((key_len = php_dba_make_key(key, &key_str, &key_free TSRMLS_CC)) == 0) {\
- RETURN_FALSE; \
- }
- #define DBA_FETCH_RESOURCE(info, id) \
- ZEND_FETCH_RESOURCE2(info, dba_info *, id, -1, "DBA identifier", le_db, le_pdb);
- #define DBA_ID_GET2 DBA_ID_PARS; DBA_GET2; DBA_FETCH_RESOURCE(info, &id)
- #define DBA_ID_GET2_3 DBA_ID_PARS; DBA_GET2_3; DBA_FETCH_RESOURCE(info, &id)
- #define DBA_ID_DONE \
- if (key_free) efree(key_free)
- #define DBA_NAMED_HND(alias, name, flags) \
- {\
- #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 \
- },
- #define DBA_HND(name, flags) DBA_NAMED_HND(name, name, flags)
- #define DBA_WRITE_CHECK \
- if(info->mode != DBA_WRITER && info->mode != DBA_TRUNC && info->mode != DBA_CREAT) { \
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "You cannot perform a modification to a database without proper access"); \
- RETURN_FALSE; \
- }
- #define DBA_WRITE_CHECK_WITH_ID \
- if(info->mode != DBA_WRITER && info->mode != DBA_TRUNC && info->mode != DBA_CREAT) { \
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "You cannot perform a modification to a database without proper access"); \
- DBA_ID_DONE; \
- RETURN_FALSE; \
- }
- static dba_handler handler[] = {
- #if DBA_GDBM
- DBA_HND(gdbm, DBA_LOCK_EXT)
- #endif
- #if DBA_DBM
- DBA_HND(dbm, DBA_LOCK_ALL)
- #endif
- #if DBA_NDBM
- DBA_HND(ndbm, DBA_LOCK_ALL)
- #endif
- #if DBA_CDB
- DBA_HND(cdb, DBA_STREAM_OPEN|DBA_LOCK_ALL)
- #endif
- #if DBA_CDB_BUILTIN
- DBA_NAMED_HND(cdb_make, cdb, DBA_STREAM_OPEN|DBA_LOCK_ALL)
- #endif
- #if DBA_DB1
- DBA_HND(db1, DBA_LOCK_ALL)
- #endif
- #if DBA_DB2
- DBA_HND(db2, DBA_LOCK_ALL)
- #endif
- #if DBA_DB3
- DBA_HND(db3, DBA_LOCK_ALL)
- #endif
- #if DBA_DB4
- DBA_HND(db4, DBA_LOCK_ALL)
- #endif
- #if DBA_INIFILE
- DBA_HND(inifile, DBA_STREAM_OPEN|DBA_LOCK_ALL|DBA_CAST_AS_FD)
- #endif
- #if DBA_FLATFILE
- DBA_HND(flatfile, DBA_STREAM_OPEN|DBA_LOCK_ALL|DBA_NO_APPEND)
- #endif
- #if DBA_QDBM
- DBA_HND(qdbm, DBA_LOCK_EXT)
- #endif
- #if DBA_TCADB
- DBA_HND(tcadb, DBA_LOCK_ALL)
- #endif
- { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
- };
- #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
- static int le_db;
- static int le_pdb;
-
- static void dba_close(dba_info *info TSRMLS_DC)
- {
- if (info->hnd) {
- info->hnd->close(info TSRMLS_CC);
- }
- if (info->path) {
- pefree(info->path, info->flags&DBA_PERSISTENT);
- }
- if (info->fp && info->fp!=info->lock.fp) {
- if(info->flags&DBA_PERSISTENT) {
- php_stream_pclose(info->fp);
- } else {
- php_stream_close(info->fp);
- }
- }
- if (info->lock.fp) {
- if(info->flags&DBA_PERSISTENT) {
- php_stream_pclose(info->lock.fp);
- } else {
- php_stream_close(info->lock.fp);
- }
- }
- if (info->lock.name) {
- pefree(info->lock.name, info->flags&DBA_PERSISTENT);
- }
- pefree(info, info->flags&DBA_PERSISTENT);
- }
- static void dba_close_rsrc(zend_rsrc_list_entry *rsrc TSRMLS_DC)
- {
- dba_info *info = (dba_info *)rsrc->ptr;
- dba_close(info TSRMLS_CC);
- }
- int dba_close_pe_rsrc_deleter(zend_rsrc_list_entry *le, void *pDba TSRMLS_DC)
- {
- return le->ptr == pDba;
- }
- static void dba_close_pe_rsrc(zend_rsrc_list_entry *rsrc TSRMLS_DC)
- {
- dba_info *info = (dba_info *)rsrc->ptr;
-
- zend_hash_apply_with_argument(&EG(persistent_list), (apply_func_arg_t) dba_close_pe_rsrc_deleter, info TSRMLS_CC);
- }
- ZEND_INI_MH(OnUpdateDefaultHandler)
- {
- dba_handler *hptr;
- if (!strlen(new_value)) {
- DBA_G(default_hptr) = NULL;
- return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
- }
- for (hptr = handler; hptr->name && strcasecmp(hptr->name, new_value); hptr++);
- if (!hptr->name) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "No such handler: %s", new_value);
- return FAILURE;
- }
- DBA_G(default_hptr) = hptr;
- return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
- }
- 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()
-
- static PHP_GINIT_FUNCTION(dba)
- {
- dba_globals->default_handler = "";
- dba_globals->default_hptr = NULL;
- }
- PHP_MINIT_FUNCTION(dba)
- {
- REGISTER_INI_ENTRIES();
- le_db = zend_register_list_destructors_ex(dba_close_rsrc, NULL, "dba", module_number);
- le_pdb = zend_register_list_destructors_ex(dba_close_pe_rsrc, dba_close_rsrc, "dba persistent", module_number);
- return SUCCESS;
- }
- PHP_MSHUTDOWN_FUNCTION(dba)
- {
- UNREGISTER_INI_ENTRIES();
- return SUCCESS;
- }
- #include "ext/standard/php_smart_str.h"
- PHP_MINFO_FUNCTION(dba)
- {
- dba_handler *hptr;
- smart_str handlers = {0};
- for(hptr = handler; hptr->name; hptr++) {
- smart_str_appends(&handlers, hptr->name);
- smart_str_appendc(&handlers, ' ');
- }
- php_info_print_table_start();
- php_info_print_table_row(2, "DBA support", "enabled");
- if (handlers.c) {
- smart_str_0(&handlers);
- php_info_print_table_row(2, "Supported handlers", handlers.c);
- smart_str_free(&handlers);
- } else {
- php_info_print_table_row(2, "Supported handlers", "none");
- }
- php_info_print_table_end();
- DISPLAY_INI_ENTRIES();
- }
- static void php_dba_update(INTERNAL_FUNCTION_PARAMETERS, int mode)
- {
- int val_len;
- zval *id;
- dba_info *info = NULL;
- int ac = ZEND_NUM_ARGS();
- zval *key;
- char *val;
- char *key_str, *key_free;
- size_t key_len;
- if (zend_parse_parameters(ac TSRMLS_CC, "zsr", &key, &val, &val_len, &id) == FAILURE) {
- return;
- }
- if ((key_len = php_dba_make_key(key, &key_str, &key_free TSRMLS_CC)) == 0) {
- RETURN_FALSE;
- }
- DBA_FETCH_RESOURCE(info, &id);
- DBA_WRITE_CHECK_WITH_ID;
- if (info->hnd->update(info, key_str, key_len, val, val_len, mode TSRMLS_CC) == SUCCESS) {
- DBA_ID_DONE;
- RETURN_TRUE;
- }
- DBA_ID_DONE;
- RETURN_FALSE;
- }
- #define FREENOW if(args) efree(args); if(key) efree(key)
- dba_info *php_dba_find(const char* path TSRMLS_DC)
- {
- zend_rsrc_list_entry *le;
- dba_info *info;
- int numitems, i;
- numitems = zend_hash_next_free_element(&EG(regular_list));
- for (i=1; i<numitems; i++) {
- if (zend_hash_index_find(&EG(regular_list), i, (void **) &le)==FAILURE) {
- continue;
- }
- if (Z_TYPE_P(le) == le_db || Z_TYPE_P(le) == le_pdb) {
- info = (dba_info *)(le->ptr);
- if (!strcmp(info->path, path)) {
- return (dba_info *)(le->ptr);
- }
- }
- }
- return NULL;
- }
- static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, int persistent)
- {
- zval ***args = (zval ***) NULL;
- int ac = ZEND_NUM_ARGS();
- dba_mode_t modenr;
- dba_info *info, *other;
- dba_handler *hptr;
- char *key = NULL, *error = NULL;
- int keylen = 0;
- int i;
- int lock_mode, lock_flag, lock_dbf = 0;
- char *file_mode;
- char mode[4], *pmode, *lock_file_mode = NULL;
- int persistent_flag = persistent ? STREAM_OPEN_PERSISTENT : 0;
- char *opened_path = NULL;
- char *lock_name;
-
- if(ac < 2) {
- WRONG_PARAM_COUNT;
- }
-
-
- args = safe_emalloc(ac, sizeof(zval *), 0);
- if (zend_get_parameters_array_ex(ac, args) != SUCCESS) {
- FREENOW;
- WRONG_PARAM_COUNT;
- }
-
-
- for (i = 0; i < ac; i++) {
- convert_to_string_ex(args[i]);
- keylen += Z_STRLEN_PP(args[i]);
- }
- if (persistent) {
- zend_rsrc_list_entry *le;
-
-
- key = safe_emalloc(keylen, 1, 1);
- key[keylen] = '\0';
- keylen = 0;
-
- for(i = 0; i < ac; i++) {
- memcpy(key+keylen, Z_STRVAL_PP(args[i]), Z_STRLEN_PP(args[i]));
- keylen += Z_STRLEN_PP(args[i]);
- }
-
- if (zend_hash_find(&EG(persistent_list), key, keylen+1, (void **) &le) == SUCCESS) {
- FREENOW;
-
- if (Z_TYPE_P(le) != le_pdb) {
- RETURN_FALSE;
- }
-
- info = (dba_info *)le->ptr;
- ZEND_REGISTER_RESOURCE(return_value, info, le_pdb);
- return;
- }
- }
-
- if (ac==2) {
- hptr = DBA_G(default_hptr);
- if (!hptr) {
- php_error_docref2(NULL TSRMLS_CC, Z_STRVAL_PP(args[0]), Z_STRVAL_PP(args[1]), E_WARNING, "No default handler selected");
- FREENOW;
- RETURN_FALSE;
- }
- } else {
- for (hptr = handler; hptr->name && strcasecmp(hptr->name, Z_STRVAL_PP(args[2])); hptr++);
- }
- if (!hptr->name) {
- php_error_docref2(NULL TSRMLS_CC, Z_STRVAL_PP(args[0]), Z_STRVAL_PP(args[1]), E_WARNING, "No such handler: %s", Z_STRVAL_PP(args[2]));
- FREENOW;
- RETURN_FALSE;
- }
-
- strlcpy(mode, Z_STRVAL_PP(args[1]), sizeof(mode));
- pmode = &mode[0];
- if (pmode[0] && (pmode[1]=='d' || pmode[1]=='l' || pmode[1]=='-')) {
- switch (pmode[1]) {
- case 'd':
- lock_dbf = 1;
- if ((hptr->flags & DBA_LOCK_ALL) == 0) {
- lock_flag = (hptr->flags & DBA_LOCK_ALL);
- break;
- }
-
- case 'l':
- lock_flag = DBA_LOCK_ALL;
- if ((hptr->flags & DBA_LOCK_ALL) == 0) {
- php_error_docref2(NULL TSRMLS_CC, Z_STRVAL_PP(args[0]), Z_STRVAL_PP(args[1]), E_NOTICE, "Handler %s does locking internally", hptr->name);
- }
- break;
- default:
- case '-':
- if ((hptr->flags & DBA_LOCK_ALL) == 0) {
- php_error_docref2(NULL TSRMLS_CC, Z_STRVAL_PP(args[0]), Z_STRVAL_PP(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++) {
- case 'r':
- modenr = DBA_READER;
- lock_mode = (lock_flag & DBA_LOCK_READER) ? LOCK_SH : 0;
- file_mode = "r";
- break;
- case 'w':
- modenr = DBA_WRITER;
- lock_mode = (lock_flag & DBA_LOCK_WRITER) ? LOCK_EX : 0;
- file_mode = "r+b";
- break;
- case 'c':
- modenr = DBA_CREAT;
- lock_mode = (lock_flag & DBA_LOCK_CREAT) ? LOCK_EX : 0;
- if (lock_mode) {
- if (lock_dbf) {
-
- file_mode = "r+b";
- lock_file_mode = "a+b";
- } else {
- file_mode = "a+b";
- lock_file_mode = "w+b";
- }
- } else {
- file_mode = "a+b";
- }
-
- break;
- case 'n':
- modenr = DBA_TRUNC;
- lock_mode = (lock_flag & DBA_LOCK_TRUNC) ? LOCK_EX : 0;
- file_mode = "w+b";
- break;
- default:
- php_error_docref2(NULL TSRMLS_CC, Z_STRVAL_PP(args[0]), Z_STRVAL_PP(args[1]), E_WARNING, "Illegal DBA mode");
- FREENOW;
- RETURN_FALSE;
- }
- if (!lock_file_mode) {
- lock_file_mode = file_mode;
- }
- if (*pmode=='d' || *pmode=='l' || *pmode=='-') {
- pmode++;
- }
- if (*pmode=='t') {
- pmode++;
- if (!lock_flag) {
- php_error_docref2(NULL TSRMLS_CC, Z_STRVAL_PP(args[0]), Z_STRVAL_PP(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) {
- php_error_docref2(NULL TSRMLS_CC, Z_STRVAL_PP(args[0]), Z_STRVAL_PP(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 {
- php_error_docref2(NULL TSRMLS_CC, Z_STRVAL_PP(args[0]), Z_STRVAL_PP(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;
- }
- }
- if (*pmode) {
- php_error_docref2(NULL TSRMLS_CC, Z_STRVAL_PP(args[0]), Z_STRVAL_PP(args[1]), E_WARNING, "Illegal DBA mode");
- FREENOW;
- RETURN_FALSE;
- }
-
- info = pemalloc(sizeof(dba_info), persistent);
- memset(info, 0, sizeof(dba_info));
- info->path = pestrdup(Z_STRVAL_PP(args[0]), persistent);
- 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 (hptr->flags & DBA_LOCK_ALL) {
- if ((other = php_dba_find(info->path TSRMLS_CC)) != 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)";
- }
- }
- }
- if (!error && lock_mode) {
- if (lock_dbf) {
- lock_name = Z_STRVAL_PP(args[0]);
- } else {
- spprintf(&lock_name, 0, "%s.lck", info->path);
- if (!strcmp(file_mode, "r")) {
-
-
- 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) {
-
- lock_file_mode = "a+b";
- } else {
- if (!persistent) {
- info->lock.name = opened_path;
- } else {
- if (opened_path) {
- info->lock.name = pestrdup(opened_path, persistent);
- efree(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) {
-
- pefree(info->path, persistent);
- info->path = pestrdup(opened_path, persistent);
- }
-
- if (!persistent) {
- info->lock.name = opened_path;
- } else {
- info->lock.name = pestrdup(opened_path, persistent);
- efree(opened_path);
- }
- }
- }
- if (!lock_dbf) {
- efree(lock_name);
- }
- if (!info->lock.fp) {
- dba_close(info TSRMLS_CC);
-
- 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";
- }
- }
-
- if (!error && (hptr->flags&DBA_STREAM_OPEN)==DBA_STREAM_OPEN) {
- if (info->lock.fp && lock_dbf) {
- info->fp = info->lock.fp;
- } else {
- info->fp = php_stream_open_wrapper(info->path, file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|persistent_flag, NULL);
- }
- if (!info->fp) {
- dba_close(info TSRMLS_CC);
-
- FREENOW;
- RETURN_FALSE;
- }
- if (hptr->flags & (DBA_NO_APPEND|DBA_CAST_AS_FD)) {
-
- if (SUCCESS != php_stream_cast(info->fp, PHP_STREAM_AS_FD, (void*)&info->fd, 1)) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not cast stream");
- dba_close(info TSRMLS_CC);
- FREENOW;
- RETURN_FALSE;
- #ifdef F_SETFL
- } else if (modenr == DBA_CREAT) {
- int flags = fcntl(info->fd, F_GETFL);
- fcntl(info->fd, F_SETFL, flags & ~O_APPEND);
- #endif
- }
-
- }
- }
- if (error || hptr->open(info, &error TSRMLS_CC) != SUCCESS) {
- dba_close(info TSRMLS_CC);
- php_error_docref2(NULL TSRMLS_CC, Z_STRVAL_PP(args[0]), Z_STRVAL_PP(args[1]), E_WARNING, "Driver initialization failed for handler: %s%s%s", hptr->name, error?": ":"", error?error:"");
- FREENOW;
- RETURN_FALSE;
- }
- info->hnd = hptr;
- info->argc = 0;
- info->argv = NULL;
- if (persistent) {
- zend_rsrc_list_entry new_le;
- Z_TYPE(new_le) = le_pdb;
- new_le.ptr = info;
- if (zend_hash_update(&EG(persistent_list), key, keylen+1, &new_le, sizeof(zend_rsrc_list_entry), NULL) == FAILURE) {
- dba_close(info TSRMLS_CC);
- php_error_docref2(NULL TSRMLS_CC, Z_STRVAL_PP(args[0]), Z_STRVAL_PP(args[1]), E_WARNING, "Could not register persistent resource");
- FREENOW;
- RETURN_FALSE;
- }
- }
- ZEND_REGISTER_RESOURCE(return_value, info, (persistent ? le_pdb : le_db));
- FREENOW;
- }
- #undef FREENOW
- PHP_FUNCTION(dba_popen)
- {
- php_dba_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
- }
- PHP_FUNCTION(dba_open)
- {
- php_dba_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
- }
- PHP_FUNCTION(dba_close)
- {
- zval *id;
- dba_info *info = NULL;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &id) == FAILURE) {
- return;
- }
- DBA_FETCH_RESOURCE(info, &id);
- zend_list_delete(Z_RESVAL_P(id));
- }
- PHP_FUNCTION(dba_exists)
- {
- DBA_ID_GET2;
- if(info->hnd->exists(info, key_str, key_len TSRMLS_CC) == SUCCESS) {
- DBA_ID_DONE;
- RETURN_TRUE;
- }
- DBA_ID_DONE;
- RETURN_FALSE;
- }
- PHP_FUNCTION(dba_fetch)
- {
- char *val;
- int len = 0;
- DBA_ID_GET2_3;
- if (ac==3) {
- if (!strcmp(info->hnd->name, "cdb")) {
- if (skip < 0) {
- php_error_docref(NULL TSRMLS_CC, 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")) {
-
- if (skip < -1) {
- php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Handler %s accepts only skip value -1 and greater, using skip=0", info->hnd->name);
- skip = 0;
- }
- } else {
- php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Handler %s does not support optional skip parameter, the value will be ignored", info->hnd->name);
- skip = 0;
- }
- } else {
- skip = 0;
- }
- if((val = info->hnd->fetch(info, key_str, key_len, skip, &len TSRMLS_CC)) != NULL) {
- DBA_ID_DONE;
- RETURN_STRINGL(val, len, 0);
- }
- DBA_ID_DONE;
- RETURN_FALSE;
- }
- PHP_FUNCTION(dba_key_split)
- {
- zval *zkey;
- char *key, *name;
- int key_len;
- if (ZEND_NUM_ARGS() != 1) {
- WRONG_PARAM_COUNT;
- }
- if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "z", &zkey) == SUCCESS) {
- if (Z_TYPE_P(zkey) == IS_NULL || (Z_TYPE_P(zkey) == IS_BOOL && !Z_LVAL_P(zkey))) {
- RETURN_BOOL(0);
- }
- }
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &key, &key_len) == FAILURE) {
- RETURN_BOOL(0);
- }
- array_init(return_value);
- if (key[0] == '[' && (name = strchr(key, ']')) != NULL) {
- add_next_index_stringl(return_value, key+1, name - (key + 1), 1);
- add_next_index_stringl(return_value, name+1, key_len - (name - key + 1), 1);
- } else {
- add_next_index_stringl(return_value, "", 0, 1);
- add_next_index_stringl(return_value, key, key_len, 1);
- }
- }
- PHP_FUNCTION(dba_firstkey)
- {
- char *fkey;
- int len;
- zval *id;
- dba_info *info = NULL;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &id) == FAILURE) {
- return;
- }
- DBA_FETCH_RESOURCE(info, &id);
- fkey = info->hnd->firstkey(info, &len TSRMLS_CC);
- if (fkey)
- RETURN_STRINGL(fkey, len, 0);
- RETURN_FALSE;
- }
- PHP_FUNCTION(dba_nextkey)
- {
- char *nkey;
- int len;
- zval *id;
- dba_info *info = NULL;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &id) == FAILURE) {
- return;
- }
- DBA_FETCH_RESOURCE(info, &id);
- nkey = info->hnd->nextkey(info, &len TSRMLS_CC);
- if (nkey)
- RETURN_STRINGL(nkey, len, 0);
- RETURN_FALSE;
- }
- PHP_FUNCTION(dba_delete)
- {
- DBA_ID_GET2;
-
- DBA_WRITE_CHECK_WITH_ID;
-
- if(info->hnd->delete(info, key_str, key_len TSRMLS_CC) == SUCCESS)
- {
- DBA_ID_DONE;
- RETURN_TRUE;
- }
- DBA_ID_DONE;
- RETURN_FALSE;
- }
- PHP_FUNCTION(dba_insert)
- {
- php_dba_update(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
- }
- PHP_FUNCTION(dba_replace)
- {
- php_dba_update(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
- }
- PHP_FUNCTION(dba_optimize)
- {
- zval *id;
- dba_info *info = NULL;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &id) == FAILURE) {
- return;
- }
- DBA_FETCH_RESOURCE(info, &id);
- DBA_WRITE_CHECK;
- if (info->hnd->optimize(info TSRMLS_CC) == SUCCESS) {
- RETURN_TRUE;
- }
- RETURN_FALSE;
- }
- PHP_FUNCTION(dba_sync)
- {
- zval *id;
- dba_info *info = NULL;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &id) == FAILURE) {
- return;
- }
- DBA_FETCH_RESOURCE(info, &id);
- if (info->hnd->sync(info TSRMLS_CC) == SUCCESS) {
- RETURN_TRUE;
- }
- RETURN_FALSE;
- }
- PHP_FUNCTION(dba_handlers)
- {
- dba_handler *hptr;
- zend_bool full_info = 0;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &full_info) == FAILURE) {
- RETURN_FALSE;
- }
- array_init(return_value);
- for(hptr = handler; hptr->name; hptr++) {
- if (full_info) {
- add_assoc_string(return_value, hptr->name, hptr->info(hptr, NULL TSRMLS_CC), 0);
- } else {
- add_next_index_string(return_value, hptr->name, 1);
- }
- }
- }
- PHP_FUNCTION(dba_list)
- {
- ulong numitems, i;
- zend_rsrc_list_entry *le;
- dba_info *info;
- 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 (zend_hash_index_find(&EG(regular_list), i, (void **) &le)==FAILURE) {
- continue;
- }
- if (Z_TYPE_P(le) == le_db || Z_TYPE_P(le) == le_pdb) {
- info = (dba_info *)(le->ptr);
- add_index_string(return_value, i, info->path, 1);
- }
- }
- }
- #endif
|