- fix libcurls used SSL library detection
[m6w6/ext-http] / http_api.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2007, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_SAPI
16 #include "php_http.h"
17
18 #include "php_output.h"
19 #include "ext/standard/url.h"
20
21 #include "php_http_api.h"
22 #include "php_http_send_api.h"
23
24 #ifdef ZEND_ENGINE_2
25 # include "php_http_exception_object.h"
26 #endif
27
28 PHP_MINIT_FUNCTION(http_support)
29 {
30 HTTP_LONG_CONSTANT("HTTP_SUPPORT", HTTP_SUPPORT);
31 HTTP_LONG_CONSTANT("HTTP_SUPPORT_REQUESTS", HTTP_SUPPORT_REQUESTS);
32 HTTP_LONG_CONSTANT("HTTP_SUPPORT_MAGICMIME", HTTP_SUPPORT_MAGICMIME);
33 HTTP_LONG_CONSTANT("HTTP_SUPPORT_ENCODINGS", HTTP_SUPPORT_ENCODINGS);
34 HTTP_LONG_CONSTANT("HTTP_SUPPORT_SSLREQUESTS", HTTP_SUPPORT_SSLREQUESTS);
35 HTTP_LONG_CONSTANT("HTTP_SUPPORT_EVENTS", HTTP_SUPPORT_EVENTS);
36
37 HTTP_LONG_CONSTANT("HTTP_PARAMS_ALLOW_COMMA", HTTP_PARAMS_ALLOW_COMMA);
38 HTTP_LONG_CONSTANT("HTTP_PARAMS_ALLOW_FAILURE", HTTP_PARAMS_ALLOW_FAILURE);
39 HTTP_LONG_CONSTANT("HTTP_PARAMS_RAISE_ERROR", HTTP_PARAMS_RAISE_ERROR);
40 HTTP_LONG_CONSTANT("HTTP_PARAMS_DEFAULT", HTTP_PARAMS_DEFAULT);
41
42 return SUCCESS;
43 }
44
45 PHP_HTTP_API long _http_support(long feature)
46 {
47 long support = HTTP_SUPPORT;
48
49 #ifdef HTTP_HAVE_CURL
50 support |= HTTP_SUPPORT_REQUESTS;
51 # ifdef HTTP_HAVE_SSL
52 support |= HTTP_SUPPORT_SSLREQUESTS;
53 # endif
54 # ifdef HTTP_HAVE_EVENT
55 support |= HTTP_SUPPORT_EVENTS;
56 # endif
57 #endif
58 #ifdef HTTP_HAVE_MAGIC
59 support |= HTTP_SUPPORT_MAGICMIME;
60 #endif
61 #ifdef HTTP_HAVE_ZLIB
62 support |= HTTP_SUPPORT_ENCODINGS;
63 #endif
64
65 if (feature) {
66 return (feature == (support & feature));
67 }
68 return support;
69 }
70
71 /* char *pretty_key(char *, size_t, zend_bool, zend_bool) */
72 char *_http_pretty_key(char *key, size_t key_len, zend_bool uctitle, zend_bool xhyphen)
73 {
74 size_t i;
75 int wasalpha;
76
77 if (key && key_len) {
78 if ((wasalpha = HTTP_IS_CTYPE(alpha, key[0]))) {
79 key[0] = (char) (uctitle ? HTTP_TO_CTYPE(upper, key[0]) : HTTP_TO_CTYPE(lower, key[0]));
80 }
81 for (i = 1; i < key_len; i++) {
82 if (HTTP_IS_CTYPE(alpha, key[i])) {
83 key[i] = (char) (((!wasalpha) && uctitle) ? HTTP_TO_CTYPE(upper, key[i]) : HTTP_TO_CTYPE(lower, key[i]));
84 wasalpha = 1;
85 } else {
86 if (xhyphen && (key[i] == '_')) {
87 key[i] = '-';
88 }
89 wasalpha = 0;
90 }
91 }
92 }
93 return key;
94 }
95 /* }}} */
96
97 /* {{{ void http_error(long, long, char*) */
98 void _http_error_ex(long type TSRMLS_DC, long code, const char *format, ...)
99 {
100 va_list args;
101
102 va_start(args, format);
103 #ifdef ZEND_ENGINE_2
104 if ((type == E_THROW) || (PG(error_handling) == EH_THROW)) {
105 char *message;
106 zend_class_entry *ce = http_exception_get_for_code(code);
107
108 http_try {
109 vspprintf(&message, 0, format, args);
110 zend_throw_exception(ce, message, code TSRMLS_CC);
111 efree(message);
112 } http_catch(PG(exception_class) ? PG(exception_class) : HTTP_EX_DEF_CE);
113 } else
114 #endif
115 php_verror(NULL, "", type, format, args TSRMLS_CC);
116 va_end(args);
117 }
118 /* }}} */
119
120 #ifdef ZEND_ENGINE_2
121 static inline void copy_bt_args(zval *from, zval *to TSRMLS_DC)
122 {
123 zval **args, **trace_0, *old_trace_0, *trace = NULL;
124
125 if ((trace = zend_read_property(ZEND_EXCEPTION_GET_DEFAULT(), from, "trace", lenof("trace"), 0 TSRMLS_CC))) {
126 if (Z_TYPE_P(trace) == IS_ARRAY && SUCCESS == zend_hash_index_find(Z_ARRVAL_P(trace), 0, (void *) &trace_0)) {
127 old_trace_0 = *trace_0;
128 if (Z_TYPE_PP(trace_0) == IS_ARRAY && SUCCESS == zend_hash_find(Z_ARRVAL_PP(trace_0), "args", sizeof("args"), (void *) &args)) {
129 if ((trace = zend_read_property(ZEND_EXCEPTION_GET_DEFAULT(), to, "trace", lenof("trace"), 0 TSRMLS_CC))) {
130 if (Z_TYPE_P(trace) == IS_ARRAY && SUCCESS == zend_hash_index_find(Z_ARRVAL_P(trace), 0, (void *) &trace_0)) {
131 ZVAL_ADDREF(*args);
132 add_assoc_zval(*trace_0, "args", *args);
133 }
134 }
135 }
136 }
137 }
138 }
139
140 /* {{{ zval *http_exception_wrap(zval *, zval *, zend_class_entry *) */
141 zval *_http_exception_wrap(zval *old_exception, zval *new_exception, zend_class_entry *ce TSRMLS_DC)
142 {
143 int inner = 1;
144 char *message;
145 zval *sub_exception, *tmp_exception;
146
147 if (!new_exception) {
148 MAKE_STD_ZVAL(new_exception);
149 object_init_ex(new_exception, ce);
150
151 zend_update_property(ce, new_exception, "innerException", lenof("innerException"), old_exception TSRMLS_CC);
152 copy_bt_args(old_exception, new_exception TSRMLS_CC);
153
154 sub_exception = old_exception;
155
156 while ((sub_exception = zend_read_property(Z_OBJCE_P(sub_exception), sub_exception, "innerException", lenof("innerException"), 0 TSRMLS_CC)) && Z_TYPE_P(sub_exception) == IS_OBJECT) {
157 ++inner;
158 }
159
160 spprintf(&message, 0, "Exception caused by %d inner exception(s)", inner);
161 zend_update_property_string(ZEND_EXCEPTION_GET_DEFAULT(), new_exception, "message", lenof("message"), message TSRMLS_CC);
162 efree(message);
163 } else {
164 sub_exception = new_exception;
165 tmp_exception = new_exception;
166
167 while ((tmp_exception = zend_read_property(Z_OBJCE_P(tmp_exception), tmp_exception, "innerException", lenof("innerException"), 0 TSRMLS_CC)) && Z_TYPE_P(tmp_exception) == IS_OBJECT) {
168 sub_exception = tmp_exception;
169 }
170
171 zend_update_property(Z_OBJCE_P(sub_exception), sub_exception, "innerException", lenof("innerException"), old_exception TSRMLS_CC);
172 copy_bt_args(old_exception, new_exception TSRMLS_CC);
173 copy_bt_args(old_exception, sub_exception TSRMLS_CC);
174 }
175
176 zval_ptr_dtor(&old_exception);
177 return new_exception;
178 }
179 /* }}} */
180
181 /* {{{ STATUS http_object_new(zend_object_value *, const char *, uint, http_object_new_t, zend_class_entry *, void *, void **) */
182 STATUS _http_object_new(zend_object_value *ov, const char *cname_str, uint cname_len, http_object_new_t create, zend_class_entry *parent_ce, void *intern_ptr, void **obj_ptr TSRMLS_DC)
183 {
184 zend_class_entry *ce = parent_ce;
185
186 if (cname_str && cname_len) {
187 if (!(ce = zend_fetch_class((char *) cname_str, cname_len, ZEND_FETCH_CLASS_DEFAULT TSRMLS_CC))) {
188 return FAILURE;
189 }
190 if (!instanceof_function(ce, parent_ce TSRMLS_CC)) {
191 http_error_ex(HE_WARNING, HTTP_E_RUNTIME, "Class %s does not extend %s", cname_str, parent_ce->name);
192 return FAILURE;
193 }
194 }
195
196 *ov = create(ce, intern_ptr, obj_ptr TSRMLS_CC);
197 return SUCCESS;
198 }
199 /* }}} */
200 #endif /* ZEND_ENGINE_2 */
201
202 /* {{{ void http_log(char *, char *, char *) */
203 void _http_log_ex(char *file, const char *ident, const char *message TSRMLS_DC)
204 {
205 time_t now;
206 struct tm nowtm;
207 char datetime[20] = {0};
208
209 now = HTTP_G->request.time;
210 strftime(datetime, sizeof(datetime), "%Y-%m-%d %H:%M:%S", php_localtime_r(&now, &nowtm));
211
212 #define HTTP_LOG_WRITE(file, type, msg) \
213 if (file && *file) { \
214 php_stream *log = php_stream_open_wrapper_ex(file, "ab", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL, HTTP_DEFAULT_STREAM_CONTEXT); \
215 \
216 if (log) { \
217 php_stream_printf(log TSRMLS_CC, "%s\t[%s]\t%s\t<%s>%s", datetime, type, msg, SG(request_info).request_uri, PHP_EOL); \
218 php_stream_close(log); \
219 } \
220 \
221 }
222
223 HTTP_LOG_WRITE(file, ident, message);
224 HTTP_LOG_WRITE(HTTP_G->log.composite, ident, message);
225 }
226 /* }}} */
227
228 static void http_ob_blackhole(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
229 {
230 *handled_output = ecalloc(1,1);
231 *handled_output_len = 0;
232 }
233
234 /* {{{ STATUS http_exit(int, char*, char*) */
235 STATUS _http_exit_ex(int status, char *header, char *body, zend_bool send_header TSRMLS_DC)
236 {
237 if ( (send_header && (SUCCESS != http_send_status_header(status, header))) ||
238 (status && (SUCCESS != http_send_status(status)))) {
239 http_error_ex(HE_WARNING, HTTP_E_HEADER, "Failed to exit with status/header: %d - %s", status, STR_PTR(header));
240 STR_FREE(header);
241 STR_FREE(body);
242 return FAILURE;
243 }
244
245 if (!OG(ob_lock)) {
246 php_end_ob_buffers(0 TSRMLS_CC);
247 }
248 if ((SUCCESS == sapi_send_headers(TSRMLS_C)) && body) {
249 PHPWRITE(body, strlen(body));
250 }
251
252 switch (status) {
253 case 301: http_log(HTTP_G->log.redirect, "301-REDIRECT", header); break;
254 case 302: http_log(HTTP_G->log.redirect, "302-REDIRECT", header); break;
255 case 303: http_log(HTTP_G->log.redirect, "303-REDIRECT", header); break;
256 case 305: http_log(HTTP_G->log.redirect, "305-REDIRECT", header); break;
257 case 307: http_log(HTTP_G->log.redirect, "307-REDIRECT", header); break;
258 case 304: http_log(HTTP_G->log.cache, "304-CACHE", header); break;
259 case 404: http_log(HTTP_G->log.not_found, "404-NOTFOUND", NULL); break;
260 case 405: http_log(HTTP_G->log.allowed_methods, "405-ALLOWED", header); break;
261 default: http_log(NULL, header, body); break;
262 }
263
264 STR_FREE(header);
265 STR_FREE(body);
266
267 if (HTTP_G->force_exit) {
268 zend_bailout();
269 } else {
270 php_ob_set_internal_handler(http_ob_blackhole, 4096, "blackhole", 0 TSRMLS_CC);
271 }
272
273 return SUCCESS;
274 }
275 /* }}} */
276
277 /* {{{ STATUS http_check_method(char *) */
278 STATUS _http_check_method_ex(const char *method, const char *methods)
279 {
280 const char *found;
281
282 if ( (found = strstr(methods, method)) &&
283 (found == method || !HTTP_IS_CTYPE(alpha, found[-1])) &&
284 (strlen(found) >= strlen(method) && !HTTP_IS_CTYPE(alpha, found[strlen(method)]))) {
285 return SUCCESS;
286 }
287 return FAILURE;
288 }
289 /* }}} */
290
291 /* {{{ zval *http_get_server_var_ex(char *, size_t) */
292 PHP_HTTP_API zval *_http_get_server_var_ex(const char *key, size_t key_len, zend_bool check TSRMLS_DC)
293 {
294 zval **hsv, **var;
295 char *env;
296
297 /* if available, this is a lot faster than accessing $_SERVER */
298 if (sapi_module.getenv) {
299 if ((!(env = sapi_module.getenv((char *) key, key_len TSRMLS_CC))) || (check && !*env)) {
300 return NULL;
301 }
302 if (HTTP_G->server_var) {
303 zval_ptr_dtor(&HTTP_G->server_var);
304 }
305 MAKE_STD_ZVAL(HTTP_G->server_var);
306 ZVAL_STRING(HTTP_G->server_var, env, 1);
307 return HTTP_G->server_var;
308 }
309
310 #ifdef ZEND_ENGINE_2
311 zend_is_auto_global("_SERVER", lenof("_SERVER") TSRMLS_CC);
312 #endif
313
314 if ((SUCCESS != zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void *) &hsv)) || (Z_TYPE_PP(hsv) != IS_ARRAY)) {
315 return NULL;
316 }
317 if ((SUCCESS != zend_hash_find(Z_ARRVAL_PP(hsv), (char *) key, key_len + 1, (void *) &var))) {
318 return NULL;
319 }
320 if (check && !((Z_TYPE_PP(var) == IS_STRING) && Z_STRVAL_PP(var) && Z_STRLEN_PP(var))) {
321 return NULL;
322 }
323 return *var;
324 }
325 /* }}} */
326
327 /* {{{ STATUS http_get_request_body(char **, size_t *) */
328 PHP_HTTP_API STATUS _http_get_request_body_ex(char **body, size_t *length, zend_bool dup TSRMLS_DC)
329 {
330 *length = 0;
331 *body = NULL;
332
333 if (SG(request_info).raw_post_data) {
334 *length = SG(request_info).raw_post_data_length;
335 *body = SG(request_info).raw_post_data;
336
337 if (dup) {
338 *body = estrndup(*body, *length);
339 }
340 return SUCCESS;
341 } else if (sapi_module.read_post && !HTTP_G->read_post_data) {
342 char buf[4096];
343 int len;
344
345 HTTP_G->read_post_data = 1;
346
347 while (0 < (len = sapi_module.read_post(buf, sizeof(buf) TSRMLS_CC))) {
348 *body = erealloc(*body, *length + len + 1);
349 memcpy(*body + *length, buf, len);
350 *length += len;
351 (*body)[*length] = '\0';
352 }
353
354 /* check for error */
355 if (len < 0) {
356 STR_FREE(*body);
357 *length = 0;
358 return FAILURE;
359 }
360
361 SG(request_info).raw_post_data = *body;
362 SG(request_info).raw_post_data_length = *length;
363
364 if (dup) {
365 *body = estrndup(*body, *length);
366 }
367 return SUCCESS;
368 }
369
370 return FAILURE;
371 }
372 /* }}} */
373
374 /* {{{ php_stream *http_get_request_body_stream(void) */
375 PHP_HTTP_API php_stream *_http_get_request_body_stream(TSRMLS_D)
376 {
377 php_stream *s = NULL;
378
379 if (SG(request_info).raw_post_data) {
380 s = php_stream_open_wrapper("php://input", "rb", 0, NULL);
381 } else if (sapi_module.read_post && !HTTP_G->read_post_data) {
382 HTTP_G->read_post_data = 1;
383
384 if ((s = php_stream_temp_new())) {
385 char buf[4096];
386 int len;
387
388 while (0 < (len = sapi_module.read_post(buf, sizeof(buf) TSRMLS_CC))) {
389 php_stream_write(s, buf, len);
390 }
391
392 if (len < 0) {
393 php_stream_close(s);
394 s = NULL;
395 } else {
396 php_stream_rewind(s);
397 }
398 }
399 }
400
401 return s;
402 }
403 /* }}} */
404
405 /* {{{ void http_parse_params_default_callback(...) */
406 PHP_HTTP_API void _http_parse_params_default_callback(void *arg, const char *key, int keylen, const char *val, int vallen TSRMLS_DC)
407 {
408 char *kdup;
409 zval tmp, *entry;
410 HashTable *ht = (HashTable *) arg;
411
412 if (ht) {
413 INIT_ZARR(tmp, ht);
414
415 if (vallen) {
416 MAKE_STD_ZVAL(entry);
417 array_init(entry);
418 if (keylen) {
419 kdup = estrndup(key, keylen);
420 add_assoc_stringl_ex(entry, kdup, keylen + 1, (char *) val, vallen, 1);
421 efree(kdup);
422 } else {
423 add_next_index_stringl(entry, (char *) val, vallen, 1);
424 }
425 add_next_index_zval(&tmp, entry);
426 } else {
427 add_next_index_stringl(&tmp, (char *) key, keylen, 1);
428 }
429 }
430 }
431 /* }}} */
432
433 /* {{{ STATUS http_parse_params(const char *, HashTable *) */
434 PHP_HTTP_API STATUS _http_parse_params_ex(const char *param, int flags, http_parse_params_callback cb, void *cb_arg TSRMLS_DC)
435 {
436 #define ST_QUOTE 1
437 #define ST_VALUE 2
438 #define ST_KEY 3
439 #define ST_ASSIGN 4
440 #define ST_ADD 5
441
442 int st = ST_KEY, keylen = 0, vallen = 0;
443 char *s, *c, *key = NULL, *val = NULL;
444
445 for(c = s = estrdup(param);;) {
446 continued:
447 #if 0
448 {
449 char *tk = NULL, *tv = NULL;
450
451 if (key) {
452 if (keylen) {
453 tk= estrndup(key, keylen);
454 } else {
455 tk = ecalloc(1, 7);
456 memcpy(tk, key, 3);
457 tk[3]='.'; tk[4]='.'; tk[5]='.';
458 }
459 }
460 if (val) {
461 if (vallen) {
462 tv = estrndup(val, vallen);
463 } else {
464 tv = ecalloc(1, 7);
465 memcpy(tv, val, 3);
466 tv[3]='.'; tv[4]='.'; tv[5]='.';
467 }
468 }
469 fprintf(stderr, "[%6s] %c \"%s=%s\"\n",
470 (
471 st == ST_QUOTE ? "QUOTE" :
472 st == ST_VALUE ? "VALUE" :
473 st == ST_KEY ? "KEY" :
474 st == ST_ASSIGN ? "ASSIGN" :
475 st == ST_ADD ? "ADD":
476 "HUH?"
477 ), *c?*c:'0', tk, tv
478 );
479 STR_FREE(tk); STR_FREE(tv);
480 }
481 #endif
482 switch (st) {
483 case ST_QUOTE:
484 quote:
485 if (*c == '"') {
486 if (*(c-1) == '\\') {
487 memmove(c-1, c, strlen(c)+1);
488 goto quote;
489 } else {
490 goto add;
491 }
492 } else {
493 if (!val) {
494 val = c;
495 }
496 if (!*c) {
497 --val;
498 st = ST_ADD;
499 }
500 }
501 break;
502
503 case ST_VALUE:
504 switch (*c) {
505 case '"':
506 if (!val) {
507 st = ST_QUOTE;
508 }
509 break;
510
511 case ' ':
512 break;
513
514 case ';':
515 case '\0':
516 goto add;
517 break;
518 case ',':
519 if (flags & HTTP_PARAMS_ALLOW_COMMA) {
520 goto add;
521 }
522 default:
523 if (!val) {
524 val = c;
525 }
526 break;
527 }
528 break;
529
530 case ST_KEY:
531 switch (*c) {
532 case ',':
533 if (flags & HTTP_PARAMS_ALLOW_COMMA) {
534 goto allow_comma;
535 }
536 case '\r':
537 case '\n':
538 case '\t':
539 case '\013':
540 case '\014':
541 goto failure;
542 break;
543
544 case '=':
545 if (key) {
546 keylen = c - key;
547 st = ST_VALUE;
548 } else {
549 goto failure;
550 }
551 break;
552
553 case ' ':
554 if (key) {
555 keylen = c - key;
556 st = ST_ASSIGN;
557 }
558 break;
559
560 case ';':
561 case '\0':
562 allow_comma:
563 if (key) {
564 keylen = c-- - key;
565 st = ST_ADD;
566 }
567 break;
568
569 default:
570 if (!key) {
571 key = c;
572 }
573 break;
574 }
575 break;
576
577 case ST_ASSIGN:
578 if (*c == '=') {
579 st = ST_VALUE;
580 } else if (!*c || *c == ';' || ((flags & HTTP_PARAMS_ALLOW_COMMA) && *c == ',')) {
581 st = ST_ADD;
582 } else if (*c != ' ') {
583 goto failure;
584 }
585 break;
586
587 case ST_ADD:
588 add:
589 if (val) {
590 vallen = c - val;
591 if (st != ST_QUOTE) {
592 while (val[vallen-1] == ' ') --vallen;
593 }
594 } else {
595 val = "";
596 vallen = 0;
597 }
598
599 cb(cb_arg, key, keylen, val, vallen TSRMLS_CC);
600
601 st = ST_KEY;
602 key = val = NULL;
603 keylen = vallen = 0;
604 break;
605 }
606 if (*c) {
607 ++c;
608 } else if (st == ST_ADD) {
609 goto add;
610 } else {
611 break;
612 }
613 }
614
615 efree(s);
616 return SUCCESS;
617
618 failure:
619 if (flags & HTTP_PARAMS_RAISE_ERROR) {
620 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Unexpected character (%c) at pos %tu of %zu", *c, c-s, strlen(s));
621 }
622 if (flags & HTTP_PARAMS_ALLOW_FAILURE) {
623 if (st == ST_KEY) {
624 if (key) {
625 keylen = c - key;
626 } else {
627 key = c;
628 }
629 } else {
630 --c;
631 }
632 st = ST_ADD;
633 goto continued;
634 }
635 efree(s);
636 return FAILURE;
637 }
638 /* }}} */
639
640 /* {{{ array_join */
641 int apply_array_append_func(void *pDest, int num_args, va_list args, zend_hash_key *hash_key)
642 {
643 int flags;
644 char *key = NULL;
645 HashTable *dst;
646 zval **data = NULL, **value = (zval **) pDest;
647
648 dst = va_arg(args, HashTable *);
649 flags = va_arg(args, int);
650
651 if ((!(flags & ARRAY_JOIN_STRONLY)) || hash_key->nKeyLength) {
652 if ((flags & ARRAY_JOIN_PRETTIFY) && hash_key->nKeyLength) {
653 key = pretty_key(estrndup(hash_key->arKey, hash_key->nKeyLength - 1), hash_key->nKeyLength - 1, 1, 1);
654 zend_hash_find(dst, key, hash_key->nKeyLength, (void *) &data);
655 } else {
656 zend_hash_quick_find(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void *) &data);
657 }
658
659 ZVAL_ADDREF(*value);
660 if (data) {
661 if (Z_TYPE_PP(data) != IS_ARRAY) {
662 convert_to_array(*data);
663 }
664 add_next_index_zval(*data, *value);
665 } else if (key) {
666 zend_hash_add(dst, key, hash_key->nKeyLength, value, sizeof(zval *), NULL);
667 } else {
668 zend_hash_quick_add(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, value, sizeof(zval *), NULL);
669 }
670
671 if (key) {
672 efree(key);
673 }
674 }
675
676 return ZEND_HASH_APPLY_KEEP;
677 }
678
679 int apply_array_merge_func(void *pDest, int num_args, va_list args, zend_hash_key *hash_key)
680 {
681 int flags;
682 char *key = NULL;
683 HashTable *dst;
684 zval **value = (zval **) pDest;
685
686 dst = va_arg(args, HashTable *);
687 flags = va_arg(args, int);
688
689 if ((!(flags & ARRAY_JOIN_STRONLY)) || hash_key->nKeyLength) {
690 ZVAL_ADDREF(*value);
691 if ((flags & ARRAY_JOIN_PRETTIFY) && hash_key->nKeyLength) {
692 key = pretty_key(estrndup(hash_key->arKey, hash_key->nKeyLength - 1), hash_key->nKeyLength - 1, 1, 1);
693 zend_hash_update(dst, key, hash_key->nKeyLength, (void *) value, sizeof(zval *), NULL);
694 efree(key);
695 } else {
696 zend_hash_quick_update(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void *) value, sizeof(zval *), NULL);
697 }
698 }
699
700 return ZEND_HASH_APPLY_KEEP;
701 }
702 /* }}} */
703
704 /*
705 * Local variables:
706 * tab-width: 4
707 * c-basic-offset: 4
708 * End:
709 * vim600: noet sw=4 ts=4 fdm=marker
710 * vim<600: noet sw=4 ts=4
711 */
712