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(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(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 = EG(current_execute_data)->prev_execute_data;
404
405    if (ex->frame_kind == VM_FRAME_NESTED_FUNCTION || ex->frame_kind == VM_FRAME_TOP_FUNCTION) {
406        RETURN_LONG(ex->num_args);
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    int 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 = EG(current_execute_data)->prev_execute_data;
433    if (ex->frame_kind != VM_FRAME_NESTED_FUNCTION && ex->frame_kind != VM_FRAME_TOP_FUNCTION) {
434        zend_error(E_WARNING, "func_get_arg():  Called from the global scope - no function context");
435        RETURN_FALSE;
436    }
437
438    arg_count = ex->num_args;
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 && (ex->num_args > first_extra_arg)) {
450        arg = EX_VAR_NUM_2(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    int arg_count, first_extra_arg;
464    int i;
465    zend_execute_data *ex = EG(current_execute_data)->prev_execute_data;
466
467    if (ex->frame_kind != VM_FRAME_NESTED_FUNCTION && ex->frame_kind != VM_FRAME_TOP_FUNCTION) {
468        zend_error(E_WARNING, "func_get_args():  Called from the global scope - no function context");
469        RETURN_FALSE;
470    }
471
472    arg_count = ex->num_args;
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 (ex->num_args > 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 = EX_VAR_NUM_2(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
543/* {{{ proto int strcmp(string str1, string str2)
544   Binary safe string comparison */
545ZEND_FUNCTION(strcmp)
546{
547    zend_string *s1, *s2;
548
549    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SS", &s1, &s2) == FAILURE) {
550        return;
551    }
552
553    RETURN_LONG(zend_binary_strcmp(s1->val, s1->len, s2->val, s2->len));
554}
555/* }}} */
556
557
558/* {{{ proto int strncmp(string str1, string str2, int len)
559   Binary safe string comparison */
560ZEND_FUNCTION(strncmp)
561{
562    zend_string *s1, *s2;
563    zend_long len;
564
565    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SSl", &s1, &s2, &len) == FAILURE) {
566        return;
567    }
568
569    if (len < 0) {
570        zend_error(E_WARNING, "Length must be greater than or equal to 0");
571        RETURN_FALSE;
572    }
573
574    RETURN_LONG(zend_binary_strncmp(s1->val, s1->len, s2->val, s2->len, len));
575}
576/* }}} */
577
578
579/* {{{ proto int strcasecmp(string str1, string str2)
580   Binary safe case-insensitive string comparison */
581ZEND_FUNCTION(strcasecmp)
582{
583    zend_string *s1, *s2;
584
585    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SS", &s1, &s2) == FAILURE) {
586        return;
587    }
588
589    RETURN_LONG(zend_binary_strcasecmp(s1->val, s1->len, s2->val, s2->len));
590}
591/* }}} */
592
593
594/* {{{ proto int strncasecmp(string str1, string str2, int len)
595   Binary safe string comparison */
596ZEND_FUNCTION(strncasecmp)
597{
598    zend_string *s1, *s2;
599    zend_long len;
600
601    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SSl", &s1, &s2, &len) == FAILURE) {
602        return;
603    }
604
605    if (len < 0) {
606        zend_error(E_WARNING, "Length must be greater than or equal to 0");
607        RETURN_FALSE;
608    }
609
610    RETURN_LONG(zend_binary_strncasecmp(s1->val, s1->len, s2->val, s2->len, len));
611}
612/* }}} */
613
614
615/* {{{ proto array each(array arr)
616   Return the currently pointed key..value pair in the passed array, and advance the pointer to the next element */
617ZEND_FUNCTION(each)
618{
619    zval *array, *entry, tmp;
620    zend_ulong num_key;
621    HashTable *target_hash;
622    zend_string *key;
623
624    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &array) == FAILURE) {
625        return;
626    }
627
628    target_hash = HASH_OF(array);
629    if (!target_hash) {
630        zend_error(E_WARNING,"Variable passed to each() is not an array or object");
631        return;
632    }
633    while (1) {
634        entry = zend_hash_get_current_data(target_hash);
635        if (!entry) {
636            RETURN_FALSE;
637        } else if (Z_TYPE_P(entry) == IS_INDIRECT) {
638            entry = Z_INDIRECT_P(entry);
639            if (Z_TYPE_P(entry) == IS_UNDEF) {
640                zend_hash_move_forward(target_hash);
641                continue;
642            }
643        }
644        break;
645    }
646    array_init_size(return_value, 4);
647    zend_hash_real_init(Z_ARRVAL_P(return_value), 0);
648
649    /* add value elements */
650    if (Z_ISREF_P(entry)) {
651        ZVAL_DUP(&tmp, Z_REFVAL_P(entry));
652        entry = &tmp;
653        if (Z_REFCOUNTED_P(entry)) Z_ADDREF_P(entry);
654    } else {
655        if (Z_REFCOUNTED_P(entry)) Z_ADDREF_P(entry);
656        if (Z_REFCOUNTED_P(entry)) Z_ADDREF_P(entry);
657    }
658    zend_hash_index_add_new(Z_ARRVAL_P(return_value), 1, entry);
659    zend_hash_str_add_new(Z_ARRVAL_P(return_value), "value", sizeof("value")-1, entry);
660
661    /* add the key elements */
662    if (zend_hash_get_current_key(target_hash, &key, &num_key, 0) == HASH_KEY_IS_STRING) {
663        ZVAL_STR(&tmp, zend_string_copy(key));
664        if (Z_REFCOUNTED(tmp)) Z_ADDREF(tmp);
665    } else {
666        ZVAL_LONG(&tmp, num_key);
667    }
668    zend_hash_index_add_new(Z_ARRVAL_P(return_value), 0, &tmp);
669    zend_hash_str_add_new(Z_ARRVAL_P(return_value), "key", sizeof("key")-1, &tmp);
670    zend_hash_move_forward(target_hash);
671}
672/* }}} */
673
674
675/* {{{ proto int error_reporting([int new_error_level])
676   Return the current error_reporting level, and if an argument was passed - change to the new level */
677ZEND_FUNCTION(error_reporting)
678{
679    char *err;
680    size_t err_len;
681    int old_error_reporting;
682
683    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &err, &err_len) == FAILURE) {
684        return;
685    }
686
687    old_error_reporting = EG(error_reporting);
688    if(ZEND_NUM_ARGS() != 0) {
689        zend_string *key = zend_string_init("error_reporting", sizeof("error_reporting")-1, 0);
690        zend_alter_ini_entry(key, err, err_len, ZEND_INI_USER, ZEND_INI_STAGE_RUNTIME);
691        zend_string_release(key);
692    }
693
694    RETVAL_LONG(old_error_reporting);
695}
696/* }}} */
697
698
699/* {{{ proto bool define(string constant_name, mixed value, boolean case_insensitive=false)
700   Define a new constant */
701ZEND_FUNCTION(define)
702{
703    zend_string *name;
704    zval *val, val_free;
705    zend_bool non_cs = 0;
706    int case_sensitive = CONST_CS;
707    zend_constant c;
708
709#ifndef FAST_ZPP
710    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Sz|b", &name, &val, &non_cs) == FAILURE) {
711        return;
712    }
713#else
714    ZEND_PARSE_PARAMETERS_START(2, 3)
715        Z_PARAM_STR(name)
716        Z_PARAM_ZVAL(val)
717        Z_PARAM_OPTIONAL
718        Z_PARAM_BOOL(non_cs)
719    ZEND_PARSE_PARAMETERS_END();
720#endif
721
722    if(non_cs) {
723        case_sensitive = 0;
724    }
725
726    /* class constant, check if there is name and make sure class is valid & exists */
727    if (zend_memnstr(name->val, "::", sizeof("::") - 1, name->val + name->len)) {
728        zend_error(E_WARNING, "Class constants cannot be defined or redefined");
729        RETURN_FALSE;
730    }
731
732    ZVAL_UNDEF(&val_free);
733
734repeat:
735    switch (Z_TYPE_P(val)) {
736        case IS_LONG:
737        case IS_DOUBLE:
738        case IS_STRING:
739        case IS_FALSE:
740        case IS_TRUE:
741        case IS_RESOURCE:
742        case IS_NULL:
743            break;
744        case IS_OBJECT:
745            if (Z_TYPE(val_free) == IS_UNDEF) {
746                if (Z_OBJ_HT_P(val)->get) {
747                    zval rv;
748                    val = Z_OBJ_HT_P(val)->get(val, &rv TSRMLS_CC);
749                    ZVAL_COPY_VALUE(&val_free, val);
750                    goto repeat;
751                } else if (Z_OBJ_HT_P(val)->cast_object) {
752                    if (Z_OBJ_HT_P(val)->cast_object(val, &val_free, IS_STRING TSRMLS_CC) == SUCCESS) {
753                        val = &val_free;
754                        break;
755                    }
756                }
757            }
758            /* no break */
759        default:
760            zend_error(E_WARNING,"Constants may only evaluate to scalar values");
761            zval_ptr_dtor(&val_free);
762            RETURN_FALSE;
763    }
764
765    ZVAL_DUP(&c.value, val);
766    zval_ptr_dtor(&val_free);
767    c.flags = case_sensitive; /* non persistent */
768    c.name = zend_string_copy(name);
769    c.module_number = PHP_USER_CONSTANT;
770    if (zend_register_constant(&c TSRMLS_CC) == SUCCESS) {
771        RETURN_TRUE;
772    } else {
773        RETURN_FALSE;
774    }
775}
776/* }}} */
777
778
779/* {{{ proto bool defined(string constant_name)
780   Check whether a constant exists
781   Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
782ZEND_FUNCTION(defined)
783{
784    zend_string *name;
785
786#ifndef FAST_ZPP
787    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S", &name) == FAILURE) {
788        return;
789    }
790#else
791    ZEND_PARSE_PARAMETERS_START(1, 1)
792        Z_PARAM_STR(name)
793    ZEND_PARSE_PARAMETERS_END();
794#endif
795
796    if (zend_get_constant_ex(name, NULL, ZEND_FETCH_CLASS_SILENT TSRMLS_CC)) {
797        RETURN_TRUE;
798    } else {
799        RETURN_FALSE;
800    }
801}
802/* }}} */
803
804
805/* {{{ proto string get_class([object object])
806   Retrieves the class name */
807ZEND_FUNCTION(get_class)
808{
809    zval *obj = NULL;
810
811    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|o!", &obj) == FAILURE) {
812        RETURN_FALSE;
813    }
814
815    if (!obj) {
816        if (EG(scope)) {
817            RETURN_STR(zend_string_copy(EG(scope)->name));
818        } else {
819            zend_error(E_WARNING, "get_class() called without object from outside a class");
820            RETURN_FALSE;
821        }
822    }
823
824    RETURN_STR(zend_get_object_classname(Z_OBJ_P(obj) TSRMLS_CC));
825}
826/* }}} */
827
828
829/* {{{ proto string get_called_class()
830   Retrieves the "Late Static Binding" class name */
831ZEND_FUNCTION(get_called_class)
832{
833    if (zend_parse_parameters_none() == FAILURE) {
834        return;
835    }
836
837    if (EG(current_execute_data)->called_scope) {
838        RETURN_STR(zend_string_copy(EG(current_execute_data)->called_scope->name));
839    } else if (!EG(scope))  {
840        zend_error(E_WARNING, "get_called_class() called from outside a class");
841    }
842    RETURN_FALSE;
843}
844/* }}} */
845
846
847/* {{{ proto string get_parent_class([mixed object])
848   Retrieves the parent class name for object or class or current scope. */
849ZEND_FUNCTION(get_parent_class)
850{
851    zval *arg;
852    zend_class_entry *ce = NULL;
853    zend_string *name;
854
855    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &arg) == FAILURE) {
856        return;
857    }
858
859    if (!ZEND_NUM_ARGS()) {
860        ce = EG(scope);
861        if (ce && ce->parent) {
862            RETURN_STR(zend_string_copy(ce->parent->name));
863        } else {
864            RETURN_FALSE;
865        }
866    }
867
868    if (Z_TYPE_P(arg) == IS_OBJECT) {
869        if (Z_OBJ_HT_P(arg)->get_class_name
870            && (name = Z_OBJ_HT_P(arg)->get_class_name(Z_OBJ_P(arg), 1 TSRMLS_CC)) != NULL) {
871            RETURN_STR(name);
872        } else {
873            ce = zend_get_class_entry(Z_OBJ_P(arg) TSRMLS_CC);
874        }
875    } else if (Z_TYPE_P(arg) == IS_STRING) {
876        ce = zend_lookup_class(Z_STR_P(arg) TSRMLS_CC);
877    }
878
879    if (ce && ce->parent) {
880        RETURN_STR(zend_string_copy(ce->parent->name));
881    } else {
882        RETURN_FALSE;
883    }
884}
885/* }}} */
886
887
888static void is_a_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool only_subclass)
889{
890    zval *obj;
891    zend_string *class_name;
892    zend_class_entry *instance_ce;
893    zend_class_entry *ce;
894    zend_bool allow_string = only_subclass;
895    zend_bool retval;
896
897#ifndef FAST_ZPP
898    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zS|b", &obj, &class_name, &allow_string) == FAILURE) {
899        return;
900    }
901#else
902    ZEND_PARSE_PARAMETERS_START(2, 3)
903        Z_PARAM_ZVAL(obj)
904        Z_PARAM_STR(class_name)
905        Z_PARAM_OPTIONAL
906        Z_PARAM_BOOL(allow_string)
907    ZEND_PARSE_PARAMETERS_END();
908#endif
909    /*
910     * allow_string - is_a default is no, is_subclass_of is yes.
911     *   if it's allowed, then the autoloader will be called if the class does not exist.
912     *   default behaviour is different, as 'is_a' used to be used to test mixed return values
913     *   and there is no easy way to deprecate this.
914     */
915
916    if (allow_string && Z_TYPE_P(obj) == IS_STRING) {
917        instance_ce = zend_lookup_class(Z_STR_P(obj) TSRMLS_CC);
918        if (!instance_ce) {
919            RETURN_FALSE;
920        }
921    } else if (Z_TYPE_P(obj) == IS_OBJECT && HAS_CLASS_ENTRY(*obj)) {
922        instance_ce = Z_OBJCE_P(obj);
923    } else {
924        RETURN_FALSE;
925    }
926
927    ce = zend_lookup_class_ex(class_name, NULL, 0 TSRMLS_CC);
928    if (!ce) {
929        retval = 0;
930    } else {
931        if (only_subclass && instance_ce == ce) {
932            retval = 0;
933        } else {
934            retval = instanceof_function(instance_ce, ce TSRMLS_CC);
935        }
936    }
937
938    RETURN_BOOL(retval);
939}
940
941
942/* {{{ proto bool is_subclass_of(mixed object_or_string, string class_name [, bool allow_string=true])
943   Returns true if the object has this class as one of its parents */
944ZEND_FUNCTION(is_subclass_of)
945{
946    is_a_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
947}
948/* }}} */
949
950
951/* {{{ proto bool is_a(mixed object_or_string, string class_name [, bool allow_string=false])
952   Returns true if the first argument is an object and is this class or has this class as one of its parents, */
953ZEND_FUNCTION(is_a)
954{
955    is_a_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
956}
957/* }}} */
958
959
960/* {{{ add_class_vars */
961static void add_class_vars(zend_class_entry *ce, int statics, zval *return_value TSRMLS_DC)
962{
963    zend_property_info *prop_info;
964    zval *prop, prop_copy;
965    zend_string *key;
966
967    ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->properties_info, key, prop_info) {
968        if (((prop_info->flags & ZEND_ACC_SHADOW) &&
969             prop_info->ce != EG(scope)) ||
970            ((prop_info->flags & ZEND_ACC_PROTECTED) &&
971             !zend_check_protected(prop_info->ce, EG(scope))) ||
972            ((prop_info->flags & ZEND_ACC_PRIVATE) &&
973              ce != EG(scope) &&
974              prop_info->ce != EG(scope))) {
975            continue;
976        }
977        prop = NULL;
978        if (prop_info->offset >= 0) {
979            if (statics && (prop_info->flags & ZEND_ACC_STATIC) != 0) {
980                prop = &ce->default_static_members_table[prop_info->offset];
981            } else if (!statics && (prop_info->flags & ZEND_ACC_STATIC) == 0) {
982                prop = &ce->default_properties_table[prop_info->offset];
983            }
984        }
985        if (!prop || Z_TYPE_P(prop) == IS_UNDEF) {
986            continue;
987        }
988
989        /* copy: enforce read only access */
990        ZVAL_DEREF(prop);
991        if (UNEXPECTED(Z_COPYABLE_P(prop))) {
992            ZVAL_DUP(&prop_copy, prop);
993            prop = &prop_copy;
994        }
995
996        /* this is necessary to make it able to work with default array
997         * properties, returned to user */
998        if (Z_OPT_CONSTANT_P(prop)) {
999            zval_update_constant(prop, 0 TSRMLS_CC);
1000        }
1001
1002        zend_hash_add_new(Z_ARRVAL_P(return_value), key, prop);
1003    } ZEND_HASH_FOREACH_END();
1004}
1005/* }}} */
1006
1007
1008/* {{{ proto array get_class_vars(string class_name)
1009   Returns an array of default properties of the class. */
1010ZEND_FUNCTION(get_class_vars)
1011{
1012    zend_string *class_name;
1013    zend_class_entry *ce;
1014
1015    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S", &class_name) == FAILURE) {
1016        return;
1017    }
1018
1019    ce = zend_lookup_class(class_name TSRMLS_CC);
1020    if (!ce) {
1021        RETURN_FALSE;
1022    } else {
1023        array_init(return_value);
1024        zend_update_class_constants(ce TSRMLS_CC);
1025        add_class_vars(ce, 0, return_value TSRMLS_CC);
1026        add_class_vars(ce, 1, return_value TSRMLS_CC);
1027    }
1028}
1029/* }}} */
1030
1031
1032/* {{{ proto array get_object_vars(object obj)
1033   Returns an array of object properties */
1034ZEND_FUNCTION(get_object_vars)
1035{
1036    zval *obj;
1037    zval *value;
1038    HashTable *properties;
1039    zend_string *key;
1040    const char *prop_name, *class_name;
1041    uint prop_len;
1042    zend_object *zobj;
1043
1044#ifndef FAST_ZPP
1045    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &obj) == FAILURE) {
1046        return;
1047    }
1048#else
1049    ZEND_PARSE_PARAMETERS_START(1, 1)
1050        Z_PARAM_OBJECT(obj)
1051    ZEND_PARSE_PARAMETERS_END();
1052#endif
1053
1054    if (Z_OBJ_HT_P(obj)->get_properties == NULL) {
1055        RETURN_FALSE;
1056    }
1057
1058    properties = Z_OBJ_HT_P(obj)->get_properties(obj TSRMLS_CC);
1059
1060    if (properties == NULL) {
1061        RETURN_FALSE;
1062    }
1063
1064    zobj = Z_OBJ_P(obj);
1065
1066    array_init(return_value);
1067
1068    ZEND_HASH_FOREACH_STR_KEY_VAL_IND(properties, key, value) {
1069        if (key) {
1070            if (zend_check_property_access(zobj, key TSRMLS_CC) == SUCCESS) {
1071                /* Not separating references */
1072                if (Z_REFCOUNTED_P(value)) Z_ADDREF_P(value);
1073                if (key->val[0] == 0) {
1074                    zend_unmangle_property_name_ex(key->val, key->len, &class_name, &prop_name, (int*) &prop_len);
1075                    zend_hash_str_add_new(Z_ARRVAL_P(return_value), prop_name, prop_len, value);
1076                } else {
1077                    zend_hash_add_new(Z_ARRVAL_P(return_value), key, value);
1078                }
1079            }
1080        }
1081    } ZEND_HASH_FOREACH_END();
1082}
1083/* }}} */
1084
1085static int same_name(const char *key, const char *name, uint32_t name_len)
1086{
1087    char *lcname = zend_str_tolower_dup(name, name_len);
1088    int ret = memcmp(lcname, key, name_len) == 0;
1089    efree(lcname);
1090    return ret;
1091}
1092
1093/* {{{ proto array get_class_methods(mixed class)
1094   Returns an array of method names for class or class instance. */
1095ZEND_FUNCTION(get_class_methods)
1096{
1097    zval *klass;
1098    zval method_name;
1099    zend_class_entry *ce = NULL;
1100    zend_function *mptr;
1101    zend_string *key;
1102
1103    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &klass) == FAILURE) {
1104        return;
1105    }
1106
1107    if (Z_TYPE_P(klass) == IS_OBJECT) {
1108        /* TBI!! new object handlers */
1109        if (!HAS_CLASS_ENTRY(*klass)) {
1110            RETURN_FALSE;
1111        }
1112        ce = Z_OBJCE_P(klass);
1113    } else if (Z_TYPE_P(klass) == IS_STRING) {
1114        ce = zend_lookup_class(Z_STR_P(klass) TSRMLS_CC);
1115    }
1116
1117    if (!ce) {
1118        RETURN_NULL();
1119    }
1120
1121    array_init(return_value);
1122
1123    ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->function_table, key, mptr) {
1124
1125        if ((mptr->common.fn_flags & ZEND_ACC_PUBLIC)
1126         || (EG(scope) &&
1127             (((mptr->common.fn_flags & ZEND_ACC_PROTECTED) &&
1128               zend_check_protected(mptr->common.scope, EG(scope)))
1129           || ((mptr->common.fn_flags & ZEND_ACC_PRIVATE) &&
1130               EG(scope) == mptr->common.scope)))) {
1131            uint len = mptr->common.function_name->len;
1132
1133            /* Do not display old-style inherited constructors */
1134            if (!key) {
1135                ZVAL_STR(&method_name, zend_string_copy(mptr->common.function_name));
1136                zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &method_name);
1137            } else if ((mptr->common.fn_flags & ZEND_ACC_CTOR) == 0 ||
1138                mptr->common.scope == ce ||
1139                zend_binary_strcasecmp(key->val, key->len, mptr->common.function_name->val, len) == 0) {
1140
1141                if (mptr->type == ZEND_USER_FUNCTION &&
1142                    *mptr->op_array.refcount > 1 &&
1143                    (len != key->len ||
1144                     !same_name(key->val, mptr->common.function_name->val, len))) {
1145                    ZVAL_STR(&method_name, zend_string_copy(zend_find_alias_name(mptr->common.scope, key)));
1146                    zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &method_name);
1147                } else {
1148                    ZVAL_STR(&method_name, zend_string_copy(mptr->common.function_name));
1149                    zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &method_name);
1150                }
1151            }
1152        }
1153    } ZEND_HASH_FOREACH_END();
1154}
1155/* }}} */
1156
1157
1158/* {{{ proto bool method_exists(object object, string method)
1159   Checks if the class method exists */
1160ZEND_FUNCTION(method_exists)
1161{
1162    zval *klass;
1163    zend_string *method_name;
1164    zend_string *lcname;
1165    zend_class_entry * ce;
1166
1167#ifndef FAST_ZPP
1168    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zS", &klass, &method_name) == FAILURE) {
1169        return;
1170    }
1171#else
1172    ZEND_PARSE_PARAMETERS_START(2, 2)
1173        Z_PARAM_ZVAL(klass)
1174        Z_PARAM_STR(method_name)
1175    ZEND_PARSE_PARAMETERS_END();
1176#endif
1177    if (Z_TYPE_P(klass) == IS_OBJECT) {
1178        ce = Z_OBJCE_P(klass);
1179    } else if (Z_TYPE_P(klass) == IS_STRING) {
1180        if ((ce = zend_lookup_class(Z_STR_P(klass) TSRMLS_CC)) == NULL) {
1181            RETURN_FALSE;
1182        }
1183    } else {
1184        RETURN_FALSE;
1185    }
1186
1187    lcname = zend_string_alloc(method_name->len, 0);
1188    zend_str_tolower_copy(lcname->val, method_name->val, method_name->len);
1189    if (zend_hash_exists(&ce->function_table, lcname)) {
1190        zend_string_free(lcname);
1191        RETURN_TRUE;
1192    } else {
1193        union _zend_function *func = NULL;
1194
1195        if (Z_TYPE_P(klass) == IS_OBJECT
1196        && Z_OBJ_HT_P(klass)->get_method != NULL
1197        && (func = Z_OBJ_HT_P(klass)->get_method(&Z_OBJ_P(klass), method_name, NULL TSRMLS_CC)) != NULL
1198        ) {
1199            if (func->type == ZEND_INTERNAL_FUNCTION
1200            && (func->common.fn_flags & ZEND_ACC_CALL_VIA_HANDLER) != 0
1201            ) {
1202                /* Returns true to the fake Closure's __invoke */
1203                RETVAL_BOOL(func->common.scope == zend_ce_closure
1204                    && zend_string_equals_literal(method_name, ZEND_INVOKE_FUNC_NAME));
1205
1206                zend_string_free(lcname);
1207                zend_string_release(func->common.function_name);
1208                efree(func);
1209                return;
1210            }
1211            efree(lcname);
1212            RETURN_TRUE;
1213        }
1214    }
1215    efree(lcname);
1216    RETURN_FALSE;
1217}
1218/* }}} */
1219
1220/* {{{ proto bool property_exists(mixed object_or_class, string property_name)
1221   Checks if the object or class has a property */
1222ZEND_FUNCTION(property_exists)
1223{
1224    zval *object;
1225    zend_string *property;
1226    zend_class_entry *ce;
1227    zend_property_info *property_info;
1228    zval property_z;
1229
1230    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zS", &object, &property) == FAILURE) {
1231        return;
1232    }
1233
1234    if (property == NULL) {
1235        RETURN_FALSE;
1236    }
1237
1238    if (Z_TYPE_P(object) == IS_STRING) {
1239        ce = zend_lookup_class(Z_STR_P(object) TSRMLS_CC);
1240        if (!ce) {
1241            RETURN_FALSE;
1242        }
1243    } else if (Z_TYPE_P(object) == IS_OBJECT) {
1244        ce = Z_OBJCE_P(object);
1245    } else {
1246        zend_error(E_WARNING, "First parameter must either be an object or the name of an existing class");
1247        RETURN_NULL();
1248    }
1249
1250    if ((property_info = zend_hash_find_ptr(&ce->properties_info, property)) != NULL
1251        && (property_info->flags & ZEND_ACC_SHADOW) == 0) {
1252        RETURN_TRUE;
1253    }
1254
1255    ZVAL_STR(&property_z, property);
1256
1257    if (Z_TYPE_P(object) ==  IS_OBJECT &&
1258        Z_OBJ_HANDLER_P(object, has_property) &&
1259        Z_OBJ_HANDLER_P(object, has_property)(object, &property_z, 2, NULL TSRMLS_CC)) {
1260        RETURN_TRUE;
1261    }
1262    RETURN_FALSE;
1263}
1264/* }}} */
1265
1266
1267/* {{{ proto bool class_exists(string classname [, bool autoload])
1268   Checks if the class exists */
1269ZEND_FUNCTION(class_exists)
1270{
1271    zend_string *class_name;
1272    zend_string *lc_name;
1273    zend_class_entry *ce;
1274    zend_bool autoload = 1;
1275
1276#ifndef FAST_ZPP
1277    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S|b", &class_name, &autoload) == FAILURE) {
1278        return;
1279    }
1280#else
1281    ZEND_PARSE_PARAMETERS_START(1, 2)
1282        Z_PARAM_STR(class_name)
1283        Z_PARAM_OPTIONAL
1284        Z_PARAM_BOOL(autoload)
1285    ZEND_PARSE_PARAMETERS_END();
1286#endif
1287
1288    if (!autoload) {
1289        if (class_name->val[0] == '\\') {
1290            /* Ignore leading "\" */
1291            lc_name = zend_string_alloc(class_name->len - 1, 0);
1292            zend_str_tolower_copy(lc_name->val, class_name->val + 1, class_name->len - 1);
1293        } else {
1294            lc_name = zend_string_alloc(class_name->len, 0);
1295            zend_str_tolower_copy(lc_name->val, class_name->val, class_name->len);
1296        }
1297        ce = zend_hash_find_ptr(EG(class_table), lc_name);
1298        zend_string_free(lc_name);
1299        RETURN_BOOL(ce && !((ce->ce_flags & (ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT)) > ZEND_ACC_EXPLICIT_ABSTRACT_CLASS));
1300    }
1301
1302    ce = zend_lookup_class(class_name TSRMLS_CC);
1303    if (ce) {
1304        RETURN_BOOL((ce->ce_flags & (ZEND_ACC_INTERFACE | (ZEND_ACC_TRAIT - ZEND_ACC_EXPLICIT_ABSTRACT_CLASS))) == 0);
1305    } else {
1306        RETURN_FALSE;
1307    }
1308}
1309/* }}} */
1310
1311/* {{{ proto bool interface_exists(string classname [, bool autoload])
1312   Checks if the class exists */
1313ZEND_FUNCTION(interface_exists)
1314{
1315    zend_string *iface_name, *lc_name;
1316    zend_class_entry *ce;
1317    zend_bool autoload = 1;
1318
1319#ifndef FAST_ZPP
1320    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S|b", &iface_name, &autoload) == FAILURE) {
1321        return;
1322    }
1323#else
1324    ZEND_PARSE_PARAMETERS_START(1, 2)
1325        Z_PARAM_STR(iface_name)
1326        Z_PARAM_OPTIONAL
1327        Z_PARAM_BOOL(autoload)
1328    ZEND_PARSE_PARAMETERS_END();
1329#endif
1330
1331    if (!autoload) {
1332        if (iface_name->val[0] == '\\') {
1333            /* Ignore leading "\" */
1334            lc_name = zend_string_alloc(iface_name->len - 1, 0);
1335            zend_str_tolower_copy(lc_name->val, iface_name->val + 1, iface_name->len - 1);
1336        } else {
1337            lc_name = zend_string_alloc(iface_name->len, 0);
1338            zend_str_tolower_copy(lc_name->val, iface_name->val, iface_name->len);
1339        }
1340        ce = zend_hash_find_ptr(EG(class_table), lc_name);
1341        zend_string_free(lc_name);
1342        RETURN_BOOL(ce && ce->ce_flags & ZEND_ACC_INTERFACE);
1343    }
1344
1345    ce = zend_lookup_class(iface_name TSRMLS_CC);
1346    if (ce) {
1347        RETURN_BOOL((ce->ce_flags & ZEND_ACC_INTERFACE) > 0);
1348    } else {
1349        RETURN_FALSE;
1350    }
1351}
1352/* }}} */
1353
1354/* {{{ proto bool trait_exists(string traitname [, bool autoload])
1355 Checks if the trait exists */
1356ZEND_FUNCTION(trait_exists)
1357{
1358    zend_string *trait_name, *lc_name;
1359    zend_class_entry *ce;
1360    zend_bool autoload = 1;
1361
1362#ifndef FAST_ZPP
1363    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S|b", &trait_name, &autoload) == FAILURE) {
1364        return;
1365    }
1366#else
1367    ZEND_PARSE_PARAMETERS_START(1, 2)
1368        Z_PARAM_STR(trait_name)
1369        Z_PARAM_OPTIONAL
1370        Z_PARAM_BOOL(autoload)
1371    ZEND_PARSE_PARAMETERS_END();
1372#endif
1373
1374    if (!autoload) {
1375        if (trait_name->val[0] == '\\') {
1376            /* Ignore leading "\" */
1377            lc_name = zend_string_alloc(trait_name->len - 1, 0);
1378            zend_str_tolower_copy(lc_name->val, trait_name->val + 1, trait_name->len - 1);
1379        } else {
1380            lc_name = zend_string_alloc(trait_name->len, 0);
1381            zend_str_tolower_copy(lc_name->val, trait_name->val, trait_name->len);
1382        }
1383        ce = zend_hash_find_ptr(EG(class_table), lc_name);
1384        zend_string_free(lc_name);
1385        RETURN_BOOL(ce && ((ce->ce_flags & ZEND_ACC_TRAIT) > ZEND_ACC_EXPLICIT_ABSTRACT_CLASS));
1386    }
1387
1388    ce = zend_lookup_class(trait_name TSRMLS_CC);
1389    if (ce) {
1390        RETURN_BOOL((ce->ce_flags & ZEND_ACC_TRAIT) > ZEND_ACC_EXPLICIT_ABSTRACT_CLASS);
1391    } else {
1392        RETURN_FALSE;
1393    }
1394}
1395/* }}} */
1396
1397
1398/* {{{ proto bool function_exists(string function_name)
1399   Checks if the function exists */
1400ZEND_FUNCTION(function_exists)
1401{
1402    char *name;
1403    size_t name_len;
1404    zend_function *func;
1405    zend_string *lcname;
1406
1407#ifndef FAST_ZPP
1408    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) {
1409        return;
1410    }
1411#else
1412    ZEND_PARSE_PARAMETERS_START(1, 1)
1413        Z_PARAM_STRING(name, name_len)
1414    ZEND_PARSE_PARAMETERS_END();
1415#endif
1416
1417    if (name[0] == '\\') {
1418        /* Ignore leading "\" */
1419        lcname = zend_string_alloc(name_len - 1, 0);
1420        zend_str_tolower_copy(lcname->val, name + 1, name_len - 1);
1421    } else {
1422        lcname = zend_string_alloc(name_len, 0);
1423        zend_str_tolower_copy(lcname->val, name, name_len);
1424    }
1425
1426    func = zend_hash_find_ptr(EG(function_table), lcname);
1427    zend_string_free(lcname);
1428
1429    /*
1430     * A bit of a hack, but not a bad one: we see if the handler of the function
1431     * is actually one that displays "function is disabled" message.
1432     */
1433    RETURN_BOOL(func && (func->type != ZEND_INTERNAL_FUNCTION ||
1434        func->internal_function.handler != zif_display_disabled_function));
1435}
1436/* }}} */
1437
1438/* {{{ proto bool class_alias(string user_class_name , string alias_name [, bool autoload])
1439   Creates an alias for user defined class */
1440ZEND_FUNCTION(class_alias)
1441{
1442    zend_string *class_name;
1443    char *alias_name;
1444    zend_class_entry *ce;
1445    size_t alias_name_len;
1446    zend_bool autoload = 1;
1447
1448    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Ss|b", &class_name, &alias_name, &alias_name_len, &autoload) == FAILURE) {
1449        return;
1450    }
1451
1452    ce = zend_lookup_class_ex(class_name, NULL, autoload TSRMLS_CC);
1453
1454    if (ce) {
1455        if (ce->type == ZEND_USER_CLASS) {
1456            if (zend_register_class_alias_ex(alias_name, alias_name_len, ce TSRMLS_CC) == SUCCESS) {
1457                RETURN_TRUE;
1458            } else {
1459                zend_error(E_WARNING, "Cannot redeclare class %s", alias_name);
1460                RETURN_FALSE;
1461            }
1462        } else {
1463            zend_error(E_WARNING, "First argument of class_alias() must be a name of user defined class");
1464            RETURN_FALSE;
1465        }
1466    } else {
1467        zend_error(E_WARNING, "Class '%s' not found", class_name->val);
1468        RETURN_FALSE;
1469    }
1470}
1471/* }}} */
1472
1473#if ZEND_DEBUG
1474/* {{{ proto void leak(int num_bytes=3)
1475   Cause an intentional memory leak, for testing/debugging purposes */
1476ZEND_FUNCTION(leak)
1477{
1478    zend_long leakbytes=3;
1479
1480    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &leakbytes) == FAILURE) {
1481        return;
1482    }
1483
1484    emalloc(leakbytes);
1485}
1486/* }}} */
1487
1488/* {{{ proto leak_variable(mixed variable [, bool leak_data]) */
1489ZEND_FUNCTION(leak_variable)
1490{
1491    zval *zv;
1492    zend_bool leak_data = 0;
1493
1494    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|b", &zv, &leak_data) == FAILURE) {
1495        return;
1496    }
1497
1498    if (!leak_data) {
1499        Z_ADDREF_P(zv);
1500    } else if (Z_TYPE_P(zv) == IS_RESOURCE) {
1501        Z_ADDREF_P(zv);
1502    } else if (Z_TYPE_P(zv) == IS_OBJECT) {
1503        Z_ADDREF_P(zv);
1504    } else {
1505        zend_error(E_WARNING, "Leaking non-zval data is only applicable to resources and objects");
1506    }
1507}
1508/* }}} */
1509
1510
1511#ifdef ZEND_TEST_EXCEPTIONS
1512ZEND_FUNCTION(crash)
1513{
1514    char *nowhere=NULL;
1515
1516    memcpy(nowhere, "something", sizeof("something"));
1517}
1518#endif
1519
1520#endif /* ZEND_DEBUG */
1521
1522/* {{{ proto array get_included_files(void)
1523   Returns an array with the file names that were include_once()'d */
1524ZEND_FUNCTION(get_included_files)
1525{
1526    zend_string *entry;
1527
1528    if (zend_parse_parameters_none() == FAILURE) {
1529        return;
1530    }
1531
1532    array_init(return_value);
1533    ZEND_HASH_FOREACH_STR_KEY(&EG(included_files), entry) {
1534        if (entry) {
1535            add_next_index_str(return_value, zend_string_copy(entry));
1536        }
1537    } ZEND_HASH_FOREACH_END();
1538}
1539/* }}} */
1540
1541
1542/* {{{ proto void trigger_error(string message [, int error_type])
1543   Generates a user-level error/warning/notice message */
1544ZEND_FUNCTION(trigger_error)
1545{
1546    zend_long error_type = E_USER_NOTICE;
1547    char *message;
1548    size_t message_len;
1549
1550    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &message, &message_len, &error_type) == FAILURE) {
1551        return;
1552    }
1553
1554    switch (error_type) {
1555        case E_USER_ERROR:
1556        case E_USER_WARNING:
1557        case E_USER_NOTICE:
1558        case E_USER_DEPRECATED:
1559            break;
1560        default:
1561            zend_error(E_WARNING, "Invalid error type specified");
1562            RETURN_FALSE;
1563            break;
1564    }
1565
1566    zend_error((int)error_type, "%s", message);
1567    RETURN_TRUE;
1568}
1569/* }}} */
1570
1571
1572/* {{{ proto string set_error_handler(string error_handler [, int error_types])
1573   Sets a user-defined error handler function.  Returns the previously defined error handler, or false on error */
1574ZEND_FUNCTION(set_error_handler)
1575{
1576    zval *error_handler;
1577    zend_string *error_handler_name = NULL;
1578    zend_long error_type = E_ALL;
1579
1580    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &error_handler, &error_type) == FAILURE) {
1581        return;
1582    }
1583
1584    if (Z_TYPE_P(error_handler) != IS_NULL) { /* NULL == unset */
1585        if (!zend_is_callable(error_handler, 0, &error_handler_name TSRMLS_CC)) {
1586            zend_error(E_WARNING, "%s() expects the argument (%s) to be a valid callback",
1587                       get_active_function_name(TSRMLS_C), error_handler_name?error_handler_name->val:"unknown");
1588            zend_string_release(error_handler_name);
1589            return;
1590        }
1591        zend_string_release(error_handler_name);
1592    }
1593
1594    if (Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
1595        RETVAL_ZVAL(&EG(user_error_handler), 1, 0);
1596
1597        zend_stack_push(&EG(user_error_handlers_error_reporting), &EG(user_error_handler_error_reporting));
1598        zend_stack_push(&EG(user_error_handlers), &EG(user_error_handler));
1599    }
1600
1601    if (Z_TYPE_P(error_handler) == IS_NULL) { /* unset user-defined handler */
1602        ZVAL_UNDEF(&EG(user_error_handler));
1603        return;
1604    }
1605
1606    ZVAL_DUP(&EG(user_error_handler), error_handler);
1607    EG(user_error_handler_error_reporting) = (int)error_type;
1608}
1609/* }}} */
1610
1611
1612/* {{{ proto void restore_error_handler(void)
1613   Restores the previously defined error handler function */
1614ZEND_FUNCTION(restore_error_handler)
1615{
1616    if (Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
1617        zval zeh;
1618
1619        ZVAL_COPY_VALUE(&zeh, &EG(user_error_handler));
1620        ZVAL_UNDEF(&EG(user_error_handler));
1621        zval_ptr_dtor(&zeh);
1622    }
1623
1624    if (zend_stack_is_empty(&EG(user_error_handlers))) {
1625        ZVAL_UNDEF(&EG(user_error_handler));
1626    } else {
1627        zval *tmp;
1628        EG(user_error_handler_error_reporting) = zend_stack_int_top(&EG(user_error_handlers_error_reporting));
1629        zend_stack_del_top(&EG(user_error_handlers_error_reporting));
1630        tmp = zend_stack_top(&EG(user_error_handlers));
1631        ZVAL_COPY_VALUE(&EG(user_error_handler), tmp);
1632        zend_stack_del_top(&EG(user_error_handlers));
1633    }
1634    RETURN_TRUE;
1635}
1636/* }}} */
1637
1638
1639/* {{{ proto string set_exception_handler(callable exception_handler)
1640   Sets a user-defined exception handler function.  Returns the previously defined exception handler, or false on error */
1641ZEND_FUNCTION(set_exception_handler)
1642{
1643    zval *exception_handler;
1644    zend_string *exception_handler_name = NULL;
1645
1646    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &exception_handler) == FAILURE) {
1647        return;
1648    }
1649
1650    if (Z_TYPE_P(exception_handler) != IS_NULL) { /* NULL == unset */
1651        if (!zend_is_callable(exception_handler, 0, &exception_handler_name TSRMLS_CC)) {
1652            zend_error(E_WARNING, "%s() expects the argument (%s) to be a valid callback",
1653                       get_active_function_name(TSRMLS_C), exception_handler_name?exception_handler_name->val:"unknown");
1654            zend_string_release(exception_handler_name);
1655            return;
1656        }
1657        zend_string_release(exception_handler_name);
1658    }
1659
1660    if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
1661        RETVAL_ZVAL(&EG(user_exception_handler), 1, 0);
1662
1663        zend_stack_push(&EG(user_exception_handlers), &EG(user_exception_handler));
1664    }
1665
1666    if (Z_TYPE_P(exception_handler) == IS_NULL) { /* unset user-defined handler */
1667        ZVAL_UNDEF(&EG(user_exception_handler));
1668        return;
1669    }
1670
1671    ZVAL_DUP(&EG(user_exception_handler), exception_handler);
1672}
1673/* }}} */
1674
1675
1676/* {{{ proto void restore_exception_handler(void)
1677   Restores the previously defined exception handler function */
1678ZEND_FUNCTION(restore_exception_handler)
1679{
1680    if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
1681        zval_ptr_dtor(&EG(user_exception_handler));
1682    }
1683    if (zend_stack_is_empty(&EG(user_exception_handlers))) {
1684        ZVAL_UNDEF(&EG(user_exception_handler));
1685    } else {
1686        zval *tmp = zend_stack_top(&EG(user_exception_handlers));
1687        ZVAL_COPY_VALUE(&EG(user_exception_handler), tmp);
1688        zend_stack_del_top(&EG(user_exception_handlers));
1689    }
1690    RETURN_TRUE;
1691}
1692/* }}} */
1693
1694static int copy_class_or_interface_name(zval *el TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
1695{
1696    zend_class_entry *ce = (zend_class_entry *)Z_PTR_P(el);
1697    zval *array = va_arg(args, zval *);
1698    uint32_t mask = va_arg(args, uint32_t);
1699    uint32_t comply = va_arg(args, uint32_t);
1700    uint32_t comply_mask = (comply)? mask:0;
1701
1702    if ((hash_key->key && hash_key->key->val[0] != 0)
1703        && (comply_mask == (ce->ce_flags & mask))) {
1704        if (ce->refcount > 1 &&
1705            (ce->name->len != hash_key->key->len ||
1706             !same_name(hash_key->key->val, ce->name->val, ce->name->len))) {
1707            add_next_index_str(array, zend_string_copy(hash_key->key));
1708        } else {
1709            add_next_index_str(array, zend_string_copy(ce->name));
1710        }
1711    }
1712    return ZEND_HASH_APPLY_KEEP;
1713}
1714
1715/* {{{ proto array get_declared_traits()
1716   Returns an array of all declared traits. */
1717ZEND_FUNCTION(get_declared_traits)
1718{
1719    uint32_t mask = ZEND_ACC_TRAIT;
1720    uint32_t comply = 1;
1721
1722    if (zend_parse_parameters_none() == FAILURE) {
1723        return;
1724    }
1725
1726    array_init(return_value);
1727    zend_hash_apply_with_arguments(EG(class_table) TSRMLS_CC, copy_class_or_interface_name, 3, return_value, mask, comply);
1728}
1729/* }}} */
1730
1731
1732/* {{{ proto array get_declared_classes()
1733   Returns an array of all declared classes. */
1734ZEND_FUNCTION(get_declared_classes)
1735{
1736    uint32_t mask = ZEND_ACC_INTERFACE | (ZEND_ACC_TRAIT & ~ZEND_ACC_EXPLICIT_ABSTRACT_CLASS);
1737    uint32_t comply = 0;
1738
1739    if (zend_parse_parameters_none() == FAILURE) {
1740        return;
1741    }
1742
1743    array_init(return_value);
1744    zend_hash_apply_with_arguments(EG(class_table) TSRMLS_CC, copy_class_or_interface_name, 3, return_value, mask, comply);
1745}
1746/* }}} */
1747
1748/* {{{ proto array get_declared_interfaces()
1749   Returns an array of all declared interfaces. */
1750ZEND_FUNCTION(get_declared_interfaces)
1751{
1752    uint32_t mask = ZEND_ACC_INTERFACE;
1753    uint32_t comply = 1;
1754
1755    if (zend_parse_parameters_none() == FAILURE) {
1756        return;
1757    }
1758
1759    array_init(return_value);
1760    zend_hash_apply_with_arguments(EG(class_table) TSRMLS_CC, copy_class_or_interface_name, 3, return_value, mask, comply);
1761}
1762/* }}} */
1763
1764
1765static int copy_function_name(zval *zv TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
1766{
1767    zend_function *func = Z_PTR_P(zv);
1768    zval *internal_ar = va_arg(args, zval *),
1769         *user_ar     = va_arg(args, zval *);
1770
1771    if (hash_key->key == NULL || hash_key->key->val[0] == 0) {
1772        return 0;
1773    }
1774
1775    if (func->type == ZEND_INTERNAL_FUNCTION) {
1776        add_next_index_str(internal_ar, zend_string_copy(hash_key->key));
1777    } else if (func->type == ZEND_USER_FUNCTION) {
1778        add_next_index_str(user_ar, zend_string_copy(hash_key->key));
1779    }
1780
1781    return 0;
1782}
1783
1784
1785/* {{{ proto array get_defined_functions(void)
1786   Returns an array of all defined functions */
1787ZEND_FUNCTION(get_defined_functions)
1788{
1789    zval internal, user, *ret;
1790
1791    if (zend_parse_parameters_none() == FAILURE) {
1792        return;
1793    }
1794
1795    array_init(&internal);
1796    array_init(&user);
1797    array_init(return_value);
1798
1799    zend_hash_apply_with_arguments(EG(function_table) TSRMLS_CC, copy_function_name, 2, &internal, &user);
1800
1801    ret = zend_hash_str_add_new(Z_ARRVAL_P(return_value), "internal", sizeof("internal")-1, &internal);
1802
1803    if (!ret) {
1804        zval_ptr_dtor(&internal);
1805        zval_ptr_dtor(&user);
1806        zval_dtor(return_value);
1807        zend_error(E_WARNING, "Cannot add internal functions to return value from get_defined_functions()");
1808        RETURN_FALSE;
1809    }
1810
1811    ret = zend_hash_str_add_new(Z_ARRVAL_P(return_value), "user", sizeof("user")-1, &user);
1812    if (!ret) {
1813        zval_ptr_dtor(&user);
1814        zval_dtor(return_value);
1815        zend_error(E_WARNING, "Cannot add user functions to return value from get_defined_functions()");
1816        RETURN_FALSE;
1817    }
1818}
1819/* }}} */
1820
1821
1822/* {{{ proto array get_defined_vars(void)
1823   Returns an associative array of names and values of all currently defined variable names (variables in the current scope) */
1824ZEND_FUNCTION(get_defined_vars)
1825{
1826    zend_array *symbol_table = zend_rebuild_symbol_table(TSRMLS_C);
1827
1828    ZVAL_NEW_ARR(return_value);
1829    zend_array_dup(Z_ARRVAL_P(return_value), &symbol_table->ht);
1830}
1831/* }}} */
1832
1833
1834#define LAMBDA_TEMP_FUNCNAME    "__lambda_func"
1835/* {{{ proto string create_function(string args, string code)
1836   Creates an anonymous function, and returns its name (funny, eh?) */
1837ZEND_FUNCTION(create_function)
1838{
1839    zend_string *function_name;
1840    char *eval_code, *function_args, *function_code;
1841    size_t eval_code_length, function_args_len, function_code_len;
1842    int retval;
1843    char *eval_name;
1844
1845    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &function_args, &function_args_len, &function_code, &function_code_len) == FAILURE) {
1846        return;
1847    }
1848
1849    eval_code = (char *) emalloc(sizeof("function " LAMBDA_TEMP_FUNCNAME)
1850            +function_args_len
1851            +2  /* for the args parentheses */
1852            +2  /* for the curly braces */
1853            +function_code_len);
1854
1855    eval_code_length = sizeof("function " LAMBDA_TEMP_FUNCNAME "(") - 1;
1856    memcpy(eval_code, "function " LAMBDA_TEMP_FUNCNAME "(", eval_code_length);
1857
1858    memcpy(eval_code + eval_code_length, function_args, function_args_len);
1859    eval_code_length += function_args_len;
1860
1861    eval_code[eval_code_length++] = ')';
1862    eval_code[eval_code_length++] = '{';
1863
1864    memcpy(eval_code + eval_code_length, function_code, function_code_len);
1865    eval_code_length += function_code_len;
1866
1867    eval_code[eval_code_length++] = '}';
1868    eval_code[eval_code_length] = '\0';
1869
1870    eval_name = zend_make_compiled_string_description("runtime-created function" TSRMLS_CC);
1871    retval = zend_eval_stringl(eval_code, eval_code_length, NULL, eval_name TSRMLS_CC);
1872    efree(eval_code);
1873    efree(eval_name);
1874
1875    if (retval==SUCCESS) {
1876        zend_op_array *func;
1877        HashTable *static_variables;
1878
1879        func = zend_hash_str_find_ptr(EG(function_table), LAMBDA_TEMP_FUNCNAME, sizeof(LAMBDA_TEMP_FUNCNAME)-1);
1880        if (!func) {
1881            zend_error(E_ERROR, "Unexpected inconsistency in create_function()");
1882            RETURN_FALSE;
1883        }
1884        (*func->refcount)++;
1885
1886        function_name = zend_string_alloc(sizeof("0lambda_")+MAX_LENGTH_OF_LONG, 0);
1887        function_name->val[0] = '\0';
1888
1889        do {
1890            function_name->len = snprintf(function_name->val + 1, sizeof("lambda_")+MAX_LENGTH_OF_LONG, "lambda_%d", ++EG(lambda_count)) + 1;
1891        } while (zend_hash_add_ptr(EG(function_table), function_name, func) == NULL);
1892        static_variables = func->static_variables;
1893        func->static_variables = NULL;
1894        zend_hash_str_del(EG(function_table), LAMBDA_TEMP_FUNCNAME, sizeof(LAMBDA_TEMP_FUNCNAME)-1);
1895        func->static_variables = static_variables;
1896        RETURN_STR(function_name);
1897    } else {
1898        zend_hash_str_del(EG(function_table), LAMBDA_TEMP_FUNCNAME, sizeof(LAMBDA_TEMP_FUNCNAME)-1);
1899        RETURN_FALSE;
1900    }
1901}
1902/* }}} */
1903
1904
1905#if ZEND_DEBUG
1906ZEND_FUNCTION(zend_test_func)
1907{
1908    zval *arg1, *arg2;
1909
1910    zend_get_parameters(ZEND_NUM_ARGS(), 2, &arg1, &arg2);
1911}
1912
1913
1914#ifdef ZTS
1915ZEND_FUNCTION(zend_thread_id)
1916{
1917    RETURN_LONG((zend_long)tsrm_thread_id());
1918}
1919#endif
1920#endif
1921
1922/* {{{ proto string get_resource_type(resource res)
1923   Get the resource type name for a given resource */
1924ZEND_FUNCTION(get_resource_type)
1925{
1926    const char *resource_type;
1927    zval *z_resource_type;
1928
1929    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_resource_type) == FAILURE) {
1930        return;
1931    }
1932
1933    resource_type = zend_rsrc_list_get_rsrc_type(Z_RES_P(z_resource_type) TSRMLS_CC);
1934    if (resource_type) {
1935        RETURN_STRING(resource_type);
1936    } else {
1937        RETURN_STRING("Unknown");
1938    }
1939}
1940/* }}} */
1941
1942/* {{{ proto array get_resources([string resouce_type])
1943   Get an array with all active resources */
1944ZEND_FUNCTION(get_resources)
1945{
1946    zend_string *type = NULL;
1947    zend_string *key;
1948    zend_ulong index;
1949    zval *val;
1950
1951    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|S", &type) == FAILURE) {
1952        return;
1953    }
1954
1955    if (!type) {
1956        array_init(return_value);
1957        ZEND_HASH_FOREACH_KEY_VAL(&EG(regular_list), index, key, val) {
1958            if (!key) {
1959                Z_ADDREF_P(val);
1960                zend_hash_index_add_new(Z_ARRVAL_P(return_value), index, val);
1961            }
1962        } ZEND_HASH_FOREACH_END();
1963    } else if (zend_string_equals_literal(type, "Unknown")) {
1964        array_init(return_value);
1965        ZEND_HASH_FOREACH_KEY_VAL(&EG(regular_list), index, key, val) {
1966            if (!key && Z_RES_TYPE_P(val) <= 0) {
1967                Z_ADDREF_P(val);
1968                zend_hash_index_add_new(Z_ARRVAL_P(return_value), index, val);
1969            }
1970        } ZEND_HASH_FOREACH_END();
1971    } else {
1972        int id = zend_fetch_list_dtor_id(type->val);
1973
1974        if (id <= 0) {
1975            zend_error(E_WARNING, "get_resources():  Unknown resource type '%s'", type->val);
1976            RETURN_FALSE;
1977        }
1978
1979        array_init(return_value);
1980        ZEND_HASH_FOREACH_KEY_VAL(&EG(regular_list), index, key, val) {
1981            if (!key && Z_RES_TYPE_P(val) == id) {
1982                Z_ADDREF_P(val);
1983                zend_hash_index_add_new(Z_ARRVAL_P(return_value), index, val);
1984            }
1985        } ZEND_HASH_FOREACH_END();
1986    }
1987}
1988/* }}} */
1989
1990static int add_extension_info(zval *item, void *arg TSRMLS_DC)
1991{
1992    zval *name_array = (zval *)arg;
1993    zend_module_entry *module = (zend_module_entry*)Z_PTR_P(item);
1994    add_next_index_string(name_array, module->name);
1995    return 0;
1996}
1997
1998static int add_zendext_info(zend_extension *ext, void *arg TSRMLS_DC)
1999{
2000    zval *name_array = (zval *)arg;
2001    add_next_index_string(name_array, ext->name);
2002    return 0;
2003}
2004
2005static int add_constant_info(zval *item, void *arg TSRMLS_DC)
2006{
2007    zval *name_array = (zval *)arg;
2008    zend_constant *constant = (zend_constant*)Z_PTR_P(item);
2009    zval const_val;
2010
2011    if (!constant->name) {
2012        /* skip special constants */
2013        return 0;
2014    }
2015
2016    ZVAL_DUP(&const_val, &constant->value);
2017    zend_hash_add_new(Z_ARRVAL_P(name_array), constant->name, &const_val);
2018    return 0;
2019}
2020
2021
2022/* {{{ proto array get_loaded_extensions([bool zend_extensions]) U
2023   Return an array containing names of loaded extensions */
2024ZEND_FUNCTION(get_loaded_extensions)
2025{
2026    zend_bool zendext = 0;
2027
2028    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &zendext) == FAILURE) {
2029        return;
2030    }
2031
2032    array_init(return_value);
2033
2034    if (zendext) {
2035        zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t)add_zendext_info, return_value TSRMLS_CC);
2036    } else {
2037        zend_hash_apply_with_argument(&module_registry, add_extension_info, return_value TSRMLS_CC);
2038    }
2039}
2040/* }}} */
2041
2042
2043/* {{{ proto array get_defined_constants([bool categorize])
2044   Return an array containing the names and values of all defined constants */
2045ZEND_FUNCTION(get_defined_constants)
2046{
2047    zend_bool categorize = 0;
2048
2049    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &categorize) == FAILURE) {
2050        return;
2051    }
2052
2053    array_init(return_value);
2054
2055    if (categorize) {
2056        zend_constant *val;
2057        int module_number;
2058        zval *modules, const_val;
2059        char **module_names;
2060        zend_module_entry *module;
2061        int i = 1;
2062
2063        modules = ecalloc(zend_hash_num_elements(&module_registry) + 2, sizeof(zval));
2064        module_names = emalloc((zend_hash_num_elements(&module_registry) + 2) * sizeof(char *));
2065
2066        module_names[0] = "internal";
2067        ZEND_HASH_FOREACH_PTR(&module_registry, module) {
2068            module_names[module->module_number] = (char *)module->name;
2069            i++;
2070        } ZEND_HASH_FOREACH_END();
2071        module_names[i] = "user";
2072
2073        ZEND_HASH_FOREACH_PTR(EG(zend_constants), val) {
2074            if (!val->name) {
2075                /* skip special constants */
2076                continue;
2077            }
2078
2079            if (val->module_number == PHP_USER_CONSTANT) {
2080                module_number = i;
2081            } else if (val->module_number > i || val->module_number < 0) {
2082                /* should not happen */
2083                continue;
2084            } else {
2085                module_number = val->module_number;
2086            }
2087
2088            if (Z_TYPE(modules[module_number]) == IS_UNDEF) {
2089                array_init(&modules[module_number]);
2090                add_assoc_zval(return_value, module_names[module_number], &modules[module_number]);
2091            }
2092
2093            ZVAL_DUP(&const_val, &val->value);
2094            zend_hash_add_new(Z_ARRVAL(modules[module_number]), val->name, &const_val);
2095        } ZEND_HASH_FOREACH_END();
2096
2097        efree(module_names);
2098        efree(modules);
2099    } else {
2100        zend_hash_apply_with_argument(EG(zend_constants), add_constant_info, return_value TSRMLS_CC);
2101    }
2102}
2103/* }}} */
2104
2105
2106static void debug_backtrace_get_args(zend_execute_data *call, zval *arg_array TSRMLS_DC)
2107{
2108    int num_args = call->num_args;
2109
2110    array_init_size(arg_array, num_args);
2111    if (num_args) {
2112        int i = 0;
2113        zval *p = ZEND_CALL_ARG(call, 1);
2114
2115        if (call->func->type == ZEND_USER_FUNCTION) {
2116            int first_extra_arg = call->func->op_array.num_args;
2117
2118            if (call->func->op_array.fn_flags & ZEND_ACC_VARIADIC) {
2119                first_extra_arg--;
2120            }
2121            if (call->num_args > first_extra_arg) {
2122                while (i < first_extra_arg) {
2123                    if (Z_REFCOUNTED_P(p)) Z_ADDREF_P(p);
2124                    zend_hash_next_index_insert_new(Z_ARRVAL_P(arg_array), p);
2125                    p++;
2126                    i++;
2127                }
2128                p = EX_VAR_NUM_2(call, call->func->op_array.last_var + call->func->op_array.T);
2129            }
2130        }
2131
2132        while (i < num_args) {
2133            if (Z_REFCOUNTED_P(p)) Z_ADDREF_P(p);
2134            zend_hash_next_index_insert_new(Z_ARRVAL_P(arg_array), p);
2135            p++;
2136            i++;
2137        }
2138    }
2139}
2140
2141void debug_print_backtrace_args(zval *arg_array TSRMLS_DC)
2142{
2143    zval *tmp;
2144    int i = 0;
2145
2146    ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(arg_array), tmp) {
2147        if (i++) {
2148            ZEND_PUTS(", ");
2149        }
2150        zend_print_flat_zval_r(tmp TSRMLS_CC);
2151    } ZEND_HASH_FOREACH_END();
2152}
2153
2154/* {{{ proto void debug_print_backtrace([int options[, int limit]]) */
2155ZEND_FUNCTION(debug_print_backtrace)
2156{
2157    zend_execute_data *call, *ptr, *skip;
2158    zend_object *object;
2159    int lineno, frameno = 0;
2160    zend_function *func;
2161    const char *function_name;
2162    const char *filename;
2163    zend_string *class_name = NULL;
2164    char *call_type;
2165    const char *include_filename = NULL;
2166    zval arg_array;
2167    int indent = 0;
2168    zend_long options = 0;
2169    zend_long limit = 0;
2170
2171    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ll", &options, &limit) == FAILURE) {
2172        return;
2173    }
2174
2175    ZVAL_UNDEF(&arg_array);
2176    ptr = EG(current_execute_data)->prev_execute_data;
2177
2178    /* skip debug_backtrace() */
2179    call = ptr;
2180    ptr = ptr->prev_execute_data;
2181
2182    while (ptr && (limit == 0 || frameno < limit)) {
2183        frameno++;
2184        class_name = NULL;
2185        call_type = NULL;
2186        ZVAL_UNDEF(&arg_array);
2187
2188        skip = ptr;
2189        /* skip internal handler */
2190        if ((!skip->func || !ZEND_USER_CODE(skip->func->common.type)) &&
2191            skip->prev_execute_data &&
2192            skip->prev_execute_data->func &&
2193            ZEND_USER_CODE(skip->prev_execute_data->func->common.type) &&
2194            skip->prev_execute_data->opline->opcode != ZEND_DO_FCALL &&
2195            skip->prev_execute_data->opline->opcode != ZEND_INCLUDE_OR_EVAL) {
2196            skip = skip->prev_execute_data;
2197        }
2198
2199        if (skip->func && ZEND_USER_CODE(skip->func->common.type)) {
2200            filename = skip->func->op_array.filename->val;
2201            if (skip->opline->opcode == ZEND_HANDLE_EXCEPTION) {
2202                if (EG(opline_before_exception)) {
2203                    lineno = EG(opline_before_exception)->lineno;
2204                } else {
2205                    lineno = skip->func->op_array.line_end;
2206                }
2207            } else {
2208                lineno = skip->opline->lineno;
2209            }
2210        } else {
2211            filename = NULL;
2212            lineno = 0;
2213        }
2214
2215        /* $this may be passed into regular internal functions */
2216        object = call->object;
2217        if (object &&
2218            call &&
2219            call->func->type == ZEND_INTERNAL_FUNCTION &&
2220            !call->func->common.scope) {
2221            object = NULL;
2222        }
2223
2224        if (call->func) {
2225            func = call->func;
2226            function_name = (func->common.scope &&
2227                             func->common.scope->trait_aliases) ?
2228                zend_resolve_method_name(
2229                    (object ?
2230                        zend_get_class_entry(object TSRMLS_CC) :
2231                        func->common.scope), func)->val :
2232                (func->common.function_name ?
2233                    func->common.function_name->val : NULL);
2234        } else {
2235            func = NULL;
2236            function_name = NULL;
2237        }
2238
2239        if (function_name) {
2240            if (object) {
2241                if (func->common.scope) {
2242                    class_name = func->common.scope->name;
2243                } else {
2244                    class_name = zend_get_object_classname(object TSRMLS_CC);
2245                }
2246
2247                call_type = "->";
2248            } else if (func->common.scope) {
2249                class_name = func->common.scope->name;
2250                call_type = "::";
2251            } else {
2252                class_name = NULL;
2253                call_type = NULL;
2254            }
2255            if (func->type != ZEND_EVAL_CODE) {
2256                if ((options & DEBUG_BACKTRACE_IGNORE_ARGS) == 0) {
2257                    debug_backtrace_get_args(call, &arg_array TSRMLS_CC);
2258                }
2259            }
2260        } else {
2261            /* i know this is kinda ugly, but i'm trying to avoid extra cycles in the main execution loop */
2262            zend_bool build_filename_arg = 1;
2263
2264            if (!ptr->func || !ZEND_USER_CODE(ptr->func->common.type) || ptr->opline->opcode != ZEND_INCLUDE_OR_EVAL) {
2265                /* can happen when calling eval from a custom sapi */
2266                function_name = "unknown";
2267                build_filename_arg = 0;
2268            } else
2269            switch (ptr->opline->extended_value) {
2270                case ZEND_EVAL:
2271                    function_name = "eval";
2272                    build_filename_arg = 0;
2273                    break;
2274                case ZEND_INCLUDE:
2275                    function_name = "include";
2276                    break;
2277                case ZEND_REQUIRE:
2278                    function_name = "require";
2279                    break;
2280                case ZEND_INCLUDE_ONCE:
2281                    function_name = "include_once";
2282                    break;
2283                case ZEND_REQUIRE_ONCE:
2284                    function_name = "require_once";
2285                    break;
2286                default:
2287                    /* this can actually happen if you use debug_backtrace() in your error_handler and
2288                     * you're in the top-scope */
2289                    function_name = "unknown";
2290                    build_filename_arg = 0;
2291                    break;
2292            }
2293
2294            if (build_filename_arg && include_filename) {
2295                array_init(&arg_array);
2296                add_next_index_string(&arg_array, (char*)include_filename);
2297            }
2298            call_type = NULL;
2299        }
2300        zend_printf("#%-2d ", indent);
2301        if (class_name) {
2302            ZEND_PUTS(class_name->val);
2303            ZEND_PUTS(call_type);
2304        }
2305        zend_printf("%s(", function_name);
2306        if (Z_TYPE(arg_array) != IS_UNDEF) {
2307            debug_print_backtrace_args(&arg_array TSRMLS_CC);
2308            zval_ptr_dtor(&arg_array);
2309        }
2310        if (filename) {
2311            zend_printf(") called at [%s:%d]\n", filename, lineno);
2312        } else {
2313            zend_execute_data *prev_call = skip;
2314            zend_execute_data *prev = skip->prev_execute_data;
2315
2316            while (prev) {
2317                if (prev_call &&
2318                    prev_call->func &&
2319                    !ZEND_USER_CODE(prev_call->func->common.type)) {
2320                    prev = NULL;
2321                    break;
2322                }
2323                if (prev->func && ZEND_USER_CODE(prev->func->common.type)) {
2324                    zend_printf(") called at [%s:%d]\n", prev->func->op_array.filename->val, prev->opline->lineno);
2325                    break;
2326                }
2327                prev_call = prev;
2328                prev = prev->prev_execute_data;
2329            }
2330            if (!prev) {
2331                ZEND_PUTS(")\n");
2332            }
2333        }
2334        include_filename = filename;
2335        call = skip;
2336        ptr = skip->prev_execute_data;
2337        ++indent;
2338    }
2339}
2340
2341/* }}} */
2342
2343ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int options, int limit TSRMLS_DC)
2344{
2345    zend_execute_data *call, *ptr, *skip;
2346    zend_object *object;
2347    int lineno, frameno = 0;
2348    zend_function *func;
2349    const char *function_name;
2350    const char *filename;
2351    zend_string *class_name;
2352    const char *include_filename = NULL;
2353    zval stack_frame;
2354
2355    call = NULL;
2356    ptr = EG(current_execute_data);
2357    if (!ptr->func || !ZEND_USER_CODE(ptr->func->common.type)) {
2358        call = ptr;
2359        ptr = ptr->prev_execute_data;
2360    }
2361
2362    if (ptr) {
2363        if (skip_last) {
2364            /* skip debug_backtrace() */
2365            call = ptr;
2366            ptr = ptr->prev_execute_data;
2367        } else {
2368            /* skip "new Exception()" */
2369            if (ptr->func && ZEND_USER_CODE(ptr->func->common.type) && (ptr->opline->opcode == ZEND_NEW)) {
2370                call = ptr;
2371                ptr = ptr->prev_execute_data;
2372            }
2373        }
2374    }
2375    if (!call) {
2376        call = ptr;
2377        ptr = ptr->prev_execute_data;
2378    }
2379
2380    array_init(return_value);
2381
2382    while (ptr && (limit == 0 || frameno < limit)) {
2383        frameno++;
2384        array_init(&stack_frame);
2385
2386        skip = ptr;
2387        /* skip internal handler */
2388        if ((!skip->func || !ZEND_USER_CODE(skip->func->common.type)) &&
2389            skip->prev_execute_data &&
2390            skip->prev_execute_data->func &&
2391            ZEND_USER_CODE(skip->prev_execute_data->func->common.type) &&
2392            skip->prev_execute_data->opline->opcode != ZEND_DO_FCALL &&
2393            skip->prev_execute_data->opline->opcode != ZEND_INCLUDE_OR_EVAL) {
2394            skip = skip->prev_execute_data;
2395        }
2396
2397        if (skip->func && ZEND_USER_CODE(skip->func->common.type)) {
2398            filename = skip->func->op_array.filename->val;
2399            if (skip->opline->opcode == ZEND_HANDLE_EXCEPTION) {
2400                if (EG(opline_before_exception)) {
2401                    lineno = EG(opline_before_exception)->lineno;
2402                } else {
2403                    lineno = skip->func->op_array.line_end;
2404                }
2405            } else {
2406                lineno = skip->opline->lineno;
2407            }
2408            add_assoc_string_ex(&stack_frame, "file", sizeof("file")-1, (char*)filename);
2409            add_assoc_long_ex(&stack_frame, "line", sizeof("line")-1, lineno);
2410
2411            /* try to fetch args only if an FCALL was just made - elsewise we're in the middle of a function
2412             * and debug_baktrace() might have been called by the error_handler. in this case we don't
2413             * want to pop anything of the argument-stack */
2414        } else {
2415            zend_execute_data *prev_call = skip;
2416            zend_execute_data *prev = skip->prev_execute_data;
2417
2418            while (prev) {
2419                if (prev_call &&
2420                    prev_call->func &&
2421                    !ZEND_USER_CODE(prev_call->func->common.type) &&
2422                    !(prev_call->func->common.type == ZEND_INTERNAL_FUNCTION &&
2423                        (prev_call->func->common.fn_flags & ZEND_ACC_CALL_VIA_HANDLER))) {
2424                    break;
2425                }
2426                if (prev->func && ZEND_USER_CODE(prev->func->common.type)) {
2427                    add_assoc_str_ex(&stack_frame, "file", sizeof("file")-1, zend_string_copy(prev->func->op_array.filename));
2428                    add_assoc_long_ex(&stack_frame, "line", sizeof("line")-1, prev->opline->lineno);
2429                    break;
2430                }
2431                prev_call = prev;
2432                prev = prev->prev_execute_data;
2433            }
2434            filename = NULL;
2435        }
2436
2437        /* $this may be passed into regular internal functions */
2438        object = call ? call->object : NULL;
2439        if (object &&
2440            call->func->type == ZEND_INTERNAL_FUNCTION &&
2441            !call->func->common.scope) {
2442            object = NULL;
2443        }
2444
2445        if (call && call->func) {
2446            func = call->func;
2447            function_name = (func->common.scope &&
2448                             func->common.scope->trait_aliases) ?
2449                zend_resolve_method_name(
2450                    (object ?
2451                        zend_get_class_entry(object TSRMLS_CC) :
2452                        func->common.scope), func)->val :
2453                (func->common.function_name ?
2454                    func->common.function_name->val : NULL);
2455        } else {
2456            func = NULL;
2457            function_name = NULL;
2458        }
2459
2460        if (function_name) {
2461            add_assoc_string_ex(&stack_frame, "function", sizeof("function")-1, (char*)function_name);
2462
2463            if (object) {
2464                if (func->common.scope) {
2465                    add_assoc_str_ex(&stack_frame, "class", sizeof("class")-1, zend_string_copy(func->common.scope->name));
2466                } else {
2467                    class_name = zend_get_object_classname(object TSRMLS_CC);
2468                    add_assoc_str_ex(&stack_frame, "class", sizeof("class")-1, zend_string_copy(class_name));
2469
2470                }
2471                if ((options & DEBUG_BACKTRACE_PROVIDE_OBJECT) != 0) {
2472                    zval zv;
2473                    ZVAL_OBJ(&zv, object);
2474                    add_assoc_zval_ex(&stack_frame, "object", sizeof("object")-1, &zv);
2475                    Z_ADDREF(zv);
2476                }
2477
2478                add_assoc_string_ex(&stack_frame, "type", sizeof("type")-1, "->");
2479            } else if (func->common.scope) {
2480                add_assoc_str_ex(&stack_frame, "class", sizeof("class")-1, zend_string_copy(func->common.scope->name));
2481                add_assoc_string_ex(&stack_frame, "type", sizeof("type")-1, "::");
2482            }
2483
2484            if ((options & DEBUG_BACKTRACE_IGNORE_ARGS) == 0 &&
2485                func->type != ZEND_EVAL_CODE) {
2486                zval args;
2487
2488                debug_backtrace_get_args(call, &args TSRMLS_CC);
2489                add_assoc_zval_ex(&stack_frame, "args", sizeof("args")-1, &args);
2490            }
2491        } else {
2492            /* i know this is kinda ugly, but i'm trying to avoid extra cycles in the main execution loop */
2493            zend_bool build_filename_arg = 1;
2494
2495            if (!ptr->func || !ZEND_USER_CODE(ptr->func->common.type) || ptr->opline->opcode != ZEND_INCLUDE_OR_EVAL) {
2496                /* can happen when calling eval from a custom sapi */
2497                function_name = "unknown";
2498                build_filename_arg = 0;
2499            } else
2500            switch (ptr->opline->extended_value) {
2501                case ZEND_EVAL:
2502                    function_name = "eval";
2503                    build_filename_arg = 0;
2504                    break;
2505                case ZEND_INCLUDE:
2506                    function_name = "include";
2507                    break;
2508                case ZEND_REQUIRE:
2509                    function_name = "require";
2510                    break;
2511                case ZEND_INCLUDE_ONCE:
2512                    function_name = "include_once";
2513                    break;
2514                case ZEND_REQUIRE_ONCE:
2515                    function_name = "require_once";
2516                    break;
2517                default:
2518                    /* this can actually happen if you use debug_backtrace() in your error_handler and
2519                     * you're in the top-scope */
2520                    function_name = "unknown";
2521                    build_filename_arg = 0;
2522                    break;
2523            }
2524
2525            if (build_filename_arg && include_filename) {
2526                zval arg_array;
2527
2528                array_init(&arg_array);
2529
2530                /* include_filename always points to the last filename of the last last called-function.
2531                   if we have called include in the frame above - this is the file we have included.
2532                 */
2533
2534                add_next_index_string(&arg_array, (char*)include_filename);
2535                add_assoc_zval_ex(&stack_frame, "args", sizeof("args")-1, &arg_array);
2536            }
2537
2538            add_assoc_string_ex(&stack_frame, "function", sizeof("function")-1, (char*)function_name);
2539        }
2540
2541        zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &stack_frame);
2542
2543        include_filename = filename;
2544
2545        call = skip;
2546        ptr = skip->prev_execute_data;
2547    }
2548}
2549/* }}} */
2550
2551
2552/* {{{ proto array debug_backtrace([int options[, int limit]])
2553   Return backtrace as array */
2554ZEND_FUNCTION(debug_backtrace)
2555{
2556    zend_long options = DEBUG_BACKTRACE_PROVIDE_OBJECT;
2557    zend_long limit = 0;
2558
2559    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ll", &options, &limit) == FAILURE) {
2560        return;
2561    }
2562
2563    zend_fetch_debug_backtrace(return_value, 1, options, limit TSRMLS_CC);
2564}
2565/* }}} */
2566
2567/* {{{ proto bool extension_loaded(string extension_name)
2568   Returns true if the named extension is loaded */
2569ZEND_FUNCTION(extension_loaded)
2570{
2571    char *extension_name;
2572    size_t extension_name_len;
2573    zend_string *lcname;
2574
2575    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &extension_name, &extension_name_len) == FAILURE) {
2576        return;
2577    }
2578
2579    lcname = zend_string_alloc(extension_name_len, 0);
2580    zend_str_tolower_copy(lcname->val, extension_name, extension_name_len);
2581    if (zend_hash_exists(&module_registry, lcname)) {
2582        RETVAL_TRUE;
2583    } else {
2584        RETVAL_FALSE;
2585    }
2586    zend_string_free(lcname);
2587}
2588/* }}} */
2589
2590
2591/* {{{ proto array get_extension_funcs(string extension_name)
2592   Returns an array with the names of functions belonging to the named extension */
2593ZEND_FUNCTION(get_extension_funcs)
2594{
2595    char *extension_name;
2596    zend_string *lcname;
2597    size_t extension_name_len;
2598    int array;
2599    zend_module_entry *module;
2600    zend_function *zif;
2601
2602    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &extension_name, &extension_name_len) == FAILURE) {
2603        return;
2604    }
2605    if (strncasecmp(extension_name, "zend", sizeof("zend"))) {
2606        lcname = zend_string_alloc(extension_name_len, 0);
2607        zend_str_tolower_copy(lcname->val, extension_name, extension_name_len);
2608    } else {
2609        lcname = zend_string_init("core", sizeof("core")-1, 0);
2610    }
2611    module = zend_hash_find_ptr(&module_registry, lcname);
2612    zend_string_free(lcname);
2613    if (!module) {
2614        RETURN_FALSE;
2615    }
2616
2617    if (module->functions) {
2618        /* avoid BC break, if functions list is empty, will return an empty array */
2619        array_init(return_value);
2620        array = 1;
2621    } else {
2622        array = 0;
2623    }
2624
2625    ZEND_HASH_FOREACH_PTR(CG(function_table), zif) {
2626        if (zif->common.type == ZEND_INTERNAL_FUNCTION
2627            && zif->internal_function.module == module) {
2628            if (!array) {
2629                array_init(return_value);
2630                array = 1;
2631            }
2632            add_next_index_str(return_value, zend_string_copy(zif->common.function_name));
2633        }
2634    } ZEND_HASH_FOREACH_END();
2635
2636    if (!array) {
2637        RETURN_FALSE;
2638    }
2639}
2640/* }}} */
2641
2642/*
2643 * Local variables:
2644 * tab-width: 4
2645 * c-basic-offset: 4
2646 * indent-tabs-mode: t
2647 * End:
2648 */
2649