Back to index

php5  5.3.10
dba.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Authors: Sascha Schumann <sascha@schumann.cx>                        |
00016    |          Marcus Boerger <helly@php.net>                              |
00017    +----------------------------------------------------------------------+
00018  */
00019 
00020 /* $Id: dba.c 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 #ifdef HAVE_CONFIG_H
00023 #include "config.h"
00024 #endif
00025 
00026 #include "php.h"
00027 
00028 #if HAVE_DBA
00029 
00030 #include "php_ini.h"
00031 #include <stdio.h> 
00032 #include <fcntl.h>
00033 #ifdef HAVE_SYS_FILE_H
00034 #include <sys/file.h>
00035 #endif
00036  
00037 #include "php_dba.h"
00038 #include "ext/standard/info.h"
00039 #include "ext/standard/php_string.h"
00040 #include "ext/standard/flock_compat.h"
00041 
00042 #include "php_gdbm.h"
00043 #include "php_ndbm.h"
00044 #include "php_dbm.h"
00045 #include "php_cdb.h"
00046 #include "php_db1.h"
00047 #include "php_db2.h"
00048 #include "php_db3.h"
00049 #include "php_db4.h"
00050 #include "php_flatfile.h"
00051 #include "php_inifile.h"
00052 #include "php_qdbm.h"
00053 
00054 /* {{{ arginfo */
00055 ZEND_BEGIN_ARG_INFO_EX(arginfo_dba_popen, 0, 0, 2)
00056        ZEND_ARG_INFO(0, path)
00057        ZEND_ARG_INFO(0, mode)
00058        ZEND_ARG_INFO(0, handlername)
00059        ZEND_ARG_INFO(0, ...)
00060 ZEND_END_ARG_INFO()
00061 
00062 ZEND_BEGIN_ARG_INFO_EX(arginfo_dba_open, 0, 0, 2)
00063        ZEND_ARG_INFO(0, path)
00064        ZEND_ARG_INFO(0, mode)
00065        ZEND_ARG_INFO(0, handlername)
00066        ZEND_ARG_INFO(0, ...)
00067 ZEND_END_ARG_INFO()
00068 
00069 ZEND_BEGIN_ARG_INFO(arginfo_dba_close, 0)
00070        ZEND_ARG_INFO(0, handle)
00071 ZEND_END_ARG_INFO()
00072 
00073 ZEND_BEGIN_ARG_INFO(arginfo_dba_exists, 0)
00074        ZEND_ARG_INFO(0, key)
00075        ZEND_ARG_INFO(0, handle)
00076 ZEND_END_ARG_INFO()
00077 
00078 ZEND_BEGIN_ARG_INFO_EX(arginfo_dba_fetch, 0, 0, 2)
00079        ZEND_ARG_INFO(0, key)
00080        ZEND_ARG_INFO(0, skip)
00081        ZEND_ARG_INFO(0, handle)
00082 ZEND_END_ARG_INFO()
00083 
00084 ZEND_BEGIN_ARG_INFO(arginfo_dba_key_split, 0)
00085        ZEND_ARG_INFO(0, key)
00086 ZEND_END_ARG_INFO()
00087 
00088 ZEND_BEGIN_ARG_INFO(arginfo_dba_firstkey, 0)
00089        ZEND_ARG_INFO(0, handle)
00090 ZEND_END_ARG_INFO()
00091 
00092 ZEND_BEGIN_ARG_INFO(arginfo_dba_nextkey, 0)
00093        ZEND_ARG_INFO(0, handle)
00094 ZEND_END_ARG_INFO()
00095 
00096 ZEND_BEGIN_ARG_INFO(arginfo_dba_delete, 0)
00097        ZEND_ARG_INFO(0, key)
00098        ZEND_ARG_INFO(0, handle)
00099 ZEND_END_ARG_INFO()
00100 
00101 ZEND_BEGIN_ARG_INFO(arginfo_dba_insert, 0)
00102        ZEND_ARG_INFO(0, key)
00103        ZEND_ARG_INFO(0, value)
00104        ZEND_ARG_INFO(0, handle)
00105 ZEND_END_ARG_INFO()
00106 
00107 ZEND_BEGIN_ARG_INFO(arginfo_dba_replace, 0)
00108        ZEND_ARG_INFO(0, key)
00109        ZEND_ARG_INFO(0, value)
00110        ZEND_ARG_INFO(0, handle)
00111 ZEND_END_ARG_INFO()
00112 
00113 ZEND_BEGIN_ARG_INFO(arginfo_dba_optimize, 0)
00114        ZEND_ARG_INFO(0, handle)
00115 ZEND_END_ARG_INFO()
00116 
00117 ZEND_BEGIN_ARG_INFO(arginfo_dba_sync, 0)
00118        ZEND_ARG_INFO(0, handle)
00119 ZEND_END_ARG_INFO()
00120 
00121 ZEND_BEGIN_ARG_INFO_EX(arginfo_dba_handlers, 0, 0, 0)
00122        ZEND_ARG_INFO(0, full_info)
00123 ZEND_END_ARG_INFO()
00124 
00125 ZEND_BEGIN_ARG_INFO(arginfo_dba_list, 0)
00126 ZEND_END_ARG_INFO()
00127 
00128 /* }}} */
00129 
00130 /* {{{ dba_functions[]
00131  */
00132 const zend_function_entry dba_functions[] = {
00133        PHP_FE(dba_open, arginfo_dba_open)
00134        PHP_FE(dba_popen, arginfo_dba_popen)
00135        PHP_FE(dba_close, arginfo_dba_close)
00136        PHP_FE(dba_delete, arginfo_dba_delete)
00137        PHP_FE(dba_exists, arginfo_dba_exists)
00138        PHP_FE(dba_fetch, arginfo_dba_fetch)
00139        PHP_FE(dba_insert, arginfo_dba_insert)
00140        PHP_FE(dba_replace, arginfo_dba_replace)
00141        PHP_FE(dba_firstkey, arginfo_dba_firstkey)
00142        PHP_FE(dba_nextkey, arginfo_dba_nextkey)
00143        PHP_FE(dba_optimize, arginfo_dba_optimize)
00144        PHP_FE(dba_sync, arginfo_dba_sync)
00145        PHP_FE(dba_handlers, arginfo_dba_handlers)
00146        PHP_FE(dba_list, arginfo_dba_list)
00147        PHP_FE(dba_key_split, arginfo_dba_key_split)
00148        PHP_FE_END
00149 };
00150 /* }}} */
00151 
00152 PHP_MINIT_FUNCTION(dba);
00153 PHP_MSHUTDOWN_FUNCTION(dba);
00154 PHP_MINFO_FUNCTION(dba);
00155 
00156 ZEND_BEGIN_MODULE_GLOBALS(dba)
00157        char *default_handler;
00158        dba_handler *default_hptr;
00159 ZEND_END_MODULE_GLOBALS(dba) 
00160 
00161 ZEND_DECLARE_MODULE_GLOBALS(dba)
00162 
00163 #ifdef ZTS
00164 #define DBA_G(v) TSRMG(dba_globals_id, zend_dba_globals *, v)
00165 #else
00166 #define DBA_G(v) (dba_globals.v)
00167 #endif 
00168 
00169 static PHP_GINIT_FUNCTION(dba);
00170 
00171 zend_module_entry dba_module_entry = {
00172        STANDARD_MODULE_HEADER,
00173        "dba",
00174        dba_functions, 
00175        PHP_MINIT(dba), 
00176        PHP_MSHUTDOWN(dba),
00177        NULL,
00178        NULL,
00179        PHP_MINFO(dba),
00180        NO_VERSION_YET,
00181        PHP_MODULE_GLOBALS(dba),
00182        PHP_GINIT(dba),
00183        NULL,
00184        NULL,
00185        STANDARD_MODULE_PROPERTIES_EX
00186 };
00187 
00188 #ifdef COMPILE_DL_DBA
00189 ZEND_GET_MODULE(dba)
00190 #endif
00191 
00192 /* {{{ macromania */
00193 
00194 #define DBA_ID_PARS                                                                        \
00195        zval *id;                                                                                         \
00196        dba_info *info = NULL;                                                                     \
00197        int ac = ZEND_NUM_ARGS()
00198 
00199 /* these are used to get the standard arguments */
00200 
00201 /* {{{ php_dba_myke_key */
00202 static size_t php_dba_make_key(zval *key, char **key_str, char **key_free TSRMLS_DC)
00203 {
00204        if (Z_TYPE_P(key) == IS_ARRAY) {
00205               zval **group, **name;
00206               HashPosition pos;
00207               size_t len;
00208        
00209               if (zend_hash_num_elements(Z_ARRVAL_P(key)) != 2) {
00210                      php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Key does not have exactly two elements: (key, name)");
00211                      return -1;
00212               }
00213               zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(key), &pos);
00214               zend_hash_get_current_data_ex(Z_ARRVAL_P(key), (void **) &group, &pos);
00215               zend_hash_move_forward_ex(Z_ARRVAL_P(key), &pos);
00216               zend_hash_get_current_data_ex(Z_ARRVAL_P(key), (void **) &name, &pos);
00217               convert_to_string_ex(group);
00218               convert_to_string_ex(name);
00219               if (Z_STRLEN_PP(group) == 0) {
00220                      *key_str = Z_STRVAL_PP(name);
00221                      *key_free = NULL;
00222                      return Z_STRLEN_PP(name);
00223               }
00224               len = spprintf(key_str, 0, "[%s]%s", Z_STRVAL_PP(group), Z_STRVAL_PP(name));
00225               *key_free = *key_str;
00226               return len;
00227        } else {
00228               *key_free = NULL;
00229 
00230               convert_to_string(key);
00231               *key_str = Z_STRVAL_P(key);
00232 
00233               return Z_STRLEN_P(key);
00234        }
00235 }
00236 /* }}} */
00237 
00238 #define DBA_GET2                                                                                  \
00239        zval *key;                                                                                        \
00240        char *key_str, *key_free;                                                           \
00241        size_t key_len;                                                                            \
00242        if (zend_parse_parameters(ac TSRMLS_CC, "zr", &key, &id) == FAILURE) {       \
00243               return;                                                                                    \
00244        }                                                                                                        \
00245        if ((key_len = php_dba_make_key(key, &key_str, &key_free TSRMLS_CC)) == 0) {\
00246               RETURN_FALSE;                                                                       \
00247        }
00248 
00249 #define DBA_GET2_3                                                                                \
00250        zval *key;                                                                                        \
00251        char *key_str, *key_free;                                                           \
00252        size_t key_len;                                                                            \
00253        long skip = 0;                                                                             \
00254        switch(ac) {                                                                               \
00255        case 2:                                                                                           \
00256               if (zend_parse_parameters(ac TSRMLS_CC, "zr", &key, &id) == FAILURE) { \
00257                      return;                                                                                    \
00258               }                                                                                                 \
00259               break;                                                                                     \
00260        case 3:                                                                                           \
00261               if (zend_parse_parameters(ac TSRMLS_CC, "zlr", &key, &skip, &id) == FAILURE) { \
00262                      return;                                                                                    \
00263               }                                                                                                 \
00264               break;                                                                                     \
00265        default:                                                                                          \
00266               WRONG_PARAM_COUNT;                                                                  \
00267        }                                                                                                        \
00268        if ((key_len = php_dba_make_key(key, &key_str, &key_free TSRMLS_CC)) == 0) {\
00269               RETURN_FALSE;                                                                       \
00270        }
00271 
00272 
00273 #define DBA_FETCH_RESOURCE(info, id)      \
00274        ZEND_FETCH_RESOURCE2(info, dba_info *, id, -1, "DBA identifier", le_db, le_pdb);
00275 
00276 #define DBA_ID_GET2   DBA_ID_PARS; DBA_GET2;   DBA_FETCH_RESOURCE(info, &id)
00277 #define DBA_ID_GET2_3 DBA_ID_PARS; DBA_GET2_3; DBA_FETCH_RESOURCE(info, &id)
00278 
00279 #define DBA_ID_DONE                                                                               \
00280        if (key_free) efree(key_free)
00281 /* a DBA handler must have specific routines */
00282 
00283 #define DBA_NAMED_HND(alias, name, flags) \
00284 {\
00285        #alias, flags, dba_open_##name, dba_close_##name, dba_fetch_##name, dba_update_##name, \
00286        dba_exists_##name, dba_delete_##name, dba_firstkey_##name, dba_nextkey_##name, \
00287        dba_optimize_##name, dba_sync_##name, dba_info_##name \
00288 },
00289 
00290 #define DBA_HND(name, flags) DBA_NAMED_HND(name, name, flags)
00291 
00292 /* check whether the user has write access */
00293 #define DBA_WRITE_CHECK \
00294        if(info->mode != DBA_WRITER && info->mode != DBA_TRUNC && info->mode != DBA_CREAT) { \
00295               php_error_docref(NULL TSRMLS_CC, E_WARNING, "You cannot perform a modification to a database without proper access"); \
00296               RETURN_FALSE; \
00297        }
00298 
00299 /* }}} */
00300 
00301 /* {{{ globals */
00302 
00303 static dba_handler handler[] = {
00304 #if DBA_GDBM
00305        DBA_HND(gdbm, DBA_LOCK_EXT) /* Locking done in library if set */
00306 #endif
00307 #if DBA_DBM
00308        DBA_HND(dbm, DBA_LOCK_ALL) /* No lock in lib */
00309 #endif
00310 #if DBA_NDBM
00311        DBA_HND(ndbm, DBA_LOCK_ALL) /* Could be done in library: filemode = 0644 + S_ENFMT */
00312 #endif
00313 #if DBA_CDB
00314        DBA_HND(cdb, DBA_STREAM_OPEN|DBA_LOCK_ALL) /* No lock in lib */
00315 #endif
00316 #if DBA_CDB_BUILTIN
00317     DBA_NAMED_HND(cdb_make, cdb, DBA_STREAM_OPEN|DBA_LOCK_ALL) /* No lock in lib */
00318 #endif
00319 #if DBA_DB1
00320        DBA_HND(db1, DBA_LOCK_ALL) /* No lock in lib */
00321 #endif
00322 #if DBA_DB2
00323        DBA_HND(db2, DBA_LOCK_ALL) /* No lock in lib */
00324 #endif
00325 #if DBA_DB3
00326        DBA_HND(db3, DBA_LOCK_ALL) /* No lock in lib */
00327 #endif
00328 #if DBA_DB4
00329        DBA_HND(db4, DBA_LOCK_ALL) /* No lock in lib */
00330 #endif
00331 #if DBA_INIFILE
00332        DBA_HND(inifile, DBA_STREAM_OPEN|DBA_LOCK_ALL|DBA_CAST_AS_FD) /* No lock in lib */
00333 #endif
00334 #if DBA_FLATFILE
00335        DBA_HND(flatfile, DBA_STREAM_OPEN|DBA_LOCK_ALL|DBA_NO_APPEND) /* No lock in lib */
00336 #endif
00337 #if DBA_QDBM
00338        DBA_HND(qdbm, DBA_LOCK_EXT)
00339 #endif
00340        { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
00341 };
00342 
00343 #if DBA_FLATFILE
00344 #define DBA_DEFAULT "flatfile"
00345 #elif DBA_DB4
00346 #define DBA_DEFAULT "db4"
00347 #elif DBA_DB3
00348 #define DBA_DEFAULT "db3"
00349 #elif DBA_DB2
00350 #define DBA_DEFAULT "db2"
00351 #elif DBA_DB1
00352 #define DBA_DEFAULT "db1"
00353 #elif DBA_GDBM
00354 #define DBA_DEFAULT "gdbm"
00355 #elif DBA_NBBM
00356 #define DBA_DEFAULT "ndbm"
00357 #elif DBA_DBM
00358 #define DBA_DEFAULT "dbm"
00359 #elif DBA_QDBM
00360 #define DBA_DEFAULT "qdbm"
00361 #else
00362 #define DBA_DEFAULT ""
00363 #endif
00364 /* cdb/cdb_make and ini are no option here */
00365 
00366 static int le_db;
00367 static int le_pdb;
00368 /* }}} */
00369 
00370 /* {{{ dba_fetch_resource
00371 PHPAPI void dba_fetch_resource(dba_info **pinfo, zval **id TSRMLS_DC)
00372 {
00373        dba_info *info;
00374        DBA_ID_FETCH
00375        *pinfo = info;
00376 }
00377 */
00378 /* }}} */
00379 
00380 /* {{{ dba_get_handler
00381 PHPAPI dba_handler *dba_get_handler(const char* handler_name)
00382 {
00383        dba_handler *hptr;
00384        for (hptr = handler; hptr->name && strcasecmp(hptr->name, handler_name); hptr++);
00385        return hptr;
00386 }
00387 */
00388 /* }}} */
00389 
00390 /* {{{ dba_close 
00391  */ 
00392 static void dba_close(dba_info *info TSRMLS_DC)
00393 {
00394        if (info->hnd) {
00395               info->hnd->close(info TSRMLS_CC);
00396        }
00397        if (info->path) {
00398               pefree(info->path, info->flags&DBA_PERSISTENT);
00399        }
00400        if (info->fp && info->fp!=info->lock.fp) {
00401               if(info->flags&DBA_PERSISTENT) {
00402                      php_stream_pclose(info->fp);
00403               } else {
00404                      php_stream_close(info->fp);
00405               }
00406        }
00407        if (info->lock.fp) {
00408               if(info->flags&DBA_PERSISTENT) {
00409                      php_stream_pclose(info->lock.fp);
00410               } else {
00411                      php_stream_close(info->lock.fp);
00412               }
00413        }
00414        if (info->lock.name) {
00415               pefree(info->lock.name, info->flags&DBA_PERSISTENT);
00416        }
00417        pefree(info, info->flags&DBA_PERSISTENT);
00418 }
00419 /* }}} */
00420 
00421 /* {{{ dba_close_rsrc
00422  */
00423 static void dba_close_rsrc(zend_rsrc_list_entry *rsrc TSRMLS_DC)
00424 {
00425        dba_info *info = (dba_info *)rsrc->ptr; 
00426 
00427        dba_close(info TSRMLS_CC);
00428 }
00429 /* }}} */
00430 
00431 /* {{{ dba_close_pe_rsrc_deleter */
00432 int dba_close_pe_rsrc_deleter(zend_rsrc_list_entry *le, void *pDba TSRMLS_DC)
00433 {
00434        return le->ptr == pDba;
00435 }
00436 /* }}} */
00437 
00438 /* {{{ dba_close_pe_rsrc */
00439 static void dba_close_pe_rsrc(zend_rsrc_list_entry *rsrc TSRMLS_DC)
00440 {
00441        dba_info *info = (dba_info *)rsrc->ptr; 
00442 
00443        /* closes the resource by calling dba_close_rsrc() */
00444        zend_hash_apply_with_argument(&EG(persistent_list), (apply_func_arg_t) dba_close_pe_rsrc_deleter, info TSRMLS_CC);
00445 }
00446 /* }}} */
00447 
00448 /* {{{ PHP_INI
00449  */
00450 ZEND_INI_MH(OnUpdateDefaultHandler)
00451 {
00452        dba_handler *hptr;
00453 
00454        if (!strlen(new_value)) {
00455               DBA_G(default_hptr) = NULL;
00456               return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
00457        }
00458 
00459        for (hptr = handler; hptr->name && strcasecmp(hptr->name, new_value); hptr++);
00460 
00461        if (!hptr->name) {
00462               php_error_docref(NULL TSRMLS_CC, E_WARNING, "No such handler: %s", new_value);
00463               return FAILURE;
00464        }
00465        DBA_G(default_hptr) = hptr;
00466        return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
00467 }
00468 
00469 PHP_INI_BEGIN()
00470     STD_PHP_INI_ENTRY("dba.default_handler", DBA_DEFAULT, PHP_INI_ALL, OnUpdateDefaultHandler, default_handler,    zend_dba_globals, dba_globals)
00471 PHP_INI_END()
00472 /* }}} */
00473  
00474 /* {{{ PHP_GINIT_FUNCTION
00475  */
00476 static PHP_GINIT_FUNCTION(dba)
00477 {
00478        dba_globals->default_handler = "";
00479        dba_globals->default_hptr    = NULL;
00480 }
00481 /* }}} */
00482 
00483 /* {{{ PHP_MINIT_FUNCTION
00484  */
00485 PHP_MINIT_FUNCTION(dba)
00486 {
00487        REGISTER_INI_ENTRIES();
00488        le_db = zend_register_list_destructors_ex(dba_close_rsrc, NULL, "dba", module_number);
00489        le_pdb = zend_register_list_destructors_ex(dba_close_pe_rsrc, dba_close_rsrc, "dba persistent", module_number);
00490        return SUCCESS;
00491 }
00492 /* }}} */
00493 
00494 /* {{{ PHP_MSHUTDOWN_FUNCTION
00495  */
00496 PHP_MSHUTDOWN_FUNCTION(dba)
00497 {
00498        UNREGISTER_INI_ENTRIES();
00499        return SUCCESS;
00500 }
00501 /* }}} */
00502 
00503 #include "ext/standard/php_smart_str.h"
00504 
00505 /* {{{ PHP_MINFO_FUNCTION
00506  */
00507 PHP_MINFO_FUNCTION(dba)
00508 {
00509        dba_handler *hptr;
00510        smart_str handlers = {0};
00511 
00512        for(hptr = handler; hptr->name; hptr++) {
00513               smart_str_appends(&handlers, hptr->name);
00514               smart_str_appendc(&handlers, ' ');
00515        }
00516 
00517        php_info_print_table_start();
00518        php_info_print_table_row(2, "DBA support", "enabled");
00519        if (handlers.c) {
00520               smart_str_0(&handlers);
00521               php_info_print_table_row(2, "Supported handlers", handlers.c);
00522               smart_str_free(&handlers);
00523        } else {
00524               php_info_print_table_row(2, "Supported handlers", "none");
00525        }
00526        php_info_print_table_end();
00527        DISPLAY_INI_ENTRIES();
00528 }
00529 /* }}} */
00530 
00531 /* {{{ php_dba_update
00532  */
00533 static void php_dba_update(INTERNAL_FUNCTION_PARAMETERS, int mode)
00534 {
00535        char *v;
00536        int val_len;
00537        zval *id;
00538        dba_info *info = NULL;
00539        int ac = ZEND_NUM_ARGS();
00540        zval *key;
00541        char *val;
00542        char *key_str, *key_free;
00543        size_t key_len;
00544 
00545        if (zend_parse_parameters(ac TSRMLS_CC, "zsr", &key, &val, &val_len, &id) == FAILURE) {
00546               return;
00547        }
00548 
00549        if ((key_len = php_dba_make_key(key, &key_str, &key_free TSRMLS_CC)) == 0) {
00550               RETURN_FALSE;
00551        }
00552 
00553        DBA_FETCH_RESOURCE(info, &id);
00554 
00555        DBA_WRITE_CHECK;
00556 
00557        if (PG(magic_quotes_runtime)) {
00558               v = estrndup(val, val_len);
00559               php_stripslashes(v, &val_len TSRMLS_CC);
00560               if (info->hnd->update(info, key_str, key_len, v, val_len, mode TSRMLS_CC) == SUCCESS) {
00561                      efree(v);
00562                      DBA_ID_DONE;
00563                      RETURN_TRUE;
00564               }
00565               efree(v);
00566        } else {
00567               if (info->hnd->update(info, key_str, key_len, val, val_len, mode TSRMLS_CC) == SUCCESS) {
00568                      DBA_ID_DONE;
00569                      RETURN_TRUE;
00570               }
00571        }
00572 
00573        DBA_ID_DONE;
00574        RETURN_FALSE;
00575 }
00576 /* }}} */
00577 
00578 #define FREENOW if(args) efree(args); if(key) efree(key)
00579 
00580 /* {{{ php_find_dbm
00581  */
00582 dba_info *php_dba_find(const char* path TSRMLS_DC)
00583 {
00584        zend_rsrc_list_entry *le;
00585        dba_info *info;
00586        int numitems, i;
00587 
00588        numitems = zend_hash_next_free_element(&EG(regular_list));
00589        for (i=1; i<numitems; i++) {
00590               if (zend_hash_index_find(&EG(regular_list), i, (void **) &le)==FAILURE) {
00591                      continue;
00592               }
00593               if (Z_TYPE_P(le) == le_db || Z_TYPE_P(le) == le_pdb) {
00594                      info = (dba_info *)(le->ptr);
00595                      if (!strcmp(info->path, path)) {
00596                             return (dba_info *)(le->ptr);
00597                      }
00598               }
00599        }
00600 
00601        return NULL;
00602 }
00603 /* }}} */
00604 
00605 /* {{{ php_dba_open
00606  */
00607 static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, int persistent)
00608 {
00609        zval ***args = (zval ***) NULL;
00610        int ac = ZEND_NUM_ARGS();
00611        dba_mode_t modenr;
00612        dba_info *info, *other;
00613        dba_handler *hptr;
00614        char *key = NULL, *error = NULL;
00615        int keylen = 0;
00616        int i;
00617        int lock_mode, lock_flag, lock_dbf = 0;
00618        char *file_mode;
00619        char mode[4], *pmode, *lock_file_mode = NULL;
00620        int persistent_flag = persistent ? STREAM_OPEN_PERSISTENT : 0;
00621        char *opened_path, *lock_name;
00622        
00623        if(ac < 2) {
00624               WRONG_PARAM_COUNT;
00625        }
00626        
00627        /* we pass additional args to the respective handler */
00628        args = safe_emalloc(ac, sizeof(zval *), 0);
00629        if (zend_get_parameters_array_ex(ac, args) != SUCCESS) {
00630               FREENOW;
00631               WRONG_PARAM_COUNT;
00632        }
00633               
00634        /* we only take string arguments */
00635        for (i = 0; i < ac; i++) {
00636               convert_to_string_ex(args[i]);
00637               keylen += Z_STRLEN_PP(args[i]);
00638        }
00639 
00640        if (persistent) {
00641               zend_rsrc_list_entry *le;
00642               
00643               /* calculate hash */
00644               key = safe_emalloc(keylen, 1, 1);
00645               key[keylen] = '\0';
00646               keylen = 0;
00647               
00648               for(i = 0; i < ac; i++) {
00649                      memcpy(key+keylen, Z_STRVAL_PP(args[i]), Z_STRLEN_PP(args[i]));
00650                      keylen += Z_STRLEN_PP(args[i]);
00651               }
00652 
00653               /* try to find if we already have this link in our persistent list */
00654               if (zend_hash_find(&EG(persistent_list), key, keylen+1, (void **) &le) == SUCCESS) {
00655                      FREENOW;
00656                      
00657                      if (Z_TYPE_P(le) != le_pdb) {
00658                             RETURN_FALSE;
00659                      }
00660               
00661                      info = (dba_info *)le->ptr;
00662 
00663                      ZEND_REGISTER_RESOURCE(return_value, info, le_pdb);
00664                      return;
00665               }
00666        }
00667        
00668        if (ac==2) {
00669               hptr = DBA_G(default_hptr);
00670               if (!hptr) {
00671                      php_error_docref2(NULL TSRMLS_CC, Z_STRVAL_PP(args[0]), Z_STRVAL_PP(args[1]), E_WARNING, "No default handler selected");
00672                      FREENOW;
00673                      RETURN_FALSE;
00674               }
00675        } else {
00676               for (hptr = handler; hptr->name && strcasecmp(hptr->name, Z_STRVAL_PP(args[2])); hptr++);
00677        }
00678 
00679        if (!hptr->name) {
00680               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]));
00681               FREENOW;
00682               RETURN_FALSE;
00683        }
00684 
00685        /* Check mode: [rwnc][fl]?t?
00686         * r: Read
00687         * w: Write
00688         * n: Create/Truncate
00689         * c: Create
00690         *
00691         * d: force lock on database file
00692         * l: force lock on lck file
00693         * -: ignore locking
00694         *
00695         * t: test open database, warning if locked
00696         */
00697        strlcpy(mode, Z_STRVAL_PP(args[1]), sizeof(mode));
00698        pmode = &mode[0];
00699        if (pmode[0] && (pmode[1]=='d' || pmode[1]=='l' || pmode[1]=='-')) { /* force lock on db file or lck file or disable locking */
00700               switch (pmode[1]) {
00701               case 'd':
00702                      lock_dbf = 1;
00703                      if ((hptr->flags & DBA_LOCK_ALL) == 0) {
00704                             lock_flag = (hptr->flags & DBA_LOCK_ALL);
00705                             break;
00706                      }
00707                      /* no break */
00708               case 'l':
00709                      lock_flag = DBA_LOCK_ALL;
00710                      if ((hptr->flags & DBA_LOCK_ALL) == 0) {
00711                             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);
00712                      }
00713                      break;
00714               default:
00715               case '-':
00716                      if ((hptr->flags & DBA_LOCK_ALL) == 0) {
00717                             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);
00718                             FREENOW;
00719                             RETURN_FALSE;
00720                      }
00721                      lock_flag = 0;
00722                      break;
00723               }
00724        } else {
00725               lock_flag = (hptr->flags&DBA_LOCK_ALL);
00726               lock_dbf = 1;
00727        }
00728        switch (*pmode++) {
00729               case 'r': 
00730                      modenr = DBA_READER; 
00731                      lock_mode = (lock_flag & DBA_LOCK_READER) ? LOCK_SH : 0;
00732                      file_mode = "r";
00733                      break;
00734               case 'w': 
00735                      modenr = DBA_WRITER; 
00736                      lock_mode = (lock_flag & DBA_LOCK_WRITER) ? LOCK_EX : 0;
00737                      file_mode = "r+b";
00738                      break;
00739               case 'c': 
00740                      modenr = DBA_CREAT; 
00741                      lock_mode = (lock_flag & DBA_LOCK_CREAT) ? LOCK_EX : 0;
00742                      if (lock_mode) {
00743                             if (lock_dbf) {
00744                                    /* the create/append check will be done on the lock
00745                                     * when the lib opens the file it is already created
00746                                     */
00747                                    file_mode = "r+b";       /* read & write, seek 0 */
00748                                    lock_file_mode = "a+b";  /* append */
00749                             } else {
00750                                    file_mode = "a+b";       /* append */
00751                                    lock_file_mode = "w+b";  /* create/truncate */
00752                             }
00753                      } else {
00754                             file_mode = "a+b";
00755                      }
00756                      /* In case of the 'a+b' append mode, the handler is responsible 
00757                       * to handle any rewind problems (see flatfile handler).
00758                       */
00759                      break;
00760               case 'n':
00761                      modenr = DBA_TRUNC;
00762                      lock_mode = (lock_flag & DBA_LOCK_TRUNC) ? LOCK_EX : 0;
00763                      file_mode = "w+b";
00764                      break;
00765               default:
00766                      php_error_docref2(NULL TSRMLS_CC, Z_STRVAL_PP(args[0]), Z_STRVAL_PP(args[1]), E_WARNING, "Illegal DBA mode");
00767                      FREENOW;
00768                      RETURN_FALSE;
00769        }
00770        if (!lock_file_mode) {
00771               lock_file_mode = file_mode;
00772        }
00773        if (*pmode=='d' || *pmode=='l' || *pmode=='-') {
00774               pmode++; /* done already - skip here */
00775        }
00776        if (*pmode=='t') {
00777               pmode++;
00778               if (!lock_flag) {
00779                      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)");
00780                      FREENOW;
00781                      RETURN_FALSE;
00782               }
00783               if (!lock_mode) {
00784                      if ((hptr->flags & DBA_LOCK_ALL) == 0) {
00785                             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);
00786                             FREENOW;
00787                             RETURN_FALSE;
00788                      } else {
00789                             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);
00790                             FREENOW;
00791                             RETURN_FALSE;
00792                      }
00793               } else {
00794                      lock_mode |= LOCK_NB; /* test =: non blocking */
00795               }
00796        }
00797        if (*pmode) {
00798               php_error_docref2(NULL TSRMLS_CC, Z_STRVAL_PP(args[0]), Z_STRVAL_PP(args[1]), E_WARNING, "Illegal DBA mode");
00799               FREENOW;
00800               RETURN_FALSE;
00801        }
00802                      
00803        info = pemalloc(sizeof(dba_info), persistent);
00804        memset(info, 0, sizeof(dba_info));
00805        info->path = pestrdup(Z_STRVAL_PP(args[0]), persistent);
00806        info->mode = modenr;
00807        info->argc = ac - 3;
00808        info->argv = args + 3;
00809        info->flags = (hptr->flags & ~DBA_LOCK_ALL) | (lock_flag & DBA_LOCK_ALL) | (persistent ? DBA_PERSISTENT : 0);
00810        info->lock.mode = lock_mode;
00811 
00812        /* if any open call is a locking call:
00813         * check if we already habe a locking call open that should block this call
00814         * the problem is some systems would allow read during write
00815         */
00816        if (hptr->flags & DBA_LOCK_ALL) {
00817               if ((other = php_dba_find(info->path TSRMLS_CC)) != NULL) {
00818                      if (   ( (lock_mode&LOCK_EX)        && (other->lock.mode&(LOCK_EX|LOCK_SH)) )
00819                          || ( (other->lock.mode&LOCK_EX) && (lock_mode&(LOCK_EX|LOCK_SH))        )
00820                         ) {
00821                             error = "Unable to establish lock (database file already open)"; /* force failure exit */
00822                      }
00823               }
00824        }
00825 
00826        if (!error && lock_mode) {
00827               if (lock_dbf) {
00828                      lock_name = Z_STRVAL_PP(args[0]);
00829               } else {
00830                      spprintf(&lock_name, 0, "%s.lck", info->path);
00831                      if (!strcmp(file_mode, "r")) {
00832                             /* when in read only mode try to use existing .lck file first */
00833                             /* do not log errors for .lck file while in read ony mode on .lck file */
00834                             lock_file_mode = "rb";
00835                             info->lock.fp = php_stream_open_wrapper(lock_name, lock_file_mode, STREAM_MUST_SEEK|IGNORE_PATH|ENFORCE_SAFE_MODE|persistent_flag, &opened_path);
00836                      }
00837                      if (!info->lock.fp) {
00838                             /* when not in read mode or failed to open .lck file read only. now try again in create(write) mode and log errors */
00839                             lock_file_mode = "a+b";
00840                      } else {
00841                             if (!persistent) {
00842                                    info->lock.name = opened_path;
00843                             } else {
00844                                    info->lock.name = pestrdup(opened_path, persistent);
00845                                    efree(opened_path);
00846                             }
00847                      }
00848               }
00849               if (!info->lock.fp) {
00850                      info->lock.fp = php_stream_open_wrapper(lock_name, lock_file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|ENFORCE_SAFE_MODE|persistent_flag, &opened_path);
00851                      if (info->lock.fp) {
00852                             if (lock_dbf) {
00853                                    /* replace the path info with the real path of the opened file */
00854                                    pefree(info->path, persistent);
00855                                    info->path = pestrdup(opened_path, persistent);
00856                             }
00857                             /* now store the name of the lock */
00858                             if (!persistent) {
00859                                    info->lock.name = opened_path;
00860                             } else {
00861                                    info->lock.name = pestrdup(opened_path, persistent);
00862                                    efree(opened_path);
00863                             }
00864                      }
00865               }
00866               if (!lock_dbf) {
00867                      efree(lock_name);
00868               }
00869               if (!info->lock.fp) {
00870                      dba_close(info TSRMLS_CC);
00871                      /* stream operation already wrote an error message */
00872                      FREENOW;
00873                      RETURN_FALSE;
00874               }
00875               if (!php_stream_supports_lock(info->lock.fp)) {
00876                      error = "Stream does not support locking";
00877               }
00878               if (php_stream_lock(info->lock.fp, lock_mode)) {
00879                      error = "Unable to establish lock"; /* force failure exit */
00880               }
00881        }
00882 
00883        /* centralised open stream for builtin */
00884        if (!error && (hptr->flags&DBA_STREAM_OPEN)==DBA_STREAM_OPEN) {
00885               if (info->lock.fp && lock_dbf) {
00886                      info->fp = info->lock.fp; /* use the same stream for locking and database access */
00887               } else {
00888                      info->fp = php_stream_open_wrapper(info->path, file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|ENFORCE_SAFE_MODE|persistent_flag, NULL);
00889               }
00890               if (!info->fp) {
00891                      dba_close(info TSRMLS_CC);
00892                      /* stream operation already wrote an error message */
00893                      FREENOW;
00894                      RETURN_FALSE;
00895               }
00896               if (hptr->flags & (DBA_NO_APPEND|DBA_CAST_AS_FD)) {
00897                      /* Needed becasue some systems do not allow to write to the original 
00898                       * file contents with O_APPEND being set.
00899                       */
00900                      if (SUCCESS != php_stream_cast(info->fp, PHP_STREAM_AS_FD, (void*)&info->fd, 1)) {
00901                             php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not cast stream");
00902                             dba_close(info TSRMLS_CC);
00903                             FREENOW;
00904                             RETURN_FALSE;
00905 #ifdef F_SETFL
00906                      } else if (modenr == DBA_CREAT) {
00907                             int flags = fcntl(info->fd, F_SETFL);
00908                             fcntl(info->fd, F_SETFL, flags & ~O_APPEND);
00909 #endif
00910                      }
00911                             
00912               }
00913        }
00914 
00915        if (error || hptr->open(info, &error TSRMLS_CC) != SUCCESS) {
00916               dba_close(info TSRMLS_CC);
00917               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:"");
00918               FREENOW;
00919               RETURN_FALSE;
00920        }
00921 
00922        info->hnd = hptr;
00923        info->argc = 0;
00924        info->argv = NULL;
00925 
00926        if (persistent) {
00927               zend_rsrc_list_entry new_le;
00928 
00929               Z_TYPE(new_le) = le_pdb;
00930               new_le.ptr = info;
00931               if (zend_hash_update(&EG(persistent_list), key, keylen+1, &new_le, sizeof(zend_rsrc_list_entry), NULL) == FAILURE) {
00932                      dba_close(info TSRMLS_CC);
00933                      php_error_docref2(NULL TSRMLS_CC, Z_STRVAL_PP(args[0]), Z_STRVAL_PP(args[1]), E_WARNING, "Could not register persistent resource");
00934                      FREENOW;
00935                      RETURN_FALSE;
00936               }
00937        }
00938 
00939        ZEND_REGISTER_RESOURCE(return_value, info, (persistent ? le_pdb : le_db));
00940        FREENOW;
00941 }
00942 /* }}} */
00943 #undef FREENOW
00944 
00945 /* {{{ proto resource dba_popen(string path, string mode [, string handlername, string ...])
00946    Opens path using the specified handler in mode persistently */
00947 PHP_FUNCTION(dba_popen)
00948 {
00949        php_dba_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
00950 }
00951 /* }}} */
00952 
00953 /* {{{ proto resource dba_open(string path, string mode [, string handlername, string ...])
00954    Opens path using the specified handler in mode*/
00955 PHP_FUNCTION(dba_open)
00956 {
00957        php_dba_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
00958 }
00959 /* }}} */
00960 
00961 /* {{{ proto void dba_close(resource handle)
00962    Closes database */
00963 PHP_FUNCTION(dba_close)
00964 {
00965        zval *id;
00966        dba_info *info = NULL;
00967 
00968        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &id) == FAILURE) {
00969               return;
00970        }
00971 
00972        DBA_FETCH_RESOURCE(info, &id);
00973 
00974        zend_list_delete(Z_RESVAL_P(id));
00975 }
00976 /* }}} */
00977 
00978 /* {{{ proto bool dba_exists(string key, resource handle)
00979    Checks, if the specified key exists */
00980 PHP_FUNCTION(dba_exists)
00981 {
00982        DBA_ID_GET2;
00983 
00984        if(info->hnd->exists(info, key_str, key_len TSRMLS_CC) == SUCCESS) {
00985               DBA_ID_DONE;
00986               RETURN_TRUE;
00987        }
00988        DBA_ID_DONE;
00989        RETURN_FALSE;
00990 }
00991 /* }}} */
00992 
00993 /* {{{ proto string dba_fetch(string key, [int skip ,] resource handle)
00994    Fetches the data associated with key */
00995 PHP_FUNCTION(dba_fetch)
00996 {
00997        char *val;
00998        int len = 0;
00999        DBA_ID_GET2_3;
01000 
01001        if (ac==3) {
01002               if (!strcmp(info->hnd->name, "cdb")) {
01003                      if (skip < 0) {
01004                             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);
01005                             skip = 0;
01006                      }
01007               } else if (!strcmp(info->hnd->name, "inifile")) {
01008                      /* "-1" is compareable to 0 but allows a non restrictive 
01009                       * access which is fater. For example 'inifile' uses this
01010                       * to allow faster access when the key was already found
01011                       * using firstkey/nextkey. However explicitly setting the
01012                       * value to 0 ensures the first value. 
01013                       */
01014                      if (skip < -1) {
01015                             php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Handler %s accepts only skip value -1 and greater, using skip=0", info->hnd->name);
01016                             skip = 0;
01017                      }
01018               } else {
01019                      php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Handler %s does not support optional skip parameter, the value will be ignored", info->hnd->name);
01020                      skip = 0;                   
01021               }
01022        } else {
01023               skip = 0; 
01024        }
01025        if((val = info->hnd->fetch(info, key_str, key_len, skip, &len TSRMLS_CC)) != NULL) {
01026               if (val && PG(magic_quotes_runtime)) {
01027                      val = php_addslashes(val, len, &len, 1 TSRMLS_CC);
01028               }
01029               DBA_ID_DONE;
01030               RETURN_STRINGL(val, len, 0);
01031        } 
01032        DBA_ID_DONE;
01033        RETURN_FALSE;
01034 }
01035 /* }}} */
01036 
01037 /* {{{ proto array|false dba_key_split(string key)
01038    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 */
01039 PHP_FUNCTION(dba_key_split)
01040 {
01041        zval *zkey;
01042        char *key, *name;
01043        int key_len;
01044 
01045        if (ZEND_NUM_ARGS() != 1) {
01046               WRONG_PARAM_COUNT;
01047        }
01048        if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "z", &zkey) == SUCCESS) {
01049               if (Z_TYPE_P(zkey) == IS_NULL || (Z_TYPE_P(zkey) == IS_BOOL && !Z_LVAL_P(zkey))) {
01050                      RETURN_BOOL(0);
01051               }
01052        }
01053        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &key, &key_len) == FAILURE) {
01054               RETURN_BOOL(0);
01055        }
01056        array_init(return_value);
01057        if (key[0] == '[' && (name = strchr(key, ']')) != NULL) {
01058               add_next_index_stringl(return_value, key+1, name - (key + 1), 1);
01059               add_next_index_stringl(return_value, name+1, key_len - (name - key + 1), 1);
01060        } else {
01061               add_next_index_stringl(return_value, "", 0, 1);
01062               add_next_index_stringl(return_value, key, key_len, 1);
01063        }
01064 }
01065 /* }}} */
01066 
01067 /* {{{ proto string dba_firstkey(resource handle)
01068    Resets the internal key pointer and returns the first key */
01069 PHP_FUNCTION(dba_firstkey)
01070 {
01071        char *fkey;
01072        int len;
01073        zval *id;
01074        dba_info *info = NULL;
01075 
01076        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &id) == FAILURE) {
01077               return;
01078        }
01079 
01080        DBA_FETCH_RESOURCE(info, &id);
01081 
01082        fkey = info->hnd->firstkey(info, &len TSRMLS_CC);
01083 
01084        if (fkey)
01085               RETURN_STRINGL(fkey, len, 0);
01086 
01087        RETURN_FALSE;
01088 }
01089 /* }}} */
01090 
01091 /* {{{ proto string dba_nextkey(resource handle)
01092    Returns the next key */
01093 PHP_FUNCTION(dba_nextkey)
01094 {
01095        char *nkey;
01096        int len;
01097        zval *id;
01098        dba_info *info = NULL;
01099 
01100        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &id) == FAILURE) {
01101               return;
01102        }
01103 
01104        DBA_FETCH_RESOURCE(info, &id);
01105 
01106        nkey = info->hnd->nextkey(info, &len TSRMLS_CC);
01107 
01108        if (nkey)
01109               RETURN_STRINGL(nkey, len, 0);
01110 
01111        RETURN_FALSE;
01112 }
01113 /* }}} */
01114 
01115 /* {{{ proto bool dba_delete(string key, resource handle)
01116    Deletes the entry associated with key
01117    If inifile: remove all other key lines */
01118 PHP_FUNCTION(dba_delete)
01119 {
01120        DBA_ID_GET2;
01121        
01122        DBA_WRITE_CHECK;
01123        
01124        if(info->hnd->delete(info, key_str, key_len TSRMLS_CC) == SUCCESS)
01125        {
01126               DBA_ID_DONE;
01127               RETURN_TRUE;
01128        }
01129        DBA_ID_DONE;
01130        RETURN_FALSE;
01131 }
01132 /* }}} */
01133 
01134 /* {{{ proto bool dba_insert(string key, string value, resource handle)
01135    If not inifile: Insert value as key, return false, if key exists already 
01136    If inifile: Add vakue as key (next instance of key) */
01137 PHP_FUNCTION(dba_insert)
01138 {
01139        php_dba_update(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
01140 }
01141 /* }}} */
01142 
01143 /* {{{ proto bool dba_replace(string key, string value, resource handle)
01144    Inserts value as key, replaces key, if key exists already
01145    If inifile: remove all other key lines */
01146 PHP_FUNCTION(dba_replace)
01147 {
01148        php_dba_update(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
01149 }
01150 /* }}} */
01151 
01152 /* {{{ proto bool dba_optimize(resource handle)
01153    Optimizes (e.g. clean up, vacuum) database */
01154 PHP_FUNCTION(dba_optimize)
01155 {
01156        zval *id;
01157        dba_info *info = NULL;
01158 
01159        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &id) == FAILURE) {
01160               return;
01161        }
01162 
01163        DBA_FETCH_RESOURCE(info, &id);
01164 
01165        DBA_WRITE_CHECK;
01166 
01167        if (info->hnd->optimize(info TSRMLS_CC) == SUCCESS) {
01168               RETURN_TRUE;
01169        }
01170 
01171        RETURN_FALSE;
01172 }
01173 /* }}} */
01174 
01175 /* {{{ proto bool dba_sync(resource handle)
01176    Synchronizes database */
01177 PHP_FUNCTION(dba_sync)
01178 {
01179        zval *id;
01180        dba_info *info = NULL;
01181 
01182        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &id) == FAILURE) {
01183               return;
01184        }
01185 
01186        DBA_FETCH_RESOURCE(info, &id);
01187 
01188        if (info->hnd->sync(info TSRMLS_CC) == SUCCESS) {
01189               RETURN_TRUE;
01190        }
01191 
01192        RETURN_FALSE;
01193 }
01194 /* }}} */
01195 
01196 /* {{{ proto array dba_handlers([bool full_info])
01197    List configured database handlers */
01198 PHP_FUNCTION(dba_handlers)
01199 {
01200        dba_handler *hptr;
01201        zend_bool full_info = 0;
01202 
01203        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &full_info) == FAILURE) {
01204               RETURN_FALSE;
01205        }
01206 
01207        array_init(return_value);
01208 
01209        for(hptr = handler; hptr->name; hptr++) {
01210               if (full_info) {
01211                      add_assoc_string(return_value, hptr->name, hptr->info(hptr, NULL TSRMLS_CC), 0);
01212               } else {
01213                      add_next_index_string(return_value, hptr->name, 1);
01214               }
01215        }
01216 }
01217 /* }}} */
01218 
01219 /* {{{ proto array dba_list()
01220    List opened databases */
01221 PHP_FUNCTION(dba_list)
01222 {
01223        ulong numitems, i;
01224        zend_rsrc_list_entry *le;
01225        dba_info *info;
01226 
01227        if (zend_parse_parameters_none() == FAILURE) {
01228               RETURN_FALSE;
01229        }
01230 
01231        array_init(return_value);
01232 
01233        numitems = zend_hash_next_free_element(&EG(regular_list));
01234        for (i=1; i<numitems; i++) {
01235               if (zend_hash_index_find(&EG(regular_list), i, (void **) &le)==FAILURE) {
01236                      continue;
01237               }
01238               if (Z_TYPE_P(le) == le_db || Z_TYPE_P(le) == le_pdb) {
01239                      info = (dba_info *)(le->ptr);
01240                      add_index_string(return_value, i, info->path, 1);
01241               }
01242        }
01243 }
01244 /* }}} */
01245 
01246 #endif /* HAVE_DBA */
01247 
01248 /*
01249  * Local variables:
01250  * tab-width: 4
01251  * c-basic-offset: 4
01252  * End:
01253  * vim600: sw=4 ts=4 fdm=marker
01254  * vim<600: sw=4 ts=4
01255  */