summaryrefslogtreecommitdiff
path: root/src/sp_utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/sp_utils.c')
-rw-r--r--src/sp_utils.c84
1 files changed, 31 insertions, 53 deletions
diff --git a/src/sp_utils.c b/src/sp_utils.c
index 1ed770b..3fe2e44 100644
--- a/src/sp_utils.c
+++ b/src/sp_utils.c
@@ -25,20 +25,11 @@ void sp_log_msg(char const *feature, char const *level, const char* fmt, ...) {
25 vspprintf(&msg, 0, fmt, args); 25 vspprintf(&msg, 0, fmt, args);
26 va_end(args); 26 va_end(args);
27 27
28 char const * const client_ip = sp_getenv("REMOTE_ADDR"); 28 char const * const client_ip = getenv("REMOTE_ADDR");
29 _sp_log_err("[snuffleupagus][%s][%s][%s] %s", client_ip?client_ip:"0.0.0.0", 29 _sp_log_err("[snuffleupagus][%s][%s][%s] %s", client_ip?client_ip:"0.0.0.0",
30 feature, level, msg); 30 feature, level, msg);
31} 31}
32 32
33
34zend_always_inline char* sp_getenv(char* var) {
35 if (sapi_module.getenv) {
36 return sapi_module.getenv(ZEND_STRL(var));
37 } else {
38 return getenv(var);
39 }
40}
41
42zend_always_inline int is_regexp_matching(const pcre* regexp, const char* str) { 33zend_always_inline int is_regexp_matching(const pcre* regexp, const char* str) {
43 int vec[30]; 34 int vec[30];
44 int ret = 0; 35 int ret = 0;
@@ -278,63 +269,50 @@ void sp_log_disable_ret(const char* restrict path,
278 } 269 }
279} 270}
280 271
281int sp_match_array_key(const zval* zv, const char* to_match, const pcre* rx) { 272bool sp_match_array_key(const zval* zv, const char* to_match, const pcre* rx) {
282 zend_string* key; 273 zend_string* key;
283 zval* value; 274 zend_ulong idx;
284 char* arg_value_str;
285 275
286 ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(zv), key, value) { 276 ZEND_HASH_FOREACH_KEY(Z_ARRVAL_P(zv), idx, key) {
287 if (Z_TYPE_P(value) == IS_ARRAY) { 277 if (key) {
288 continue; 278 if (sp_match_value(ZSTR_VAL(key), to_match, rx)) {
289 } 279 return true;
290 arg_value_str = sp_convert_to_string(value); 280 }
291 if (!sp_match_value(arg_value_str, to_match, rx)) {
292 efree(arg_value_str);
293 continue;
294 } else { 281 } else {
295 efree(arg_value_str); 282 char *idx_str = NULL;
296 return 1; 283
284 // Could use a log.
285 idx_str = emalloc(snprintf(NULL, 0, "%lu", idx));
286 sprintf(idx_str, "%lu", idx);
287 if (sp_match_value(idx_str, to_match, rx)) {
288 efree(idx_str);
289 return true;
290 }
291 efree(idx_str);
297 } 292 }
298 } 293 }
299 ZEND_HASH_FOREACH_END(); 294 ZEND_HASH_FOREACH_END();
300 295 return false;
301 (void)key; // silence a compiler warning
302
303 return 0;
304} 296}
305 297
306int sp_match_array_key_recurse(const zval* arr, sp_node_t* keys, 298bool sp_match_array_value(const zval* arr, const char* to_match, const pcre* rx) {
307 const char* to_match, const pcre* rx) {
308 zend_string* key;
309 zval* value; 299 zval* value;
310 sp_node_t* current = keys; 300
311 if (current == NULL) { 301 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(arr), value) {
312 return 0; 302 if (Z_TYPE_P(value) != IS_ARRAY) {
313 } 303 char *value_str = sp_convert_to_string(value);
314 ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(arr), key, value) { 304 if (sp_match_value(value_str, to_match, rx)) {
315 if (Z_TYPE_P(value) == IS_ARRAY && !strcmp(ZSTR_VAL(key), current->data)) { 305 efree(value_str);
316 return sp_match_array_key_recurse(value, current->next, to_match, rx); 306 return true;
317 }
318 if (!strcmp(ZSTR_VAL(key), current->data) && current->next == NULL) {
319 if (!to_match && !rx) {
320 return 1;
321 }
322 if (Z_TYPE_P(value) == IS_ARRAY) {
323 return sp_match_array_key(value, to_match, rx);
324 } else { 307 } else {
325 char *value_str = sp_convert_to_string(value); 308 efree (value_str);
326 if (sp_match_value(value_str, to_match, rx)) {
327 efree(value_str);
328 return 1;
329 } else {
330 efree (value_str);
331 return 0;
332 }
333 } 309 }
310 } else if (sp_match_array_value(value, to_match, rx)) {
311 return true;
334 } 312 }
335 } 313 }
336 ZEND_HASH_FOREACH_END(); 314 ZEND_HASH_FOREACH_END();
337 return 0; 315 return false;
338} 316}
339 317
340 318