PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/dba - dba.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 384 435 88.3 %
Date: 2016-05-26 Functions: 28 28 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2016 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Sascha Schumann <sascha@schumann.cx>                        |
      16             :    |          Marcus Boerger <helly@php.net>                              |
      17             :    +----------------------------------------------------------------------+
      18             :  */
      19             : 
      20             : /* $Id$ */
      21             : 
      22             : #ifdef HAVE_CONFIG_H
      23             : #include "config.h"
      24             : #endif
      25             : 
      26             : #include "php.h"
      27             : 
      28             : #if HAVE_DBA
      29             : 
      30             : #include "php_ini.h"
      31             : #include <stdio.h>
      32             : #include <fcntl.h>
      33             : #ifdef HAVE_SYS_FILE_H
      34             : #include <sys/file.h>
      35             : #endif
      36             : 
      37             : #include "php_dba.h"
      38             : #include "ext/standard/info.h"
      39             : #include "ext/standard/php_string.h"
      40             : #include "ext/standard/flock_compat.h"
      41             : 
      42             : #include "php_gdbm.h"
      43             : #include "php_ndbm.h"
      44             : #include "php_dbm.h"
      45             : #include "php_cdb.h"
      46             : #include "php_db1.h"
      47             : #include "php_db2.h"
      48             : #include "php_db3.h"
      49             : #include "php_db4.h"
      50             : #include "php_flatfile.h"
      51             : #include "php_inifile.h"
      52             : #include "php_qdbm.h"
      53             : #include "php_tcadb.h"
      54             : 
      55             : /* {{{ arginfo */
      56             : ZEND_BEGIN_ARG_INFO_EX(arginfo_dba_popen, 0, 0, 2)
      57             :         ZEND_ARG_INFO(0, path)
      58             :         ZEND_ARG_INFO(0, mode)
      59             :         ZEND_ARG_INFO(0, handlername)
      60             :         ZEND_ARG_VARIADIC_INFO(0, handler_parameters)
      61             : ZEND_END_ARG_INFO()
      62             : 
      63             : ZEND_BEGIN_ARG_INFO_EX(arginfo_dba_open, 0, 0, 2)
      64             :         ZEND_ARG_INFO(0, path)
      65             :         ZEND_ARG_INFO(0, mode)
      66             :         ZEND_ARG_INFO(0, handlername)
      67             :         ZEND_ARG_VARIADIC_INFO(0, handler_parameters)
      68             : ZEND_END_ARG_INFO()
      69             : 
      70             : ZEND_BEGIN_ARG_INFO(arginfo_dba_close, 0)
      71             :         ZEND_ARG_INFO(0, handle)
      72             : ZEND_END_ARG_INFO()
      73             : 
      74             : ZEND_BEGIN_ARG_INFO(arginfo_dba_exists, 0)
      75             :         ZEND_ARG_INFO(0, key)
      76             :         ZEND_ARG_INFO(0, handle)
      77             : ZEND_END_ARG_INFO()
      78             : 
      79             : ZEND_BEGIN_ARG_INFO_EX(arginfo_dba_fetch, 0, 0, 2)
      80             :         ZEND_ARG_INFO(0, key)
      81             :         ZEND_ARG_INFO(0, skip)
      82             :         ZEND_ARG_INFO(0, handle)
      83             : ZEND_END_ARG_INFO()
      84             : 
      85             : ZEND_BEGIN_ARG_INFO(arginfo_dba_key_split, 0)
      86             :         ZEND_ARG_INFO(0, key)
      87             : ZEND_END_ARG_INFO()
      88             : 
      89             : ZEND_BEGIN_ARG_INFO(arginfo_dba_firstkey, 0)
      90             :         ZEND_ARG_INFO(0, handle)
      91             : ZEND_END_ARG_INFO()
      92             : 
      93             : ZEND_BEGIN_ARG_INFO(arginfo_dba_nextkey, 0)
      94             :         ZEND_ARG_INFO(0, handle)
      95             : ZEND_END_ARG_INFO()
      96             : 
      97             : ZEND_BEGIN_ARG_INFO(arginfo_dba_delete, 0)
      98             :         ZEND_ARG_INFO(0, key)
      99             :         ZEND_ARG_INFO(0, handle)
     100             : ZEND_END_ARG_INFO()
     101             : 
     102             : ZEND_BEGIN_ARG_INFO(arginfo_dba_insert, 0)
     103             :         ZEND_ARG_INFO(0, key)
     104             :         ZEND_ARG_INFO(0, value)
     105             :         ZEND_ARG_INFO(0, handle)
     106             : ZEND_END_ARG_INFO()
     107             : 
     108             : ZEND_BEGIN_ARG_INFO(arginfo_dba_replace, 0)
     109             :         ZEND_ARG_INFO(0, key)
     110             :         ZEND_ARG_INFO(0, value)
     111             :         ZEND_ARG_INFO(0, handle)
     112             : ZEND_END_ARG_INFO()
     113             : 
     114             : ZEND_BEGIN_ARG_INFO(arginfo_dba_optimize, 0)
     115             :         ZEND_ARG_INFO(0, handle)
     116             : ZEND_END_ARG_INFO()
     117             : 
     118             : ZEND_BEGIN_ARG_INFO(arginfo_dba_sync, 0)
     119             :         ZEND_ARG_INFO(0, handle)
     120             : ZEND_END_ARG_INFO()
     121             : 
     122             : ZEND_BEGIN_ARG_INFO_EX(arginfo_dba_handlers, 0, 0, 0)
     123             :         ZEND_ARG_INFO(0, full_info)
     124             : ZEND_END_ARG_INFO()
     125             : 
     126             : ZEND_BEGIN_ARG_INFO(arginfo_dba_list, 0)
     127             : ZEND_END_ARG_INFO()
     128             : 
     129             : /* }}} */
     130             : 
     131             : /* {{{ dba_functions[]
     132             :  */
     133             : const zend_function_entry dba_functions[] = {
     134             :         PHP_FE(dba_open, arginfo_dba_open)
     135             :         PHP_FE(dba_popen, arginfo_dba_popen)
     136             :         PHP_FE(dba_close, arginfo_dba_close)
     137             :         PHP_FE(dba_delete, arginfo_dba_delete)
     138             :         PHP_FE(dba_exists, arginfo_dba_exists)
     139             :         PHP_FE(dba_fetch, arginfo_dba_fetch)
     140             :         PHP_FE(dba_insert, arginfo_dba_insert)
     141             :         PHP_FE(dba_replace, arginfo_dba_replace)
     142             :         PHP_FE(dba_firstkey, arginfo_dba_firstkey)
     143             :         PHP_FE(dba_nextkey, arginfo_dba_nextkey)
     144             :         PHP_FE(dba_optimize, arginfo_dba_optimize)
     145             :         PHP_FE(dba_sync, arginfo_dba_sync)
     146             :         PHP_FE(dba_handlers, arginfo_dba_handlers)
     147             :         PHP_FE(dba_list, arginfo_dba_list)
     148             :         PHP_FE(dba_key_split, arginfo_dba_key_split)
     149             :         PHP_FE_END
     150             : };
     151             : /* }}} */
     152             : 
     153             : PHP_MINIT_FUNCTION(dba);
     154             : PHP_MSHUTDOWN_FUNCTION(dba);
     155             : PHP_MINFO_FUNCTION(dba);
     156             : 
     157             : ZEND_BEGIN_MODULE_GLOBALS(dba)
     158             :         char *default_handler;
     159             :         dba_handler *default_hptr;
     160             : ZEND_END_MODULE_GLOBALS(dba)
     161             : 
     162             : ZEND_DECLARE_MODULE_GLOBALS(dba)
     163             : 
     164             : #ifdef ZTS
     165             : #define DBA_G(v) TSRMG(dba_globals_id, zend_dba_globals *, v)
     166             : #else
     167             : #define DBA_G(v) (dba_globals.v)
     168             : #endif
     169             : 
     170             : static PHP_GINIT_FUNCTION(dba);
     171             : 
     172             : zend_module_entry dba_module_entry = {
     173             :         STANDARD_MODULE_HEADER,
     174             :         "dba",
     175             :         dba_functions,
     176             :         PHP_MINIT(dba),
     177             :         PHP_MSHUTDOWN(dba),
     178             :         NULL,
     179             :         NULL,
     180             :         PHP_MINFO(dba),
     181             :         PHP_DBA_VERSION,
     182             :         PHP_MODULE_GLOBALS(dba),
     183             :         PHP_GINIT(dba),
     184             :         NULL,
     185             :         NULL,
     186             :         STANDARD_MODULE_PROPERTIES_EX
     187             : };
     188             : 
     189             : #ifdef COMPILE_DL_DBA
     190             : ZEND_GET_MODULE(dba)
     191             : #endif
     192             : 
     193             : /* {{{ macromania */
     194             : 
     195             : #define DBA_ID_PARS                                                                                     \
     196             :         zval *id;                                                                                                       \
     197             :         dba_info *info = NULL;                                                                          \
     198             :         int ac = ZEND_NUM_ARGS()
     199             : 
     200             : /* these are used to get the standard arguments */
     201             : 
     202             : /* {{{ php_dba_myke_key */
     203         420 : static size_t php_dba_make_key(zval *key, char **key_str, char **key_free)
     204             : {
     205         420 :         if (Z_TYPE_P(key) == IS_ARRAY) {
     206             :                 zval *group, *name;
     207             :                 HashPosition pos;
     208             :                 size_t len;
     209             : 
     210           9 :                 if (zend_hash_num_elements(Z_ARRVAL_P(key)) != 2) {
     211           2 :                         php_error_docref(NULL, E_RECOVERABLE_ERROR, "Key does not have exactly two elements: (key, name)");
     212           0 :                         return 0;
     213             :                 }
     214           7 :                 zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(key), &pos);
     215           7 :                 group = zend_hash_get_current_data_ex(Z_ARRVAL_P(key), &pos);
     216           7 :                 zend_hash_move_forward_ex(Z_ARRVAL_P(key), &pos);
     217           7 :                 name = zend_hash_get_current_data_ex(Z_ARRVAL_P(key), &pos);
     218           7 :                 convert_to_string_ex(group);
     219           7 :                 convert_to_string_ex(name);
     220           7 :                 if (Z_STRLEN_P(group) == 0) {
     221           2 :                         *key_str = Z_STRVAL_P(name);
     222           2 :                         *key_free = NULL;
     223           2 :                         return Z_STRLEN_P(name);
     224             :                 }
     225           5 :                 len = spprintf(key_str, 0, "[%s]%s", Z_STRVAL_P(group), Z_STRVAL_P(name));
     226           5 :                 *key_free = *key_str;
     227           5 :                 return len;
     228             :         } else {
     229             :                 zval tmp;
     230             :                 int len;
     231             : 
     232         411 :                 ZVAL_COPY(&tmp, key);
     233         411 :                 convert_to_string(&tmp);
     234             : 
     235         411 :                 len = Z_STRLEN(tmp);
     236         411 :                 if (len) {
     237         411 :                         *key_free = *key_str = estrndup(Z_STRVAL(tmp), Z_STRLEN(tmp));
     238             :                 }
     239         411 :                 zval_ptr_dtor(&tmp);
     240         411 :                 return len;
     241             :         }
     242             : }
     243             : /* }}} */
     244             : 
     245             : #define DBA_GET2                                                                                                \
     246             :         zval *key;                                                                                                      \
     247             :         char *key_str, *key_free;                                                                       \
     248             :         size_t key_len;                                                                                         \
     249             :         if (zend_parse_parameters(ac, "zr", &key, &id) == FAILURE) {  \
     250             :                 return;                                                                                                 \
     251             :         }                                                                                                                       \
     252             :         if ((key_len = php_dba_make_key(key, &key_str, &key_free)) == 0) {\
     253             :                 RETURN_FALSE;                                                                                   \
     254             :         }
     255             : 
     256             : #define DBA_GET2_3                                                                                              \
     257             :         zval *key;                                                                                                      \
     258             :         char *key_str, *key_free;                                                                       \
     259             :         size_t key_len;                                                                                         \
     260             :         zend_long skip = 0;                                                                                     \
     261             :         switch(ac) {                                                                                            \
     262             :         case 2:                                                                                                         \
     263             :                 if (zend_parse_parameters(ac, "zr", &key, &id) == FAILURE) { \
     264             :                         return;                                                                                         \
     265             :                 }                                                                                                               \
     266             :                 break;                                                                                                  \
     267             :         case 3:                                                                                                         \
     268             :                 if (zend_parse_parameters(ac, "zlr", &key, &skip, &id) == FAILURE) { \
     269             :                         return;                                                                                         \
     270             :                 }                                                                                                               \
     271             :                 break;                                                                                                  \
     272             :         default:                                                                                                        \
     273             :                 WRONG_PARAM_COUNT;                                                                              \
     274             :         }                                                                                                                       \
     275             :         if ((key_len = php_dba_make_key(key, &key_str, &key_free)) == 0) {\
     276             :                 RETURN_FALSE;                                                                                   \
     277             :         }
     278             : 
     279             : 
     280             : #define DBA_FETCH_RESOURCE(info, id)    \
     281             :         if ((info = (dba_info *)zend_fetch_resource2(Z_RES_P(id), "DBA identifier", le_db, le_pdb)) == NULL) { \
     282             :                 RETURN_FALSE; \
     283             :         }
     284             : 
     285             : #define DBA_FETCH_RESOURCE_WITH_ID(info, id)    \
     286             :         if ((info = (dba_info *)zend_fetch_resource2(Z_RES_P(id), "DBA identifier", le_db, le_pdb)) == NULL) { \
     287             :                 DBA_ID_DONE; \
     288             :                 RETURN_FALSE; \
     289             :         }
     290             : 
     291             : #define DBA_ID_GET2   DBA_ID_PARS; DBA_GET2;   DBA_FETCH_RESOURCE_WITH_ID(info, id)
     292             : #define DBA_ID_GET2_3 DBA_ID_PARS; DBA_GET2_3; DBA_FETCH_RESOURCE_WITH_ID(info, id)
     293             : 
     294             : #define DBA_ID_DONE                                                                                             \
     295             :         if (key_free) efree(key_free)
     296             : /* a DBA handler must have specific routines */
     297             : 
     298             : #define DBA_NAMED_HND(alias, name, flags) \
     299             : {\
     300             :         #alias, flags, dba_open_##name, dba_close_##name, dba_fetch_##name, dba_update_##name, \
     301             :         dba_exists_##name, dba_delete_##name, dba_firstkey_##name, dba_nextkey_##name, \
     302             :         dba_optimize_##name, dba_sync_##name, dba_info_##name \
     303             : },
     304             : 
     305             : #define DBA_HND(name, flags) DBA_NAMED_HND(name, name, flags)
     306             : 
     307             : /* check whether the user has write access */
     308             : #define DBA_WRITE_CHECK \
     309             :         if(info->mode != DBA_WRITER && info->mode != DBA_TRUNC && info->mode != DBA_CREAT) { \
     310             :                 php_error_docref(NULL, E_WARNING, "You cannot perform a modification to a database without proper access"); \
     311             :                 RETURN_FALSE; \
     312             :         }
     313             : 
     314             : /* the same check, but with a call to DBA_ID_DONE before returning */
     315             : #define DBA_WRITE_CHECK_WITH_ID \
     316             :         if(info->mode != DBA_WRITER && info->mode != DBA_TRUNC && info->mode != DBA_CREAT) { \
     317             :                 php_error_docref(NULL, E_WARNING, "You cannot perform a modification to a database without proper access"); \
     318             :                 DBA_ID_DONE; \
     319             :                 RETURN_FALSE; \
     320             :         }
     321             : 
     322             : /* }}} */
     323             : 
     324             : /* {{{ globals */
     325             : 
     326             : static dba_handler handler[] = {
     327             : #if DBA_GDBM
     328             :         DBA_HND(gdbm, DBA_LOCK_EXT) /* Locking done in library if set */
     329             : #endif
     330             : #if DBA_DBM
     331             :         DBA_HND(dbm, DBA_LOCK_ALL) /* No lock in lib */
     332             : #endif
     333             : #if DBA_NDBM
     334             :         DBA_HND(ndbm, DBA_LOCK_ALL) /* Could be done in library: filemode = 0644 + S_ENFMT */
     335             : #endif
     336             : #if DBA_CDB
     337             :         DBA_HND(cdb, DBA_STREAM_OPEN|DBA_LOCK_ALL) /* No lock in lib */
     338             : #endif
     339             : #if DBA_CDB_BUILTIN
     340             :     DBA_NAMED_HND(cdb_make, cdb, DBA_STREAM_OPEN|DBA_LOCK_ALL) /* No lock in lib */
     341             : #endif
     342             : #if DBA_DB1
     343             :         DBA_HND(db1, DBA_LOCK_ALL) /* No lock in lib */
     344             : #endif
     345             : #if DBA_DB2
     346             :         DBA_HND(db2, DBA_LOCK_ALL) /* No lock in lib */
     347             : #endif
     348             : #if DBA_DB3
     349             :         DBA_HND(db3, DBA_LOCK_ALL) /* No lock in lib */
     350             : #endif
     351             : #if DBA_DB4
     352             :         DBA_HND(db4, DBA_LOCK_ALL) /* No lock in lib */
     353             : #endif
     354             : #if DBA_INIFILE
     355             :         DBA_HND(inifile, DBA_STREAM_OPEN|DBA_LOCK_ALL|DBA_CAST_AS_FD) /* No lock in lib */
     356             : #endif
     357             : #if DBA_FLATFILE
     358             :         DBA_HND(flatfile, DBA_STREAM_OPEN|DBA_LOCK_ALL|DBA_NO_APPEND) /* No lock in lib */
     359             : #endif
     360             : #if DBA_QDBM
     361             :         DBA_HND(qdbm, DBA_LOCK_EXT)
     362             : #endif
     363             : #if DBA_TCADB
     364             :         DBA_HND(tcadb, DBA_LOCK_ALL)
     365             : #endif
     366             :         { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
     367             : };
     368             : 
     369             : #if DBA_FLATFILE
     370             : #define DBA_DEFAULT "flatfile"
     371             : #elif DBA_DB4
     372             : #define DBA_DEFAULT "db4"
     373             : #elif DBA_DB3
     374             : #define DBA_DEFAULT "db3"
     375             : #elif DBA_DB2
     376             : #define DBA_DEFAULT "db2"
     377             : #elif DBA_DB1
     378             : #define DBA_DEFAULT "db1"
     379             : #elif DBA_GDBM
     380             : #define DBA_DEFAULT "gdbm"
     381             : #elif DBA_NBBM
     382             : #define DBA_DEFAULT "ndbm"
     383             : #elif DBA_DBM
     384             : #define DBA_DEFAULT "dbm"
     385             : #elif DBA_QDBM
     386             : #define DBA_DEFAULT "qdbm"
     387             : #elif DBA_TCADB
     388             : #define DBA_DEFAULT "tcadb"
     389             : #else
     390             : #define DBA_DEFAULT ""
     391             : #endif
     392             : /* cdb/cdb_make and ini are no option here */
     393             : 
     394             : static int le_db;
     395             : static int le_pdb;
     396             : /* }}} */
     397             : 
     398             : /* {{{ dba_fetch_resource
     399             : PHPAPI void dba_fetch_resource(dba_info **pinfo, zval **id)
     400             : {
     401             :         dba_info *info;
     402             :         DBA_ID_FETCH
     403             :         *pinfo = info;
     404             : }
     405             : */
     406             : /* }}} */
     407             : 
     408             : /* {{{ dba_get_handler
     409             : PHPAPI dba_handler *dba_get_handler(const char* handler_name)
     410             : {
     411             :         dba_handler *hptr;
     412             :         for (hptr = handler; hptr->name && strcasecmp(hptr->name, handler_name); hptr++);
     413             :         return hptr;
     414             : }
     415             : */
     416             : /* }}} */
     417             : 
     418             : /* {{{ dba_close
     419             :  */
     420         109 : static void dba_close(dba_info *info)
     421             : {
     422         109 :         if (info->hnd) {
     423          95 :                 info->hnd->close(info);
     424             :         }
     425         109 :         if (info->path) {
     426         109 :                 pefree(info->path, info->flags&DBA_PERSISTENT);
     427             :         }
     428         109 :         if (info->fp && info->fp != info->lock.fp) {
     429          16 :                 if (info->flags & DBA_PERSISTENT) {
     430           4 :                         php_stream_pclose(info->fp);
     431             :                 } else {
     432          12 :                         php_stream_close(info->fp);
     433             :                 }
     434             :         }
     435         109 :         if (info->lock.fp) {
     436          91 :                 if (info->flags & DBA_PERSISTENT) {
     437          22 :                         php_stream_pclose(info->lock.fp);
     438             :                 } else {
     439          69 :                         php_stream_close(info->lock.fp);
     440             :                 }
     441             :         }
     442         109 :         if (info->lock.name) {
     443          91 :                 pefree(info->lock.name, info->flags&DBA_PERSISTENT);
     444             :         }
     445         109 :         pefree(info, info->flags&DBA_PERSISTENT);
     446         109 : }
     447             : /* }}} */
     448             : 
     449             : /* {{{ dba_close_rsrc
     450             :  */
     451          95 : static void dba_close_rsrc(zend_resource *rsrc)
     452             : {
     453          95 :         dba_info *info = (dba_info *)rsrc->ptr;
     454             : 
     455          95 :         dba_close(info);
     456          95 : }
     457             : /* }}} */
     458             : 
     459             : /* {{{ dba_close_pe_rsrc_deleter */
     460          56 : int dba_close_pe_rsrc_deleter(zval *el, void *pDba)
     461             : {
     462          56 :         if (Z_RES_P(el)->ptr == pDba) {
     463          29 :                 if (Z_DELREF_P(el) == 0) {
     464          27 :                         return ZEND_HASH_APPLY_REMOVE;
     465             :                 } else {
     466           2 :                         return ZEND_HASH_APPLY_KEEP | ZEND_HASH_APPLY_STOP;
     467             :                 }
     468             :         } else {
     469          27 :                 return ZEND_HASH_APPLY_KEEP;
     470             :         }
     471             : }
     472             : /* }}} */
     473             : 
     474             : /* {{{ dba_close_pe_rsrc */
     475          29 : static void dba_close_pe_rsrc(zend_resource *rsrc)
     476             : {
     477          29 :         dba_info *info = (dba_info *)rsrc->ptr;
     478             : 
     479             :         /* closes the resource by calling dba_close_rsrc() */
     480          29 :         zend_hash_apply_with_argument(&EG(persistent_list), dba_close_pe_rsrc_deleter, info);
     481          29 : }
     482             : /* }}} */
     483             : 
     484             : /* {{{ PHP_INI
     485             :  */
     486       22590 : ZEND_INI_MH(OnUpdateDefaultHandler)
     487             : {
     488             :         dba_handler *hptr;
     489             : 
     490       22590 :         if (!ZSTR_LEN(new_value)) {
     491           1 :                 DBA_G(default_hptr) = NULL;
     492           1 :                 return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
     493             :         }
     494             : 
     495       22589 :         for (hptr = handler; hptr->name && strcasecmp(hptr->name, ZSTR_VAL(new_value)); hptr++);
     496             : 
     497       22589 :         if (!hptr->name) {
     498           1 :                 php_error_docref(NULL, E_WARNING, "No such handler: %s", ZSTR_VAL(new_value));
     499           1 :                 return FAILURE;
     500             :         }
     501       22588 :         DBA_G(default_hptr) = hptr;
     502       22588 :         return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
     503             : }
     504             : 
     505             : PHP_INI_BEGIN()
     506             :     STD_PHP_INI_ENTRY("dba.default_handler", DBA_DEFAULT, PHP_INI_ALL, OnUpdateDefaultHandler, default_handler,    zend_dba_globals, dba_globals)
     507             : PHP_INI_END()
     508             : /* }}} */
     509             : 
     510             : /* {{{ PHP_GINIT_FUNCTION
     511             :  */
     512       22587 : static PHP_GINIT_FUNCTION(dba)
     513             : {
     514       22587 :         dba_globals->default_handler = "";
     515       22587 :         dba_globals->default_hptr    = NULL;
     516       22587 : }
     517             : /* }}} */
     518             : 
     519             : /* {{{ PHP_MINIT_FUNCTION
     520             :  */
     521       22587 : PHP_MINIT_FUNCTION(dba)
     522             : {
     523       22587 :         REGISTER_INI_ENTRIES();
     524       22587 :         le_db = zend_register_list_destructors_ex(dba_close_rsrc, NULL, "dba", module_number);
     525       22587 :         le_pdb = zend_register_list_destructors_ex(dba_close_pe_rsrc, dba_close_rsrc, "dba persistent", module_number);
     526       22587 :         return SUCCESS;
     527             : }
     528             : /* }}} */
     529             : 
     530             : /* {{{ PHP_MSHUTDOWN_FUNCTION
     531             :  */
     532       22623 : PHP_MSHUTDOWN_FUNCTION(dba)
     533             : {
     534       22623 :         UNREGISTER_INI_ENTRIES();
     535       22623 :         return SUCCESS;
     536             : }
     537             : /* }}} */
     538             : 
     539             : #include "zend_smart_str.h"
     540             : 
     541             : /* {{{ PHP_MINFO_FUNCTION
     542             :  */
     543         149 : PHP_MINFO_FUNCTION(dba)
     544             : {
     545             :         dba_handler *hptr;
     546         149 :         smart_str handlers = {0};
     547             : 
     548        1043 :         for(hptr = handler; hptr->name; hptr++) {
     549         894 :                 smart_str_appends(&handlers, hptr->name);
     550             :                 smart_str_appendc(&handlers, ' ');
     551             :         }
     552             : 
     553         149 :         php_info_print_table_start();
     554         149 :         php_info_print_table_row(2, "DBA support", "enabled");
     555         149 :         if (handlers.s) {
     556             :                 smart_str_0(&handlers);
     557         149 :                 php_info_print_table_row(2, "Supported handlers", ZSTR_VAL(handlers.s));
     558             :                 smart_str_free(&handlers);
     559             :         } else {
     560           0 :                 php_info_print_table_row(2, "Supported handlers", "none");
     561             :         }
     562         149 :         php_info_print_table_end();
     563         149 :         DISPLAY_INI_ENTRIES();
     564         149 : }
     565             : /* }}} */
     566             : 
     567             : /* {{{ php_dba_update
     568             :  */
     569         163 : static void php_dba_update(INTERNAL_FUNCTION_PARAMETERS, int mode)
     570             : {
     571             :         size_t val_len;
     572             :         zval *id;
     573         163 :         dba_info *info = NULL;
     574         163 :         int ac = ZEND_NUM_ARGS();
     575             :         zval *key;
     576             :         char *val;
     577             :         char *key_str, *key_free;
     578             :         size_t key_len;
     579             : 
     580         163 :         if (zend_parse_parameters(ac, "zsr", &key, &val, &val_len, &id) == FAILURE) {
     581           0 :                 return;
     582             :         }
     583             : 
     584         163 :         if ((key_len = php_dba_make_key(key, &key_str, &key_free)) == 0) {
     585           0 :                 RETURN_FALSE;
     586             :         }
     587             : 
     588         161 :         DBA_FETCH_RESOURCE_WITH_ID(info, id);
     589             : 
     590         161 :         DBA_WRITE_CHECK_WITH_ID;
     591             : 
     592         159 :         if (info->hnd->update(info, key_str, key_len, val, val_len, mode) == SUCCESS) {
     593         149 :                 DBA_ID_DONE;
     594         149 :                 RETURN_TRUE;
     595             :         }
     596             : 
     597          10 :         DBA_ID_DONE;
     598          10 :         RETURN_FALSE;
     599             : }
     600             : /* }}} */
     601             : 
     602             : #define FREENOW if(args) {int i; for (i=0; i<ac; i++) { zval_ptr_dtor(&args[i]); } efree(args);} if(key) efree(key)
     603             : 
     604             : /* {{{ php_find_dbm
     605             :  */
     606         104 : dba_info *php_dba_find(const char* path)
     607             : {
     608             :         zend_resource *le;
     609             :         dba_info *info;
     610             :         int numitems, i;
     611             : 
     612         104 :         numitems = zend_hash_next_free_element(&EG(regular_list));
     613        1634 :         for (i=1; i<numitems; i++) {
     614        3086 :                 if ((le = zend_hash_index_find_ptr(&EG(regular_list), i)) == NULL) {
     615        1011 :                         continue;
     616             :                 }
     617         532 :                 if (le->type == le_db || le->type == le_pdb) {
     618          19 :                         info = (dba_info *)(le->ptr);
     619          19 :                         if (!strcmp(info->path, path)) {
     620          13 :                                 return (dba_info *)(le->ptr);
     621             :                         }
     622             :                 }
     623             :         }
     624             : 
     625          91 :         return NULL;
     626             : }
     627             : /* }}} */
     628             : 
     629             : /* {{{ php_dba_open
     630             :  */
     631         117 : static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, int persistent)
     632             : {
     633         117 :         zval *args = NULL;
     634         117 :         int ac = ZEND_NUM_ARGS();
     635             :         dba_mode_t modenr;
     636             :         dba_info *info, *other;
     637             :         dba_handler *hptr;
     638         117 :         char *key = NULL, *error = NULL;
     639         117 :         int keylen = 0;
     640             :         int i;
     641         117 :         int lock_mode, lock_flag, lock_dbf = 0;
     642             :         char *file_mode;
     643         117 :         char mode[4], *pmode, *lock_file_mode = NULL;
     644         117 :         int persistent_flag = persistent ? STREAM_OPEN_PERSISTENT : 0;
     645         117 :         zend_string *opened_path = NULL;
     646             :         char *lock_name;
     647             : 
     648         117 :         if (ac < 2) {
     649           1 :                 WRONG_PARAM_COUNT;
     650             :         }
     651             : 
     652             :         /* we pass additional args to the respective handler */
     653         116 :         args = safe_emalloc(ac, sizeof(zval), 0);
     654         116 :         if (zend_get_parameters_array_ex(ac, args) != SUCCESS) {
     655           0 :                 efree(args);
     656           0 :                 WRONG_PARAM_COUNT;
     657             :         }
     658             : 
     659             :         /* we only take string arguments */
     660         467 :         for (i = 0; i < ac; i++) {
     661         702 :                 if (Z_TYPE(args[i]) != IS_STRING) {
     662          21 :                         convert_to_string_ex(&args[i]);
     663         344 :                 } else if (Z_REFCOUNTED(args[i])) {
     664         140 :                         Z_ADDREF(args[i]);
     665             :                 }
     666         351 :                 keylen += Z_STRLEN(args[i]);
     667             :         }
     668             : 
     669         116 :         if (persistent) {
     670             :                 zend_resource *le;
     671             : 
     672             :                 /* calculate hash */
     673          31 :                 key = safe_emalloc(keylen, 1, 1);
     674          31 :                 key[keylen] = '\0';
     675          31 :                 keylen = 0;
     676             : 
     677         123 :                 for(i = 0; i < ac; i++) {
     678          92 :                         memcpy(key+keylen, Z_STRVAL(args[i]), Z_STRLEN(args[i]));
     679          92 :                         keylen += Z_STRLEN(args[i]);
     680             :                 }
     681             : 
     682             :                 /* try to find if we already have this link in our persistent list */
     683          62 :                 if ((le = zend_hash_str_find_ptr(&EG(persistent_list), key, keylen)) != NULL) {
     684           2 :                         FREENOW;
     685             : 
     686           2 :                         if (le->type != le_pdb) {
     687           0 :                                 RETURN_FALSE;
     688             :                         }
     689             : 
     690           2 :                         info = (dba_info *)le->ptr;
     691             : 
     692           2 :                         GC_REFCOUNT(le)++;
     693           2 :                         RETURN_RES(zend_register_resource(info, le_pdb));
     694             :                         return;
     695             :                 }
     696             :         }
     697             : 
     698         114 :         if (ac==2) {
     699           4 :                 hptr = DBA_G(default_hptr);
     700           4 :                 if (!hptr) {
     701           1 :                         php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "No default handler selected");
     702           1 :                         FREENOW;
     703           1 :                         RETURN_FALSE;
     704             :                 }
     705             :         } else {
     706         110 :                 for (hptr = handler; hptr->name && strcasecmp(hptr->name, Z_STRVAL(args[2])); hptr++);
     707             :         }
     708             : 
     709         113 :         if (!hptr->name) {
     710           1 :                 php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "No such handler: %s", Z_STRVAL(args[2]));
     711           1 :                 FREENOW;
     712           1 :                 RETURN_FALSE;
     713             :         }
     714             : 
     715             :         /* Check mode: [rwnc][fl]?t?
     716             :          * r: Read
     717             :          * w: Write
     718             :          * n: Create/Truncate
     719             :          * c: Create
     720             :          *
     721             :          * d: force lock on database file
     722             :          * l: force lock on lck file
     723             :          * -: ignore locking
     724             :          *
     725             :          * t: test open database, warning if locked
     726             :          */
     727         112 :         strlcpy(mode, Z_STRVAL(args[1]), sizeof(mode));
     728         112 :         pmode = &mode[0];
     729         142 :         if (pmode[0] && (pmode[1]=='d' || pmode[1]=='l' || pmode[1]=='-')) { /* force lock on db file or lck file or disable locking */
     730          30 :                 switch (pmode[1]) {
     731             :                 case 'd':
     732           1 :                         lock_dbf = 1;
     733           1 :                         if ((hptr->flags & DBA_LOCK_ALL) == 0) {
     734           0 :                                 lock_flag = (hptr->flags & DBA_LOCK_ALL);
     735           0 :                                 break;
     736             :                         }
     737             :                         /* no break */
     738             :                 case 'l':
     739          24 :                         lock_flag = DBA_LOCK_ALL;
     740          24 :                         if ((hptr->flags & DBA_LOCK_ALL) == 0) {
     741           0 :                                 php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_NOTICE, "Handler %s does locking internally", hptr->name);
     742             :                         }
     743          24 :                         break;
     744             :                 default:
     745             :                 case '-':
     746           6 :                         if ((hptr->flags & DBA_LOCK_ALL) == 0) {
     747           0 :                                 php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Locking cannot be disabled for handler %s", hptr->name);
     748           0 :                                 FREENOW;
     749           0 :                                 RETURN_FALSE;
     750             :                         }
     751           6 :                         lock_flag = 0;
     752             :                         break;
     753             :                 }
     754             :         } else {
     755          82 :                 lock_flag = (hptr->flags&DBA_LOCK_ALL);
     756          82 :                 lock_dbf = 1;
     757             :         }
     758         112 :         switch (*pmode++) {
     759             :                 case 'r':
     760          37 :                         modenr = DBA_READER;
     761          37 :                         lock_mode = (lock_flag & DBA_LOCK_READER) ? LOCK_SH : 0;
     762          37 :                         file_mode = "r";
     763          37 :                         break;
     764             :                 case 'w':
     765           3 :                         modenr = DBA_WRITER;
     766           3 :                         lock_mode = (lock_flag & DBA_LOCK_WRITER) ? LOCK_EX : 0;
     767           3 :                         file_mode = "r+b";
     768           3 :                         break;
     769             :                 case 'c':
     770          25 :                         modenr = DBA_CREAT;
     771          25 :                         lock_mode = (lock_flag & DBA_LOCK_CREAT) ? LOCK_EX : 0;
     772          25 :                         if (lock_mode) {
     773          24 :                                 if (lock_dbf) {
     774             :                                         /* the create/append check will be done on the lock
     775             :                                          * when the lib opens the file it is already created
     776             :                                          */
     777          20 :                                         file_mode = "r+b";       /* read & write, seek 0 */
     778          20 :                                         lock_file_mode = "a+b";  /* append */
     779             :                                 } else {
     780           4 :                                         file_mode = "a+b";       /* append */
     781           4 :                                         lock_file_mode = "w+b";  /* create/truncate */
     782             :                                 }
     783             :                         } else {
     784           1 :                                 file_mode = "a+b";
     785             :                         }
     786             :                         /* In case of the 'a+b' append mode, the handler is responsible
     787             :                          * to handle any rewind problems (see flatfile handler).
     788             :                          */
     789          25 :                         break;
     790             :                 case 'n':
     791          46 :                         modenr = DBA_TRUNC;
     792          46 :                         lock_mode = (lock_flag & DBA_LOCK_TRUNC) ? LOCK_EX : 0;
     793          46 :                         file_mode = "w+b";
     794          46 :                         break;
     795             :                 default:
     796           1 :                         php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Illegal DBA mode");
     797           1 :                         FREENOW;
     798           1 :                         RETURN_FALSE;
     799             :         }
     800         111 :         if (!lock_file_mode) {
     801          87 :                 lock_file_mode = file_mode;
     802             :         }
     803         111 :         if (*pmode=='d' || *pmode=='l' || *pmode=='-') {
     804          30 :                 pmode++; /* done already - skip here */
     805             :         }
     806         111 :         if (*pmode=='t') {
     807           5 :                 pmode++;
     808           5 :                 if (!lock_flag) {
     809           1 :                         php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "You cannot combine modifiers - (no lock) and t (test lock)");
     810           1 :                         FREENOW;
     811           1 :                         RETURN_FALSE;
     812             :                 }
     813           4 :                 if (!lock_mode) {
     814           0 :                         if ((hptr->flags & DBA_LOCK_ALL) == 0) {
     815           0 :                                 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);
     816           0 :                                 FREENOW;
     817           0 :                                 RETURN_FALSE;
     818             :                         } else {
     819           0 :                                 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);
     820           0 :                                 FREENOW;
     821           0 :                                 RETURN_FALSE;
     822             :                         }
     823             :                 } else {
     824           4 :                         lock_mode |= LOCK_NB; /* test =: non blocking */
     825             :                 }
     826             :         }
     827         110 :         if (*pmode) {
     828           1 :                 php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Illegal DBA mode");
     829           1 :                 FREENOW;
     830           1 :                 RETURN_FALSE;
     831             :         }
     832             : 
     833         109 :         info = pemalloc(sizeof(dba_info), persistent);
     834         109 :         memset(info, 0, sizeof(dba_info));
     835         109 :         info->path = pestrdup(Z_STRVAL(args[0]), persistent);
     836         109 :         info->mode = modenr;
     837         109 :         info->argc = ac - 3;
     838         109 :         info->argv = args + 3;
     839         109 :         info->flags = (hptr->flags & ~DBA_LOCK_ALL) | (lock_flag & DBA_LOCK_ALL) | (persistent ? DBA_PERSISTENT : 0);
     840         109 :         info->lock.mode = lock_mode;
     841             : 
     842             :         /* if any open call is a locking call:
     843             :          * check if we already habe a locking call open that should block this call
     844             :          * the problem is some systems would allow read during write
     845             :          */
     846         109 :         if (hptr->flags & DBA_LOCK_ALL) {
     847         104 :                 if ((other = php_dba_find(info->path)) != NULL) {
     848          34 :                         if (   ( (lock_mode&LOCK_EX)        && (other->lock.mode&(LOCK_EX|LOCK_SH)) )
     849          21 :                             || ( (other->lock.mode&LOCK_EX) && (lock_mode&(LOCK_EX|LOCK_SH))        )
     850             :                            ) {
     851           8 :                                 error = "Unable to establish lock (database file already open)"; /* force failure exit */
     852             :                         }
     853             :                 }
     854             :         }
     855             : 
     856         109 :         if (!error && lock_mode) {
     857          91 :                 if (lock_dbf) {
     858          72 :                         lock_name = Z_STRVAL(args[0]);
     859             :                 } else {
     860          19 :                         spprintf(&lock_name, 0, "%s.lck", info->path);
     861          19 :                         if (!strcmp(file_mode, "r")) {
     862             :                                 /* when in read only mode try to use existing .lck file first */
     863             :                                 /* do not log errors for .lck file while in read ony mode on .lck file */
     864           8 :                                 lock_file_mode = "rb";
     865           8 :                                 info->lock.fp = php_stream_open_wrapper(lock_name, lock_file_mode, STREAM_MUST_SEEK|IGNORE_PATH|persistent_flag, &opened_path);
     866             :                         }
     867          19 :                         if (!info->lock.fp) {
     868             :                                 /* when not in read mode or failed to open .lck file read only. now try again in create(write) mode and log errors */
     869          13 :                                 lock_file_mode = "a+b";
     870             :                         } else {
     871           6 :                                 if (opened_path) {
     872           6 :                                         info->lock.name = pestrndup(ZSTR_VAL(opened_path), ZSTR_LEN(opened_path), persistent);
     873           6 :                                         zend_string_release(opened_path);
     874             :                                 }
     875             :                         }
     876             :                 }
     877          91 :                 if (!info->lock.fp) {
     878          85 :                         info->lock.fp = php_stream_open_wrapper(lock_name, lock_file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|persistent_flag, &opened_path);
     879          85 :                         if (info->lock.fp) {
     880          85 :                                 if (lock_dbf) {
     881             :                                         /* replace the path info with the real path of the opened file */
     882          72 :                                         pefree(info->path, persistent);
     883          72 :                                         info->path = pestrndup(ZSTR_VAL(opened_path), ZSTR_LEN(opened_path), persistent);
     884             :                                 }
     885             :                                 /* now store the name of the lock */
     886          85 :                                 info->lock.name = pestrndup(ZSTR_VAL(opened_path), ZSTR_LEN(opened_path), persistent);
     887          85 :                                 zend_string_release(opened_path);
     888             :                         }
     889             :                 }
     890          91 :                 if (!lock_dbf) {
     891          19 :                         efree(lock_name);
     892             :                 }
     893          91 :                 if (!info->lock.fp) {
     894           0 :                         dba_close(info);
     895             :                         /* stream operation already wrote an error message */
     896           0 :                         FREENOW;
     897           0 :                         RETURN_FALSE;
     898             :                 }
     899          91 :                 if (!php_stream_supports_lock(info->lock.fp)) {
     900           0 :                         error = "Stream does not support locking";
     901             :                 }
     902          91 :                 if (php_stream_lock(info->lock.fp, lock_mode)) {
     903           0 :                         error = "Unable to establish lock"; /* force failure exit */
     904             :                 }
     905             :         }
     906             : 
     907             :         /* centralised open stream for builtin */
     908         109 :         if (!error && (hptr->flags&DBA_STREAM_OPEN)==DBA_STREAM_OPEN) {
     909         108 :                 if (info->lock.fp && lock_dbf) {
     910          46 :                         info->fp = info->lock.fp; /* use the same stream for locking and database access */
     911             :                 } else {
     912          16 :                         info->fp = php_stream_open_wrapper(info->path, file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|persistent_flag, NULL);
     913             :                 }
     914          62 :                 if (!info->fp) {
     915           0 :                         dba_close(info);
     916             :                         /* stream operation already wrote an error message */
     917           0 :                         FREENOW;
     918           0 :                         RETURN_FALSE;
     919             :                 }
     920          62 :                 if (hptr->flags & (DBA_NO_APPEND|DBA_CAST_AS_FD)) {
     921             :                         /* Needed because some systems do not allow to write to the original
     922             :                          * file contents with O_APPEND being set.
     923             :                          */
     924          62 :                         if (SUCCESS != php_stream_cast(info->fp, PHP_STREAM_AS_FD, (void*)&info->fd, 1)) {
     925           0 :                                 php_error_docref(NULL, E_WARNING, "Could not cast stream");
     926           0 :                                 dba_close(info);
     927           0 :                                 FREENOW;
     928           0 :                                 RETURN_FALSE;
     929             : #ifdef F_SETFL
     930          62 :                         } else if (modenr == DBA_CREAT) {
     931           9 :                                 int flags = fcntl(info->fd, F_GETFL);
     932           9 :                                 fcntl(info->fd, F_SETFL, flags & ~O_APPEND);
     933             : #endif
     934             :                         }
     935             : 
     936             :                 }
     937             :         }
     938             : 
     939         109 :         if (error || hptr->open(info, &error) != SUCCESS) {
     940          14 :                 dba_close(info);
     941          14 :                 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:"");
     942          14 :                 FREENOW;
     943          14 :                 RETURN_FALSE;
     944             :         }
     945             : 
     946          95 :         info->hnd = hptr;
     947          95 :         info->argc = 0;
     948          95 :         info->argv = NULL;
     949             : 
     950          95 :         if (persistent) {
     951             :                 zval new_le;
     952             : 
     953          27 :                 ZVAL_NEW_PERSISTENT_RES(&new_le, -1, info, le_pdb);
     954          27 :                 if (zend_hash_str_update(&EG(persistent_list), key, keylen, &new_le) == NULL) {
     955           0 :                         dba_close(info);
     956           0 :                         php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Could not register persistent resource");
     957           0 :                         FREENOW;
     958           0 :                         RETURN_FALSE;
     959             :                 }
     960             :         }
     961             : 
     962          95 :         RETVAL_RES(zend_register_resource(info, (persistent ? le_pdb : le_db)));
     963          95 :         FREENOW;
     964             : }
     965             : /* }}} */
     966             : #undef FREENOW
     967             : 
     968             : /* {{{ proto resource dba_popen(string path, string mode [, string handlername, string ...])
     969             :    Opens path using the specified handler in mode persistently */
     970          31 : PHP_FUNCTION(dba_popen)
     971             : {
     972          31 :         php_dba_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
     973          31 : }
     974             : /* }}} */
     975             : 
     976             : /* {{{ proto resource dba_open(string path, string mode [, string handlername, string ...])
     977             :    Opens path using the specified handler in mode*/
     978          86 : PHP_FUNCTION(dba_open)
     979             : {
     980          86 :         php_dba_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
     981          86 : }
     982             : /* }}} */
     983             : 
     984             : /* {{{ proto void dba_close(resource handle)
     985             :    Closes database */
     986          73 : PHP_FUNCTION(dba_close)
     987             : {
     988             :         zval *id;
     989          73 :         dba_info *info = NULL;
     990             : 
     991          73 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &id) == FAILURE) {
     992           0 :                 return;
     993             :         }
     994             : 
     995          73 :         DBA_FETCH_RESOURCE(info, id);
     996             : 
     997          73 :         zend_list_close(Z_RES_P(id));
     998             : }
     999             : /* }}} */
    1000             : 
    1001             : /* {{{ proto bool dba_exists(string key, resource handle)
    1002             :    Checks, if the specified key exists */
    1003          90 : PHP_FUNCTION(dba_exists)
    1004             : {
    1005          90 :         DBA_ID_GET2;
    1006             : 
    1007          90 :         if(info->hnd->exists(info, key_str, key_len) == SUCCESS) {
    1008          65 :                 DBA_ID_DONE;
    1009          65 :                 RETURN_TRUE;
    1010             :         }
    1011          25 :         DBA_ID_DONE;
    1012          25 :         RETURN_FALSE;
    1013             : }
    1014             : /* }}} */
    1015             : 
    1016             : /* {{{ proto string dba_fetch(string key, [int skip ,] resource handle)
    1017             :    Fetches the data associated with key */
    1018         135 : PHP_FUNCTION(dba_fetch)
    1019             : {
    1020             :         char *val;
    1021         135 :         int len = 0;
    1022         135 :         DBA_ID_GET2_3;
    1023             : 
    1024         135 :         if (ac==3) {
    1025           7 :                 if (!strcmp(info->hnd->name, "cdb")) {
    1026           7 :                         if (skip < 0) {
    1027           0 :                                 php_error_docref(NULL, E_NOTICE, "Handler %s accepts only skip values greater than or equal to zero, using skip=0", info->hnd->name);
    1028           0 :                                 skip = 0;
    1029             :                         }
    1030           0 :                 } else if (!strcmp(info->hnd->name, "inifile")) {
    1031             :                         /* "-1" is compareable to 0 but allows a non restrictive
    1032             :                          * access which is fater. For example 'inifile' uses this
    1033             :                          * to allow faster access when the key was already found
    1034             :                          * using firstkey/nextkey. However explicitly setting the
    1035             :                          * value to 0 ensures the first value.
    1036             :                          */
    1037           0 :                         if (skip < -1) {
    1038           0 :                                 php_error_docref(NULL, E_NOTICE, "Handler %s accepts only skip value -1 and greater, using skip=0", info->hnd->name);
    1039           0 :                                 skip = 0;
    1040             :                         }
    1041             :                 } else {
    1042           0 :                         php_error_docref(NULL, E_NOTICE, "Handler %s does not support optional skip parameter, the value will be ignored", info->hnd->name);
    1043           0 :                         skip = 0;
    1044             :                 }
    1045             :         } else {
    1046         128 :                 skip = 0;
    1047             :         }
    1048         135 :         if((val = info->hnd->fetch(info, key_str, key_len, skip, &len)) != NULL) {
    1049         133 :                 DBA_ID_DONE;
    1050         266 :                 RETVAL_STRINGL(val, len);
    1051         133 :                 efree(val);
    1052         133 :                 return;
    1053             :         }
    1054           2 :         DBA_ID_DONE;
    1055           2 :         RETURN_FALSE;
    1056             : }
    1057             : /* }}} */
    1058             : 
    1059             : /* {{{ proto array|false dba_key_split(string key)
    1060             :    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 */
    1061          11 : PHP_FUNCTION(dba_key_split)
    1062             : {
    1063             :         zval *zkey;
    1064             :         char *key, *name;
    1065             :         size_t key_len;
    1066             : 
    1067          11 :         if (ZEND_NUM_ARGS() != 1) {
    1068           1 :                 WRONG_PARAM_COUNT;
    1069             :         }
    1070          10 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "z", &zkey) == SUCCESS) {
    1071          29 :                 if (Z_TYPE_P(zkey) == IS_NULL || (Z_TYPE_P(zkey) == IS_FALSE)) {
    1072           1 :                         RETURN_BOOL(0);
    1073             :                 }
    1074             :         }
    1075           9 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &key, &key_len) == FAILURE) {
    1076           0 :                 RETURN_BOOL(0);
    1077             :         }
    1078           9 :         array_init(return_value);
    1079          13 :         if (key[0] == '[' && (name = strchr(key, ']')) != NULL) {
    1080           4 :                 add_next_index_stringl(return_value, key+1, name - (key + 1));
    1081           4 :                 add_next_index_stringl(return_value, name+1, key_len - (name - key + 1));
    1082             :         } else {
    1083           5 :                 add_next_index_stringl(return_value, "", 0);
    1084           5 :                 add_next_index_stringl(return_value, key, key_len);
    1085             :         }
    1086             : }
    1087             : /* }}} */
    1088             : 
    1089             : /* {{{ proto string dba_firstkey(resource handle)
    1090             :    Resets the internal key pointer and returns the first key */
    1091          30 : PHP_FUNCTION(dba_firstkey)
    1092             : {
    1093             :         char *fkey;
    1094             :         int len;
    1095             :         zval *id;
    1096          30 :         dba_info *info = NULL;
    1097             : 
    1098          30 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &id) == FAILURE) {
    1099           0 :                 return;
    1100             :         }
    1101             : 
    1102          30 :         DBA_FETCH_RESOURCE(info, id);
    1103             : 
    1104          30 :         fkey = info->hnd->firstkey(info, &len);
    1105             : 
    1106          30 :         if (fkey) {
    1107          58 :                 RETVAL_STRINGL(fkey, len);
    1108          29 :                 efree(fkey);
    1109          29 :                 return;
    1110             :         }
    1111             : 
    1112           1 :         RETURN_FALSE;
    1113             : }
    1114             : /* }}} */
    1115             : 
    1116             : /* {{{ proto string dba_nextkey(resource handle)
    1117             :    Returns the next key */
    1118         103 : PHP_FUNCTION(dba_nextkey)
    1119             : {
    1120             :         char *nkey;
    1121             :         int len;
    1122             :         zval *id;
    1123         103 :         dba_info *info = NULL;
    1124             : 
    1125         103 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &id) == FAILURE) {
    1126           0 :                 return;
    1127             :         }
    1128             : 
    1129         103 :         DBA_FETCH_RESOURCE(info, id);
    1130             : 
    1131         103 :         nkey = info->hnd->nextkey(info, &len);
    1132             : 
    1133         103 :         if (nkey) {
    1134         148 :                 RETVAL_STRINGL(nkey, len);
    1135          74 :                 efree(nkey);
    1136          74 :                 return;
    1137             :         }
    1138             : 
    1139          29 :         RETURN_FALSE;
    1140             : }
    1141             : /* }}} */
    1142             : 
    1143             : /* {{{ proto bool dba_delete(string key, resource handle)
    1144             :    Deletes the entry associated with key
    1145             :    If inifile: remove all other key lines */
    1146          32 : PHP_FUNCTION(dba_delete)
    1147             : {
    1148          32 :         DBA_ID_GET2;
    1149             : 
    1150          32 :         DBA_WRITE_CHECK_WITH_ID;
    1151             : 
    1152          32 :         if(info->hnd->delete(info, key_str, key_len) == SUCCESS)
    1153             :         {
    1154          30 :                 DBA_ID_DONE;
    1155          30 :                 RETURN_TRUE;
    1156             :         }
    1157           2 :         DBA_ID_DONE;
    1158           2 :         RETURN_FALSE;
    1159             : }
    1160             : /* }}} */
    1161             : 
    1162             : /* {{{ proto bool dba_insert(string key, string value, resource handle)
    1163             :    If not inifile: Insert value as key, return false, if key exists already
    1164             :    If inifile: Add vakue as key (next instance of key) */
    1165         143 : PHP_FUNCTION(dba_insert)
    1166             : {
    1167         143 :         php_dba_update(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
    1168         141 : }
    1169             : /* }}} */
    1170             : 
    1171             : /* {{{ proto bool dba_replace(string key, string value, resource handle)
    1172             :    Inserts value as key, replaces key, if key exists already
    1173             :    If inifile: remove all other key lines */
    1174          20 : PHP_FUNCTION(dba_replace)
    1175             : {
    1176          20 :         php_dba_update(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
    1177          20 : }
    1178             : /* }}} */
    1179             : 
    1180             : /* {{{ proto bool dba_optimize(resource handle)
    1181             :    Optimizes (e.g. clean up, vacuum) database */
    1182           5 : PHP_FUNCTION(dba_optimize)
    1183             : {
    1184             :         zval *id;
    1185           5 :         dba_info *info = NULL;
    1186             : 
    1187           5 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &id) == FAILURE) {
    1188           2 :                 return;
    1189             :         }
    1190             : 
    1191           3 :         DBA_FETCH_RESOURCE(info, id);
    1192             : 
    1193           3 :         DBA_WRITE_CHECK;
    1194             : 
    1195           3 :         if (info->hnd->optimize(info) == SUCCESS) {
    1196           3 :                 RETURN_TRUE;
    1197             :         }
    1198             : 
    1199           0 :         RETURN_FALSE;
    1200             : }
    1201             : /* }}} */
    1202             : 
    1203             : /* {{{ proto bool dba_sync(resource handle)
    1204             :    Synchronizes database */
    1205           5 : PHP_FUNCTION(dba_sync)
    1206             : {
    1207             :         zval *id;
    1208           5 :         dba_info *info = NULL;
    1209             : 
    1210           5 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &id) == FAILURE) {
    1211           2 :                 return;
    1212             :         }
    1213             : 
    1214           3 :         DBA_FETCH_RESOURCE(info, id);
    1215             : 
    1216           3 :         if (info->hnd->sync(info) == SUCCESS) {
    1217           3 :                 RETURN_TRUE;
    1218             :         }
    1219             : 
    1220           0 :         RETURN_FALSE;
    1221             : }
    1222             : /* }}} */
    1223             : 
    1224             : /* {{{ proto array dba_handlers([bool full_info])
    1225             :    List configured database handlers */
    1226         231 : PHP_FUNCTION(dba_handlers)
    1227             : {
    1228             :         dba_handler *hptr;
    1229         231 :         zend_bool full_info = 0;
    1230             : 
    1231         231 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &full_info) == FAILURE) {
    1232           1 :                 RETURN_FALSE;
    1233             :         }
    1234             : 
    1235         230 :         array_init(return_value);
    1236             : 
    1237        1610 :         for(hptr = handler; hptr->name; hptr++) {
    1238        1380 :                 if (full_info) {
    1239             :                         // TODO: avoid reallocation ???
    1240          12 :                         char *str = hptr->info(hptr, NULL);
    1241          12 :                         add_assoc_string(return_value, hptr->name, str);
    1242          12 :                         efree(str);
    1243             :                 } else {
    1244        1368 :                         add_next_index_string(return_value, hptr->name);
    1245             :                 }
    1246             :         }
    1247             : }
    1248             : /* }}} */
    1249             : 
    1250             : /* {{{ proto array dba_list()
    1251             :    List opened databases */
    1252           2 : PHP_FUNCTION(dba_list)
    1253             : {
    1254             :         zend_ulong numitems, i;
    1255             :         zend_resource *le;
    1256             :         dba_info *info;
    1257             : 
    1258           2 :         if (zend_parse_parameters_none() == FAILURE) {
    1259           0 :                 RETURN_FALSE;
    1260             :         }
    1261             : 
    1262           2 :         array_init(return_value);
    1263             : 
    1264           2 :         numitems = zend_hash_next_free_element(&EG(regular_list));
    1265          26 :         for (i=1; i<numitems; i++) {
    1266          24 :                 if ((le = zend_hash_index_find_ptr(&EG(regular_list), i)) == NULL) {
    1267           4 :                         continue;
    1268             :                 }
    1269          20 :                 if (le->type == le_db || le->type == le_pdb) {
    1270           6 :                         info = (dba_info *)(le->ptr);
    1271           6 :                         add_index_string(return_value, i, info->path);
    1272             :                 }
    1273             :         }
    1274             : }
    1275             : /* }}} */
    1276             : 
    1277             : #endif /* HAVE_DBA */
    1278             : 
    1279             : /*
    1280             :  * Local variables:
    1281             :  * tab-width: 4
    1282             :  * c-basic-offset: 4
    1283             :  * End:
    1284             :  * vim600: sw=4 ts=4 fdm=marker
    1285             :  * vim<600: sw=4 ts=4
    1286             :  */

Generated by: LCOV version 1.10

Generated at Thu, 26 May 2016 12:47:55 +0000 (9 hours ago)

Copyright © 2005-2016 The PHP Group
All rights reserved.