1/*
2   +----------------------------------------------------------------------+
3   | Zend Engine                                                          |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1998-2014 Zend Technologies Ltd. (http://www.zend.com) |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 2.00 of the Zend 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.zend.com/license/2_00.txt.                                |
11   | If you did not receive a copy of the Zend license and are unable to  |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@zend.com so we can mail you a copy immediately.              |
14   +----------------------------------------------------------------------+
15   | Authors: Andi Gutmans <andi@zend.com>                                |
16   |          Zeev Suraski <zeev@zend.com>                                |
17   +----------------------------------------------------------------------+
18*/
19
20/* $Id$ */
21
22#include "zend.h"
23#include "zend_API.h"
24#include "zend_builtin_functions.h"
25#include "zend_constants.h"
26#include "zend_ini.h"
27#include "zend_exceptions.h"
28#include "zend_extensions.h"
29#include "zend_closures.h"
30
31#undef ZEND_TEST_EXCEPTIONS
32
33static ZEND_FUNCTION(zend_version);
34static ZEND_FUNCTION(func_num_args);
35static ZEND_FUNCTION(func_get_arg);
36static ZEND_FUNCTION(func_get_args);
37static ZEND_FUNCTION(strlen);
38static ZEND_FUNCTION(strcmp);
39static ZEND_FUNCTION(strncmp);
40static ZEND_FUNCTION(strcasecmp);
41static ZEND_FUNCTION(strncasecmp);
42static ZEND_FUNCTION(each);
43static ZEND_FUNCTION(error_reporting);
44static ZEND_FUNCTION(define);
45static ZEND_FUNCTION(defined);
46static ZEND_FUNCTION(get_class);
47static ZEND_FUNCTION(get_called_class);
48static ZEND_FUNCTION(get_parent_class);
49static ZEND_FUNCTION(method_exists);
50static ZEND_FUNCTION(property_exists);
51static ZEND_FUNCTION(class_exists);
52static ZEND_FUNCTION(interface_exists);
53static ZEND_FUNCTION(trait_exists);
54static ZEND_FUNCTION(function_exists);
55static ZEND_FUNCTION(class_alias);
56#if ZEND_DEBUG
57static ZEND_FUNCTION(leak);
58static ZEND_FUNCTION(leak_variable);
59#ifdef ZEND_TEST_EXCEPTIONS
60static ZEND_FUNCTION(crash);
61#endif
62#endif
63static ZEND_FUNCTION(get_included_files);
64static ZEND_FUNCTION(is_subclass_of);
65static ZEND_FUNCTION(is_a);
66static ZEND_FUNCTION(get_class_vars);
67static ZEND_FUNCTION(get_object_vars);
68static ZEND_FUNCTION(get_class_methods);
69static ZEND_FUNCTION(trigger_error);
70static ZEND_FUNCTION(set_error_handler);
71static ZEND_FUNCTION(restore_error_handler);
72static ZEND_FUNCTION(set_exception_handler);
73static ZEND_FUNCTION(restore_exception_handler);
74static ZEND_FUNCTION(get_declared_classes);
75static ZEND_FUNCTION(get_declared_traits);
76static ZEND_FUNCTION(get_declared_interfaces);
77static ZEND_FUNCTION(get_defined_functions);
78static ZEND_FUNCTION(get_defined_vars);
79static ZEND_FUNCTION(create_function);
80static ZEND_FUNCTION(get_resource_type);
81static ZEND_FUNCTION(get_resources);
82static ZEND_FUNCTION(get_loaded_extensions);
83static ZEND_FUNCTION(extension_loaded);
84static ZEND_FUNCTION(get_extension_funcs);
85static ZEND_FUNCTION(get_defined_constants);
86static ZEND_FUNCTION(debug_backtrace);
87static ZEND_FUNCTION(debug_print_backtrace);
88#if ZEND_DEBUG
89static ZEND_FUNCTION(zend_test_func);
90#ifdef ZTS
91static ZEND_FUNCTION(zend_thread_id);
92#endif
93#endif
94static ZEND_FUNCTION(gc_collect_cycles);
95static ZEND_FUNCTION(gc_enabled);
96static ZEND_FUNCTION(gc_enable);
97static ZEND_FUNCTION(gc_disable);
98
99/* {{{ arginfo */
100ZEND_BEGIN_ARG_INFO(arginfo_zend__void, 0)
101ZEND_END_ARG_INFO()
102
103ZEND_BEGIN_ARG_INFO_EX(arginfo_func_get_arg, 0, 0, 1)
104    ZEND_ARG_INFO(0, arg_num)
105ZEND_END_ARG_INFO()
106
107ZEND_BEGIN_ARG_INFO_EX(arginfo_strlen, 0, 0, 1)
108    ZEND_ARG_INFO(0, str)
109ZEND_END_ARG_INFO()
110
111ZEND_BEGIN_ARG_INFO_EX(arginfo_strcmp, 0, 0, 2)
112    ZEND_ARG_INFO(0, str1)
113    ZEND_ARG_INFO(0, str2)
114ZEND_END_ARG_INFO()
115
116ZEND_BEGIN_ARG_INFO_EX(arginfo_strncmp, 0, 0, 3)
117    ZEND_ARG_INFO(0, str1)
118    ZEND_ARG_INFO(0, str2)
119    ZEND_ARG_INFO(0, len)
120ZEND_END_ARG_INFO()
121
122ZEND_BEGIN_ARG_INFO_EX(arginfo_each, 0, 0, 1)
123    ZEND_ARG_INFO(1, arr)
124ZEND_END_ARG_INFO()
125
126ZEND_BEGIN_ARG_INFO_EX(arginfo_error_reporting, 0, 0, 0)
127    ZEND_ARG_INFO(0, new_error_level)
128ZEND_END_ARG_INFO()
129
130ZEND_BEGIN_ARG_INFO_EX(arginfo_define, 0, 0, 3)
131    ZEND_ARG_INFO(0, constant_name)
132    ZEND_ARG_INFO(0, value)
133    ZEND_ARG_INFO(0, case_insensitive)
134ZEND_END_ARG_INFO()
135
136ZEND_BEGIN_ARG_INFO_EX(arginfo_defined, 0, 0, 1)
137    ZEND_ARG_INFO(0, constant_name)
138ZEND_END_ARG_INFO()
139
140ZEND_BEGIN_ARG_INFO_EX(arginfo_get_class, 0, 0, 0)
141    ZEND_ARG_INFO(0, object)
142ZEND_END_ARG_INFO()
143
144ZEND_BEGIN_ARG_INFO_EX(arginfo_is_subclass_of, 0, 0, 2)
145    ZEND_ARG_INFO(0, object)
146    ZEND_ARG_INFO(0, class_name)
147    ZEND_ARG_INFO(0, allow_string)
148ZEND_END_ARG_INFO()
149
150ZEND_BEGIN_ARG_INFO_EX(arginfo_get_class_vars, 0, 0, 1)
151    ZEND_ARG_INFO(0, class_name)
152ZEND_END_ARG_INFO()
153
154ZEND_BEGIN_ARG_INFO_EX(arginfo_get_object_vars, 0, 0, 1)
155    ZEND_ARG_INFO(0, obj)
156ZEND_END_ARG_INFO()
157
158ZEND_BEGIN_ARG_INFO_EX(arginfo_get_class_methods, 0, 0, 1)
159    ZEND_ARG_INFO(0, class)
160ZEND_END_ARG_INFO()
161
162ZEND_BEGIN_ARG_INFO_EX(arginfo_method_exists, 0, 0, 2)
163    ZEND_ARG_INFO(0, object)
164    ZEND_ARG_INFO(0, method)
165ZEND_END_ARG_INFO()
166
167ZEND_BEGIN_ARG_INFO_EX(arginfo_property_exists, 0, 0, 2)
168    ZEND_ARG_INFO(0, object_or_class)
169    ZEND_ARG_INFO(0, property_name)
170ZEND_END_ARG_INFO()
171
172ZEND_BEGIN_ARG_INFO_EX(arginfo_class_exists, 0, 0, 1)
173    ZEND_ARG_INFO(0, classname)
174    ZEND_ARG_INFO(0, autoload)
175ZEND_END_ARG_INFO()
176
177ZEND_BEGIN_ARG_INFO_EX(arginfo_trait_exists, 0, 0, 1)
178    ZEND_ARG_INFO(0, traitname)
179    ZEND_ARG_INFO(0, autoload)
180ZEND_END_ARG_INFO()
181
182ZEND_BEGIN_ARG_INFO_EX(arginfo_function_exists, 0, 0, 1)
183    ZEND_ARG_INFO(0, function_name)
184ZEND_END_ARG_INFO()
185
186ZEND_BEGIN_ARG_INFO_EX(arginfo_class_alias, 0, 0, 2)
187    ZEND_ARG_INFO(0, user_class_name)
188    ZEND_ARG_INFO(0, alias_name)
189    ZEND_ARG_INFO(0, autoload)
190ZEND_END_ARG_INFO()
191
192#if ZEND_DEBUG
193ZEND_BEGIN_ARG_INFO_EX(arginfo_leak_variable, 0, 0, 1)
194    ZEND_ARG_INFO(0, variable)
195    ZEND_ARG_INFO(0, leak_data)
196ZEND_END_ARG_INFO()
197#endif
198
199ZEND_BEGIN_ARG_INFO_EX(arginfo_trigger_error, 0, 0, 1)
200    ZEND_ARG_INFO(0, message)
201    ZEND_ARG_INFO(0, error_type)
202ZEND_END_ARG_INFO()
203
204ZEND_BEGIN_ARG_INFO_EX(arginfo_set_error_handler, 0, 0, 1)
205    ZEND_ARG_INFO(0, error_handler)
206    ZEND_ARG_INFO(0, error_types)
207ZEND_END_ARG_INFO()
208
209ZEND_BEGIN_ARG_INFO_EX(arginfo_set_exception_handler, 0, 0, 1)
210    ZEND_ARG_INFO(0, exception_handler)
211ZEND_END_ARG_INFO()
212
213ZEND_BEGIN_ARG_INFO_EX(arginfo_create_function, 0, 0, 2)
214    ZEND_ARG_INFO(0, args)
215    ZEND_ARG_INFO(0, code)
216ZEND_END_ARG_INFO()
217
218ZEND_BEGIN_ARG_INFO_EX(arginfo_get_resource_type, 0, 0, 1)
219    ZEND_ARG_INFO(0, res)
220ZEND_END_ARG_INFO()
221
222ZEND_BEGIN_ARG_INFO_EX(arginfo_get_resources, 0, 0, 0)
223    ZEND_ARG_INFO(0, type)
224ZEND_END_ARG_INFO()
225
226ZEND_BEGIN_ARG_INFO_EX(arginfo_get_loaded_extensions, 0, 0, 0)
227    ZEND_ARG_INFO(0, zend_extensions)
228ZEND_END_ARG_INFO()
229
230ZEND_BEGIN_ARG_INFO_EX(arginfo_get_defined_constants, 0, 0, 0)
231    ZEND_ARG_INFO(0, categorize)
232ZEND_END_ARG_INFO()
233
234ZEND_BEGIN_ARG_INFO_EX(arginfo_debug_backtrace, 0, 0, 0)
235    ZEND_ARG_INFO(0, options)
236    ZEND_ARG_INFO(0, limit)
237ZEND_END_ARG_INFO()
238
239ZEND_BEGIN_ARG_INFO_EX(arginfo_debug_print_backtrace, 0, 0, 0)
240    ZEND_ARG_INFO(0, options)
241ZEND_END_ARG_INFO()
242
243ZEND_BEGIN_ARG_INFO_EX(arginfo_extension_loaded, 0, 0, 1)
244    ZEND_ARG_INFO(0, extension_name)
245ZEND_END_ARG_INFO()
246/* }}} */
247
248static const zend_function_entry builtin_functions[] = { /* {{{ */
249    ZEND_FE(zend_version,       arginfo_zend__void)
250    ZEND_FE(func_num_args,      arginfo_zend__void)
251    ZEND_FE(func_get_arg,       arginfo_func_get_arg)
252    ZEND_FE(func_get_args,      arginfo_zend__void)
253    ZEND_FE(strlen,         arginfo_strlen)
254    ZEND_FE(strcmp,         arginfo_strcmp)
255    ZEND_FE(strncmp,        arginfo_strncmp)
256    ZEND_FE(strcasecmp,     arginfo_strcmp)
257    ZEND_FE(strncasecmp,        arginfo_strncmp)
258    ZEND_FE(each,           arginfo_each)
259    ZEND_FE(error_reporting,    arginfo_error_reporting)
260    ZEND_FE(define,         arginfo_define)
261    ZEND_FE(defined,        arginfo_defined)
262    ZEND_FE(get_class,      arginfo_get_class)
263    ZEND_FE(get_called_class,   arginfo_zend__void)
264    ZEND_FE(get_parent_class,   arginfo_get_class)
265    ZEND_FE(method_exists,      arginfo_method_exists)
266    ZEND_FE(property_exists,    arginfo_property_exists)
267    ZEND_FE(class_exists,       arginfo_class_exists)
268    ZEND_FE(interface_exists,   arginfo_class_exists)
269    ZEND_FE(trait_exists,       arginfo_trait_exists)
270    ZEND_FE(function_exists,    arginfo_function_exists)
271    ZEND_FE(class_alias,        arginfo_class_alias)
272#if ZEND_DEBUG
273    ZEND_FE(leak,               NULL)
274    ZEND_FE(leak_variable,      arginfo_leak_variable)
275#ifdef ZEND_TEST_EXCEPTIONS
276    ZEND_FE(crash,              NULL)
277#endif
278#endif
279    ZEND_FE(get_included_files, arginfo_zend__void)
280    ZEND_FALIAS(get_required_files, get_included_files,     arginfo_zend__void)
281    ZEND_FE(is_subclass_of,     arginfo_is_subclass_of)
282    ZEND_FE(is_a,           arginfo_is_subclass_of)
283    ZEND_FE(get_class_vars,     arginfo_get_class_vars)
284    ZEND_FE(get_object_vars,    arginfo_get_object_vars)
285    ZEND_FE(get_class_methods,  arginfo_get_class_methods)
286    ZEND_FE(trigger_error,      arginfo_trigger_error)
287    ZEND_FALIAS(user_error,     trigger_error,      arginfo_trigger_error)
288    ZEND_FE(set_error_handler,      arginfo_set_error_handler)
289    ZEND_FE(restore_error_handler,      arginfo_zend__void)
290    ZEND_FE(set_exception_handler,      arginfo_set_exception_handler)
291    ZEND_FE(restore_exception_handler,  arginfo_zend__void)
292    ZEND_FE(get_declared_classes,       arginfo_zend__void)
293    ZEND_FE(get_declared_traits,        arginfo_zend__void)
294    ZEND_FE(get_declared_interfaces,    arginfo_zend__void)
295    ZEND_FE(get_defined_functions,      arginfo_zend__void)
296    ZEND_FE(get_defined_vars,       arginfo_zend__void)
297    ZEND_FE(create_function,        arginfo_create_function)
298    ZEND_FE(get_resource_type,      arginfo_get_resource_type)
299    ZEND_FE(get_resources,          arginfo_get_resources)
300    ZEND_FE(get_loaded_extensions,      arginfo_get_loaded_extensions)
301    ZEND_FE(extension_loaded,       arginfo_extension_loaded)
302    ZEND_FE(get_extension_funcs,        arginfo_extension_loaded)
303    ZEND_FE(get_defined_constants,      arginfo_get_defined_constants)
304    ZEND_FE(debug_backtrace,        arginfo_debug_backtrace)
305    ZEND_FE(debug_print_backtrace,      arginfo_debug_print_backtrace)
306#if ZEND_DEBUG
307    ZEND_FE(zend_test_func,     NULL)
308#ifdef ZTS
309    ZEND_FE(zend_thread_id,     NULL)
310#endif
311#endif
312    ZEND_FE(gc_collect_cycles,  arginfo_zend__void)
313    ZEND_FE(gc_enabled,         arginfo_zend__void)
314    ZEND_FE(gc_enable,      arginfo_zend__void)
315    ZEND_FE(gc_disable,         arginfo_zend__void)
316    ZEND_FE_END
317};
318/* }}} */
319
320ZEND_MINIT_FUNCTION(core) { /* {{{ */
321    zend_class_entry class_entry;
322
323    INIT_CLASS_ENTRY(class_entry, "stdClass", NULL);
324    zend_standard_class_def = zend_register_internal_class(&class_entry TSRMLS_CC);
325
326    zend_register_default_classes(TSRMLS_C);
327
328    return SUCCESS;
329}
330/* }}} */
331
332zend_module_entry zend_builtin_module = { /* {{{ */
333    STANDARD_MODULE_HEADER,
334    "Core",
335    builtin_functions,
336    ZEND_MINIT(core),
337    NULL,
338    NULL,
339    NULL,
340    NULL,
341    ZEND_VERSION,
342    STANDARD_MODULE_PROPERTIES
343};
344/* }}} */
345
346int zend_startup_builtin_functions(TSRMLS_D) /* {{{ */
347{
348    zend_builtin_module.module_number = 0;
349    zend_builtin_module.type = MODULE_PERSISTENT;
350    return (EG(current_module) = zend_register_module_ex(&zend_builtin_module TSRMLS_CC)) == NULL ? FAILURE : SUCCESS;
351}
352/* }}} */
353
354/* {{{ proto string zend_version(void)
355   Get the version of the Zend Engine */
356ZEND_FUNCTION(zend_version)
357{
358    RETURN_STRINGL(ZEND_VERSION, sizeof(ZEND_VERSION)-1);
359}
360/* }}} */
361
362/* {{{ proto int gc_collect_cycles(void)
363   Forces collection of any existing garbage cycles.
364   Returns number of freed zvals */
365ZEND_FUNCTION(gc_collect_cycles)
366{
367    RETURN_LONG(gc_collect_cycles(TSRMLS_C));
368}
369/* }}} */
370
371/* {{{ proto void gc_enabled(void)
372   Returns status of the circular reference collector */
373ZEND_FUNCTION(gc_enabled)
374{
375    RETURN_BOOL(GC_G(gc_enabled));
376}
377/* }}} */
378
379/* {{{ proto void gc_enable(void)
380   Activates the circular reference collector */
381ZEND_FUNCTION(gc_enable)
382{
383    zend_string *key = zend_string_init("zend.enable_gc", sizeof("zend.enable_gc")-1, 0);
384    zend_alter_ini_entry_chars(key, "1", sizeof("1")-1, ZEND_INI_USER, ZEND_INI_STAGE_RUNTIME);
385    zend_string_release(key);
386}
387/* }}} */
388
389/* {{{ proto void gc_disable(void)
390   Deactivates the circular reference collector */
391ZEND_FUNCTION(gc_disable)
392{
393    zend_string *key = zend_string_init("zend.enable_gc", sizeof("zend.enable_gc")-1, 0);
394    zend_alter_ini_entry_chars(key, "0", sizeof("0")-1, ZEND_INI_USER, ZEND_INI_STAGE_RUNTIME);
395    zend_string_release(key);
396}
397/* }}} */
398
399/* {{{ proto int func_num_args(void)
400   Get the number of arguments that were passed to the function */
401ZEND_FUNCTION(func_num_args)
402{
403    zend_execute_data *ex = EX(prev_execute_data);
404
405    if (!(ZEND_CALL_INFO(ex) & ZEND_CALL_CODE)) {
406        RETURN_LONG(ZEND_CALL_NUM_ARGS(ex));
407    } else {
408        zend_error(E_WARNING, "func_num_args():  Called from the global scope - no function context");
409        RETURN_LONG(-1);
410    }
411}
412/* }}} */
413
414/* {{{ proto mixed func_get_arg(int arg_num)
415   Get the $arg_num'th argument that was passed to the function */
416ZEND_FUNCTION(func_get_arg)
417{
418    uint32_t arg_count, first_extra_arg;
419    zval *arg;
420    zend_long requested_offset;
421    zend_execute_data *ex;
422
423    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &requested_offset) == FAILURE) {
424        return;
425    }
426
427    if (requested_offset < 0) {
428        zend_error(E_WARNING, "func_get_arg():  The argument number should be >= 0");
429        RETURN_FALSE;
430    }
431
432    ex = EX(prev_execute_data);
433    if (ZEND_CALL_INFO(ex) & ZEND_CALL_CODE) {
434        zend_error(E_WARNING, "func_get_arg():  Called from the global scope - no function context");
435        RETURN_FALSE;
436    }
437
438    arg_count = ZEND_CALL_NUM_ARGS(ex);
439
440    if (requested_offset >= arg_count) {
441        zend_error(E_WARNING, "func_get_arg():  Argument " ZEND_LONG_FMT " not passed to function", requested_offset);
442        RETURN_FALSE;
443    }
444
445    first_extra_arg = ex->func->op_array.num_args;
446    if (ex->func->op_array.fn_flags & ZEND_ACC_VARIADIC) {
447        first_extra_arg--;
448    }
449    if (requested_offset >= first_extra_arg && (ZEND_CALL_NUM_ARGS(ex) > first_extra_arg)) {
450        arg = ZEND_CALL_VAR_NUM(ex, ex->func->op_array.last_var + ex->func->op_array.T) + (requested_offset - first_extra_arg);
451    } else {
452        arg = ZEND_CALL_ARG(ex, requested_offset + 1);
453    }
454    RETURN_ZVAL_FAST(arg);
455}
456/* }}} */
457
458/* {{{ proto array func_get_args()
459   Get an array of the arguments that were passed to the function */
460ZEND_FUNCTION(func_get_args)
461{
462    zval *p;
463    uint32_t arg_count, first_extra_arg;
464    uint32_t i;
465    zend_execute_data *ex = EX(prev_execute_data);
466
467    if (ZEND_CALL_INFO(ex) & ZEND_CALL_CODE) {
468        zend_error(E_WARNING, "func_get_args():  Called from the global scope - no function context");
469        RETURN_FALSE;
470    }
471
472    arg_count = ZEND_CALL_NUM_ARGS(ex);
473
474    array_init_size(return_value, arg_count);
475    if (arg_count) {
476        Bucket *q;
477
478        first_extra_arg = ex->func->op_array.num_args;
479        if (ex->func->op_array.fn_flags & ZEND_ACC_VARIADIC) {
480            first_extra_arg--;
481        }
482        zend_hash_real_init(Z_ARRVAL_P(return_value), 1);
483        i = 0;
484        q = Z_ARRVAL_P(return_value)->arData;
485        p = ZEND_CALL_ARG(ex, 1);
486        if (ZEND_CALL_NUM_ARGS(ex) > first_extra_arg) {
487            while (i < first_extra_arg) {
488                q->h = i;
489                q->key = NULL;
490                if (!Z_ISREF_P(p)) {
491                    ZVAL_COPY(&q->val, p);
492                } else {
493                    ZVAL_DUP(&q->val, Z_REFVAL_P(p));
494                }
495                p++;
496                q++;
497                i++;
498            }
499            p = ZEND_CALL_VAR_NUM(ex, ex->func->op_array.last_var + ex->func->op_array.T);
500        }
501        while (i < arg_count) {
502            q->h = i;
503            q->key = NULL;
504            if (!Z_ISREF_P(p)) {
505                ZVAL_COPY(&q->val, p);
506            } else {
507                ZVAL_DUP(&q->val, Z_REFVAL_P(p));
508            }
509            p++;
510            q++;
511            i++;
512        }
513        Z_ARRVAL_P(return_value)->nNumUsed = i;
514        Z_ARRVAL_P(return_value)->nNumOfElements = i;
515        Z_ARRVAL_P(return_value)->nNextFreeElement = i + 1;
516        Z_ARRVAL_P(return_value)->nInternalPointer = 0;
517    }
518}
519/* }}} */
520
521/* {{{ proto int strlen(string str)
522   Get string length
523   Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
524ZEND_FUNCTION(strlen)
525{
526    zend_string *s;
527
528#ifndef FAST_ZPP
529    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S", &s) == FAILURE) {
530        return;
531    }
532#else
533    ZEND_PARSE_PARAMETERS_START(1, 1)
534        Z_PARAM_STR(s)
535    ZEND_PARSE_PARAMETERS_END();
536#endif
537
538    RETVAL_LONG(s->len);
539}
540/* }}} */
541
542/* {{{ proto int strcmp(string str1, string str2)
543   Binary safe string comparison */
544ZEND_FUNCTION(strcmp)
545{
546    zend_string *s1, *s2;
547
548    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SS", &s1, &s2) == FAILURE) {
549        return;
550    }
551
552    RETURN_LONG(zend_binary_strcmp(s1->val, s1->len, s2->val, s2->len));
553}
554/* }}} */
555
556/* {{{ proto int strncmp(string str1, string str2, int len)
557   Binary safe string comparison */
558ZEND_FUNCTION(strncmp)
559{
560    zend_string *s1, *s2;
561    zend_long len;
562
563    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SSl", &s1, &s2, &len) == FAILURE) {
564        return;
565    }
566
567    if (len < 0) {
568        zend_error(E_WARNING, "Length must be greater than or equal to 0");
569        RETURN_FALSE;
570    }
571
572    RETURN_LONG(zend_binary_strncmp(s1->val, s1->len, s2->val, s2->len, len));
573}
574/* }}} */
575
576/* {{{ proto int strcasecmp(string str1, string str2)
577   Binary safe case-insensitive string comparison */
578ZEND_FUNCTION(strcasecmp)
579{
580    zend_string *s1, *s2;
581
582    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SS", &s1, &s2) == FAILURE) {
583        return;
584    }
585
586    RETURN_LONG(zend_binary_strcasecmp(s1->val, s1->len, s2->val, s2->len));
587}
588/* }}} */
589
590/* {{{ proto int strncasecmp(string str1, string str2, int len)
591   Binary safe string comparison */
592ZEND_FUNCTION(strncasecmp)
593{
594    zend_string *s1, *s2;
595    zend_long len;
596
597    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SSl", &s1, &s2, &len) == FAILURE) {
598        return;
599    }
600
601    if (len < 0) {
602        zend_error(E_WARNING, "Length must be greater than or equal to 0");
603        RETURN_FALSE;
604    }
605
606    RETURN_LONG(zend_binary_strncasecmp(s1->val, s1->len, s2->val, s2->len, len));
607}
608/* }}} */
609
610/* {{{ proto array each(array arr)
611   Return the currently pointed key..value pair in the passed array, and advance the pointer to the next element */
612ZEND_FUNCTION(each)
613{
614    zval *array, *entry, tmp;
615    zend_ulong num_key;
616    HashTable *target_hash;
617    zend_string *key;
618
619    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &array) == FAILURE) {
620        return;
621    }
622
623    target_hash = HASH_OF(array);
624    if (!target_hash) {
625        zend_error(E_WARNING,"Variable passed to each() is not an array or object");
626        return;
627    }
628    while (1) {
629        entry = zend_hash_get_current_data(target_hash);
630        if (!entry) {
631            RETURN_FALSE;
632        } else if (Z_TYPE_P(entry) == IS_INDIRECT) {
633            entry = Z_INDIRECT_P(entry);
634            if (Z_TYPE_P(entry) == IS_UNDEF) {
635                zend_hash_move_forward(target_hash);
636                continue;
637            }
638        }
639        break;
640    }
641    array_init_size(return_value, 4);
642    zend_hash_real_init(Z_ARRVAL_P(return_value), 0);
643
644    /* add value elements */
645    if (Z_ISREF_P(entry)) {
646        ZVAL_DUP(&tmp, Z_REFVAL_P(entry));
647        entry = &tmp;
648        if (Z_REFCOUNTED_P(entry)) Z_ADDREF_P(entry);
649    } else {
650        if (Z_REFCOUNTED_P(entry)) Z_ADDREF_P(entry);
651        if (Z_REFCOUNTED_P(entry)) Z_ADDREF_P(entry);
652    }
653    zend_hash_index_add_new(Z_ARRVAL_P(return_value), 1, entry);
654    zend_hash_str_add_new(Z_ARRVAL_P(return_value), "value", sizeof("value")-1, entry);
655
656    /* add the key elements */
657    if (zend_hash_get_current_key(target_hash, &key, &num_key, 0) == HASH_KEY_IS_STRING) {
658        ZVAL_STR_COPY(&tmp, key);
659        if (Z_REFCOUNTED(tmp)) Z_ADDREF(tmp);
660    } else {
661        ZVAL_LONG(&tmp, num_key);
662    }
663    zend_hash_index_add_new(Z_ARRVAL_P(return_value), 0, &tmp);
664    zend_hash_str_add_new(Z_ARRVAL_P(return_value), "key", sizeof("key")-1, &tmp);
665    zend_hash_move_forward(target_hash);
666}
667/* }}} */
668
669/* {{{ proto int error_reporting([int new_error_level])
670   Return the current error_reporting level, and if an argument was passed - change to the new level */
671ZEND_FUNCTION(error_reporting)
672{
673    zend_string *err;
674    int old_error_reporting;
675
676    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|S", &err) == FAILURE) {
677        return;
678    }
679
680    old_error_reporting = EG(error_reporting);
681    if(ZEND_NUM_ARGS() != 0) {
682        zend_string *key = zend_string_init("error_reporting", sizeof("error_reporting")-1, 0);
683        zend_alter_ini_entry(key, err, ZEND_INI_USER, ZEND_INI_STAGE_RUNTIME);
684        zend_string_release(key);
685    }
686
687    RETVAL_LONG(old_error_reporting);
688}
689/* }}} */
690
691/* {{{ proto bool define(string constant_name, mixed value, boolean case_insensitive=false)
692   Define a new constant */
693ZEND_FUNCTION(define)
694{
695    zend_string *name;
696    zval *val, val_free;
697    zend_bool non_cs = 0;
698    int case_sensitive = CONST_CS;
699    zend_constant c;
700
701#ifndef FAST_ZPP
702    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Sz|b", &name, &val, &non_cs) == FAILURE) {
703        return;
704    }
705#else
706    ZEND_PARSE_PARAMETERS_START(2, 3)
707        Z_PARAM_STR(name)
708        Z_PARAM_ZVAL(val)
709        Z_PARAM_OPTIONAL
710        Z_PARAM_BOOL(non_cs)
711    ZEND_PARSE_PARAMETERS_END();
712#endif
713
714    if(non_cs) {
715        case_sensitive = 0;
716    }
717
718    /* class constant, check if there is name and make sure class is valid & exists */
719    if (zend_memnstr(name->val, "::", sizeof("::") - 1, name->val + name->len)) {
720        zend_error(E_WARNING, "Class constants cannot be defined or redefined");
721        RETURN_FALSE;
722    }
723
724    ZVAL_UNDEF(&val_free);
725
726repeat:
727    switch (Z_TYPE_P(val)) {
728        case IS_LONG:
729        case IS_DOUBLE:
730        case IS_STRING:
731        case IS_FALSE:
732        case IS_TRUE:
733        case IS_RESOURCE:
734        case IS_NULL:
735            break;
736        case IS_OBJECT:
737            if (Z_TYPE(val_free) == IS_UNDEF) {
738                if (Z_OBJ_HT_P(val)->get) {
739                    zval rv;
740                    val = Z_OBJ_HT_P(val)->get(val, &rv TSRMLS_CC);
741                    ZVAL_COPY_VALUE(&val_free, val);
742                    goto repeat;
743                } else if (Z_OBJ_HT_P(val)->cast_object) {
744                    if (Z_OBJ_HT_P(val)->cast_object(val, &val_free, IS_STRING TSRMLS_CC) == SUCCESS) {
745                        val = &val_free;
746                        break;
747                    }
748                }
749            }
750            /* no break */
751        default:
752            zend_error(E_WARNING,"Constants may only evaluate to scalar values");
753            zval_ptr_dtor(&val_free);
754            RETURN_FALSE;
755    }
756
757    ZVAL_DUP(&c.value, val);
758    zval_ptr_dtor(&val_free);
759    c.flags = case_sensitive; /* non persistent */
760    c.name = zend_string_copy(name);
761    c.module_number = PHP_USER_CONSTANT;
762    if (zend_register_constant(&c TSRMLS_CC) == SUCCESS) {
763        RETURN_TRUE;
764    } else {
765        RETURN_FALSE;
766    }
767}
768/* }}} */
769
770/* {{{ proto bool defined(string constant_name)
771   Check whether a constant exists
772   Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
773ZEND_FUNCTION(defined)
774{
775    zend_string *name;
776
777#ifndef FAST_ZPP
778    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S", &name) == FAILURE) {
779        return;
780    }
781#else
782    ZEND_PARSE_PARAMETERS_START(1, 1)
783        Z_PARAM_STR(name)
784    ZEND_PARSE_PARAMETERS_END();
785#endif
786
787    if (zend_get_constant_ex(name, NULL, ZEND_FETCH_CLASS_SILENT TSRMLS_CC)) {
788        RETURN_TRUE;
789    } else {
790        RETURN_FALSE;
791    }
792}
793/* }}} */
794
795/* {{{ proto string get_class([object object])
796   Retrieves the class name */
797ZEND_FUNCTION(get_class)
798{
799    zval *obj = NULL;
800
801    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|o!", &obj) == FAILURE) {
802        RETURN_FALSE;
803    }
804
805    if (!obj) {
806        if (EG(scope)) {
807            RETURN_STR(zend_string_copy(EG(scope)->name));
808        } else {
809            zend_error(E_WARNING, "get_class() called without object from outside a class");
810            RETURN_FALSE;
811        }
812    }
813
814    RETURN_STR(zend_string_copy(Z_OBJCE_P(obj)->name));
815}
816/* }}} */
817
818/* {{{ proto string get_called_class()
819   Retrieves the "Late Static Binding" class name */
820ZEND_FUNCTION(get_called_class)
821{
822    if (zend_parse_parameters_none() == FAILURE) {
823        return;
824    }
825
826    if (EX(called_scope)) {
827        RETURN_STR(zend_string_copy(EX(called_scope)->name));
828    } else if (!EG(scope))  {
829        zend_error(E_WARNING, "get_called_class() called from outside a class");
830    }
831    RETURN_FALSE;
832}
833/* }}} */
834
835/* {{{ proto string get_parent_class([mixed object])
836   Retrieves the parent class name for object or class or current scope. */
837ZEND_FUNCTION(get_parent_class)
838{
839    zval *arg;
840    zend_class_entry *ce = NULL;
841
842    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &arg) == FAILURE) {
843        return;
844    }
845
846    if (!ZEND_NUM_ARGS()) {
847        ce = EG(scope);
848        if (ce && ce->parent) {
849            RETURN_STR(zend_string_copy(ce->parent->name));
850        } else {
851            RETURN_FALSE;
852        }
853    }
854
855    if (Z_TYPE_P(arg) == IS_OBJECT) {
856        ce = Z_OBJ_P(arg)->ce;
857    } else if (Z_TYPE_P(arg) == IS_STRING) {
858        ce = zend_lookup_class(Z_STR_P(arg) TSRMLS_CC);
859    }
860
861    if (ce && ce->parent) {
862        RETURN_STR(zend_string_copy(ce->parent->name));
863    } else {
864        RETURN_FALSE;
865    }
866}
867/* }}} */
868
869static void is_a_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool only_subclass) /* {{{ */
870{
871    zval *obj;
872    zend_string *class_name;
873    zend_class_entry *instance_ce;
874    zend_class_entry *ce;
875    zend_bool allow_string = only_subclass;
876    zend_bool retval;
877
878#ifndef FAST_ZPP
879    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zS|b", &obj, &class_name, &allow_string) == FAILURE) {
880        return;
881    }
882#else
883    ZEND_PARSE_PARAMETERS_START(2, 3)
884        Z_PARAM_ZVAL(obj)
885        Z_PARAM_STR(class_name)
886        Z_PARAM_OPTIONAL
887        Z_PARAM_BOOL(allow_string)
888    ZEND_PARSE_PARAMETERS_END();
889#endif
890    /*
891     * allow_string - is_a default is no, is_subclass_of is yes.
892     *   if it's allowed, then the autoloader will be called if the class does not exist.
893     *   default behaviour is different, as 'is_a' used to be used to test mixed return values
894     *   and there is no easy way to deprecate this.
895     */
896
897    if (allow_string && Z_TYPE_P(obj) == IS_STRING) {
898        instance_ce = zend_lookup_class(Z_STR_P(obj) TSRMLS_CC);
899        if (!instance_ce) {
900            RETURN_FALSE;
901        }
902    } else if (Z_TYPE_P(obj) == IS_OBJECT) {
903        instance_ce = Z_OBJCE_P(obj);
904    } else {
905        RETURN_FALSE;
906    }
907
908    ce = zend_lookup_class_ex(class_name, NULL, 0 TSRMLS_CC);
909    if (!ce) {
910        retval = 0;
911    } else {
912        if (only_subclass && instance_ce == ce) {
913            retval = 0;
914        } else {
915            retval = instanceof_function(instance_ce, ce TSRMLS_CC);
916        }
917    }
918
919    RETURN_BOOL(retval);
920}
921/* }}} */
922
923/* {{{ proto bool is_subclass_of(mixed object_or_string, string class_name [, bool allow_string=true])
924   Returns true if the object has this class as one of its parents */
925ZEND_FUNCTION(is_subclass_of)
926{
927    is_a_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
928}
929/* }}} */
930
931/* {{{ proto bool is_a(mixed object_or_string, string class_name [, bool allow_string=false])
932   Returns true if the first argument is an object and is this class or has this class as one of its parents, */
933ZEND_FUNCTION(is_a)
934{
935    is_a_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
936}
937/* }}} */
938
939/* {{{ add_class_vars */
940static void add_class_vars(zend_class_entry *ce, int statics, zval *return_value TSRMLS_DC)
941{
942    zend_property_info *prop_info;
943    zval *prop, prop_copy;
944    zend_string *key;
945
946    ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->properties_info, key, prop_info) {
947        if (((prop_info->flags & ZEND_ACC_SHADOW) &&
948             prop_info->ce != EG(scope)) ||
949            ((prop_info->flags & ZEND_ACC_PROTECTED) &&
950             !zend_check_protected(prop_info->ce, EG(scope))) ||
951            ((prop_info->flags & ZEND_ACC_PRIVATE) &&
952              ce != EG(scope) &&
953              prop_info->ce != EG(scope))) {
954            continue;
955        }
956        prop = NULL;
957        if (statics && (prop_info->flags & ZEND_ACC_STATIC) != 0) {
958            prop = &ce->default_static_members_table[prop_info->offset];
959        } else if (!statics && (prop_info->flags & ZEND_ACC_STATIC) == 0) {
960            prop = &ce->default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
961        }
962        if (!prop || Z_TYPE_P(prop) == IS_UNDEF) {
963            continue;
964        }
965
966        /* copy: enforce read only access */
967        ZVAL_DEREF(prop);
968        if (UNEXPECTED(Z_COPYABLE_P(prop))) {
969            ZVAL_DUP(&prop_copy, prop);
970            prop = &prop_copy;
971        }
972
973        /* this is necessary to make it able to work with default array
974         * properties, returned to user */
975        if (Z_OPT_CONSTANT_P(prop)) {
976            zval_update_constant(prop, 0 TSRMLS_CC);
977        }
978
979        zend_hash_add_new(Z_ARRVAL_P(return_value), key, prop);
980    } ZEND_HASH_FOREACH_END();
981}
982/* }}} */
983
984/* {{{ proto array get_class_vars(string class_name)
985   Returns an array of default properties of the class. */
986ZEND_FUNCTION(get_class_vars)
987{
988    zend_string *class_name;
989    zend_class_entry *ce;
990
991    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S", &class_name) == FAILURE) {
992        return;
993    }
994
995    ce = zend_lookup_class(class_name TSRMLS_CC);
996    if (!ce) {
997        RETURN_FALSE;
998    } else {
999        array_init(return_value);
1000        zend_update_class_constants(ce TSRMLS_CC);
1001        add_class_vars(ce, 0, return_value TSRMLS_CC);
1002        add_class_vars(ce, 1, return_value TSRMLS_CC);
1003    }
1004}
1005/* }}} */
1006
1007/* {{{ proto array get_object_vars(object obj)
1008   Returns an array of object properties */
1009ZEND_FUNCTION(get_object_vars)
1010{
1011    zval *obj;
1012    zval *value;
1013    HashTable *properties;
1014    zend_string *key;
1015    zend_object *zobj;
1016
1017#ifndef FAST_ZPP
1018    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &obj) == FAILURE) {
1019        return;
1020    }
1021#else
1022    ZEND_PARSE_PARAMETERS_START(1, 1)
1023        Z_PARAM_OBJECT(obj)
1024    ZEND_PARSE_PARAMETERS_END();
1025#endif
1026
1027    if (Z_OBJ_HT_P(obj)->get_properties == NULL) {
1028        RETURN_FALSE;
1029    }
1030
1031    properties = Z_OBJ_HT_P(obj)->get_properties(obj TSRMLS_CC);
1032
1033    if (properties == NULL) {
1034        RETURN_FALSE;
1035    }
1036
1037    zobj = Z_OBJ_P(obj);
1038
1039    array_init_size(return_value, zend_hash_num_elements(properties));
1040
1041    ZEND_HASH_FOREACH_STR_KEY_VAL_IND(properties, key, value) {
1042        if (key) {
1043            if (zend_check_property_access(zobj, key TSRMLS_CC) == SUCCESS) {
1044                /* Not separating references */
1045                if (Z_REFCOUNTED_P(value)) Z_ADDREF_P(value);
1046                if (key->val[0] == 0) {
1047                    const char *prop_name, *class_name;
1048                    size_t prop_len;
1049                    zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_len);
1050                    zend_hash_str_add_new(Z_ARRVAL_P(return_value), prop_name, prop_len, value);
1051                } else {
1052                    zend_hash_add_new(Z_ARRVAL_P(return_value), key, value);
1053                }
1054            }
1055        }
1056    } ZEND_HASH_FOREACH_END();
1057}
1058/* }}} */
1059
1060static int same_name(const char *key, const char *name, size_t name_len) /* {{{ */
1061{
1062    char *lcname = zend_str_tolower_dup(name, name_len);
1063    int ret = memcmp(lcname, key, name_len) == 0;
1064    efree(lcname);
1065    return ret;
1066}
1067/* }}} */
1068
1069/* {{{ proto array get_class_methods(mixed class)
1070   Returns an array of method names for class or class instance. */
1071ZEND_FUNCTION(get_class_methods)
1072{
1073    zval *klass;
1074    zval method_name;
1075    zend_class_entry *ce = NULL;
1076    zend_function *mptr;
1077    zend_string *key;
1078
1079    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &klass) == FAILURE) {
1080        return;
1081    }
1082
1083    if (Z_TYPE_P(klass) == IS_OBJECT) {
1084        ce = Z_OBJCE_P(klass);
1085    } else if (Z_TYPE_P(klass) == IS_STRING) {
1086        ce = zend_lookup_class(Z_STR_P(klass) TSRMLS_CC);
1087    }
1088
1089    if (!ce) {
1090        RETURN_NULL();
1091    }
1092
1093    array_init(return_value);
1094
1095    ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->function_table, key, mptr) {
1096
1097        if ((mptr->common.fn_flags & ZEND_ACC_PUBLIC)
1098         || (EG(scope) &&
1099             (((mptr->common.fn_flags & ZEND_ACC_PROTECTED) &&
1100               zend_check_protected(mptr->common.scope, EG(scope)))
1101           || ((mptr->common.fn_flags & ZEND_ACC_PRIVATE) &&
1102               EG(scope) == mptr->common.scope)))) {
1103            size_t len = mptr->common.function_name->len;
1104
1105            /* Do not display old-style inherited constructors */
1106            if (!key) {
1107                ZVAL_STR_COPY(&method_name, mptr->common.function_name);
1108                zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &method_name);
1109            } else if ((mptr->common.fn_flags & ZEND_ACC_CTOR) == 0 ||
1110                mptr->common.scope == ce ||
1111                zend_binary_strcasecmp(key->val, key->len, mptr->common.function_name->val, len) == 0) {
1112
1113                if (mptr->type == ZEND_USER_FUNCTION &&
1114                    *mptr->op_array.refcount > 1 &&
1115                    (len != key->len ||
1116                     !same_name(key->val, mptr->common.function_name->val, len))) {
1117                    ZVAL_STR_COPY(&method_name, zend_find_alias_name(mptr->common.scope, key));
1118                    zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &method_name);
1119                } else {
1120                    ZVAL_STR_COPY(&method_name, mptr->common.function_name);
1121                    zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &method_name);
1122                }
1123            }
1124        }
1125    } ZEND_HASH_FOREACH_END();
1126}
1127/* }}} */
1128
1129/* {{{ proto bool method_exists(object object, string method)
1130   Checks if the class method exists */
1131ZEND_FUNCTION(method_exists)
1132{
1133    zval *klass;
1134    zend_string *method_name;
1135    zend_string *lcname;
1136    zend_class_entry * ce;
1137
1138#ifndef FAST_ZPP
1139    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zS", &klass, &method_name) == FAILURE) {
1140        return;
1141    }
1142#else
1143    ZEND_PARSE_PARAMETERS_START(2, 2)
1144        Z_PARAM_ZVAL(klass)
1145        Z_PARAM_STR(method_name)
1146    ZEND_PARSE_PARAMETERS_END();
1147#endif
1148    if (Z_TYPE_P(klass) == IS_OBJECT) {
1149        ce = Z_OBJCE_P(klass);
1150    } else if (Z_TYPE_P(klass) == IS_STRING) {
1151        if ((ce = zend_lookup_class(Z_STR_P(klass) TSRMLS_CC)) == NULL) {
1152            RETURN_FALSE;
1153        }
1154    } else {
1155        RETURN_FALSE;
1156    }
1157
1158    lcname = zend_string_alloc(method_name->len, 0);
1159    zend_str_tolower_copy(lcname->val, method_name->val, method_name->len);
1160    if (zend_hash_exists(&ce->function_table, lcname)) {
1161        zend_string_free(lcname);
1162        RETURN_TRUE;
1163    } else {
1164        union _zend_function *func = NULL;
1165
1166        if (Z_TYPE_P(klass) == IS_OBJECT
1167        && Z_OBJ_HT_P(klass)->get_method != NULL
1168        && (func = Z_OBJ_HT_P(klass)->get_method(&Z_OBJ_P(klass), method_name, NULL TSRMLS_CC)) != NULL
1169        ) {
1170            if (func->type == ZEND_INTERNAL_FUNCTION
1171            && (func->common.fn_flags & ZEND_ACC_CALL_VIA_HANDLER) != 0
1172            ) {
1173                /* Returns true to the fake Closure's __invoke */
1174                RETVAL_BOOL(func->common.scope == zend_ce_closure
1175                    && zend_string_equals_literal(method_name, ZEND_INVOKE_FUNC_NAME));
1176
1177                zend_string_free(lcname);
1178                zend_string_release(func->common.function_name);
1179                efree(func);
1180                return;
1181            }
1182            efree(lcname);
1183            RETURN_TRUE;
1184        }
1185    }
1186    efree(lcname);
1187    RETURN_FALSE;
1188}
1189/* }}} */
1190
1191/* {{{ proto bool property_exists(mixed object_or_class, string property_name)
1192   Checks if the object or class has a property */
1193ZEND_FUNCTION(property_exists)
1194{
1195    zval *object;
1196    zend_string *property;
1197    zend_class_entry *ce;
1198    zend_property_info *property_info;
1199    zval property_z;
1200
1201    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zS", &object, &property) == FAILURE) {
1202        return;
1203    }
1204
1205    if (property == NULL) {
1206        RETURN_FALSE;
1207    }
1208
1209    if (Z_TYPE_P(object) == IS_STRING) {
1210        ce = zend_lookup_class(Z_STR_P(object) TSRMLS_CC);
1211        if (!ce) {
1212            RETURN_FALSE;
1213        }
1214    } else if (Z_TYPE_P(object) == IS_OBJECT) {
1215        ce = Z_OBJCE_P(object);
1216    } else {
1217        zend_error(E_WARNING, "First parameter must either be an object or the name of an existing class");
1218        RETURN_NULL();
1219    }
1220
1221    if ((property_info = zend_hash_find_ptr(&ce->properties_info, property)) != NULL
1222        && (property_info->flags & ZEND_ACC_SHADOW) == 0) {
1223        RETURN_TRUE;
1224    }
1225
1226    ZVAL_STR(&property_z, property);
1227
1228    if (Z_TYPE_P(object) ==  IS_OBJECT &&
1229        Z_OBJ_HANDLER_P(object, has_property) &&
1230        Z_OBJ_HANDLER_P(object, has_property)(object, &property_z, 2, NULL TSRMLS_CC)) {
1231        RETURN_TRUE;
1232    }
1233    RETURN_FALSE;
1234}
1235/* }}} */
1236
1237/* {{{ proto bool class_exists(string classname [, bool autoload])
1238   Checks if the class exists */
1239ZEND_FUNCTION(class_exists)
1240{
1241    zend_string *class_name;
1242    zend_string *lc_name;
1243    zend_class_entry *ce;
1244    zend_bool autoload = 1;
1245
1246#ifndef FAST_ZPP
1247    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S|b", &class_name, &autoload) == FAILURE) {
1248        return;
1249    }
1250#else
1251    ZEND_PARSE_PARAMETERS_START(1, 2)
1252        Z_PARAM_STR(class_name)
1253        Z_PARAM_OPTIONAL
1254        Z_PARAM_BOOL(autoload)
1255    ZEND_PARSE_PARAMETERS_END();
1256#endif
1257
1258    if (!autoload) {
1259        if (class_name->val[0] == '\\') {
1260            /* Ignore leading "\" */
1261            lc_name = zend_string_alloc(class_name->len - 1, 0);
1262            zend_str_tolower_copy(lc_name->val, class_name->val + 1, class_name->len - 1);
1263        } else {
1264            lc_name = zend_string_alloc(class_name->len, 0);
1265            zend_str_tolower_copy(lc_name->val, class_name->val, class_name->len);
1266        }
1267        ce = zend_hash_find_ptr(EG(class_table), lc_name);
1268        zend_string_free(lc_name);
1269        RETURN_BOOL(ce && !((ce->ce_flags & (ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT)) > ZEND_ACC_EXPLICIT_ABSTRACT_CLASS));
1270    }
1271
1272    ce = zend_lookup_class(class_name TSRMLS_CC);
1273    if (ce) {
1274        RETURN_BOOL((ce->ce_flags & (ZEND_ACC_INTERFACE | (ZEND_ACC_TRAIT - ZEND_ACC_EXPLICIT_ABSTRACT_CLASS))) == 0);
1275    } else {
1276        RETURN_FALSE;
1277    }
1278}
1279/* }}} */
1280
1281/* {{{ proto bool interface_exists(string classname [, bool autoload])
1282   Checks if the class exists */
1283ZEND_FUNCTION(interface_exists)
1284{
1285    zend_string *iface_name, *lc_name;
1286    zend_class_entry *ce;
1287    zend_bool autoload = 1;
1288
1289#ifndef FAST_ZPP
1290    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S|b", &iface_name, &autoload) == FAILURE) {
1291        return;
1292    }
1293#else
1294    ZEND_PARSE_PARAMETERS_START(1, 2)
1295        Z_PARAM_STR(iface_name)
1296        Z_PARAM_OPTIONAL
1297        Z_PARAM_BOOL(autoload)
1298    ZEND_PARSE_PARAMETERS_END();
1299#endif
1300
1301    if (!autoload) {
1302        if (iface_name->val[0] == '\\') {
1303            /* Ignore leading "\" */
1304            lc_name = zend_string_alloc(iface_name->len - 1, 0);
1305            zend_str_tolower_copy(lc_name->val, iface_name->val + 1, iface_name->len - 1);
1306        } else {
1307            lc_name = zend_string_alloc(iface_name->len, 0);
1308            zend_str_tolower_copy(lc_name->val, iface_name->val, iface_name->len);
1309        }
1310        ce = zend_hash_find_ptr(EG(class_table), lc_name);
1311        zend_string_free(lc_name);
1312        RETURN_BOOL(ce && ce->ce_flags & ZEND_ACC_INTERFACE);
1313    }
1314
1315    ce = zend_lookup_class(iface_name TSRMLS_CC);
1316    if (ce) {
1317        RETURN_BOOL((ce->ce_flags & ZEND_ACC_INTERFACE) > 0);
1318    } else {
1319        RETURN_FALSE;
1320    }
1321}
1322/* }}} */
1323
1324/* {{{ proto bool trait_exists(string traitname [, bool autoload])
1325 Checks if the trait exists */
1326ZEND_FUNCTION(trait_exists)
1327{
1328    zend_string *trait_name, *lc_name;
1329    zend_class_entry *ce;
1330    zend_bool autoload = 1;
1331
1332#ifndef FAST_ZPP
1333    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S|b", &trait_name, &autoload) == FAILURE) {
1334        return;
1335    }
1336#else
1337    ZEND_PARSE_PARAMETERS_START(1, 2)
1338        Z_PARAM_STR(trait_name)
1339        Z_PARAM_OPTIONAL
1340        Z_PARAM_BOOL(autoload)
1341    ZEND_PARSE_PARAMETERS_END();
1342#endif
1343
1344    if (!autoload) {
1345        if (trait_name->val[0] == '\\') {
1346            /* Ignore leading "\" */
1347            lc_name = zend_string_alloc(trait_name->len - 1, 0);
1348            zend_str_tolower_copy(lc_name->val, trait_name->val + 1, trait_name->len - 1);
1349        } else {
1350            lc_name = zend_string_alloc(trait_name->len, 0);
1351            zend_str_tolower_copy(lc_name->val, trait_name->val, trait_name->len);
1352        }
1353        ce = zend_hash_find_ptr(EG(class_table), lc_name);
1354        zend_string_free(lc_name);
1355        RETURN_BOOL(ce && ((ce->ce_flags & ZEND_ACC_TRAIT) > ZEND_ACC_EXPLICIT_ABSTRACT_CLASS));
1356    }
1357
1358    ce = zend_lookup_class(trait_name TSRMLS_CC);
1359    if (ce) {
1360        RETURN_BOOL((ce->ce_flags & ZEND_ACC_TRAIT) > ZEND_ACC_EXPLICIT_ABSTRACT_CLASS);
1361    } else {
1362        RETURN_FALSE;
1363    }
1364}
1365/* }}} */
1366
1367/* {{{ proto bool function_exists(string function_name)
1368   Checks if the function exists */
1369ZEND_FUNCTION(function_exists)
1370{
1371    char *name;
1372    size_t name_len;
1373    zend_function *func;
1374    zend_string *lcname;
1375
1376#ifndef FAST_ZPP
1377    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) {
1378        return;
1379    }
1380#else
1381    ZEND_PARSE_PARAMETERS_START(1, 1)
1382        Z_PARAM_STRING(name, name_len)
1383    ZEND_PARSE_PARAMETERS_END();
1384#endif
1385
1386    if (name[0] == '\\') {
1387        /* Ignore leading "\" */
1388        lcname = zend_string_alloc(name_len - 1, 0);
1389        zend_str_tolower_copy(lcname->val, name + 1, name_len - 1);
1390    } else {
1391        lcname = zend_string_alloc(name_len, 0);
1392        zend_str_tolower_copy(lcname->val, name, name_len);
1393    }
1394
1395    func = zend_hash_find_ptr(EG(function_table), lcname);
1396    zend_string_free(lcname);
1397
1398    /*
1399     * A bit of a hack, but not a bad one: we see if the handler of the function
1400     * is actually one that displays "function is disabled" message.
1401     */
1402    RETURN_BOOL(func && (func->type != ZEND_INTERNAL_FUNCTION ||
1403        func->internal_function.handler != zif_display_disabled_function));
1404}
1405/* }}} */
1406
1407/* {{{ proto bool class_alias(string user_class_name , string alias_name [, bool autoload])
1408   Creates an alias for user defined class */
1409ZEND_FUNCTION(class_alias)
1410{
1411    zend_string *class_name;
1412    char *alias_name;
1413    zend_class_entry *ce;
1414    size_t alias_name_len;
1415    zend_bool autoload = 1;
1416
1417    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Ss|b", &class_name, &alias_name, &alias_name_len, &autoload) == FAILURE) {
1418        return;
1419    }
1420
1421    ce = zend_lookup_class_ex(class_name, NULL, autoload TSRMLS_CC);
1422
1423    if (ce) {
1424        if (ce->type == ZEND_USER_CLASS) {
1425            if (zend_register_class_alias_ex(alias_name, alias_name_len, ce TSRMLS_CC) == SUCCESS) {
1426                RETURN_TRUE;
1427            } else {
1428                zend_error(E_WARNING, "Cannot redeclare class %s", alias_name);
1429                RETURN_FALSE;
1430            }
1431        } else {
1432            zend_error(E_WARNING, "First argument of class_alias() must be a name of user defined class");
1433            RETURN_FALSE;
1434        }
1435    } else {
1436        zend_error(E_WARNING, "Class '%s' not found", class_name->val);
1437        RETURN_FALSE;
1438    }
1439}
1440/* }}} */
1441
1442#if ZEND_DEBUG
1443/* {{{ proto void leak(int num_bytes=3)
1444   Cause an intentional memory leak, for testing/debugging purposes */
1445ZEND_FUNCTION(leak)
1446{
1447    zend_long leakbytes=3;
1448
1449    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &leakbytes) == FAILURE) {
1450        return;
1451    }
1452
1453    emalloc(leakbytes);
1454}
1455/* }}} */
1456
1457/* {{{ proto leak_variable(mixed variable [, bool leak_data]) */
1458ZEND_FUNCTION(leak_variable)
1459{
1460    zval *zv;
1461    zend_bool leak_data = 0;
1462
1463    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|b", &zv, &leak_data) == FAILURE) {
1464        return;
1465    }
1466
1467    if (!leak_data) {
1468        Z_ADDREF_P(zv);
1469    } else if (Z_TYPE_P(zv) == IS_RESOURCE) {
1470        Z_ADDREF_P(zv);
1471    } else if (Z_TYPE_P(zv) == IS_OBJECT) {
1472        Z_ADDREF_P(zv);
1473    } else {
1474        zend_error(E_WARNING, "Leaking non-zval data is only applicable to resources and objects");
1475    }
1476}
1477/* }}} */
1478
1479
1480#ifdef ZEND_TEST_EXCEPTIONS
1481ZEND_FUNCTION(crash)
1482{
1483    char *nowhere=NULL;
1484
1485    memcpy(nowhere, "something", sizeof("something"));
1486}
1487#endif
1488
1489#endif /* ZEND_DEBUG */
1490
1491/* {{{ proto array get_included_files(void)
1492   Returns an array with the file names that were include_once()'d */
1493ZEND_FUNCTION(get_included_files)
1494{
1495    zend_string *entry;
1496
1497    if (zend_parse_parameters_none() == FAILURE) {
1498        return;
1499    }
1500
1501    array_init(return_value);
1502    ZEND_HASH_FOREACH_STR_KEY(&EG(included_files), entry) {
1503        if (entry) {
1504            add_next_index_str(return_value, zend_string_copy(entry));
1505        }
1506    } ZEND_HASH_FOREACH_END();
1507}
1508/* }}} */
1509
1510/* {{{ proto void trigger_error(string message [, int error_type])
1511   Generates a user-level error/warning/notice message */
1512ZEND_FUNCTION(trigger_error)
1513{
1514    zend_long error_type = E_USER_NOTICE;
1515    char *message;
1516    size_t message_len;
1517
1518    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &message, &message_len, &error_type) == FAILURE) {
1519        return;
1520    }
1521
1522    switch (error_type) {
1523        case E_USER_ERROR:
1524        case E_USER_WARNING:
1525        case E_USER_NOTICE:
1526        case E_USER_DEPRECATED:
1527            break;
1528        default:
1529            zend_error(E_WARNING, "Invalid error type specified");
1530            RETURN_FALSE;
1531            break;
1532    }
1533
1534    zend_error((int)error_type, "%s", message);
1535    RETURN_TRUE;
1536}
1537/* }}} */
1538
1539/* {{{ proto string set_error_handler(string error_handler [, int error_types])
1540   Sets a user-defined error handler function.  Returns the previously defined error handler, or false on error */
1541ZEND_FUNCTION(set_error_handler)
1542{
1543    zval *error_handler;
1544    zend_string *error_handler_name = NULL;
1545    zend_long error_type = E_ALL;
1546
1547    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &error_handler, &error_type) == FAILURE) {
1548        return;
1549    }
1550
1551    if (Z_TYPE_P(error_handler) != IS_NULL) { /* NULL == unset */
1552        if (!zend_is_callable(error_handler, 0, &error_handler_name TSRMLS_CC)) {
1553            zend_error(E_WARNING, "%s() expects the argument (%s) to be a valid callback",
1554                       get_active_function_name(TSRMLS_C), error_handler_name?error_handler_name->val:"unknown");
1555            zend_string_release(error_handler_name);
1556            return;
1557        }
1558        zend_string_release(error_handler_name);
1559    }
1560
1561    if (Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
1562        RETVAL_ZVAL(&EG(user_error_handler), 1, 0);
1563
1564        zend_stack_push(&EG(user_error_handlers_error_reporting), &EG(user_error_handler_error_reporting));
1565        zend_stack_push(&EG(user_error_handlers), &EG(user_error_handler));
1566    }
1567
1568    if (Z_TYPE_P(error_handler) == IS_NULL) { /* unset user-defined handler */
1569        ZVAL_UNDEF(&EG(user_error_handler));
1570        return;
1571    }
1572
1573    ZVAL_DUP(&EG(user_error_handler), error_handler);
1574    EG(user_error_handler_error_reporting) = (int)error_type;
1575}
1576/* }}} */
1577
1578/* {{{ proto void restore_error_handler(void)
1579   Restores the previously defined error handler function */
1580ZEND_FUNCTION(restore_error_handler)
1581{
1582    if (Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
1583        zval zeh;
1584
1585        ZVAL_COPY_VALUE(&zeh, &EG(user_error_handler));
1586        ZVAL_UNDEF(&EG(user_error_handler));
1587        zval_ptr_dtor(&zeh);
1588    }
1589
1590    if (zend_stack_is_empty(&EG(user_error_handlers))) {
1591        ZVAL_UNDEF(&EG(user_error_handler));
1592    } else {
1593        zval *tmp;
1594        EG(user_error_handler_error_reporting) = zend_stack_int_top(&EG(user_error_handlers_error_reporting));
1595        zend_stack_del_top(&EG(user_error_handlers_error_reporting));
1596        tmp = zend_stack_top(&EG(user_error_handlers));
1597        ZVAL_COPY_VALUE(&EG(user_error_handler), tmp);
1598        zend_stack_del_top(&EG(user_error_handlers));
1599    }
1600    RETURN_TRUE;
1601}
1602/* }}} */
1603
1604/* {{{ proto string set_exception_handler(callable exception_handler)
1605   Sets a user-defined exception handler function.  Returns the previously defined exception handler, or false on error */
1606ZEND_FUNCTION(set_exception_handler)
1607{
1608    zval *exception_handler;
1609    zend_string *exception_handler_name = NULL;
1610
1611    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &exception_handler) == FAILURE) {
1612        return;
1613    }
1614
1615    if (Z_TYPE_P(exception_handler) != IS_NULL) { /* NULL == unset */
1616        if (!zend_is_callable(exception_handler, 0, &exception_handler_name TSRMLS_CC)) {
1617            zend_error(E_WARNING, "%s() expects the argument (%s) to be a valid callback",
1618                       get_active_function_name(TSRMLS_C), exception_handler_name?exception_handler_name->val:"unknown");
1619            zend_string_release(exception_handler_name);
1620            return;
1621        }
1622        zend_string_release(exception_handler_name);
1623    }
1624
1625    if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
1626        RETVAL_ZVAL(&EG(user_exception_handler), 1, 0);
1627
1628        zend_stack_push(&EG(user_exception_handlers), &EG(user_exception_handler));
1629    }
1630
1631    if (Z_TYPE_P(exception_handler) == IS_NULL) { /* unset user-defined handler */
1632        ZVAL_UNDEF(&EG(user_exception_handler));
1633        return;
1634    }
1635
1636    ZVAL_DUP(&EG(user_exception_handler), exception_handler);
1637}
1638/* }}} */
1639
1640/* {{{ proto void restore_exception_handler(void)
1641   Restores the previously defined exception handler function */
1642ZEND_FUNCTION(restore_exception_handler)
1643{
1644    if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
1645        zval_ptr_dtor(&EG(user_exception_handler));
1646    }
1647    if (zend_stack_is_empty(&EG(user_exception_handlers))) {
1648        ZVAL_UNDEF(&EG(user_exception_handler));
1649    } else {
1650        zval *tmp = zend_stack_top(&EG(user_exception_handlers));
1651        ZVAL_COPY_VALUE(&EG(user_exception_handler), tmp);
1652        zend_stack_del_top(&EG(user_exception_handlers));
1653    }
1654    RETURN_TRUE;
1655}
1656/* }}} */
1657
1658static int copy_class_or_interface_name(zval *el TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) /* {{{ */
1659{
1660    zend_class_entry *ce = (zend_class_entry *)Z_PTR_P(el);
1661    zval *array = va_arg(args, zval *);
1662    uint32_t mask = va_arg(args, uint32_t);
1663    uint32_t comply = va_arg(args, uint32_t);
1664    uint32_t comply_mask = (comply)? mask:0;
1665
1666    if ((hash_key->key && hash_key->key->val[0] != 0)
1667        && (comply_mask == (ce->ce_flags & mask))) {
1668        if (ce->refcount > 1 &&
1669            (ce->name->len != hash_key->key->len ||
1670             !same_name(hash_key->key->val, ce->name->val, ce->name->len))) {
1671            add_next_index_str(array, zend_string_copy(hash_key->key));
1672        } else {
1673            add_next_index_str(array, zend_string_copy(ce->name));
1674        }
1675    }
1676    return ZEND_HASH_APPLY_KEEP;
1677}
1678/* }}} */
1679
1680/* {{{ proto array get_declared_traits()
1681   Returns an array of all declared traits. */
1682ZEND_FUNCTION(get_declared_traits)
1683{
1684    uint32_t mask = ZEND_ACC_TRAIT;
1685    uint32_t comply = 1;
1686
1687    if (zend_parse_parameters_none() == FAILURE) {
1688        return;
1689    }
1690
1691    array_init(return_value);
1692    zend_hash_apply_with_arguments(EG(class_table) TSRMLS_CC, copy_class_or_interface_name, 3, return_value, mask, comply);
1693}
1694/* }}} */
1695
1696/* {{{ proto array get_declared_classes()
1697   Returns an array of all declared classes. */
1698ZEND_FUNCTION(get_declared_classes)
1699{
1700    uint32_t mask = ZEND_ACC_INTERFACE | (ZEND_ACC_TRAIT & ~ZEND_ACC_EXPLICIT_ABSTRACT_CLASS);
1701    uint32_t comply = 0;
1702
1703    if (zend_parse_parameters_none() == FAILURE) {
1704        return;
1705    }
1706
1707    array_init(return_value);
1708    zend_hash_apply_with_arguments(EG(class_table) TSRMLS_CC, copy_class_or_interface_name, 3, return_value, mask, comply);
1709}
1710/* }}} */
1711
1712/* {{{ proto array get_declared_interfaces()
1713   Returns an array of all declared interfaces. */
1714ZEND_FUNCTION(get_declared_interfaces)
1715{
1716    uint32_t mask = ZEND_ACC_INTERFACE;
1717    uint32_t comply = 1;
1718
1719    if (zend_parse_parameters_none() == FAILURE) {
1720        return;
1721    }
1722
1723    array_init(return_value);
1724    zend_hash_apply_with_arguments(EG(class_table) TSRMLS_CC, copy_class_or_interface_name, 3, return_value, mask, comply);
1725}
1726/* }}} */
1727
1728static int copy_function_name(zval *zv TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) /* {{{ */
1729{
1730    zend_function *func = Z_PTR_P(zv);
1731    zval *internal_ar = va_arg(args, zval *),
1732         *user_ar     = va_arg(args, zval *);
1733
1734    if (hash_key->key == NULL || hash_key->key->val[0] == 0) {
1735        return 0;
1736    }
1737
1738    if (func->type == ZEND_INTERNAL_FUNCTION) {
1739        add_next_index_str(internal_ar, zend_string_copy(hash_key->key));
1740    } else if (func->type == ZEND_USER_FUNCTION) {
1741        add_next_index_str(user_ar, zend_string_copy(hash_key->key));
1742    }
1743
1744    return 0;
1745}
1746/* }}} */
1747
1748/* {{{ proto array get_defined_functions(void)
1749   Returns an array of all defined functions */
1750ZEND_FUNCTION(get_defined_functions)
1751{
1752    zval internal, user;
1753
1754    if (zend_parse_parameters_none() == FAILURE) {
1755        return;
1756    }
1757
1758    array_init(&internal);
1759    array_init(&user);
1760    array_init(return_value);
1761
1762    zend_hash_apply_with_arguments(EG(function_table) TSRMLS_CC, copy_function_name, 2, &internal, &user);
1763
1764    zend_hash_str_add_new(Z_ARRVAL_P(return_value), "internal", sizeof("internal")-1, &internal);
1765    zend_hash_str_add_new(Z_ARRVAL_P(return_value), "user", sizeof("user")-1, &user);
1766}
1767/* }}} */
1768
1769/* {{{ proto array get_defined_vars(void)
1770   Returns an associative array of names and values of all currently defined variable names (variables in the current scope) */
1771ZEND_FUNCTION(get_defined_vars)
1772{
1773    zend_array *symbol_table = zend_rebuild_symbol_table(TSRMLS_C);
1774
1775    ZVAL_NEW_ARR(return_value);
1776    zend_array_dup(Z_ARRVAL_P(return_value), &symbol_table->ht);
1777}
1778/* }}} */
1779
1780#define LAMBDA_TEMP_FUNCNAME    "__lambda_func"
1781/* {{{ proto string create_function(string args, string code)
1782   Creates an anonymous function, and returns its name (funny, eh?) */
1783ZEND_FUNCTION(create_function)
1784{
1785    zend_string *function_name;
1786    char *eval_code, *function_args, *function_code;
1787    size_t eval_code_length, function_args_len, function_code_len;
1788    int retval;
1789    char *eval_name;
1790
1791    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &function_args, &function_args_len, &function_code, &function_code_len) == FAILURE) {
1792        return;
1793    }
1794
1795    eval_code = (char *) emalloc(sizeof("function " LAMBDA_TEMP_FUNCNAME)
1796            +function_args_len
1797            +2  /* for the args parentheses */
1798            +2  /* for the curly braces */
1799            +function_code_len);
1800
1801    eval_code_length = sizeof("function " LAMBDA_TEMP_FUNCNAME "(") - 1;
1802    memcpy(eval_code, "function " LAMBDA_TEMP_FUNCNAME "(", eval_code_length);
1803
1804    memcpy(eval_code + eval_code_length, function_args, function_args_len);
1805    eval_code_length += function_args_len;
1806
1807    eval_code[eval_code_length++] = ')';
1808    eval_code[eval_code_length++] = '{';
1809
1810    memcpy(eval_code + eval_code_length, function_code, function_code_len);
1811    eval_code_length += function_code_len;
1812
1813    eval_code[eval_code_length++] = '}';
1814    eval_code[eval_code_length] = '\0';
1815
1816    eval_name = zend_make_compiled_string_description("runtime-created function" TSRMLS_CC);
1817    retval = zend_eval_stringl(eval_code, eval_code_length, NULL, eval_name TSRMLS_CC);
1818    efree(eval_code);
1819    efree(eval_name);
1820
1821    if (retval==SUCCESS) {
1822        zend_op_array *func;
1823        HashTable *static_variables;
1824
1825        func = zend_hash_str_find_ptr(EG(function_table), LAMBDA_TEMP_FUNCNAME, sizeof(LAMBDA_TEMP_FUNCNAME)-1);
1826        if (!func) {
1827            zend_error(E_ERROR, "Unexpected inconsistency in create_function()");
1828            RETURN_FALSE;
1829        }
1830        (*func->refcount)++;
1831        static_variables = func->static_variables;
1832        func->static_variables = NULL;
1833        zend_hash_str_del(EG(function_table), LAMBDA_TEMP_FUNCNAME, sizeof(LAMBDA_TEMP_FUNCNAME)-1);
1834        func->static_variables = static_variables;
1835
1836        function_name = zend_string_alloc(sizeof("0lambda_")+MAX_LENGTH_OF_LONG, 0);
1837        function_name->val[0] = '\0';
1838
1839        do {
1840            function_name->len = snprintf(function_name->val + 1, sizeof("lambda_")+MAX_LENGTH_OF_LONG, "lambda_%d", ++EG(lambda_count)) + 1;
1841        } while (zend_hash_add_ptr(EG(function_table), function_name, func) == NULL);
1842        RETURN_STR(function_name);
1843    } else {
1844        zend_hash_str_del(EG(function_table), LAMBDA_TEMP_FUNCNAME, sizeof(LAMBDA_TEMP_FUNCNAME)-1);
1845        RETURN_FALSE;
1846    }
1847}
1848/* }}} */
1849
1850#if ZEND_DEBUG
1851ZEND_FUNCTION(zend_test_func)
1852{
1853    zval *arg1, *arg2;
1854
1855    zend_get_parameters(ZEND_NUM_ARGS(), 2, &arg1, &arg2);
1856}
1857
1858
1859#ifdef ZTS
1860ZEND_FUNCTION(zend_thread_id)
1861{
1862    RETURN_LONG((zend_long)tsrm_thread_id());
1863}
1864#endif
1865#endif
1866
1867/* {{{ proto string get_resource_type(resource res)
1868   Get the resource type name for a given resource */
1869ZEND_FUNCTION(get_resource_type)
1870{
1871    const char *resource_type;
1872    zval *z_resource_type;
1873
1874    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_resource_type) == FAILURE) {
1875        return;
1876    }
1877
1878    resource_type = zend_rsrc_list_get_rsrc_type(Z_RES_P(z_resource_type) TSRMLS_CC);
1879    if (resource_type) {
1880        RETURN_STRING(resource_type);
1881    } else {
1882        RETURN_STRING("Unknown");
1883    }
1884}
1885/* }}} */
1886
1887/* {{{ proto array get_resources([string resouce_type])
1888   Get an array with all active resources */
1889ZEND_FUNCTION(get_resources)
1890{
1891    zend_string *type = NULL;
1892    zend_string *key;
1893    zend_ulong index;
1894    zval *val;
1895
1896    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|S", &type) == FAILURE) {
1897        return;
1898    }
1899
1900    if (!type) {
1901        array_init(return_value);
1902        ZEND_HASH_FOREACH_KEY_VAL(&EG(regular_list), index, key, val) {
1903            if (!key) {
1904                Z_ADDREF_P(val);
1905                zend_hash_index_add_new(Z_ARRVAL_P(return_value), index, val);
1906            }
1907        } ZEND_HASH_FOREACH_END();
1908    } else if (zend_string_equals_literal(type, "Unknown")) {
1909        array_init(return_value);
1910        ZEND_HASH_FOREACH_KEY_VAL(&EG(regular_list), index, key, val) {
1911            if (!key && Z_RES_TYPE_P(val) <= 0) {
1912                Z_ADDREF_P(val);
1913                zend_hash_index_add_new(Z_ARRVAL_P(return_value), index, val);
1914            }
1915        } ZEND_HASH_FOREACH_END();
1916    } else {
1917        int id = zend_fetch_list_dtor_id(type->val);
1918
1919        if (id <= 0) {
1920            zend_error(E_WARNING, "get_resources():  Unknown resource type '%s'", type->val);
1921            RETURN_FALSE;
1922        }
1923
1924        array_init(return_value);
1925        ZEND_HASH_FOREACH_KEY_VAL(&EG(regular_list), index, key, val) {
1926            if (!key && Z_RES_TYPE_P(val) == id) {
1927                Z_ADDREF_P(val);
1928                zend_hash_index_add_new(Z_ARRVAL_P(return_value), index, val);
1929            }
1930        } ZEND_HASH_FOREACH_END();
1931    }
1932}
1933/* }}} */
1934
1935static int add_extension_info(zval *item, void *arg TSRMLS_DC) /* {{{ */
1936{
1937    zval *name_array = (zval *)arg;
1938    zend_module_entry *module = (zend_module_entry*)Z_PTR_P(item);
1939    add_next_index_string(name_array, module->name);
1940    return 0;
1941}
1942/* }}} */
1943
1944static int add_zendext_info(zend_extension *ext, void *arg TSRMLS_DC) /* {{{ */
1945{
1946    zval *name_array = (zval *)arg;
1947    add_next_index_string(name_array, ext->name);
1948    return 0;
1949}
1950/* }}} */
1951
1952static int add_constant_info(zval *item, void *arg TSRMLS_DC) /* {{{ */
1953{
1954    zval *name_array = (zval *)arg;
1955    zend_constant *constant = (zend_constant*)Z_PTR_P(item);
1956    zval const_val;
1957
1958    if (!constant->name) {
1959        /* skip special constants */
1960        return 0;
1961    }
1962
1963    ZVAL_DUP(&const_val, &constant->value);
1964    zend_hash_add_new(Z_ARRVAL_P(name_array), constant->name, &const_val);
1965    return 0;
1966}
1967/* }}} */
1968
1969/* {{{ proto array get_loaded_extensions([bool zend_extensions]) U
1970   Return an array containing names of loaded extensions */
1971ZEND_FUNCTION(get_loaded_extensions)
1972{
1973    zend_bool zendext = 0;
1974
1975    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &zendext) == FAILURE) {
1976        return;
1977    }
1978
1979    array_init(return_value);
1980
1981    if (zendext) {
1982        zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t)add_zendext_info, return_value TSRMLS_CC);
1983    } else {
1984        zend_hash_apply_with_argument(&module_registry, add_extension_info, return_value TSRMLS_CC);
1985    }
1986}
1987/* }}} */
1988
1989/* {{{ proto array get_defined_constants([bool categorize])
1990   Return an array containing the names and values of all defined constants */
1991ZEND_FUNCTION(get_defined_constants)
1992{
1993    zend_bool categorize = 0;
1994
1995    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &categorize) == FAILURE) {
1996        return;
1997    }
1998
1999    array_init(return_value);
2000
2001    if (categorize) {
2002        zend_constant *val;
2003        int module_number;
2004        zval *modules, const_val;
2005        char **module_names;
2006        zend_module_entry *module;
2007        int i = 1;
2008
2009        modules = ecalloc(zend_hash_num_elements(&module_registry) + 2, sizeof(zval));
2010        module_names = emalloc((zend_hash_num_elements(&module_registry) + 2) * sizeof(char *));
2011
2012        module_names[0] = "internal";
2013        ZEND_HASH_FOREACH_PTR(&module_registry, module) {
2014            module_names[module->module_number] = (char *)module->name;
2015            i++;
2016        } ZEND_HASH_FOREACH_END();
2017        module_names[i] = "user";
2018
2019        ZEND_HASH_FOREACH_PTR(EG(zend_constants), val) {
2020            if (!val->name) {
2021                /* skip special constants */
2022                continue;
2023            }
2024
2025            if (val->module_number == PHP_USER_CONSTANT) {
2026                module_number = i;
2027            } else if (val->module_number > i || val->module_number < 0) {
2028                /* should not happen */
2029                continue;
2030            } else {
2031                module_number = val->module_number;
2032            }
2033
2034            if (Z_TYPE(modules[module_number]) == IS_UNDEF) {
2035                array_init(&modules[module_number]);
2036                add_assoc_zval(return_value, module_names[module_number], &modules[module_number]);
2037            }
2038
2039            ZVAL_DUP(&const_val, &val->value);
2040            zend_hash_add_new(Z_ARRVAL(modules[module_number]), val->name, &const_val);
2041        } ZEND_HASH_FOREACH_END();
2042
2043        efree(module_names);
2044        efree(modules);
2045    } else {
2046        zend_hash_apply_with_argument(EG(zend_constants), add_constant_info, return_value TSRMLS_CC);
2047    }
2048}
2049/* }}} */
2050
2051static void debug_backtrace_get_args(zend_execute_data *call, zval *arg_array TSRMLS_DC) /* {{{ */
2052{
2053    uint32_t num_args = ZEND_CALL_NUM_ARGS(call);
2054
2055    array_init_size(arg_array, num_args);
2056    if (num_args) {
2057        uint32_t i = 0;
2058        zval *p = ZEND_CALL_ARG(call, 1);
2059
2060        if (call->func->type == ZEND_USER_FUNCTION) {
2061            uint32_t first_extra_arg = call->func->op_array.num_args;
2062
2063            if (call->func->op_array.fn_flags & ZEND_ACC_VARIADIC) {
2064                first_extra_arg--;
2065            }
2066            if (ZEND_CALL_NUM_ARGS(call) > first_extra_arg) {
2067                while (i < first_extra_arg) {
2068                    if (Z_REFCOUNTED_P(p)) Z_ADDREF_P(p);
2069                    zend_hash_next_index_insert_new(Z_ARRVAL_P(arg_array), p);
2070                    p++;
2071                    i++;
2072                }
2073                p = ZEND_CALL_VAR_NUM(call, call->func->op_array.last_var + call->func->op_array.T);
2074            }
2075        }
2076
2077        while (i < num_args) {
2078            if (Z_REFCOUNTED_P(p)) Z_ADDREF_P(p);
2079            zend_hash_next_index_insert_new(Z_ARRVAL_P(arg_array), p);
2080            p++;
2081            i++;
2082        }
2083    }
2084}
2085/* }}} */
2086
2087void debug_print_backtrace_args(zval *arg_array TSRMLS_DC) /* {{{ */
2088{
2089    zval *tmp;
2090    int i = 0;
2091
2092    ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(arg_array), tmp) {
2093        if (i++) {
2094            ZEND_PUTS(", ");
2095        }
2096        zend_print_flat_zval_r(tmp TSRMLS_CC);
2097    } ZEND_HASH_FOREACH_END();
2098}
2099/* }}} */
2100
2101/* {{{ proto void debug_print_backtrace([int options[, int limit]]) */
2102ZEND_FUNCTION(debug_print_backtrace)
2103{
2104    zend_execute_data *call, *ptr, *skip;
2105    zend_object *object;
2106    int lineno, frameno = 0;
2107    zend_function *func;
2108    const char *function_name;
2109    const char *filename;
2110    zend_string *class_name = NULL;
2111    char *call_type;
2112    const char *include_filename = NULL;
2113    zval arg_array;
2114    int indent = 0;
2115    zend_long options = 0;
2116    zend_long limit = 0;
2117
2118    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ll", &options, &limit) == FAILURE) {
2119        return;
2120    }
2121
2122    ZVAL_UNDEF(&arg_array);
2123    ptr = EX(prev_execute_data);
2124
2125    /* skip debug_backtrace() */
2126    call = ptr;
2127    ptr = ptr->prev_execute_data;
2128
2129    while (ptr && (limit == 0 || frameno < limit)) {
2130        frameno++;
2131        class_name = NULL;
2132        call_type = NULL;
2133        ZVAL_UNDEF(&arg_array);
2134
2135        skip = ptr;
2136        /* skip internal handler */
2137        if ((!skip->func || !ZEND_USER_CODE(skip->func->common.type)) &&
2138            skip->prev_execute_data &&
2139            skip->prev_execute_data->func &&
2140            ZEND_USER_CODE(skip->prev_execute_data->func->common.type) &&
2141            skip->prev_execute_data->opline->opcode != ZEND_DO_FCALL &&
2142            skip->prev_execute_data->opline->opcode != ZEND_INCLUDE_OR_EVAL) {
2143            skip = skip->prev_execute_data;
2144        }
2145
2146        if (skip->func && ZEND_USER_CODE(skip->func->common.type)) {
2147            filename = skip->func->op_array.filename->val;
2148            if (skip->opline->opcode == ZEND_HANDLE_EXCEPTION) {
2149                if (EG(opline_before_exception)) {
2150                    lineno = EG(opline_before_exception)->lineno;
2151                } else {
2152                    lineno = skip->func->op_array.line_end;
2153                }
2154            } else {
2155                lineno = skip->opline->lineno;
2156            }
2157        } else {
2158            filename = NULL;
2159            lineno = 0;
2160        }
2161
2162        /* $this may be passed into regular internal functions */
2163        object = Z_OBJ(call->This);
2164        if (object &&
2165            call &&
2166            call->func->type == ZEND_INTERNAL_FUNCTION &&
2167            !call->func->common.scope) {
2168            object = NULL;
2169        }
2170
2171        if (call->func) {
2172            func = call->func;
2173            function_name = (func->common.scope &&
2174                             func->common.scope->trait_aliases) ?
2175                zend_resolve_method_name(
2176                    (object ? object->ce : func->common.scope), func)->val :
2177                (func->common.function_name ?
2178                    func->common.function_name->val : NULL);
2179        } else {
2180            func = NULL;
2181            function_name = NULL;
2182        }
2183
2184        if (function_name) {
2185            if (object) {
2186                if (func->common.scope) {
2187                    class_name = func->common.scope->name;
2188                } else {
2189                    class_name = object->ce->name;
2190                }
2191
2192                call_type = "->";
2193            } else if (func->common.scope) {
2194                class_name = func->common.scope->name;
2195                call_type = "::";
2196            } else {
2197                class_name = NULL;
2198                call_type = NULL;
2199            }
2200            if (func->type != ZEND_EVAL_CODE) {
2201                if ((options & DEBUG_BACKTRACE_IGNORE_ARGS) == 0) {
2202                    debug_backtrace_get_args(call, &arg_array TSRMLS_CC);
2203                }
2204            }
2205        } else {
2206            /* i know this is kinda ugly, but i'm trying to avoid extra cycles in the main execution loop */
2207            zend_bool build_filename_arg = 1;
2208
2209            if (!ptr->func || !ZEND_USER_CODE(ptr->func->common.type) || ptr->opline->opcode != ZEND_INCLUDE_OR_EVAL) {
2210                /* can happen when calling eval from a custom sapi */
2211                function_name = "unknown";
2212                build_filename_arg = 0;
2213            } else
2214            switch (ptr->opline->extended_value) {
2215                case ZEND_EVAL:
2216                    function_name = "eval";
2217                    build_filename_arg = 0;
2218                    break;
2219                case ZEND_INCLUDE:
2220                    function_name = "include";
2221                    break;
2222                case ZEND_REQUIRE:
2223                    function_name = "require";
2224                    break;
2225                case ZEND_INCLUDE_ONCE:
2226                    function_name = "include_once";
2227                    break;
2228                case ZEND_REQUIRE_ONCE:
2229                    function_name = "require_once";
2230                    break;
2231                default:
2232                    /* this can actually happen if you use debug_backtrace() in your error_handler and
2233                     * you're in the top-scope */
2234                    function_name = "unknown";
2235                    build_filename_arg = 0;
2236                    break;
2237            }
2238
2239            if (build_filename_arg && include_filename) {
2240                array_init(&arg_array);
2241                add_next_index_string(&arg_array, (char*)include_filename);
2242            }
2243            call_type = NULL;
2244        }
2245        zend_printf("#%-2d ", indent);
2246        if (class_name) {
2247            ZEND_PUTS(class_name->val);
2248            ZEND_PUTS(call_type);
2249        }
2250        zend_printf("%s(", function_name);
2251        if (Z_TYPE(arg_array) != IS_UNDEF) {
2252            debug_print_backtrace_args(&arg_array TSRMLS_CC);
2253            zval_ptr_dtor(&arg_array);
2254        }
2255        if (filename) {
2256            zend_printf(") called at [%s:%d]\n", filename, lineno);
2257        } else {
2258            zend_execute_data *prev_call = skip;
2259            zend_execute_data *prev = skip->prev_execute_data;
2260
2261            while (prev) {
2262                if (prev_call &&
2263                    prev_call->func &&
2264                    !ZEND_USER_CODE(prev_call->func->common.type)) {
2265                    prev = NULL;
2266                    break;
2267                }
2268                if (prev->func && ZEND_USER_CODE(prev->func->common.type)) {
2269                    zend_printf(") called at [%s:%d]\n", prev->func->op_array.filename->val, prev->opline->lineno);
2270                    break;
2271                }
2272                prev_call = prev;
2273                prev = prev->prev_execute_data;
2274            }
2275            if (!prev) {
2276                ZEND_PUTS(")\n");
2277            }
2278        }
2279        include_filename = filename;
2280        call = skip;
2281        ptr = skip->prev_execute_data;
2282        ++indent;
2283    }
2284}
2285
2286/* }}} */
2287
2288ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int options, int limit TSRMLS_DC) /* {{{ */
2289{
2290    zend_execute_data *call, *ptr, *skip;
2291    zend_object *object;
2292    int lineno, frameno = 0;
2293    zend_function *func;
2294    const char *function_name;
2295    const char *filename;
2296    const char *include_filename = NULL;
2297    zval stack_frame;
2298
2299    call = NULL;
2300    ptr = EG(current_execute_data);
2301    if (!ptr->func || !ZEND_USER_CODE(ptr->func->common.type)) {
2302        call = ptr;
2303        ptr = ptr->prev_execute_data;
2304    }
2305
2306    if (ptr) {
2307        if (skip_last) {
2308            /* skip debug_backtrace() */
2309            call = ptr;
2310            ptr = ptr->prev_execute_data;
2311        } else {
2312            /* skip "new Exception()" */
2313            if (ptr->func && ZEND_USER_CODE(ptr->func->common.type) && (ptr->opline->opcode == ZEND_NEW)) {
2314                call = ptr;
2315                ptr = ptr->prev_execute_data;
2316            }
2317        }
2318    }
2319    if (!call) {
2320        call = ptr;
2321        ptr = ptr->prev_execute_data;
2322    }
2323
2324    array_init(return_value);
2325
2326    while (ptr && (limit == 0 || frameno < limit)) {
2327        frameno++;
2328        array_init(&stack_frame);
2329
2330        skip = ptr;
2331        /* skip internal handler */
2332        if ((!skip->func || !ZEND_USER_CODE(skip->func->common.type)) &&
2333            skip->prev_execute_data &&
2334            skip->prev_execute_data->func &&
2335            ZEND_USER_CODE(skip->prev_execute_data->func->common.type) &&
2336            skip->prev_execute_data->opline->opcode != ZEND_DO_FCALL &&
2337            skip->prev_execute_data->opline->opcode != ZEND_INCLUDE_OR_EVAL) {
2338            skip = skip->prev_execute_data;
2339        }
2340
2341        if (skip->func && ZEND_USER_CODE(skip->func->common.type)) {
2342            filename = skip->func->op_array.filename->val;
2343            if (skip->opline->opcode == ZEND_HANDLE_EXCEPTION) {
2344                if (EG(opline_before_exception)) {
2345                    lineno = EG(opline_before_exception)->lineno;
2346                } else {
2347                    lineno = skip->func->op_array.line_end;
2348                }
2349            } else {
2350                lineno = skip->opline->lineno;
2351            }
2352            add_assoc_string_ex(&stack_frame, "file", sizeof("file")-1, (char*)filename);
2353            add_assoc_long_ex(&stack_frame, "line", sizeof("line")-1, lineno);
2354
2355            /* try to fetch args only if an FCALL was just made - elsewise we're in the middle of a function
2356             * and debug_baktrace() might have been called by the error_handler. in this case we don't
2357             * want to pop anything of the argument-stack */
2358        } else {
2359            zend_execute_data *prev_call = skip;
2360            zend_execute_data *prev = skip->prev_execute_data;
2361
2362            while (prev) {
2363                if (prev_call &&
2364                    prev_call->func &&
2365                    !ZEND_USER_CODE(prev_call->func->common.type) &&
2366                    !(prev_call->func->common.type == ZEND_INTERNAL_FUNCTION &&
2367                        (prev_call->func->common.fn_flags & ZEND_ACC_CALL_VIA_HANDLER))) {
2368                    break;
2369                }
2370                if (prev->func && ZEND_USER_CODE(prev->func->common.type)) {
2371                    add_assoc_str_ex(&stack_frame, "file", sizeof("file")-1, zend_string_copy(prev->func->op_array.filename));
2372                    add_assoc_long_ex(&stack_frame, "line", sizeof("line")-1, prev->opline->lineno);
2373                    break;
2374                }
2375                prev_call = prev;
2376                prev = prev->prev_execute_data;
2377            }
2378            filename = NULL;
2379        }
2380
2381        /* $this may be passed into regular internal functions */
2382        object = call ? Z_OBJ(call->This) : NULL;
2383        if (object &&
2384            call->func &&
2385            call->func->type == ZEND_INTERNAL_FUNCTION &&
2386            !call->func->common.scope) {
2387            object = NULL;
2388        }
2389
2390        if (call && call->func) {
2391            func = call->func;
2392            function_name = (func->common.scope &&
2393                             func->common.scope->trait_aliases) ?
2394                zend_resolve_method_name(
2395                    (object ? object->ce : func->common.scope), func)->val :
2396                (func->common.function_name ?
2397                    func->common.function_name->val : NULL);
2398        } else {
2399            func = NULL;
2400            function_name = NULL;
2401        }
2402
2403        if (function_name) {
2404            add_assoc_string_ex(&stack_frame, "function", sizeof("function")-1, (char*)function_name);
2405
2406            if (object) {
2407                if (func->common.scope) {
2408                    add_assoc_str_ex(&stack_frame, "class", sizeof("class")-1, zend_string_copy(func->common.scope->name));
2409                } else {
2410                    add_assoc_str_ex(&stack_frame, "class", sizeof("class")-1, zend_string_copy(object->ce->name));
2411
2412                }
2413                if ((options & DEBUG_BACKTRACE_PROVIDE_OBJECT) != 0) {
2414                    zval zv;
2415                    ZVAL_OBJ(&zv, object);
2416                    add_assoc_zval_ex(&stack_frame, "object", sizeof("object")-1, &zv);
2417                    Z_ADDREF(zv);
2418                }
2419
2420                add_assoc_string_ex(&stack_frame, "type", sizeof("type")-1, "->");
2421            } else if (func->common.scope) {
2422                add_assoc_str_ex(&stack_frame, "class", sizeof("class")-1, zend_string_copy(func->common.scope->name));
2423                add_assoc_string_ex(&stack_frame, "type", sizeof("type")-1, "::");
2424            }
2425
2426            if ((options & DEBUG_BACKTRACE_IGNORE_ARGS) == 0 &&
2427                func->type != ZEND_EVAL_CODE) {
2428                zval args;
2429
2430                debug_backtrace_get_args(call, &args TSRMLS_CC);
2431                add_assoc_zval_ex(&stack_frame, "args", sizeof("args")-1, &args);
2432            }
2433        } else {
2434            /* i know this is kinda ugly, but i'm trying to avoid extra cycles in the main execution loop */
2435            zend_bool build_filename_arg = 1;
2436
2437            if (!ptr->func || !ZEND_USER_CODE(ptr->func->common.type) || ptr->opline->opcode != ZEND_INCLUDE_OR_EVAL) {
2438                /* can happen when calling eval from a custom sapi */
2439                function_name = "unknown";
2440                build_filename_arg = 0;
2441            } else
2442            switch (ptr->opline->extended_value) {
2443                case ZEND_EVAL:
2444                    function_name = "eval";
2445                    build_filename_arg = 0;
2446                    break;
2447                case ZEND_INCLUDE:
2448                    function_name = "include";
2449                    break;
2450                case ZEND_REQUIRE:
2451                    function_name = "require";
2452                    break;
2453                case ZEND_INCLUDE_ONCE:
2454                    function_name = "include_once";
2455                    break;
2456                case ZEND_REQUIRE_ONCE:
2457                    function_name = "require_once";
2458                    break;
2459                default:
2460                    /* this can actually happen if you use debug_backtrace() in your error_handler and
2461                     * you're in the top-scope */
2462                    function_name = "unknown";
2463                    build_filename_arg = 0;
2464                    break;
2465            }
2466
2467            if (build_filename_arg && include_filename) {
2468                zval arg_array;
2469
2470                array_init(&arg_array);
2471
2472                /* include_filename always points to the last filename of the last last called-function.
2473                   if we have called include in the frame above - this is the file we have included.
2474                 */
2475
2476                add_next_index_string(&arg_array, (char*)include_filename);
2477                add_assoc_zval_ex(&stack_frame, "args", sizeof("args")-1, &arg_array);
2478            }
2479
2480            add_assoc_string_ex(&stack_frame, "function", sizeof("function")-1, (char*)function_name);
2481        }
2482
2483        zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &stack_frame);
2484
2485        include_filename = filename;
2486
2487        call = skip;
2488        ptr = skip->prev_execute_data;
2489    }
2490}
2491/* }}} */
2492
2493/* {{{ proto array debug_backtrace([int options[, int limit]])
2494   Return backtrace as array */
2495ZEND_FUNCTION(debug_backtrace)
2496{
2497    zend_long options = DEBUG_BACKTRACE_PROVIDE_OBJECT;
2498    zend_long limit = 0;
2499
2500    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ll", &options, &limit) == FAILURE) {
2501        return;
2502    }
2503
2504    zend_fetch_debug_backtrace(return_value, 1, options, limit TSRMLS_CC);
2505}
2506/* }}} */
2507
2508/* {{{ proto bool extension_loaded(string extension_name)
2509   Returns true if the named extension is loaded */
2510ZEND_FUNCTION(extension_loaded)
2511{
2512    char *extension_name;
2513    size_t extension_name_len;
2514    zend_string *lcname;
2515
2516    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &extension_name, &extension_name_len) == FAILURE) {
2517        return;
2518    }
2519
2520    lcname = zend_string_alloc(extension_name_len, 0);
2521    zend_str_tolower_copy(lcname->val, extension_name, extension_name_len);
2522    if (zend_hash_exists(&module_registry, lcname)) {
2523        RETVAL_TRUE;
2524    } else {
2525        RETVAL_FALSE;
2526    }
2527    zend_string_free(lcname);
2528}
2529/* }}} */
2530
2531/* {{{ proto array get_extension_funcs(string extension_name)
2532   Returns an array with the names of functions belonging to the named extension */
2533ZEND_FUNCTION(get_extension_funcs)
2534{
2535    char *extension_name;
2536    zend_string *lcname;
2537    size_t extension_name_len;
2538    int array;
2539    zend_module_entry *module;
2540    zend_function *zif;
2541
2542    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &extension_name, &extension_name_len) == FAILURE) {
2543        return;
2544    }
2545    if (strncasecmp(extension_name, "zend", sizeof("zend"))) {
2546        lcname = zend_string_alloc(extension_name_len, 0);
2547        zend_str_tolower_copy(lcname->val, extension_name, extension_name_len);
2548    } else {
2549        lcname = zend_string_init("core", sizeof("core")-1, 0);
2550    }
2551    module = zend_hash_find_ptr(&module_registry, lcname);
2552    zend_string_free(lcname);
2553    if (!module) {
2554        RETURN_FALSE;
2555    }
2556
2557    if (module->functions) {
2558        /* avoid BC break, if functions list is empty, will return an empty array */
2559        array_init(return_value);
2560        array = 1;
2561    } else {
2562        array = 0;
2563    }
2564
2565    ZEND_HASH_FOREACH_PTR(CG(function_table), zif) {
2566        if (zif->common.type == ZEND_INTERNAL_FUNCTION
2567            && zif->internal_function.module == module) {
2568            if (!array) {
2569                array_init(return_value);
2570                array = 1;
2571            }
2572            add_next_index_str(return_value, zend_string_copy(zif->common.function_name));
2573        }
2574    } ZEND_HASH_FOREACH_END();
2575
2576    if (!array) {
2577        RETURN_FALSE;
2578    }
2579}
2580/* }}} */
2581
2582/*
2583 * Local variables:
2584 * tab-width: 4
2585 * c-basic-offset: 4
2586 * indent-tabs-mode: t
2587 * End:
2588 */
2589