1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Author: Marcus Boerger <helly@php.net> |
14 +----------------------------------------------------------------------+
15 */
16
17 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif
20
21 #include "php.h"
22
23 #ifdef DBA_INIFILE
24 #include "php_inifile.h"
25
26 #include "libinifile/inifile.h"
27
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34
DBA_OPEN_FUNC(inifile)35 DBA_OPEN_FUNC(inifile)
36 {
37 info->dbf = inifile_alloc(info->fp, info->mode == DBA_READER, info->flags&DBA_PERSISTENT);
38
39 return info->dbf ? SUCCESS : FAILURE;
40 }
41
DBA_CLOSE_FUNC(inifile)42 DBA_CLOSE_FUNC(inifile)
43 {
44 inifile *dba = info->dbf;
45
46 inifile_free(dba, info->flags&DBA_PERSISTENT);
47 }
48
DBA_FETCH_FUNC(inifile)49 DBA_FETCH_FUNC(inifile)
50 {
51 inifile *dba = info->dbf;
52 val_type ini_val;
53 key_type ini_key;
54 zend_string *fetched_val = NULL;
55
56 if (!key) {
57 php_error_docref(NULL, E_WARNING, "No key specified");
58 return 0;
59 }
60 ini_key = inifile_key_split(ZSTR_VAL(key)); /* keylen not needed here */
61
62 ini_val = inifile_fetch(dba, &ini_key, skip);
63 inifile_key_free(&ini_key);
64 if (ini_val.value) {
65 fetched_val = zend_string_init(ini_val.value, strlen(ini_val.value), /* persistent */ false);
66 inifile_val_free(&ini_val);
67 }
68 return fetched_val;
69 }
70
DBA_UPDATE_FUNC(inifile)71 DBA_UPDATE_FUNC(inifile)
72 {
73 inifile *dba = info->dbf;
74 val_type ini_val;
75 int res;
76 key_type ini_key;
77
78 if (!key) {
79 php_error_docref(NULL, E_WARNING, "No key specified");
80 return 0;
81 }
82 ini_key = inifile_key_split(ZSTR_VAL(key)); /* keylen not needed here */
83
84 ini_val.value = ZSTR_VAL(val);
85
86 if (mode == 1) {
87 res = inifile_append(dba, &ini_key, &ini_val);
88 } else {
89 res = inifile_replace(dba, &ini_key, &ini_val);
90 }
91 inifile_key_free(&ini_key);
92 switch(res) {
93 case -1:
94 // TODO Check when this happens and confirm this can even happen
95 php_error_docref(NULL, E_WARNING, "Operation not possible");
96 return FAILURE;
97 default:
98 case 0:
99 return SUCCESS;
100 case 1:
101 return FAILURE;
102 }
103 }
104
DBA_EXISTS_FUNC(inifile)105 DBA_EXISTS_FUNC(inifile)
106 {
107 inifile *dba = info->dbf;
108 val_type ini_val;
109 key_type ini_key;
110
111 if (!key) {
112 php_error_docref(NULL, E_WARNING, "No key specified");
113 return 0;
114 }
115 ini_key = inifile_key_split(ZSTR_VAL(key)); /* keylen not needed here */
116
117 ini_val = inifile_fetch(dba, &ini_key, 0);
118 inifile_key_free(&ini_key);
119 if (ini_val.value) {
120 inifile_val_free(&ini_val);
121 return SUCCESS;
122 }
123 return FAILURE;
124 }
125
DBA_DELETE_FUNC(inifile)126 DBA_DELETE_FUNC(inifile)
127 {
128 inifile *dba = info->dbf;
129 int res;
130 bool found = 0;
131 key_type ini_key;
132
133 if (!key) {
134 php_error_docref(NULL, E_WARNING, "No key specified");
135 return 0;
136 }
137 ini_key = inifile_key_split(ZSTR_VAL(key)); /* keylen not needed here */
138
139 res = inifile_delete_ex(dba, &ini_key, &found);
140
141 inifile_key_free(&ini_key);
142 return (res == -1 || !found ? FAILURE : SUCCESS);
143 }
144
DBA_FIRSTKEY_FUNC(inifile)145 DBA_FIRSTKEY_FUNC(inifile)
146 {
147 inifile *dba = info->dbf;
148
149 if (inifile_firstkey(dba)) {
150 char *result = inifile_key_string(&dba->curr.key);
151 zend_string *key = zend_string_init(result, strlen(result), /* persistent */ false);
152 efree(result);
153 return key;
154 } else {
155 return NULL;
156 }
157 }
158
DBA_NEXTKEY_FUNC(inifile)159 DBA_NEXTKEY_FUNC(inifile)
160 {
161 inifile *dba = info->dbf;
162
163 if (!dba->curr.key.group && !dba->curr.key.name) {
164 return NULL;
165 }
166
167 if (inifile_nextkey(dba)) {
168 char *result = inifile_key_string(&dba->curr.key);
169 zend_string *key = zend_string_init(result, strlen(result), /* persistent */ false);
170 efree(result);
171 return key;
172 } else {
173 return NULL;
174 }
175 }
176
DBA_OPTIMIZE_FUNC(inifile)177 DBA_OPTIMIZE_FUNC(inifile)
178 {
179 /* dummy */
180 return SUCCESS;
181 }
182
DBA_SYNC_FUNC(inifile)183 DBA_SYNC_FUNC(inifile)
184 {
185 /* dummy */
186 return SUCCESS;
187 }
188
DBA_INFO_FUNC(inifile)189 DBA_INFO_FUNC(inifile)
190 {
191 return estrdup(inifile_version());
192 }
193
194 #endif
195