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