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

Generated by: LCOV version 1.10

Generated at Tue, 14 Apr 2015 11:48:41 +0000 (11 days ago)

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