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