-
Notifications
You must be signed in to change notification settings - Fork 2
/
aware_storage.c
304 lines (250 loc) · 9.74 KB
/
aware_storage.c
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
/*
+----------------------------------------------------------------------+
| PHP Version 5 / aware |
+----------------------------------------------------------------------+
| Copyright (c) 2009 Mikko Koppanen |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.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. |
+----------------------------------------------------------------------+
| Author: Mikko Kopppanen <mkoppanen@php.net> |
+----------------------------------------------------------------------+
*/
#include "php_aware_private.h"
#include "php_aware_storage.h"
#include "ext/standard/php_var.h"
#include "ext/standard/php_smart_str.h"
#include "ext/standard/php_string.h"
ZEND_DECLARE_MODULE_GLOBALS(aware)
/*
MAX_MODULES defines how many active modules there can be any given time.
'10 should be more than enough for everybody..'
*/
#define MAX_MODULES 10
/*
The module structure follows session module structure quite closely
*/
static php_aware_storage_module *php_aware_storage_modules[MAX_MODULES + 1] = { 0 };
/* {{{ static zend_bool php_aware_storage_module_is_configured(const char *mod_name TSRMLS_DC)
*/
static zend_bool php_aware_storage_module_is_configured(const char *mod_name TSRMLS_DC)
{
zend_bool retval = 0;
char *pch, *last, *ptr;
/* If aware is not enabled, don't register anything */
if (!AWARE_G(enabled)) {
return 0;
}
ptr = estrdup(AWARE_G(storage_modules));
pch = php_strtok_r(ptr, ",", &last);
while (pch != NULL) {
char *mod = php_trim(pch, strlen(pch), NULL, 0, NULL, 3 TSRMLS_CC);
if (mod) {
if (!strcmp(mod, mod_name)) {
retval = 1;
}
efree(mod);
}
/* all done ? */
if (retval)
break;
pch = php_strtok_r(NULL, ",", &last);
}
efree(ptr);
return retval;
}
/* }}} */
/* {{{ MY_AWARE_EXPORTS AwareModuleRegisterStatus php_aware_register_storage_module(php_aware_storage_module *mod TSRMLS_DC)
*/
MY_AWARE_EXPORTS AwareModuleRegisterStatus php_aware_register_storage_module(php_aware_storage_module *mod TSRMLS_DC)
{
int i, ret = AwareModuleFailed;
aware_printf("Registering storage module: %s\n", mod->name);
if (!php_aware_storage_module_is_configured(mod->name TSRMLS_CC)) {
aware_printf("Storage module %s is not configured\n", mod->name);
return AwareModuleNotConfigured;
}
for (i = 0; i < MAX_MODULES; i++) {
if (!php_aware_storage_modules[i]) {
php_aware_storage_modules[i] = mod;
ret = AwareModuleRegistered;
break;
}
}
return ret;
}
/* }}} */
/* {{{ php_aware_storage_module *php_aware_find_storage_module(const char *mod_name)
*/
php_aware_storage_module *php_aware_find_storage_module(const char *mod_name)
{
int i;
php_aware_storage_module *ret = NULL;
aware_printf("Finding storage module %s\n", mod_name);
for (i = 0; i < MAX_MODULES; i++) {
if (php_aware_storage_modules[i] && !strcmp(mod_name, php_aware_storage_modules[i]->name)) {
aware_printf("Found storage module %s\n", mod_name);
ret = php_aware_storage_modules[i];
break;
}
}
return ret;
}
/* }}} */
/* {{{ void php_aware_storage_module_list(zval *return_value)
* return_value must be initialized as an array before calling this function
*/
void php_aware_storage_module_list(zval *return_value)
{
int i;
for (i = 0; i < MAX_MODULES; i++) {
if (php_aware_storage_modules[i]) {
add_next_index_string(return_value, php_aware_storage_modules[i]->name, 1);
}
}
}
/* }}} */
/* {{{ MY_AWARE_EXPORTS void php_aware_storage_serialize(const char *uuid, zval *event, smart_str *data_var TSRMLS_DC)
*/
MY_AWARE_EXPORTS void php_aware_storage_serialize(const char *uuid, zval *event, smart_str *data_var TSRMLS_DC)
{
php_serialize_data_t var_hash;
if (AWARE_G(use_cache)) {
if (php_aware_cache_get(&AWARE_G(s_cache), uuid, data_var)) {
return;
}
}
PHP_VAR_SERIALIZE_INIT(var_hash);
php_var_serialize(data_var, &event, &var_hash TSRMLS_CC);
PHP_VAR_SERIALIZE_DESTROY(var_hash);
if (AWARE_G(use_cache)) {
php_aware_cache_store(&AWARE_G(s_cache), uuid, data_var);
}
}
/* }}} */
/* {{{ MY_AWARE_EXPORTS zend_bool php_aware_storage_unserialize(const char *string, int string_len, zval *retval TSRMLS_DC)
*/
MY_AWARE_EXPORTS zend_bool php_aware_storage_unserialize(const char *string, int string_len, zval *retval TSRMLS_DC)
{
zend_bool unserialized;
php_unserialize_data_t var_hash;
const unsigned char *p, *s;
p = s = (const unsigned char *)string;
PHP_VAR_UNSERIALIZE_INIT(var_hash);
unserialized = php_var_unserialize(&retval, (const unsigned char **)&p, (const unsigned char *) s + string_len, &var_hash TSRMLS_CC);
PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
return unserialized;
}
/* Sends the event to storage module */
void php_aware_storage_store(php_aware_storage_module *mod, const char *uuid, zval *event, long type, const char *error_filename, long error_lineno TSRMLS_DC)
{
aware_printf("Storing event to module: %s\n", mod->name);
/*
User can override the storage module error reporting per module basis
This means that we need to check here if the module is configured to
store events of this level.
*/
if (zend_hash_num_elements(&AWARE_G(module_error_reporting)) > 0) {
long **level;
/* This means that we might have overriden error reporting level */
if (zend_hash_find(&AWARE_G(module_error_reporting), mod->name, strlen(mod->name) + 1, (void **)&level) == SUCCESS) {
/* Check if module is configured for this sort of errors */
if (!(**level & type)) {
return;
}
}
}
/* Connect failed, report error and bail out */
if (mod->connect(TSRMLS_C) == AwareOperationFailed) {
php_aware_original_error_cb(E_WARNING TSRMLS_CC, "Failed to connect the storage module (%s)", mod->name);
return;
}
if (mod->store(uuid, event, error_filename, error_lineno TSRMLS_CC) == AwareOperationFailed) {
php_aware_original_error_cb(E_WARNING TSRMLS_CC, "Failed to store the event %s (%s)", uuid, mod->name);
}
if (mod->disconnect(TSRMLS_C) == AwareOperationFailed) {
php_aware_original_error_cb(E_WARNING TSRMLS_CC, "Failed to disconnect storage module (%s)", mod->name);
}
}
/* Sends the event to all available storage modules */
void php_aware_storage_store_all(const char *uuid, zval *event, long type, const char *error_filename, long error_lineno TSRMLS_DC)
{
int i;
for (i = 0; i < MAX_MODULES; i++) {
if (php_aware_storage_modules[i]) {
php_aware_storage_store(php_aware_storage_modules[i], uuid, event, type, error_filename, error_lineno TSRMLS_CC);
}
}
}
/* get the event from storage modules */
void php_aware_storage_get(const char *mod_name, const char *uuid, zval *return_value TSRMLS_DC)
{
php_aware_storage_module *mod;
mod = php_aware_find_storage_module(mod_name);
if (!mod) {
php_aware_original_error_cb(E_WARNING TSRMLS_CC, "Storage module(%s) does not exist", mod_name);
return;
}
/* Connect failed, report error and bail out */
if (mod->connect(TSRMLS_C) == AwareOperationFailed) {
php_aware_original_error_cb(E_WARNING TSRMLS_CC, "Failed to connect the storage module (%s)", mod_name);
return;
}
if (mod->get(uuid, return_value TSRMLS_CC) == AwareOperationFailed) {
php_aware_original_error_cb(E_WARNING TSRMLS_CC, "Failed to get the event %s (%s)", uuid, mod_name);
}
if (mod->disconnect(TSRMLS_C) == AwareOperationFailed) {
php_aware_original_error_cb(E_WARNING TSRMLS_CC, "Failed to disconnect storage module (%s)", mod->name);
}
}
/* get the event from storage modules */
void php_aware_storage_get_list(const char *mod_name, long start, long limit, zval *return_value TSRMLS_DC)
{
php_aware_storage_module *mod;
mod = php_aware_find_storage_module(mod_name);
if (!mod) {
php_aware_original_error_cb(E_WARNING TSRMLS_CC, "Storage module(%s) does not exist", mod_name);
return;
}
/* Connect failed, report error and bail out */
if (mod->connect(TSRMLS_C) == AwareOperationFailed) {
php_aware_original_error_cb(E_WARNING TSRMLS_CC, "Failed to connect the storage module (%s)", mod_name);
return;
}
if (mod->get_list(start, limit, return_value TSRMLS_CC) == AwareOperationFailed) {
php_aware_original_error_cb(E_WARNING TSRMLS_CC, "Failed to get event list (%s)", mod_name);
}
if (mod->disconnect(TSRMLS_C) == AwareOperationFailed) {
php_aware_original_error_cb(E_WARNING TSRMLS_CC, "Failed to disconnect storage module (%s)", mod->name);
}
}
/* get the event from storage modules */
zend_bool php_aware_storage_delete(const char *mod_name, const char *uuid TSRMLS_DC)
{
zend_bool status = 1;
php_aware_storage_module *mod;
mod = php_aware_find_storage_module(mod_name);
if (!mod) {
php_aware_original_error_cb(E_WARNING TSRMLS_CC, "Unable to find storage module(%s)", mod_name);
return 0;
}
/* Connect failed, report error and bail out */
if (mod->connect(TSRMLS_C) == AwareOperationFailed) {
php_aware_original_error_cb(E_WARNING TSRMLS_CC, "Failed to connect the storage module (%s)", mod_name);
return 0;
}
if (mod->delete(uuid TSRMLS_CC) == AwareOperationFailed) {
php_aware_original_error_cb(E_WARNING TSRMLS_CC, "Failed to delete the event %s (%s)", uuid, mod_name);
status = 0;
}
if (mod->disconnect(TSRMLS_C) == AwareOperationFailed) {
php_aware_original_error_cb(E_WARNING TSRMLS_CC, "Failed to disconnect storage module (%s)", mod->name);
status = 0;
}
return status;
}