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