1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
|
/*
+----------------------------------------------------------------------+
| PHP Version 4 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2002 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_02.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Rasmus Lerdorf <rasmus@php.net> |
| Jim Winstead <jimw@php.net> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#define IS_EXT_MODULE
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_globals.h"
#include "safe_mode.h"
#include "fopen_wrappers.h"
#include "ext/standard/flock_compat.h"
#include "ext/standard/info.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_FILE_H
#include <sys/file.h>
#endif
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
#if GDBM
#include <gdbm.h>
#define DBM_TYPE GDBM_FILE
#define DBM_MODE_TYPE int
#define DBM_WRITE_MODE GDBM_WRITER
#define DBM_CREATE_MODE GDBM_WRCREAT
#define DBM_NEW_MODE GDBM_NEWDB
#define DBM_DEFAULT_MODE GDBM_READER
#define DBM_OPEN(filename, mode) gdbm_open(filename, 512, mode, 0666, 0)
#define DBM_CLOSE(dbf) gdbm_close(dbf)
#define DBM_STORE(dbf, key, value, mode) gdbm_store(dbf, key, value, mode)
#define DBM_FETCH(dbf, key) gdbm_fetch(dbf, key)
#define DBM_EXISTS(dbf, key) gdbm_exists(dbf, key)
#define DBM_DELETE(dbf, key) gdbm_delete(dbf, key)
#define DBM_FIRSTKEY(dbf) gdbm_firstkey(dbf)
#define DBM_NEXTKEY(dbf, key) gdbm_nextkey(dbf, key)
#define DBM_INSERT GDBM_INSERT
#define DBM_REPLACE GDBM_REPLACE
#endif
#if NDBM && !GDBM
#if BSD2
#define DB_DBM_HSEARCH 1
#include <db.h>
#else
#ifdef HAVE_DB1_NDBM_H
#include <db1/ndbm.h>
#else
#include <ndbm.h>
#endif
#endif
#define DBM_TYPE DBM *
#define DBM_MODE_TYPE int
#define DBM_WRITE_MODE O_RDWR
#define DBM_CREATE_MODE O_RDWR | O_CREAT
#define DBM_NEW_MODE O_RDWR | O_CREAT | O_TRUNC
#define DBM_DEFAULT_MODE O_RDONLY
#define DBM_OPEN(filename, mode) dbm_open(filename, mode, 0666)
#define DBM_CLOSE(dbf) dbm_close(dbf)
#define DBM_STORE(dbf, key, value, mode) dbm_store(dbf, key, value, mode)
#define DBM_FETCH(dbf, key) dbm_fetch(dbf, key)
#define DBM_EXISTS(dbf, key) php_dbm_key_exists(dbf, key)
#define DBM_DELETE(dbf, key) dbm_delete(dbf, key)
#define DBM_FIRSTKEY(dbf) dbm_firstkey(dbf)
#define DBM_NEXTKEY(dbf, key) dbm_nextkey(dbf)
/* {{{ php_dbm_key_exists
*/
static int php_dbm_key_exists(DBM *dbf, datum key_datum) {
datum value_datum;
int ret;
value_datum = dbm_fetch(dbf, key_datum);
if (value_datum.dptr)
ret = 1;
else
ret = 0;
return ret;
}
/* }}} */
#endif
#if !NDBM && !GDBM
#define DBM_TYPE FILE *
#define DBM_MODE_TYPE char *
#define DBM_WRITE_MODE "r+b"
#define DBM_CREATE_MODE "a+b"
#define DBM_NEW_MODE "w+b"
#define DBM_DEFAULT_MODE "r"
#define DBM_OPEN(filename, mode) VCWD_FOPEN(filename, mode)
#define DBM_CLOSE(dbf) fclose(dbf)
#define DBM_STORE(dbf, key, value, mode) flatfile_store(dbf, key, value, mode)
#define DBM_FETCH(dbf, key) flatfile_fetch(dbf, key)
#define DBM_EXISTS(dbf, key) flatfile_findkey(dbf, key)
#define DBM_DELETE(dbf, key) flatfile_delete(dbf, key)
#define DBM_FIRSTKEY(dbf) flatfile_firstkey(dbf)
#define DBM_NEXTKEY(dbf, key) flatfile_nextkey(dbf)
#define DBM_INSERT 0
#define DBM_REPLACE 1
typedef struct {
char *dptr;
int dsize;
} datum;
int flatfile_store(FILE *dbf, datum key, datum value, int mode);
datum flatfile_fetch(FILE *dbf, datum key);
int flatfile_findkey(FILE *dbf, datum key);
int flatfile_delete(FILE *dbf, datum key);
datum flatfile_firstkey(FILE *dbf);
datum flatfile_nextkey(FILE *dbf);
#endif
#include "php_db.h"
#include "ext/standard/php_string.h"
static int le_db;
/* {{{ php_find_dbm
*/
dbm_info *php_find_dbm(pval *id TSRMLS_DC)
{
list_entry *le;
dbm_info *info;
int numitems, i;
int info_type;
if (Z_TYPE_P(id) == IS_STRING) {
numitems = zend_hash_next_free_element(&EG(regular_list));
for (i=1; i<numitems; i++) {
if (zend_hash_index_find(&EG(regular_list), i, (void **) &le)==FAILURE) {
continue;
}
if (Z_TYPE_P(le) == le_db) {
info = (dbm_info *)(le->ptr);
if (!strcmp(info->filename, Z_STRVAL_P(id))) {
return (dbm_info *)(le->ptr);
}
}
}
}
/* didn't find it as a database filename, try as a number */
convert_to_long(id);
info = zend_list_find(Z_LVAL_P(id), &info_type);
if (info_type != le_db)
return NULL;
return info;
}
/* }}} */
/* {{{ proto array dblist(void)
Return an associative array id->filename */
#if HELLY_0
/* New function not needed yet */
PHP_FUNCTION(db_id_list)
{
ulong numitems, i;
zend_rsrc_list_entry *le;
dbm_info *info;
if (ZEND_NUM_ARGS()!=0) {
ZEND_WRONG_PARAM_COUNT();
RETURN_FALSE;
}
if (array_init(return_value) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Unable to initialize array");
RETURN_FALSE;
}
numitems = zend_hash_next_free_element(&EG(regular_list));
for (i=1; i<numitems; i++) {
if (zend_hash_index_find(&EG(regular_list), i, (void **) &le)==FAILURE) {
continue;
}
if (Z_TYPE_P(le) == le_db) {
info = (dbm_info *)(le->ptr);
add_next_index_string(return_value, info->filename, 1);
}
}
}
/* }}} */
#endif
/* {{{ php_get_info_db
*/
static char *php_get_info_db(void)
{
static char temp1[128];
static char temp[256];
temp1[0]='\0';
temp[0]='\0';
#ifdef DB_VERSION_STRING /* using sleepycat dbm */
strcat(temp, DB_VERSION_STRING);
#endif
#if GDBM
sprintf(temp1, "%s", gdbm_version);
strcat(temp, temp1);
#endif
#if NDBM && !GDBM
strcat(temp, "ndbm support enabled");
#endif
#if !GDBM && !NDBM
strcat(temp, "flat file support enabled");
#endif
#if NFS_HACK
strcat(temp, "NFS hack in effect");
#endif
if (!*temp)
strcat(temp, "No database support");
return temp;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(db)
{
/* this isn't pretty ... should break out the info a bit more (cmv) */
php_info_print_box_start(0);
php_printf(php_get_info_db());
php_info_print_box_end();
}
/* }}} */
/* {{{ proto string dblist(void)
Describes the dbm-compatible library being used */
PHP_FUNCTION(dblist)
{
char *str;
if (ZEND_NUM_ARGS() != 0) {
WRONG_PARAM_COUNT;
}
str = php_get_info_db();
RETURN_STRING(str, 1);
}
/* }}} */
/* {{{ proto int dbmopen(string filename, string mode)
Opens a dbm database */
PHP_FUNCTION(dbmopen)
{
pval *filename, *mode;
dbm_info *info=NULL;
int ret;
if (ZEND_NUM_ARGS()!=2 || zend_get_parameters(ht, 2, &filename, &mode)==FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string(filename);
convert_to_string(mode);
info = php_dbm_open(Z_STRVAL_P(filename), Z_STRVAL_P(mode) TSRMLS_CC);
if (info) {
ret = zend_list_insert(info, le_db);
RETURN_LONG(ret);
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ php_dbm_open
*/
dbm_info *php_dbm_open(char *filename, char *mode TSRMLS_DC)
{
dbm_info *info;
int ret, lock=0;
char *lockfn = NULL;
int lockfd = 0;
#if NFS_HACK
int last_try = 0;
struct stat sb;
int retries = 0;
#endif
DBM_TYPE dbf=NULL;
DBM_MODE_TYPE imode;
if (filename == NULL) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "NULL filename passed");
return NULL;
}
if (PG(safe_mode) && (!php_checkuid(filename, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
return NULL;
}
if (php_check_open_basedir(filename TSRMLS_CC)) {
return NULL;
}
switch (*mode) {
case 'w':
imode = DBM_WRITE_MODE;
lock = 1;
break;
case 'c':
imode = DBM_CREATE_MODE;
lock = 1;
break;
case 'n':
imode = DBM_NEW_MODE;
lock = 1;
break;
default:
imode = DBM_DEFAULT_MODE;
lock = 0;
break;
}
if (lock) {
lockfn = emalloc(strlen(filename) + 5);
strcpy(lockfn, filename);
strcat(lockfn, ".lck");
#if NFS_HACK
while((last_try = VCWD_STAT(lockfn, &sb))==0) {
retries++;
php_sleep(1);
if (retries>30) break;
}
if (last_try!=0) {
lockfd = open(lockfn, O_RDWR|O_CREAT, 0644);
close(lockfd);
} else {
php_error_docref1(NULL TSRMLS_CC, filename, E_WARNING, "File appears to be locked [%s]", lockfn);
return -1;
}
#else /* NFS_HACK */
lockfd = VCWD_OPEN_MODE(lockfn, O_RDWR|O_CREAT, 0644);
if (lockfd) {
flock(lockfd, LOCK_EX);
close(lockfd);
} else {
php_error_docref1(NULL TSRMLS_CC, filename, E_WARNING, "Unable to establish lock");
}
#endif /* else NFS_HACK */
}
dbf = DBM_OPEN(filename, imode);
#if !NDBM && !GDBM
if (dbf) {
setvbuf(dbf, NULL, _IONBF, 0);
}
#endif
if (dbf) {
info = (dbm_info *)emalloc(sizeof(dbm_info));
if (!info) {
php_error_docref1(NULL TSRMLS_CC, filename, E_ERROR, "Problem allocating memory!");
return NULL;
}
info->filename = estrdup(filename);
info->lockfn = lockfn;
info->lockfd = lockfd;
info->dbf = dbf;
return info;
} else {
#if GDBM
php_error_docref1(NULL TSRMLS_CC, filename, E_WARNING, "%d [%s], %d [%s]", gdbm_errno, gdbm_strerror(gdbm_errno), errno, strerror(errno));
if (gdbm_errno)
ret = gdbm_errno;
else if (errno)
ret = errno;
else
ret = -1;
#else
#if NDBM
#if PHP_DEBUG
php_error_docref1(NULL TSRMLS_CC, filename, E_WARNING, "errno = %d [%s]\n", errno, strerror(errno));
#endif
if (errno) ret=errno;
else ret = -1;
#else
#if PHP_DEBUG
php_error_docref1(NULL TSRMLS_CC, filename, E_WARNING, "errno = %d [%s]\n", errno, strerror(errno));
#endif
if (errno) ret=errno;
else ret = -1;
#endif
#endif
#if NFS_HACK
if (lockfn) {
VCWD_UNLINK(lockfn);
}
#endif
if (lockfn) efree(lockfn);
}
return NULL;
}
/* }}} */
/* {{{ proto bool dbmclose(int dbm_identifier)
Closes a dbm database */
PHP_FUNCTION(dbmclose)
{
pval *id;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters(ht, 1, &id)==FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(id);
if (zend_list_delete(Z_LVAL_P(id)) == SUCCESS) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ php_dbm_close
*/
void php_dbm_close(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
dbm_info *info = (dbm_info *)rsrc->ptr;
DBM_TYPE dbf;
int lockfd;
dbf = info->dbf;
#if NFS_HACK
VCWD_UNLINK(info->lockfn);
#else
if (info->lockfn) {
lockfd = VCWD_OPEN_MODE(info->lockfn, O_RDWR, 0644);
flock(lockfd, LOCK_UN);
close(lockfd);
}
#endif
if (dbf)
DBM_CLOSE(dbf);
/* free the memory used by the dbm_info struct */
if (info->filename) efree(info->filename);
if (info->lockfn) efree(info->lockfn);
efree(info);
}
/* }}} */
/*
* ret = -1 means that database was opened for read-only
* ret = 0 success
* ret = 1 key already exists - nothing done
*/
/* {{{ proto int dbminsert(int dbm_identifier, string key, string value)
Inserts a value for a key in a dbm database */
PHP_FUNCTION(dbminsert)
{
pval *id, *key, *value;
dbm_info *info;
int ret;
if (ZEND_NUM_ARGS()!=3||zend_get_parameters(ht, 3, &id, &key, &value) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string(key);
convert_to_string(value);
info = php_find_dbm(id TSRMLS_CC);
if (!info) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid database identifier %d", Z_LVAL_P(id));
RETURN_FALSE;
}
ret = php_dbm_insert_replace(info, Z_STRVAL_P(key), Z_STRVAL_P(value), 0 TSRMLS_CC);
RETURN_LONG(ret);
}
/* }}} */
/* {{{ proto int dbmreplace(int dbm_identifier, string key, string value)
Replaces the value for a key in a dbm database */
PHP_FUNCTION(dbmreplace)
{
pval *id, *key, *value;
dbm_info *info;
int ret;
if (ZEND_NUM_ARGS()!=3||zend_get_parameters(ht, 3, &id, &key, &value) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string(key);
convert_to_string(value);
info = php_find_dbm(id TSRMLS_CC);
if (!info) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid database identifier %d", Z_LVAL_P(id));
RETURN_FALSE;
}
ret = php_dbm_insert_replace(info, Z_STRVAL_P(key), Z_STRVAL_P(value), 1 TSRMLS_CC);
RETURN_LONG(ret);
}
/* }}} */
/* {{{ php_dbm_replace
*/
int php_dbm_insert_replace(dbm_info *info, char *key, char *value, int replace_mode TSRMLS_DC)
{
DBM_TYPE dbf;
int ret;
datum key_datum, value_datum;
key = estrdup(key);
value = estrdup(value);
if (PG(magic_quotes_runtime)) {
php_stripslashes(key, NULL TSRMLS_CC);
php_stripslashes(value, NULL TSRMLS_CC);
}
value_datum.dptr = value;
value_datum.dsize = strlen(value);
key_datum.dptr = key;
key_datum.dsize = strlen(key);
#if GDBM_FIX
key_datum.dsize++;
#endif
dbf = info->dbf;
if (!dbf) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to locate dbm file");
ret = 1;
} else {
if (!replace_mode) {
ret = DBM_STORE(dbf, key_datum, value_datum, DBM_INSERT);
} else {
ret = DBM_STORE(dbf, key_datum, value_datum, DBM_REPLACE);
}
}
/* free the memory */
efree(key_datum.dptr); efree(value_datum.dptr);
return(ret);
}
/* }}} */
/* {{{ proto string dbmfetch(int dbm_identifier, string key)
Fetches a value for a key from a dbm database */
PHP_FUNCTION(dbmfetch)
{
pval *id, *key;
dbm_info *info;
char *ret;
if (ZEND_NUM_ARGS()!=2||zend_get_parameters(ht, 2, &id, &key)==FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string(key);
info = php_find_dbm(id TSRMLS_CC);
if (!info) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid database identifier %d", Z_LVAL_P(id));
RETURN_FALSE;
}
ret = php_dbm_fetch(info, Z_STRVAL_P(key) TSRMLS_CC);
if (ret) {
RETVAL_STRING(ret, 0);
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ php_dbm_fetch
*/
char *php_dbm_fetch(dbm_info *info, char *key TSRMLS_DC)
{
datum key_datum, value_datum;
char *ret;
DBM_TYPE dbf;
key_datum.dptr = key;
key_datum.dsize = strlen(key);
#if GDBM_FIX
key_datum.dsize++;
#endif
value_datum.dptr = NULL;
value_datum.dsize = 0;
dbf = info->dbf;
if (!dbf) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to locate dbm file");
return(NULL);
}
value_datum = DBM_FETCH(dbf, key_datum);
if (value_datum.dptr) {
ret = (char *)emalloc(sizeof(char) * value_datum.dsize + 1);
strncpy(ret, value_datum.dptr, value_datum.dsize);
ret[value_datum.dsize] = '\0';
#if GDBM
/* all but NDBM use malloc to allocate the content blocks, so we need to free it */
free(value_datum.dptr);
#else
# if !NDBM
efree(value_datum.dptr);
# endif
#endif
}
else
ret = NULL;
if (ret && PG(magic_quotes_runtime)) {
ret = php_addslashes(ret, value_datum.dsize, NULL, 1 TSRMLS_CC);
}
return(ret);
}
/* }}} */
/* {{{ proto int dbmexists(int dbm_identifier, string key)
Tells if a value exists for a key in a dbm database */
PHP_FUNCTION(dbmexists)
{
pval *id, *key;
dbm_info *info;
int ret;
if (ZEND_NUM_ARGS()!=2||zend_get_parameters(ht, 2, &id, &key)==FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string(key);
info = php_find_dbm(id TSRMLS_CC);
if (!info) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid database identifier %d", Z_LVAL_P(id));
RETURN_FALSE;
}
ret = php_dbm_exists(info, Z_STRVAL_P(key) TSRMLS_CC);
RETURN_LONG(ret);
}
/* }}} */
/* {{{ php_dbm_exists
*/
int php_dbm_exists(dbm_info *info, char *key TSRMLS_DC) {
datum key_datum;
int ret;
DBM_TYPE dbf;
key_datum.dptr = key;
key_datum.dsize = strlen(key);
#if GDBM_FIX
key_datum.dsize++;
#endif
dbf = info->dbf;
if (!dbf) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to locate dbm file");
return(0);
}
ret = DBM_EXISTS(dbf, key_datum);
return(ret);
}
/* }}} */
/* {{{ proto int dbmdelete(int dbm_identifier, string key)
Deletes the value for a key from a dbm database */
PHP_FUNCTION(dbmdelete)
{
pval *id, *key;
dbm_info *info;
int ret;
if (ZEND_NUM_ARGS()!=2||zend_get_parameters(ht, 2, &id, &key)==FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string(key);
info = php_find_dbm(id TSRMLS_CC);
if (!info) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid database identifier %d", Z_LVAL_P(id));
RETURN_FALSE;
}
ret = php_dbm_delete(info, Z_STRVAL_P(key) TSRMLS_CC);
RETURN_LONG(ret);
}
/* }}} */
/* {{{ php_dbm_delete
*/
int php_dbm_delete(dbm_info *info, char *key TSRMLS_DC) {
datum key_datum;
int ret;
DBM_TYPE dbf;
key_datum.dptr = key;
key_datum.dsize = strlen(key);
#if GDBM_FIX
key_datum.dsize++;
#endif
dbf = info->dbf;
if (!dbf) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to locate dbm file");
return(0);
}
ret = DBM_DELETE(dbf, key_datum);
return(ret);
}
/* }}} */
/* {{{ proto string dbmfirstkey(int dbm_identifier)
Retrieves the first key from a dbm database */
PHP_FUNCTION(dbmfirstkey)
{
pval *id;
dbm_info *info;
char *ret;
if (ZEND_NUM_ARGS()!=1||zend_get_parameters(ht, 1, &id)==FAILURE) {
WRONG_PARAM_COUNT;
}
info = php_find_dbm(id TSRMLS_CC);
if (!info) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid database identifier %d", Z_LVAL_P(id));
RETURN_FALSE;
}
ret = php_dbm_first_key(info TSRMLS_CC);
if (!ret) {
RETURN_FALSE;
} else {
RETVAL_STRING(ret, 0);
}
}
/* }}} */
/* {{{ php_dbm_first_key
*/
char *php_dbm_first_key(dbm_info *info TSRMLS_DC) {
datum ret_datum;
char *ret;
DBM_TYPE dbf;
dbf = info->dbf;
if (!dbf) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to locate dbm file");
return(NULL);
}
/* explicitly zero-out ret_datum */
ret_datum.dptr = NULL;
ret_datum.dsize = 0;
ret_datum = DBM_FIRSTKEY(dbf);
if (!ret_datum.dptr)
return NULL;
ret = (char *)emalloc((ret_datum.dsize + 1) * sizeof(char));
strncpy(ret, ret_datum.dptr, ret_datum.dsize);
ret[ret_datum.dsize] = '\0';
#if !NDBM & !GDBM
efree(ret_datum.dptr);
#endif
return (ret);
}
/* }}} */
/* {{{ proto string dbmnextkey(int dbm_identifier, string key)
Retrieves the next key from a dbm database */
PHP_FUNCTION(dbmnextkey)
{
pval *id, *key;
dbm_info *info;
char *ret;
if (ZEND_NUM_ARGS()!=2||zend_get_parameters(ht, 2, &id, &key)==FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string(key);
info = php_find_dbm(id TSRMLS_CC);
if (!info) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid database identifier %d", Z_LVAL_P(id));
RETURN_FALSE;
}
ret = php_dbm_nextkey(info, Z_STRVAL_P(key) TSRMLS_CC);
if (!ret) {
RETURN_FALSE;
} else {
RETVAL_STRING(ret, 0);
}
}
/* }}} */
/* {{{ php_dbm_nextkey
*/
char *php_dbm_nextkey(dbm_info *info, char *key TSRMLS_DC)
{
datum key_datum, ret_datum;
char *ret;
DBM_TYPE dbf;
key_datum.dptr = key;
key_datum.dsize = strlen(key);
#if GDBM_FIX
key_datum.dsize++;
#endif
dbf = info->dbf;
if (!dbf) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to locate dbm file");
return(NULL);
}
/* explicitly zero-out ret_datum */
ret_datum.dptr = NULL;
ret_datum.dsize = 0;
ret_datum = DBM_NEXTKEY(dbf, key_datum);
if (ret_datum.dptr) {
ret = (char *)emalloc(sizeof(char) * ret_datum.dsize + 1);
strncpy(ret, ret_datum.dptr, ret_datum.dsize);
ret[ret_datum.dsize] = '\0';
#if GDBM
/* GDBM uses malloc to allocate the value_datum block, so we need to free it */
free(ret_datum.dptr);
#else
# if !NDBM
efree(ret_datum.dptr);
# endif
#endif
}
else ret=NULL;
if (ret && PG(magic_quotes_runtime)) {
ret = php_addslashes(ret, ret_datum.dsize, NULL, 1 TSRMLS_CC);
}
return(ret);
}
/* }}} */
#if !GDBM && !NDBM
static long CurrentFlatFilePos = 0L;
/* {{{ flatfile_store
*/
int flatfile_store(FILE *dbf, datum key_datum, datum value_datum, int mode) {
int ret;
if (mode == DBM_INSERT) {
if (flatfile_findkey(dbf, key_datum)) {
return 1;
}
fseek(dbf, 0L, SEEK_END);
fprintf(dbf, "%d\n", key_datum.dsize);
fflush(dbf);
ret = fwrite(key_datum.dptr, sizeof(char), key_datum.dsize, dbf);
fprintf(dbf, "%d\n", value_datum.dsize);
fflush(dbf);
ret = fwrite(value_datum.dptr, sizeof(char), value_datum.dsize, dbf);
} else { /* DBM_REPLACE */
flatfile_delete(dbf, key_datum);
fprintf(dbf, "%d\n", key_datum.dsize);
fflush(dbf);
ret = fwrite(key_datum.dptr, sizeof(char), key_datum.dsize, dbf);
fprintf(dbf, "%d\n", value_datum.dsize);
ret = fwrite(value_datum.dptr, sizeof(char), value_datum.dsize, dbf);
}
if (ret>0)
ret=0;
return ret;
}
/* }}} */
/* {{{ flatfile_fetch
*/
datum flatfile_fetch(FILE *dbf, datum key_datum) {
datum value_datum = {NULL, 0};
int num=0, buf_size=1024;
char *buf;
if (flatfile_findkey(dbf, key_datum)) {
buf = emalloc((buf_size+1) * sizeof(char));
if (fgets(buf, 15, dbf)) {
num = atoi(buf);
if (num > buf_size) {
buf_size+=num;
buf = erealloc(buf, (buf_size+1)*sizeof(char));
}
fread(buf, sizeof(char), num, dbf);
value_datum.dptr = buf;
value_datum.dsize = num;
}
}
return value_datum;
}
/* }}} */
/* {{{ flatfile_delete
*/
int flatfile_delete(FILE *dbf, datum key_datum) {
char *key = key_datum.dptr;
int size = key_datum.dsize;
char *buf;
int num, buf_size = 1024;
long pos;
rewind(dbf);
buf = emalloc((buf_size + 1)*sizeof(char));
while(!feof(dbf)) {
/* read in the length of the key name */
if (!fgets(buf, 15, dbf))
break;
num = atoi(buf);
if (num > buf_size) {
buf_size += num;
buf = erealloc(buf, (buf_size+1)*sizeof(char));
}
pos = ftell(dbf);
/* read in the key name */
num = fread(buf, sizeof(char), num, dbf);
if (num<0) break;
*(buf+num) = '\0';
if (size == num && !memcmp(buf, key, size)) {
fseek(dbf, pos, SEEK_SET);
fputc(0, dbf);
fflush(dbf);
fseek(dbf, 0L, SEEK_END);
if (buf) efree(buf);
return SUCCESS;
}
/* read in the length of the value */
if (!fgets(buf, 15, dbf))
break;
num = atoi(buf);
if (num > buf_size) {
buf_size+=num;
if (buf) efree(buf);
buf = emalloc((buf_size+1)*sizeof(char));
}
/* read in the value */
num = fread(buf, sizeof(char), num, dbf);
if (num<0)
break;
}
if (buf) efree(buf);
return FAILURE;
}
/* }}} */
/* {{{ flatfile_findkey
*/
int flatfile_findkey(FILE *dbf, datum key_datum) {
char *buf = NULL;
int num;
int buf_size=1024;
int ret=0;
void *key = key_datum.dptr;
int size = key_datum.dsize;
rewind(dbf);
buf = emalloc((buf_size+1)*sizeof(char));
while (!feof(dbf)) {
if (!fgets(buf, 15, dbf)) break;
num = atoi(buf);
if (num > buf_size) {
if (buf) efree(buf);
buf_size+=num;
buf = emalloc((buf_size+1)*sizeof(char));
}
num = fread(buf, sizeof(char), num, dbf);
if (num<0) break;
*(buf+num) = '\0';
if (size == num) {
if (!memcmp(buf, key, size)) {
ret = 1;
break;
}
}
if (!fgets(buf, 15, dbf))
break;
num = atoi(buf);
if (num > buf_size) {
if (buf) efree(buf);
buf_size+=num;
buf = emalloc((buf_size+1)*sizeof(char));
}
num = fread(buf, sizeof(char), num, dbf);
if (num<0) break;
*(buf+num) = '\0';
}
if (buf) efree(buf);
return(ret);
}
/* }}} */
/* {{{ flatfile_firstkey
*/
datum flatfile_firstkey(FILE *dbf) {
datum buf;
int num;
int buf_size=1024;
rewind(dbf);
buf.dptr = emalloc((buf_size+1)*sizeof(char));
while(!feof(dbf)) {
if (!fgets(buf.dptr, 15, dbf)) break;
num = atoi(buf.dptr);
if (num > buf_size) {
buf_size+=num;
if (buf.dptr) efree(buf.dptr);
buf.dptr = emalloc((buf_size+1)*sizeof(char));
}
num = fread(buf.dptr, sizeof(char), num, dbf);
if (num<0) break;
buf.dsize = num;
if (*(buf.dptr)!=0) {
CurrentFlatFilePos = ftell(dbf);
return(buf);
}
if (!fgets(buf.dptr, 15, dbf)) break;
num = atoi(buf.dptr);
if (num > buf_size) {
buf_size+=num;
if (buf.dptr) efree(buf.dptr);
buf.dptr = emalloc((buf_size+1)*sizeof(char));
}
num = fread(buf.dptr, sizeof(char), num, dbf);
if (num<0) break;
}
if (buf.dptr) efree(buf.dptr);
buf.dptr = NULL;
return(buf);
}
/* }}} */
/* {{{ latfile_nextkey
*/
datum flatfile_nextkey(FILE *dbf) {
datum buf;
int num;
int buf_size=1024;
fseek(dbf, CurrentFlatFilePos, SEEK_SET);
buf.dptr = emalloc((buf_size+1)*sizeof(char));
while(!feof(dbf)) {
if (!fgets(buf.dptr, 15, dbf)) break;
num = atoi(buf.dptr);
if (num > buf_size) {
buf_size+=num;
if (buf.dptr) efree(buf.dptr);
buf.dptr = emalloc((buf_size+1)*sizeof(char));
}
num = fread(buf.dptr, sizeof(char), num, dbf);
if (num<0) break;
if (!fgets(buf.dptr, 15, dbf)) break;
num = atoi(buf.dptr);
if (num > buf_size) {
buf_size+=num;
if (buf.dptr) efree(buf.dptr);
buf.dptr = emalloc((buf_size+1)*sizeof(char));
}
num = fread(buf.dptr, sizeof(char), num, dbf);
if (num<0) break;
buf.dsize = num;
if (*(buf.dptr)!=0) {
CurrentFlatFilePos = ftell(dbf);
return(buf);
}
}
if (buf.dptr) efree(buf.dptr);
buf.dptr = NULL;
return(buf);
}
/* }}} */
#endif
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(db)
{
le_db = zend_register_list_destructors_ex(php_dbm_close, NULL, "dbm", module_number);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(db)
{
#if !GDBM && !NDBM
CurrentFlatFilePos = 0L;
#endif
return SUCCESS;
}
/* }}} */
/* {{{ dbm_functions[]
*/
function_entry dbm_functions[] = {
PHP_FE(dblist, NULL)
PHP_FE(dbmopen, NULL)
PHP_FE(dbmclose, NULL)
PHP_FE(dbminsert, NULL)
PHP_FE(dbmfetch, NULL)
PHP_FE(dbmreplace, NULL)
PHP_FE(dbmexists, NULL)
PHP_FE(dbmdelete, NULL)
PHP_FE(dbmfirstkey, NULL)
PHP_FE(dbmnextkey, NULL)
#if HELLY_0
PHP_FE(db_id_list, NULL)
#endif
{NULL, NULL, NULL}
};
/* }}} */
zend_module_entry dbm_module_entry = {
STANDARD_MODULE_HEADER,
"db",
dbm_functions,
PHP_MINIT(db),
NULL,
PHP_RINIT(db),
NULL,
PHP_MINFO(db),
NO_VERSION_YET,
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_DB
ZEND_GET_MODULE(dbm)
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/
|