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