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