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

LTP GCOV extension - code coverage report
Current view: directory - dba - dba.c
Test: PHP Code Coverage
Date: 2009-11-19 Instrumented lines: 411
Code covered: 69.3 % Executed lines: 285
Legend: not executed executed

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

Generated by: LTP GCOV extension version 1.5

Generated at Thu, 19 Nov 2009 08:20:06 +0000 (5 days ago)

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