1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 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 | Author: Marcus Boerger <helly@php.net> |
16 +----------------------------------------------------------------------+
17 */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include "php.h"
24
25 #if DBA_INIFILE
26 #include "php_inifile.h"
27
28 #include "libinifile/inifile.h"
29
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36
37 #define INIFILE_DATA \
38 inifile *dba = info->dbf
39
40 #define INIFILE_GKEY \
41 key_type ini_key; \
42 if (!key) { \
43 php_error_docref(NULL, E_WARNING, "No key specified"); \
44 return 0; \
45 } \
46 ini_key = inifile_key_split((char*)key) /* keylen not needed here */
47
48 #define INIFILE_DONE \
49 inifile_key_free(&ini_key)
50
DBA_OPEN_FUNC(inifile)51 DBA_OPEN_FUNC(inifile)
52 {
53 info->dbf = inifile_alloc(info->fp, info->mode == DBA_READER, info->flags&DBA_PERSISTENT);
54
55 return info->dbf ? SUCCESS : FAILURE;
56 }
57
DBA_CLOSE_FUNC(inifile)58 DBA_CLOSE_FUNC(inifile)
59 {
60 INIFILE_DATA;
61
62 inifile_free(dba, info->flags&DBA_PERSISTENT);
63 }
64
DBA_FETCH_FUNC(inifile)65 DBA_FETCH_FUNC(inifile)
66 {
67 val_type ini_val;
68
69 INIFILE_DATA;
70 INIFILE_GKEY;
71
72 ini_val = inifile_fetch(dba, &ini_key, skip);
73 *newlen = ini_val.value ? strlen(ini_val.value) : 0;
74 INIFILE_DONE;
75 return ini_val.value;
76 }
77
DBA_UPDATE_FUNC(inifile)78 DBA_UPDATE_FUNC(inifile)
79 {
80 val_type ini_val;
81 int res;
82
83 INIFILE_DATA;
84 INIFILE_GKEY;
85
86 ini_val.value = val;
87
88 if (mode == 1) {
89 res = inifile_append(dba, &ini_key, &ini_val);
90 } else {
91 res = inifile_replace(dba, &ini_key, &ini_val);
92 }
93 INIFILE_DONE;
94 switch(res) {
95 case -1:
96 php_error_docref1(NULL, key, E_WARNING, "Operation not possible");
97 return FAILURE;
98 default:
99 case 0:
100 return SUCCESS;
101 case 1:
102 return FAILURE;
103 }
104 }
105
DBA_EXISTS_FUNC(inifile)106 DBA_EXISTS_FUNC(inifile)
107 {
108 val_type ini_val;
109
110 INIFILE_DATA;
111 INIFILE_GKEY;
112
113 ini_val = inifile_fetch(dba, &ini_key, 0);
114 INIFILE_DONE;
115 if (ini_val.value) {
116 inifile_val_free(&ini_val);
117 return SUCCESS;
118 }
119 return FAILURE;
120 }
121
DBA_DELETE_FUNC(inifile)122 DBA_DELETE_FUNC(inifile)
123 {
124 int res;
125 zend_bool found = 0;
126
127 INIFILE_DATA;
128 INIFILE_GKEY;
129
130 res = inifile_delete_ex(dba, &ini_key, &found);
131
132 INIFILE_DONE;
133 return (res == -1 || !found ? FAILURE : SUCCESS);
134 }
135
DBA_FIRSTKEY_FUNC(inifile)136 DBA_FIRSTKEY_FUNC(inifile)
137 {
138 INIFILE_DATA;
139
140 if (inifile_firstkey(dba)) {
141 char *result = inifile_key_string(&dba->curr.key);
142 *newlen = strlen(result);
143 return result;
144 } else {
145 return NULL;
146 }
147 }
148
DBA_NEXTKEY_FUNC(inifile)149 DBA_NEXTKEY_FUNC(inifile)
150 {
151 INIFILE_DATA;
152
153 if (!dba->curr.key.group && !dba->curr.key.name) {
154 return NULL;
155 }
156
157 if (inifile_nextkey(dba)) {
158 char *result = inifile_key_string(&dba->curr.key);
159 *newlen = strlen(result);
160 return result;
161 } else {
162 return NULL;
163 }
164 }
165
DBA_OPTIMIZE_FUNC(inifile)166 DBA_OPTIMIZE_FUNC(inifile)
167 {
168 /* dummy */
169 return SUCCESS;
170 }
171
DBA_SYNC_FUNC(inifile)172 DBA_SYNC_FUNC(inifile)
173 {
174 /* dummy */
175 return SUCCESS;
176 }
177
DBA_INFO_FUNC(inifile)178 DBA_INFO_FUNC(inifile)
179 {
180 return estrdup(inifile_version());
181 }
182
183 #endif
184