This source file includes following definitions.
- php_dba_make_key
- dba_close
- dba_close_rsrc
- dba_close_pe_rsrc_deleter
- dba_close_pe_rsrc
- ZEND_INI_MH
- PHP_INI_BEGIN
- PHP_MINIT_FUNCTION
- PHP_MSHUTDOWN_FUNCTION
- PHP_MINFO_FUNCTION
- php_dba_update
- php_dba_find
- php_dba_open
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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
132
133 const zend_function_entry dba_functions[] = {
134 PHP_FE(dba_open, arginfo_dba_open)
135 PHP_FE(dba_popen, arginfo_dba_popen)
136 PHP_FE(dba_close, arginfo_dba_close)
137 PHP_FE(dba_delete, arginfo_dba_delete)
138 PHP_FE(dba_exists, arginfo_dba_exists)
139 PHP_FE(dba_fetch, arginfo_dba_fetch)
140 PHP_FE(dba_insert, arginfo_dba_insert)
141 PHP_FE(dba_replace, arginfo_dba_replace)
142 PHP_FE(dba_firstkey, arginfo_dba_firstkey)
143 PHP_FE(dba_nextkey, arginfo_dba_nextkey)
144 PHP_FE(dba_optimize, arginfo_dba_optimize)
145 PHP_FE(dba_sync, arginfo_dba_sync)
146 PHP_FE(dba_handlers, arginfo_dba_handlers)
147 PHP_FE(dba_list, arginfo_dba_list)
148 PHP_FE(dba_key_split, arginfo_dba_key_split)
149 PHP_FE_END
150 };
151
152
153 PHP_MINIT_FUNCTION(dba);
154 PHP_MSHUTDOWN_FUNCTION(dba);
155 PHP_MINFO_FUNCTION(dba);
156
157 ZEND_BEGIN_MODULE_GLOBALS(dba)
158 char *default_handler;
159 dba_handler *default_hptr;
160 ZEND_END_MODULE_GLOBALS(dba)
161
162 ZEND_DECLARE_MODULE_GLOBALS(dba)
163
164 #ifdef ZTS
165 #define DBA_G(v) TSRMG(dba_globals_id, zend_dba_globals *, v)
166 #else
167 #define DBA_G(v) (dba_globals.v)
168 #endif
169
170 static PHP_GINIT_FUNCTION(dba);
171
172 zend_module_entry dba_module_entry = {
173 STANDARD_MODULE_HEADER,
174 "dba",
175 dba_functions,
176 PHP_MINIT(dba),
177 PHP_MSHUTDOWN(dba),
178 NULL,
179 NULL,
180 PHP_MINFO(dba),
181 PHP_DBA_VERSION,
182 PHP_MODULE_GLOBALS(dba),
183 PHP_GINIT(dba),
184 NULL,
185 NULL,
186 STANDARD_MODULE_PROPERTIES_EX
187 };
188
189 #ifdef COMPILE_DL_DBA
190 ZEND_GET_MODULE(dba)
191 #endif
192
193
194
195 #define DBA_ID_PARS \
196 zval *id; \
197 dba_info *info = NULL; \
198 int ac = ZEND_NUM_ARGS()
199
200
201
202
203 static size_t php_dba_make_key(zval *key, char **key_str, char **key_free)
204 {
205 if (Z_TYPE_P(key) == IS_ARRAY) {
206 zval *group, *name;
207 HashPosition pos;
208 size_t len;
209
210 if (zend_hash_num_elements(Z_ARRVAL_P(key)) != 2) {
211 php_error_docref(NULL, E_RECOVERABLE_ERROR, "Key does not have exactly two elements: (key, name)");
212 return 0;
213 }
214 zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(key), &pos);
215 group = zend_hash_get_current_data_ex(Z_ARRVAL_P(key), &pos);
216 zend_hash_move_forward_ex(Z_ARRVAL_P(key), &pos);
217 name = zend_hash_get_current_data_ex(Z_ARRVAL_P(key), &pos);
218 convert_to_string_ex(group);
219 convert_to_string_ex(name);
220 if (Z_STRLEN_P(group) == 0) {
221 *key_str = Z_STRVAL_P(name);
222 *key_free = NULL;
223 return Z_STRLEN_P(name);
224 }
225 len = spprintf(key_str, 0, "[%s]%s", Z_STRVAL_P(group), Z_STRVAL_P(name));
226 *key_free = *key_str;
227 return len;
228 } else {
229 zval tmp;
230 int len;
231
232 ZVAL_COPY(&tmp, key);
233 convert_to_string(&tmp);
234
235 len = Z_STRLEN(tmp);
236 if (len) {
237 *key_free = *key_str = estrndup(Z_STRVAL(tmp), Z_STRLEN(tmp));
238 }
239 zval_ptr_dtor(&tmp);
240 return len;
241 }
242 }
243
244
245 #define DBA_GET2 \
246 zval *key; \
247 char *key_str, *key_free; \
248 size_t key_len; \
249 if (zend_parse_parameters(ac, "zr", &key, &id) == FAILURE) { \
250 return; \
251 } \
252 if ((key_len = php_dba_make_key(key, &key_str, &key_free)) == 0) {\
253 RETURN_FALSE; \
254 }
255
256 #define DBA_GET2_3 \
257 zval *key; \
258 char *key_str, *key_free; \
259 size_t key_len; \
260 zend_long skip = 0; \
261 switch(ac) { \
262 case 2: \
263 if (zend_parse_parameters(ac, "zr", &key, &id) == FAILURE) { \
264 return; \
265 } \
266 break; \
267 case 3: \
268 if (zend_parse_parameters(ac, "zlr", &key, &skip, &id) == FAILURE) { \
269 return; \
270 } \
271 break; \
272 default: \
273 WRONG_PARAM_COUNT; \
274 } \
275 if ((key_len = php_dba_make_key(key, &key_str, &key_free)) == 0) {\
276 RETURN_FALSE; \
277 }
278
279
280 #define DBA_FETCH_RESOURCE(info, id) \
281 if ((info = (dba_info *)zend_fetch_resource2(Z_RES_P(id), "DBA identifier", le_db, le_pdb)) == NULL) { \
282 RETURN_FALSE; \
283 }
284
285 #define DBA_FETCH_RESOURCE_WITH_ID(info, id) \
286 if ((info = (dba_info *)zend_fetch_resource2(Z_RES_P(id), "DBA identifier", le_db, le_pdb)) == NULL) { \
287 DBA_ID_DONE; \
288 RETURN_FALSE; \
289 }
290
291 #define DBA_ID_GET2 DBA_ID_PARS; DBA_GET2; DBA_FETCH_RESOURCE_WITH_ID(info, id)
292 #define DBA_ID_GET2_3 DBA_ID_PARS; DBA_GET2_3; DBA_FETCH_RESOURCE_WITH_ID(info, id)
293
294 #define DBA_ID_DONE \
295 if (key_free) efree(key_free)
296
297
298 #define DBA_NAMED_HND(alias, name, flags) \
299 {\
300 #alias, flags, dba_open_##name, dba_close_##name, dba_fetch_##name, dba_update_##name, \
301 dba_exists_##name, dba_delete_##name, dba_firstkey_##name, dba_nextkey_##name, \
302 dba_optimize_##name, dba_sync_##name, dba_info_##name \
303 },
304
305 #define DBA_HND(name, flags) DBA_NAMED_HND(name, name, flags)
306
307
308 #define DBA_WRITE_CHECK \
309 if(info->mode != DBA_WRITER && info->mode != DBA_TRUNC && info->mode != DBA_CREAT) { \
310 php_error_docref(NULL, E_WARNING, "You cannot perform a modification to a database without proper access"); \
311 RETURN_FALSE; \
312 }
313
314
315 #define DBA_WRITE_CHECK_WITH_ID \
316 if(info->mode != DBA_WRITER && info->mode != DBA_TRUNC && info->mode != DBA_CREAT) { \
317 php_error_docref(NULL, E_WARNING, "You cannot perform a modification to a database without proper access"); \
318 DBA_ID_DONE; \
319 RETURN_FALSE; \
320 }
321
322
323
324
325
326 static dba_handler handler[] = {
327 #if DBA_GDBM
328 DBA_HND(gdbm, DBA_LOCK_EXT)
329 #endif
330 #if DBA_DBM
331 DBA_HND(dbm, DBA_LOCK_ALL)
332 #endif
333 #if DBA_NDBM
334 DBA_HND(ndbm, DBA_LOCK_ALL)
335 #endif
336 #if DBA_CDB
337 DBA_HND(cdb, DBA_STREAM_OPEN|DBA_LOCK_ALL)
338 #endif
339 #if DBA_CDB_BUILTIN
340 DBA_NAMED_HND(cdb_make, cdb, DBA_STREAM_OPEN|DBA_LOCK_ALL)
341 #endif
342 #if DBA_DB1
343 DBA_HND(db1, DBA_LOCK_ALL)
344 #endif
345 #if DBA_DB2
346 DBA_HND(db2, DBA_LOCK_ALL)
347 #endif
348 #if DBA_DB3
349 DBA_HND(db3, DBA_LOCK_ALL)
350 #endif
351 #if DBA_DB4
352 DBA_HND(db4, DBA_LOCK_ALL)
353 #endif
354 #if DBA_INIFILE
355 DBA_HND(inifile, DBA_STREAM_OPEN|DBA_LOCK_ALL|DBA_CAST_AS_FD)
356 #endif
357 #if DBA_FLATFILE
358 DBA_HND(flatfile, DBA_STREAM_OPEN|DBA_LOCK_ALL|DBA_NO_APPEND)
359 #endif
360 #if DBA_QDBM
361 DBA_HND(qdbm, DBA_LOCK_EXT)
362 #endif
363 #if DBA_TCADB
364 DBA_HND(tcadb, DBA_LOCK_ALL)
365 #endif
366 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
367 };
368
369 #if DBA_FLATFILE
370 #define DBA_DEFAULT "flatfile"
371 #elif DBA_DB4
372 #define DBA_DEFAULT "db4"
373 #elif DBA_DB3
374 #define DBA_DEFAULT "db3"
375 #elif DBA_DB2
376 #define DBA_DEFAULT "db2"
377 #elif DBA_DB1
378 #define DBA_DEFAULT "db1"
379 #elif DBA_GDBM
380 #define DBA_DEFAULT "gdbm"
381 #elif DBA_NBBM
382 #define DBA_DEFAULT "ndbm"
383 #elif DBA_DBM
384 #define DBA_DEFAULT "dbm"
385 #elif DBA_QDBM
386 #define DBA_DEFAULT "qdbm"
387 #elif DBA_TCADB
388 #define DBA_DEFAULT "tcadb"
389 #else
390 #define DBA_DEFAULT ""
391 #endif
392
393
394 static int le_db;
395 static int le_pdb;
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420 static void dba_close(dba_info *info)
421 {
422 if (info->hnd) {
423 info->hnd->close(info);
424 }
425 if (info->path) {
426 pefree(info->path, info->flags&DBA_PERSISTENT);
427 }
428 if (info->fp && info->fp != info->lock.fp) {
429 if (info->flags & DBA_PERSISTENT) {
430 php_stream_pclose(info->fp);
431 } else {
432 php_stream_close(info->fp);
433 }
434 }
435 if (info->lock.fp) {
436 if (info->flags & DBA_PERSISTENT) {
437 php_stream_pclose(info->lock.fp);
438 } else {
439 php_stream_close(info->lock.fp);
440 }
441 }
442 if (info->lock.name) {
443 pefree(info->lock.name, info->flags&DBA_PERSISTENT);
444 }
445 pefree(info, info->flags&DBA_PERSISTENT);
446 }
447
448
449
450
451 static void dba_close_rsrc(zend_resource *rsrc)
452 {
453 dba_info *info = (dba_info *)rsrc->ptr;
454
455 dba_close(info);
456 }
457
458
459
460 int dba_close_pe_rsrc_deleter(zval *el, void *pDba)
461 {
462 if (Z_RES_P(el)->ptr == pDba) {
463 if (Z_DELREF_P(el) == 0) {
464 return ZEND_HASH_APPLY_REMOVE;
465 } else {
466 return ZEND_HASH_APPLY_KEEP | ZEND_HASH_APPLY_STOP;
467 }
468 } else {
469 return ZEND_HASH_APPLY_KEEP;
470 }
471 }
472
473
474
475 static void dba_close_pe_rsrc(zend_resource *rsrc)
476 {
477 dba_info *info = (dba_info *)rsrc->ptr;
478
479
480 zend_hash_apply_with_argument(&EG(persistent_list), dba_close_pe_rsrc_deleter, info);
481 }
482
483
484
485
486 ZEND_INI_MH(OnUpdateDefaultHandler)
487 {
488 dba_handler *hptr;
489
490 if (!ZSTR_LEN(new_value)) {
491 DBA_G(default_hptr) = NULL;
492 return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
493 }
494
495 for (hptr = handler; hptr->name && strcasecmp(hptr->name, ZSTR_VAL(new_value)); hptr++);
496
497 if (!hptr->name) {
498 php_error_docref(NULL, E_WARNING, "No such handler: %s", ZSTR_VAL(new_value));
499 return FAILURE;
500 }
501 DBA_G(default_hptr) = hptr;
502 return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
503 }
504
505 PHP_INI_BEGIN()
506 STD_PHP_INI_ENTRY("dba.default_handler", DBA_DEFAULT, PHP_INI_ALL, OnUpdateDefaultHandler, default_handler, zend_dba_globals, dba_globals)
507 PHP_INI_END()
508
509
510
511
512 static PHP_GINIT_FUNCTION(dba)
513 {
514 dba_globals->default_handler = "";
515 dba_globals->default_hptr = NULL;
516 }
517
518
519
520
521 PHP_MINIT_FUNCTION(dba)
522 {
523 REGISTER_INI_ENTRIES();
524 le_db = zend_register_list_destructors_ex(dba_close_rsrc, NULL, "dba", module_number);
525 le_pdb = zend_register_list_destructors_ex(dba_close_pe_rsrc, dba_close_rsrc, "dba persistent", module_number);
526 return SUCCESS;
527 }
528
529
530
531
532 PHP_MSHUTDOWN_FUNCTION(dba)
533 {
534 UNREGISTER_INI_ENTRIES();
535 return SUCCESS;
536 }
537
538
539 #include "zend_smart_str.h"
540
541
542
543 PHP_MINFO_FUNCTION(dba)
544 {
545 dba_handler *hptr;
546 smart_str handlers = {0};
547
548 for(hptr = handler; hptr->name; hptr++) {
549 smart_str_appends(&handlers, hptr->name);
550 smart_str_appendc(&handlers, ' ');
551 }
552
553 php_info_print_table_start();
554 php_info_print_table_row(2, "DBA support", "enabled");
555 if (handlers.s) {
556 smart_str_0(&handlers);
557 php_info_print_table_row(2, "Supported handlers", ZSTR_VAL(handlers.s));
558 smart_str_free(&handlers);
559 } else {
560 php_info_print_table_row(2, "Supported handlers", "none");
561 }
562 php_info_print_table_end();
563 DISPLAY_INI_ENTRIES();
564 }
565
566
567
568
569 static void php_dba_update(INTERNAL_FUNCTION_PARAMETERS, int mode)
570 {
571 size_t val_len;
572 zval *id;
573 dba_info *info = NULL;
574 int ac = ZEND_NUM_ARGS();
575 zval *key;
576 char *val;
577 char *key_str, *key_free;
578 size_t key_len;
579
580 if (zend_parse_parameters(ac, "zsr", &key, &val, &val_len, &id) == FAILURE) {
581 return;
582 }
583
584 if ((key_len = php_dba_make_key(key, &key_str, &key_free)) == 0) {
585 RETURN_FALSE;
586 }
587
588 DBA_FETCH_RESOURCE_WITH_ID(info, id);
589
590 DBA_WRITE_CHECK_WITH_ID;
591
592 if (info->hnd->update(info, key_str, key_len, val, val_len, mode) == SUCCESS) {
593 DBA_ID_DONE;
594 RETURN_TRUE;
595 }
596
597 DBA_ID_DONE;
598 RETURN_FALSE;
599 }
600
601
602 #define FREENOW if(args) {int i; for (i=0; i<ac; i++) { zval_ptr_dtor(&args[i]); } efree(args);} if(key) efree(key)
603
604
605
606 dba_info *php_dba_find(const char* path)
607 {
608 zend_resource *le;
609 dba_info *info;
610 int numitems, i;
611
612 numitems = zend_hash_next_free_element(&EG(regular_list));
613 for (i=1; i<numitems; i++) {
614 if ((le = zend_hash_index_find_ptr(&EG(regular_list), i)) == NULL) {
615 continue;
616 }
617 if (le->type == le_db || le->type == le_pdb) {
618 info = (dba_info *)(le->ptr);
619 if (!strcmp(info->path, path)) {
620 return (dba_info *)(le->ptr);
621 }
622 }
623 }
624
625 return NULL;
626 }
627
628
629
630
631 static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, int persistent)
632 {
633 zval *args = NULL;
634 int ac = ZEND_NUM_ARGS();
635 dba_mode_t modenr;
636 dba_info *info, *other;
637 dba_handler *hptr;
638 char *key = NULL, *error = NULL;
639 int keylen = 0;
640 int i;
641 int lock_mode, lock_flag, lock_dbf = 0;
642 char *file_mode;
643 char mode[4], *pmode, *lock_file_mode = NULL;
644 int persistent_flag = persistent ? STREAM_OPEN_PERSISTENT : 0;
645 zend_string *opened_path = NULL;
646 char *lock_name;
647
648 if (ac < 2) {
649 WRONG_PARAM_COUNT;
650 }
651
652
653 args = safe_emalloc(ac, sizeof(zval), 0);
654 if (zend_get_parameters_array_ex(ac, args) != SUCCESS) {
655 efree(args);
656 WRONG_PARAM_COUNT;
657 }
658
659
660 for (i = 0; i < ac; i++) {
661 if (Z_TYPE(args[i]) != IS_STRING) {
662 convert_to_string_ex(&args[i]);
663 } else if (Z_REFCOUNTED(args[i])) {
664 Z_ADDREF(args[i]);
665 }
666 keylen += Z_STRLEN(args[i]);
667 }
668
669 if (persistent) {
670 zend_resource *le;
671
672
673 key = safe_emalloc(keylen, 1, 1);
674 key[keylen] = '\0';
675 keylen = 0;
676
677 for(i = 0; i < ac; i++) {
678 memcpy(key+keylen, Z_STRVAL(args[i]), Z_STRLEN(args[i]));
679 keylen += Z_STRLEN(args[i]);
680 }
681
682
683 if ((le = zend_hash_str_find_ptr(&EG(persistent_list), key, keylen)) != NULL) {
684 FREENOW;
685
686 if (le->type != le_pdb) {
687 RETURN_FALSE;
688 }
689
690 info = (dba_info *)le->ptr;
691
692 GC_REFCOUNT(le)++;
693 RETURN_RES(zend_register_resource(info, le_pdb));
694 return;
695 }
696 }
697
698 if (ac==2) {
699 hptr = DBA_G(default_hptr);
700 if (!hptr) {
701 php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "No default handler selected");
702 FREENOW;
703 RETURN_FALSE;
704 }
705 } else {
706 for (hptr = handler; hptr->name && strcasecmp(hptr->name, Z_STRVAL(args[2])); hptr++);
707 }
708
709 if (!hptr->name) {
710 php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "No such handler: %s", Z_STRVAL(args[2]));
711 FREENOW;
712 RETURN_FALSE;
713 }
714
715
716
717
718
719
720
721
722
723
724
725
726
727 strlcpy(mode, Z_STRVAL(args[1]), sizeof(mode));
728 pmode = &mode[0];
729 if (pmode[0] && (pmode[1]=='d' || pmode[1]=='l' || pmode[1]=='-')) {
730 switch (pmode[1]) {
731 case 'd':
732 lock_dbf = 1;
733 if ((hptr->flags & DBA_LOCK_ALL) == 0) {
734 lock_flag = (hptr->flags & DBA_LOCK_ALL);
735 break;
736 }
737
738 case 'l':
739 lock_flag = DBA_LOCK_ALL;
740 if ((hptr->flags & DBA_LOCK_ALL) == 0) {
741 php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_NOTICE, "Handler %s does locking internally", hptr->name);
742 }
743 break;
744 default:
745 case '-':
746 if ((hptr->flags & DBA_LOCK_ALL) == 0) {
747 php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Locking cannot be disabled for handler %s", hptr->name);
748 FREENOW;
749 RETURN_FALSE;
750 }
751 lock_flag = 0;
752 break;
753 }
754 } else {
755 lock_flag = (hptr->flags&DBA_LOCK_ALL);
756 lock_dbf = 1;
757 }
758 switch (*pmode++) {
759 case 'r':
760 modenr = DBA_READER;
761 lock_mode = (lock_flag & DBA_LOCK_READER) ? LOCK_SH : 0;
762 file_mode = "r";
763 break;
764 case 'w':
765 modenr = DBA_WRITER;
766 lock_mode = (lock_flag & DBA_LOCK_WRITER) ? LOCK_EX : 0;
767 file_mode = "r+b";
768 break;
769 case 'c':
770 modenr = DBA_CREAT;
771 lock_mode = (lock_flag & DBA_LOCK_CREAT) ? LOCK_EX : 0;
772 if (lock_mode) {
773 if (lock_dbf) {
774
775
776
777 file_mode = "r+b";
778 lock_file_mode = "a+b";
779 } else {
780 file_mode = "a+b";
781 lock_file_mode = "w+b";
782 }
783 } else {
784 file_mode = "a+b";
785 }
786
787
788
789 break;
790 case 'n':
791 modenr = DBA_TRUNC;
792 lock_mode = (lock_flag & DBA_LOCK_TRUNC) ? LOCK_EX : 0;
793 file_mode = "w+b";
794 break;
795 default:
796 php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Illegal DBA mode");
797 FREENOW;
798 RETURN_FALSE;
799 }
800 if (!lock_file_mode) {
801 lock_file_mode = file_mode;
802 }
803 if (*pmode=='d' || *pmode=='l' || *pmode=='-') {
804 pmode++;
805 }
806 if (*pmode=='t') {
807 pmode++;
808 if (!lock_flag) {
809 php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "You cannot combine modifiers - (no lock) and t (test lock)");
810 FREENOW;
811 RETURN_FALSE;
812 }
813 if (!lock_mode) {
814 if ((hptr->flags & DBA_LOCK_ALL) == 0) {
815 php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Handler %s uses its own locking which doesn't support mode modifier t (test lock)", hptr->name);
816 FREENOW;
817 RETURN_FALSE;
818 } else {
819 php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Handler %s doesn't uses locking for this mode which makes modifier t (test lock) obsolete", hptr->name);
820 FREENOW;
821 RETURN_FALSE;
822 }
823 } else {
824 lock_mode |= LOCK_NB;
825 }
826 }
827 if (*pmode) {
828 php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Illegal DBA mode");
829 FREENOW;
830 RETURN_FALSE;
831 }
832
833 info = pemalloc(sizeof(dba_info), persistent);
834 memset(info, 0, sizeof(dba_info));
835 info->path = pestrdup(Z_STRVAL(args[0]), persistent);
836 info->mode = modenr;
837 info->argc = ac - 3;
838 info->argv = args + 3;
839 info->flags = (hptr->flags & ~DBA_LOCK_ALL) | (lock_flag & DBA_LOCK_ALL) | (persistent ? DBA_PERSISTENT : 0);
840 info->lock.mode = lock_mode;
841
842
843
844
845
846 if (hptr->flags & DBA_LOCK_ALL) {
847 if ((other = php_dba_find(info->path)) != NULL) {
848 if ( ( (lock_mode&LOCK_EX) && (other->lock.mode&(LOCK_EX|LOCK_SH)) )
849 || ( (other->lock.mode&LOCK_EX) && (lock_mode&(LOCK_EX|LOCK_SH)) )
850 ) {
851 error = "Unable to establish lock (database file already open)";
852 }
853 }
854 }
855
856 if (!error && lock_mode) {
857 if (lock_dbf) {
858 lock_name = Z_STRVAL(args[0]);
859 } else {
860 spprintf(&lock_name, 0, "%s.lck", info->path);
861 if (!strcmp(file_mode, "r")) {
862
863
864 lock_file_mode = "rb";
865 info->lock.fp = php_stream_open_wrapper(lock_name, lock_file_mode, STREAM_MUST_SEEK|IGNORE_PATH|persistent_flag, &opened_path);
866 }
867 if (!info->lock.fp) {
868
869 lock_file_mode = "a+b";
870 } else {
871 if (opened_path) {
872 info->lock.name = pestrndup(ZSTR_VAL(opened_path), ZSTR_LEN(opened_path), persistent);
873 zend_string_release(opened_path);
874 }
875 }
876 }
877 if (!info->lock.fp) {
878 info->lock.fp = php_stream_open_wrapper(lock_name, lock_file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|persistent_flag, &opened_path);
879 if (info->lock.fp) {
880 if (lock_dbf) {
881
882 pefree(info->path, persistent);
883 info->path = pestrndup(ZSTR_VAL(opened_path), ZSTR_LEN(opened_path), persistent);
884 }
885
886 info->lock.name = pestrndup(ZSTR_VAL(opened_path), ZSTR_LEN(opened_path), persistent);
887 zend_string_release(opened_path);
888 }
889 }
890 if (!lock_dbf) {
891 efree(lock_name);
892 }
893 if (!info->lock.fp) {
894 dba_close(info);
895
896 FREENOW;
897 RETURN_FALSE;
898 }
899 if (!php_stream_supports_lock(info->lock.fp)) {
900 error = "Stream does not support locking";
901 }
902 if (php_stream_lock(info->lock.fp, lock_mode)) {
903 error = "Unable to establish lock";
904 }
905 }
906
907
908 if (!error && (hptr->flags&DBA_STREAM_OPEN)==DBA_STREAM_OPEN) {
909 if (info->lock.fp && lock_dbf) {
910 info->fp = info->lock.fp;
911 } else {
912 info->fp = php_stream_open_wrapper(info->path, file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|persistent_flag, NULL);
913 }
914 if (!info->fp) {
915 dba_close(info);
916
917 FREENOW;
918 RETURN_FALSE;
919 }
920 if (hptr->flags & (DBA_NO_APPEND|DBA_CAST_AS_FD)) {
921
922
923
924 if (SUCCESS != php_stream_cast(info->fp, PHP_STREAM_AS_FD, (void*)&info->fd, 1)) {
925 php_error_docref(NULL, E_WARNING, "Could not cast stream");
926 dba_close(info);
927 FREENOW;
928 RETURN_FALSE;
929 #ifdef F_SETFL
930 } else if (modenr == DBA_CREAT) {
931 int flags = fcntl(info->fd, F_GETFL);
932 fcntl(info->fd, F_SETFL, flags & ~O_APPEND);
933 #endif
934 }
935
936 }
937 }
938
939 if (error || hptr->open(info, &error) != SUCCESS) {
940 dba_close(info);
941 php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Driver initialization failed for handler: %s%s%s", hptr->name, error?": ":"", error?error:"");
942 FREENOW;
943 RETURN_FALSE;
944 }
945
946 info->hnd = hptr;
947 info->argc = 0;
948 info->argv = NULL;
949
950 if (persistent) {
951 zval new_le;
952
953 ZVAL_NEW_PERSISTENT_RES(&new_le, -1, info, le_pdb);
954 if (zend_hash_str_update(&EG(persistent_list), key, keylen, &new_le) == NULL) {
955 dba_close(info);
956 php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Could not register persistent resource");
957 FREENOW;
958 RETURN_FALSE;
959 }
960 }
961
962 RETVAL_RES(zend_register_resource(info, (persistent ? le_pdb : le_db)));
963 FREENOW;
964 }
965
966 #undef FREENOW
967
968
969
970 PHP_FUNCTION(dba_popen)
971 {
972 php_dba_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
973 }
974
975
976
977
978 PHP_FUNCTION(dba_open)
979 {
980 php_dba_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
981 }
982
983
984
985
986 PHP_FUNCTION(dba_close)
987 {
988 zval *id;
989 dba_info *info = NULL;
990
991 if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &id) == FAILURE) {
992 return;
993 }
994
995 DBA_FETCH_RESOURCE(info, id);
996
997 zend_list_close(Z_RES_P(id));
998 }
999
1000
1001
1002
1003 PHP_FUNCTION(dba_exists)
1004 {
1005 DBA_ID_GET2;
1006
1007 if(info->hnd->exists(info, key_str, key_len) == SUCCESS) {
1008 DBA_ID_DONE;
1009 RETURN_TRUE;
1010 }
1011 DBA_ID_DONE;
1012 RETURN_FALSE;
1013 }
1014
1015
1016
1017
1018 PHP_FUNCTION(dba_fetch)
1019 {
1020 char *val;
1021 int len = 0;
1022 DBA_ID_GET2_3;
1023
1024 if (ac==3) {
1025 if (!strcmp(info->hnd->name, "cdb")) {
1026 if (skip < 0) {
1027 php_error_docref(NULL, E_NOTICE, "Handler %s accepts only skip values greater than or equal to zero, using skip=0", info->hnd->name);
1028 skip = 0;
1029 }
1030 } else if (!strcmp(info->hnd->name, "inifile")) {
1031
1032
1033
1034
1035
1036
1037 if (skip < -1) {
1038 php_error_docref(NULL, E_NOTICE, "Handler %s accepts only skip value -1 and greater, using skip=0", info->hnd->name);
1039 skip = 0;
1040 }
1041 } else {
1042 php_error_docref(NULL, E_NOTICE, "Handler %s does not support optional skip parameter, the value will be ignored", info->hnd->name);
1043 skip = 0;
1044 }
1045 } else {
1046 skip = 0;
1047 }
1048 if((val = info->hnd->fetch(info, key_str, key_len, skip, &len)) != NULL) {
1049 DBA_ID_DONE;
1050 RETVAL_STRINGL(val, len);
1051 efree(val);
1052 return;
1053 }
1054 DBA_ID_DONE;
1055 RETURN_FALSE;
1056 }
1057
1058
1059
1060
1061 PHP_FUNCTION(dba_key_split)
1062 {
1063 zval *zkey;
1064 char *key, *name;
1065 size_t key_len;
1066
1067 if (ZEND_NUM_ARGS() != 1) {
1068 WRONG_PARAM_COUNT;
1069 }
1070 if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "z", &zkey) == SUCCESS) {
1071 if (Z_TYPE_P(zkey) == IS_NULL || (Z_TYPE_P(zkey) == IS_FALSE)) {
1072 RETURN_BOOL(0);
1073 }
1074 }
1075 if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &key, &key_len) == FAILURE) {
1076 RETURN_BOOL(0);
1077 }
1078 array_init(return_value);
1079 if (key[0] == '[' && (name = strchr(key, ']')) != NULL) {
1080 add_next_index_stringl(return_value, key+1, name - (key + 1));
1081 add_next_index_stringl(return_value, name+1, key_len - (name - key + 1));
1082 } else {
1083 add_next_index_stringl(return_value, "", 0);
1084 add_next_index_stringl(return_value, key, key_len);
1085 }
1086 }
1087
1088
1089
1090
1091 PHP_FUNCTION(dba_firstkey)
1092 {
1093 char *fkey;
1094 int len;
1095 zval *id;
1096 dba_info *info = NULL;
1097
1098 if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &id) == FAILURE) {
1099 return;
1100 }
1101
1102 DBA_FETCH_RESOURCE(info, id);
1103
1104 fkey = info->hnd->firstkey(info, &len);
1105
1106 if (fkey) {
1107 RETVAL_STRINGL(fkey, len);
1108 efree(fkey);
1109 return;
1110 }
1111
1112 RETURN_FALSE;
1113 }
1114
1115
1116
1117
1118 PHP_FUNCTION(dba_nextkey)
1119 {
1120 char *nkey;
1121 int len;
1122 zval *id;
1123 dba_info *info = NULL;
1124
1125 if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &id) == FAILURE) {
1126 return;
1127 }
1128
1129 DBA_FETCH_RESOURCE(info, id);
1130
1131 nkey = info->hnd->nextkey(info, &len);
1132
1133 if (nkey) {
1134 RETVAL_STRINGL(nkey, len);
1135 efree(nkey);
1136 return;
1137 }
1138
1139 RETURN_FALSE;
1140 }
1141
1142
1143
1144
1145
1146 PHP_FUNCTION(dba_delete)
1147 {
1148 DBA_ID_GET2;
1149
1150 DBA_WRITE_CHECK_WITH_ID;
1151
1152 if(info->hnd->delete(info, key_str, key_len) == SUCCESS)
1153 {
1154 DBA_ID_DONE;
1155 RETURN_TRUE;
1156 }
1157 DBA_ID_DONE;
1158 RETURN_FALSE;
1159 }
1160
1161
1162
1163
1164
1165 PHP_FUNCTION(dba_insert)
1166 {
1167 php_dba_update(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1168 }
1169
1170
1171
1172
1173
1174 PHP_FUNCTION(dba_replace)
1175 {
1176 php_dba_update(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1177 }
1178
1179
1180
1181
1182 PHP_FUNCTION(dba_optimize)
1183 {
1184 zval *id;
1185 dba_info *info = NULL;
1186
1187 if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &id) == FAILURE) {
1188 return;
1189 }
1190
1191 DBA_FETCH_RESOURCE(info, id);
1192
1193 DBA_WRITE_CHECK;
1194
1195 if (info->hnd->optimize(info) == SUCCESS) {
1196 RETURN_TRUE;
1197 }
1198
1199 RETURN_FALSE;
1200 }
1201
1202
1203
1204
1205 PHP_FUNCTION(dba_sync)
1206 {
1207 zval *id;
1208 dba_info *info = NULL;
1209
1210 if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &id) == FAILURE) {
1211 return;
1212 }
1213
1214 DBA_FETCH_RESOURCE(info, id);
1215
1216 if (info->hnd->sync(info) == SUCCESS) {
1217 RETURN_TRUE;
1218 }
1219
1220 RETURN_FALSE;
1221 }
1222
1223
1224
1225
1226 PHP_FUNCTION(dba_handlers)
1227 {
1228 dba_handler *hptr;
1229 zend_bool full_info = 0;
1230
1231 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &full_info) == FAILURE) {
1232 RETURN_FALSE;
1233 }
1234
1235 array_init(return_value);
1236
1237 for(hptr = handler; hptr->name; hptr++) {
1238 if (full_info) {
1239
1240 char *str = hptr->info(hptr, NULL);
1241 add_assoc_string(return_value, hptr->name, str);
1242 efree(str);
1243 } else {
1244 add_next_index_string(return_value, hptr->name);
1245 }
1246 }
1247 }
1248
1249
1250
1251
1252 PHP_FUNCTION(dba_list)
1253 {
1254 zend_ulong numitems, i;
1255 zend_resource *le;
1256 dba_info *info;
1257
1258 if (zend_parse_parameters_none() == FAILURE) {
1259 RETURN_FALSE;
1260 }
1261
1262 array_init(return_value);
1263
1264 numitems = zend_hash_next_free_element(&EG(regular_list));
1265 for (i=1; i<numitems; i++) {
1266 if ((le = zend_hash_index_find_ptr(&EG(regular_list), i)) == NULL) {
1267 continue;
1268 }
1269 if (le->type == le_db || le->type == le_pdb) {
1270 info = (dba_info *)(le->ptr);
1271 add_index_string(return_value, i, info->path);
1272 }
1273 }
1274 }
1275
1276
1277 #endif
1278
1279
1280
1281
1282
1283
1284
1285
1286