1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | http://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Authors: Timm Friebe <thekid@thekid.de> |
14 | George Schlossnagle <george@omniti.com> |
15 | Andrei Zmievski <andrei@gravitonic.com> |
16 | Marcus Boerger <helly@php.net> |
17 | Johannes Schlueter <johannes@php.net> |
18 +----------------------------------------------------------------------+
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "php.h"
26 #include "php_ini.h"
27 #include "php_reflection.h"
28 #include "ext/standard/info.h"
29 #include "ext/standard/sha1.h"
30 #include "ext/standard/php_random.h"
31
32 #include "zend.h"
33 #include "zend_API.h"
34 #include "zend_ast.h"
35 #include "zend_attributes.h"
36 #include "zend_exceptions.h"
37 #include "zend_operators.h"
38 #include "zend_constants.h"
39 #include "zend_ini.h"
40 #include "zend_interfaces.h"
41 #include "zend_closures.h"
42 #include "zend_generators.h"
43 #include "zend_extensions.h"
44 #include "zend_builtin_functions.h"
45 #include "zend_smart_str.h"
46 #include "php_reflection_arginfo.h"
47
48 /* Key used to avoid leaking addresses in ReflectionProperty::getId() */
49 #define REFLECTION_KEY_LEN 16
50 ZEND_BEGIN_MODULE_GLOBALS(reflection)
51 zend_bool key_initialized;
52 unsigned char key[REFLECTION_KEY_LEN];
53 ZEND_END_MODULE_GLOBALS(reflection)
ZEND_DECLARE_MODULE_GLOBALS(reflection)54 ZEND_DECLARE_MODULE_GLOBALS(reflection)
55
56 #define REFLECTION_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(reflection, v)
57
58 static zend_always_inline zval *reflection_prop_name(zval *object) {
59 /* $name is always in the first property slot. */
60 ZEND_ASSERT(Z_OBJCE_P(object)->default_properties_count >= 1);
61 return &Z_OBJ_P(object)->properties_table[0];
62 }
63
reflection_prop_class(zval * object)64 static zend_always_inline zval *reflection_prop_class(zval *object) {
65 /* $class is always in the second property slot. */
66 ZEND_ASSERT(Z_OBJCE_P(object)->default_properties_count >= 2);
67 return &Z_OBJ_P(object)->properties_table[1];
68 }
69
70 /* Class entry pointers */
71 PHPAPI zend_class_entry *reflector_ptr;
72 PHPAPI zend_class_entry *reflection_exception_ptr;
73 PHPAPI zend_class_entry *reflection_ptr;
74 PHPAPI zend_class_entry *reflection_function_abstract_ptr;
75 PHPAPI zend_class_entry *reflection_function_ptr;
76 PHPAPI zend_class_entry *reflection_generator_ptr;
77 PHPAPI zend_class_entry *reflection_parameter_ptr;
78 PHPAPI zend_class_entry *reflection_type_ptr;
79 PHPAPI zend_class_entry *reflection_named_type_ptr;
80 PHPAPI zend_class_entry *reflection_union_type_ptr;
81 PHPAPI zend_class_entry *reflection_class_ptr;
82 PHPAPI zend_class_entry *reflection_object_ptr;
83 PHPAPI zend_class_entry *reflection_method_ptr;
84 PHPAPI zend_class_entry *reflection_property_ptr;
85 PHPAPI zend_class_entry *reflection_class_constant_ptr;
86 PHPAPI zend_class_entry *reflection_extension_ptr;
87 PHPAPI zend_class_entry *reflection_zend_extension_ptr;
88 PHPAPI zend_class_entry *reflection_reference_ptr;
89 PHPAPI zend_class_entry *reflection_attribute_ptr;
90
91 /* Exception throwing macro */
92 #define _DO_THROW(msg) \
93 zend_throw_exception(reflection_exception_ptr, msg, 0);
94
95 #define GET_REFLECTION_OBJECT() do { \
96 intern = Z_REFLECTION_P(ZEND_THIS); \
97 if (intern->ptr == NULL) { \
98 if (EG(exception) && EG(exception)->ce == reflection_exception_ptr) { \
99 RETURN_THROWS(); \
100 } \
101 zend_throw_error(NULL, "Internal error: Failed to retrieve the reflection object"); \
102 RETURN_THROWS(); \
103 } \
104 } while (0)
105
106 #define GET_REFLECTION_OBJECT_PTR(target) do { \
107 GET_REFLECTION_OBJECT(); \
108 target = intern->ptr; \
109 } while (0)
110
111 /* Class constants */
112 #define REGISTER_REFLECTION_CLASS_CONST_LONG(class_name, const_name, value) \
113 zend_declare_class_constant_long(reflection_ ## class_name ## _ptr, const_name, sizeof(const_name)-1, (zend_long)value);
114
115 #define REFLECTION_ATTRIBUTE_IS_INSTANCEOF (1 << 1)
116
117 /* {{{ Object structure */
118
119 /* Struct for properties */
120 typedef struct _property_reference {
121 zend_property_info *prop;
122 zend_string *unmangled_name;
123 } property_reference;
124
125 /* Struct for parameters */
126 typedef struct _parameter_reference {
127 uint32_t offset;
128 zend_bool required;
129 struct _zend_arg_info *arg_info;
130 zend_function *fptr;
131 } parameter_reference;
132
133 /* Struct for type hints */
134 typedef struct _type_reference {
135 zend_type type;
136 /* Whether to use backwards compatible null representation */
137 zend_bool legacy_behavior;
138 } type_reference;
139
140 /* Struct for attributes */
141 typedef struct _attribute_reference {
142 HashTable *attributes;
143 zend_attribute *data;
144 zend_class_entry *scope;
145 zend_string *filename;
146 uint32_t target;
147 } attribute_reference;
148
149 typedef enum {
150 REF_TYPE_OTHER, /* Must be 0 */
151 REF_TYPE_FUNCTION,
152 REF_TYPE_GENERATOR,
153 REF_TYPE_PARAMETER,
154 REF_TYPE_TYPE,
155 REF_TYPE_PROPERTY,
156 REF_TYPE_CLASS_CONSTANT,
157 REF_TYPE_ATTRIBUTE
158 } reflection_type_t;
159
160 /* Struct for reflection objects */
161 typedef struct {
162 zval obj;
163 void *ptr;
164 zend_class_entry *ce;
165 reflection_type_t ref_type;
166 unsigned int ignore_visibility:1;
167 zend_object zo;
168 } reflection_object;
169
reflection_object_from_obj(zend_object * obj)170 static inline reflection_object *reflection_object_from_obj(zend_object *obj) {
171 return (reflection_object*)((char*)(obj) - XtOffsetOf(reflection_object, zo));
172 }
173
174 #define Z_REFLECTION_P(zv) reflection_object_from_obj(Z_OBJ_P((zv)))
175 /* }}} */
176
177 static zend_object_handlers reflection_object_handlers;
178
prop_get_flags(property_reference * ref)179 static zend_always_inline uint32_t prop_get_flags(property_reference *ref) {
180 return ref->prop ? ref->prop->flags : ZEND_ACC_PUBLIC;
181 }
182
is_closure_invoke(zend_class_entry * ce,zend_string * lcname)183 static inline zend_bool is_closure_invoke(zend_class_entry *ce, zend_string *lcname) {
184 return ce == zend_ce_closure
185 && zend_string_equals_literal(lcname, ZEND_INVOKE_FUNC_NAME);
186 }
187
_default_get_name(zval * object,zval * return_value)188 static void _default_get_name(zval *object, zval *return_value) /* {{{ */
189 {
190 zval *name = reflection_prop_name(object);
191 if (Z_ISUNDEF_P(name)) {
192 RETURN_FALSE;
193 }
194 ZVAL_COPY(return_value, name);
195 }
196 /* }}} */
197
_copy_function(zend_function * fptr)198 static zend_function *_copy_function(zend_function *fptr) /* {{{ */
199 {
200 if (fptr
201 && (fptr->internal_function.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE))
202 {
203 zend_function *copy_fptr;
204 copy_fptr = emalloc(sizeof(zend_function));
205 memcpy(copy_fptr, fptr, sizeof(zend_function));
206 copy_fptr->internal_function.function_name = zend_string_copy(fptr->internal_function.function_name);
207 return copy_fptr;
208 } else {
209 /* no copy needed */
210 return fptr;
211 }
212 }
213 /* }}} */
214
_free_function(zend_function * fptr)215 static void _free_function(zend_function *fptr) /* {{{ */
216 {
217 if (fptr
218 && (fptr->internal_function.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE))
219 {
220 zend_string_release_ex(fptr->internal_function.function_name, 0);
221 zend_free_trampoline(fptr);
222 }
223 }
224 /* }}} */
225
reflection_free_objects_storage(zend_object * object)226 static void reflection_free_objects_storage(zend_object *object) /* {{{ */
227 {
228 reflection_object *intern = reflection_object_from_obj(object);
229 parameter_reference *reference;
230 property_reference *prop_reference;
231
232 if (intern->ptr) {
233 switch (intern->ref_type) {
234 case REF_TYPE_PARAMETER:
235 reference = (parameter_reference*)intern->ptr;
236 _free_function(reference->fptr);
237 efree(intern->ptr);
238 break;
239 case REF_TYPE_TYPE:
240 {
241 type_reference *type_ref = intern->ptr;
242 if (ZEND_TYPE_HAS_NAME(type_ref->type)) {
243 zend_string_release(ZEND_TYPE_NAME(type_ref->type));
244 }
245 efree(type_ref);
246 break;
247 }
248 case REF_TYPE_FUNCTION:
249 _free_function(intern->ptr);
250 break;
251 case REF_TYPE_PROPERTY:
252 prop_reference = (property_reference*)intern->ptr;
253 zend_string_release_ex(prop_reference->unmangled_name, 0);
254 efree(intern->ptr);
255 break;
256 case REF_TYPE_ATTRIBUTE: {
257 attribute_reference *attr_ref = intern->ptr;
258 if (attr_ref->filename) {
259 zend_string_release(attr_ref->filename);
260 }
261 efree(intern->ptr);
262 break;
263 }
264 case REF_TYPE_GENERATOR:
265 case REF_TYPE_CLASS_CONSTANT:
266 case REF_TYPE_OTHER:
267 break;
268 }
269 }
270 intern->ptr = NULL;
271 zval_ptr_dtor(&intern->obj);
272 zend_object_std_dtor(object);
273 }
274 /* }}} */
275
reflection_get_gc(zend_object * obj,zval ** gc_data,int * gc_data_count)276 static HashTable *reflection_get_gc(zend_object *obj, zval **gc_data, int *gc_data_count) /* {{{ */
277 {
278 reflection_object *intern = reflection_object_from_obj(obj);
279 *gc_data = &intern->obj;
280 *gc_data_count = 1;
281 return zend_std_get_properties(obj);
282 }
283 /* }}} */
284
reflection_objects_new(zend_class_entry * class_type)285 static zend_object *reflection_objects_new(zend_class_entry *class_type) /* {{{ */
286 {
287 reflection_object *intern = zend_object_alloc(sizeof(reflection_object), class_type);
288
289 zend_object_std_init(&intern->zo, class_type);
290 object_properties_init(&intern->zo, class_type);
291 intern->zo.handlers = &reflection_object_handlers;
292 return &intern->zo;
293 }
294 /* }}} */
295
reflection_instantiate(zend_class_entry * pce,zval * object)296 static zval *reflection_instantiate(zend_class_entry *pce, zval *object) /* {{{ */
297 {
298 object_init_ex(object, pce);
299 return object;
300 }
301 /* }}} */
302
303 static void _const_string(smart_str *str, char *name, zval *value, char *indent);
304 static void _function_string(smart_str *str, zend_function *fptr, zend_class_entry *scope, char* indent);
305 static void _property_string(smart_str *str, zend_property_info *prop, const char *prop_name, char* indent);
306 static void _class_const_string(smart_str *str, char *name, zend_class_constant *c, char* indent);
307 static void _class_string(smart_str *str, zend_class_entry *ce, zval *obj, char *indent);
308 static void _extension_string(smart_str *str, zend_module_entry *module, char *indent);
309 static void _zend_extension_string(smart_str *str, zend_extension *extension, char *indent);
310
311 /* {{{ _class_string */
_class_string(smart_str * str,zend_class_entry * ce,zval * obj,char * indent)312 static void _class_string(smart_str *str, zend_class_entry *ce, zval *obj, char *indent)
313 {
314 int count, count_static_props = 0, count_static_funcs = 0, count_shadow_props = 0;
315 zend_string *sub_indent = strpprintf(0, "%s ", indent);
316
317 /* TBD: Repair indenting of doc comment (or is this to be done in the parser?) */
318 if (ce->type == ZEND_USER_CLASS && ce->info.user.doc_comment) {
319 smart_str_append_printf(str, "%s%s", indent, ZSTR_VAL(ce->info.user.doc_comment));
320 smart_str_appendc(str, '\n');
321 }
322
323 if (obj && Z_TYPE_P(obj) == IS_OBJECT) {
324 smart_str_append_printf(str, "%sObject of class [ ", indent);
325 } else {
326 char *kind = "Class";
327 if (ce->ce_flags & ZEND_ACC_INTERFACE) {
328 kind = "Interface";
329 } else if (ce->ce_flags & ZEND_ACC_TRAIT) {
330 kind = "Trait";
331 }
332 smart_str_append_printf(str, "%s%s [ ", indent, kind);
333 }
334 smart_str_append_printf(str, (ce->type == ZEND_USER_CLASS) ? "<user" : "<internal");
335 if (ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module) {
336 smart_str_append_printf(str, ":%s", ce->info.internal.module->name);
337 }
338 smart_str_append_printf(str, "> ");
339 if (ce->get_iterator != NULL) {
340 smart_str_append_printf(str, "<iterateable> ");
341 }
342 if (ce->ce_flags & ZEND_ACC_INTERFACE) {
343 smart_str_append_printf(str, "interface ");
344 } else if (ce->ce_flags & ZEND_ACC_TRAIT) {
345 smart_str_append_printf(str, "trait ");
346 } else {
347 if (ce->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
348 smart_str_append_printf(str, "abstract ");
349 }
350 if (ce->ce_flags & ZEND_ACC_FINAL) {
351 smart_str_append_printf(str, "final ");
352 }
353 smart_str_append_printf(str, "class ");
354 }
355 smart_str_append_printf(str, "%s", ZSTR_VAL(ce->name));
356 if (ce->parent) {
357 smart_str_append_printf(str, " extends %s", ZSTR_VAL(ce->parent->name));
358 }
359
360 if (ce->num_interfaces) {
361 uint32_t i;
362
363 ZEND_ASSERT(ce->ce_flags & ZEND_ACC_LINKED);
364 if (ce->ce_flags & ZEND_ACC_INTERFACE) {
365 smart_str_append_printf(str, " extends %s", ZSTR_VAL(ce->interfaces[0]->name));
366 } else {
367 smart_str_append_printf(str, " implements %s", ZSTR_VAL(ce->interfaces[0]->name));
368 }
369 for (i = 1; i < ce->num_interfaces; ++i) {
370 smart_str_append_printf(str, ", %s", ZSTR_VAL(ce->interfaces[i]->name));
371 }
372 }
373 smart_str_append_printf(str, " ] {\n");
374
375 /* The information where a class is declared is only available for user classes */
376 if (ce->type == ZEND_USER_CLASS) {
377 smart_str_append_printf(str, "%s @@ %s %d-%d\n", indent, ZSTR_VAL(ce->info.user.filename),
378 ce->info.user.line_start, ce->info.user.line_end);
379 }
380
381 /* Constants */
382 smart_str_append_printf(str, "\n");
383 count = zend_hash_num_elements(&ce->constants_table);
384 smart_str_append_printf(str, "%s - Constants [%d] {\n", indent, count);
385 if (count > 0) {
386 zend_string *key;
387 zend_class_constant *c;
388
389 ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->constants_table, key, c) {
390 _class_const_string(str, ZSTR_VAL(key), c, ZSTR_VAL(sub_indent));
391 if (UNEXPECTED(EG(exception))) {
392 zend_string_release(sub_indent);
393 return;
394 }
395 } ZEND_HASH_FOREACH_END();
396 }
397 smart_str_append_printf(str, "%s }\n", indent);
398
399 /* Static properties */
400 /* counting static properties */
401 count = zend_hash_num_elements(&ce->properties_info);
402 if (count > 0) {
403 zend_property_info *prop;
404
405 ZEND_HASH_FOREACH_PTR(&ce->properties_info, prop) {
406 if ((prop->flags & ZEND_ACC_PRIVATE) && prop->ce != ce) {
407 count_shadow_props++;
408 } else if (prop->flags & ZEND_ACC_STATIC) {
409 count_static_props++;
410 }
411 } ZEND_HASH_FOREACH_END();
412 }
413
414 /* static properties */
415 smart_str_append_printf(str, "\n%s - Static properties [%d] {\n", indent, count_static_props);
416 if (count_static_props > 0) {
417 zend_property_info *prop;
418
419 ZEND_HASH_FOREACH_PTR(&ce->properties_info, prop) {
420 if ((prop->flags & ZEND_ACC_STATIC) && (!(prop->flags & ZEND_ACC_PRIVATE) || prop->ce == ce)) {
421 _property_string(str, prop, NULL, ZSTR_VAL(sub_indent));
422 }
423 } ZEND_HASH_FOREACH_END();
424 }
425 smart_str_append_printf(str, "%s }\n", indent);
426
427 /* Static methods */
428 /* counting static methods */
429 count = zend_hash_num_elements(&ce->function_table);
430 if (count > 0) {
431 zend_function *mptr;
432
433 ZEND_HASH_FOREACH_PTR(&ce->function_table, mptr) {
434 if ((mptr->common.fn_flags & ZEND_ACC_STATIC)
435 && ((mptr->common.fn_flags & ZEND_ACC_PRIVATE) == 0 || mptr->common.scope == ce))
436 {
437 count_static_funcs++;
438 }
439 } ZEND_HASH_FOREACH_END();
440 }
441
442 /* static methods */
443 smart_str_append_printf(str, "\n%s - Static methods [%d] {", indent, count_static_funcs);
444 if (count_static_funcs > 0) {
445 zend_function *mptr;
446
447 ZEND_HASH_FOREACH_PTR(&ce->function_table, mptr) {
448 if ((mptr->common.fn_flags & ZEND_ACC_STATIC)
449 && ((mptr->common.fn_flags & ZEND_ACC_PRIVATE) == 0 || mptr->common.scope == ce))
450 {
451 smart_str_append_printf(str, "\n");
452 _function_string(str, mptr, ce, ZSTR_VAL(sub_indent));
453 }
454 } ZEND_HASH_FOREACH_END();
455 } else {
456 smart_str_append_printf(str, "\n");
457 }
458 smart_str_append_printf(str, "%s }\n", indent);
459
460 /* Default/Implicit properties */
461 count = zend_hash_num_elements(&ce->properties_info) - count_static_props - count_shadow_props;
462 smart_str_append_printf(str, "\n%s - Properties [%d] {\n", indent, count);
463 if (count > 0) {
464 zend_property_info *prop;
465
466 ZEND_HASH_FOREACH_PTR(&ce->properties_info, prop) {
467 if (!(prop->flags & ZEND_ACC_STATIC)
468 && (!(prop->flags & ZEND_ACC_PRIVATE) || prop->ce == ce)) {
469 _property_string(str, prop, NULL, ZSTR_VAL(sub_indent));
470 }
471 } ZEND_HASH_FOREACH_END();
472 }
473 smart_str_append_printf(str, "%s }\n", indent);
474
475 if (obj && Z_TYPE_P(obj) == IS_OBJECT) {
476 HashTable *properties = Z_OBJ_HT_P(obj)->get_properties(Z_OBJ_P(obj));
477 zend_string *prop_name;
478 smart_str prop_str = {0};
479
480 count = 0;
481 if (properties && zend_hash_num_elements(properties)) {
482 ZEND_HASH_FOREACH_STR_KEY(properties, prop_name) {
483 if (prop_name && ZSTR_LEN(prop_name) && ZSTR_VAL(prop_name)[0]) { /* skip all private and protected properties */
484 if (!zend_hash_exists(&ce->properties_info, prop_name)) {
485 count++;
486 _property_string(&prop_str, NULL, ZSTR_VAL(prop_name), ZSTR_VAL(sub_indent));
487 }
488 }
489 } ZEND_HASH_FOREACH_END();
490 }
491
492 smart_str_append_printf(str, "\n%s - Dynamic properties [%d] {\n", indent, count);
493 smart_str_append_smart_str(str, &prop_str);
494 smart_str_append_printf(str, "%s }\n", indent);
495 smart_str_free(&prop_str);
496 }
497
498 /* Non static methods */
499 count = zend_hash_num_elements(&ce->function_table) - count_static_funcs;
500 if (count > 0) {
501 zend_function *mptr;
502 smart_str method_str = {0};
503
504 count = 0;
505 ZEND_HASH_FOREACH_PTR(&ce->function_table, mptr) {
506 if ((mptr->common.fn_flags & ZEND_ACC_STATIC) == 0
507 && ((mptr->common.fn_flags & ZEND_ACC_PRIVATE) == 0 || mptr->common.scope == ce))
508 {
509 zend_function *closure;
510 /* see if this is a closure */
511 if (obj && is_closure_invoke(ce, mptr->common.function_name)
512 && (closure = zend_get_closure_invoke_method(Z_OBJ_P(obj))) != NULL)
513 {
514 mptr = closure;
515 } else {
516 closure = NULL;
517 }
518 smart_str_appendc(&method_str, '\n');
519 _function_string(&method_str, mptr, ce, ZSTR_VAL(sub_indent));
520 count++;
521 _free_function(closure);
522 }
523 } ZEND_HASH_FOREACH_END();
524 smart_str_append_printf(str, "\n%s - Methods [%d] {", indent, count);
525 smart_str_append_smart_str(str, &method_str);
526 if (!count) {
527 smart_str_append_printf(str, "\n");
528 }
529 smart_str_free(&method_str);
530 } else {
531 smart_str_append_printf(str, "\n%s - Methods [0] {\n", indent);
532 }
533 smart_str_append_printf(str, "%s }\n", indent);
534
535 smart_str_append_printf(str, "%s}\n", indent);
536 zend_string_release_ex(sub_indent, 0);
537 }
538 /* }}} */
539
540 /* {{{ _const_string */
_const_string(smart_str * str,char * name,zval * value,char * indent)541 static void _const_string(smart_str *str, char *name, zval *value, char *indent)
542 {
543 const char *type = zend_zval_type_name(value);
544
545 if (Z_TYPE_P(value) == IS_ARRAY) {
546 smart_str_append_printf(str, "%s Constant [ %s %s ] { Array }\n",
547 indent, type, name);
548 } else if (Z_TYPE_P(value) == IS_STRING) {
549 smart_str_append_printf(str, "%s Constant [ %s %s ] { %s }\n",
550 indent, type, name, Z_STRVAL_P(value));
551 } else {
552 zend_string *tmp_value_str;
553 zend_string *value_str = zval_get_tmp_string(value, &tmp_value_str);
554 smart_str_append_printf(str, "%s Constant [ %s %s ] { %s }\n",
555 indent, type, name, ZSTR_VAL(value_str));
556 zend_tmp_string_release(tmp_value_str);
557 }
558 }
559 /* }}} */
560
561 /* {{{ _class_const_string */
_class_const_string(smart_str * str,char * name,zend_class_constant * c,char * indent)562 static void _class_const_string(smart_str *str, char *name, zend_class_constant *c, char *indent)
563 {
564 char *visibility = zend_visibility_string(Z_ACCESS_FLAGS(c->value));
565 const char *type;
566
567 if (zval_update_constant_ex(&c->value, c->ce) == FAILURE) {
568 return;
569 }
570
571 type = zend_zval_type_name(&c->value);
572
573 if (Z_TYPE(c->value) == IS_ARRAY) {
574 smart_str_append_printf(str, "%sConstant [ %s %s %s ] { Array }\n",
575 indent, visibility, type, name);
576 } else {
577 zend_string *tmp_value_str;
578 zend_string *value_str = zval_get_tmp_string(&c->value, &tmp_value_str);
579
580 smart_str_append_printf(str, "%sConstant [ %s %s %s ] { %s }\n",
581 indent, visibility, type, name, ZSTR_VAL(value_str));
582
583 zend_tmp_string_release(tmp_value_str);
584 }
585 }
586 /* }}} */
587
get_recv_op(zend_op_array * op_array,uint32_t offset)588 static zend_op *get_recv_op(zend_op_array *op_array, uint32_t offset)
589 {
590 zend_op *op = op_array->opcodes;
591 zend_op *end = op + op_array->last;
592
593 ++offset;
594 while (op < end) {
595 if ((op->opcode == ZEND_RECV || op->opcode == ZEND_RECV_INIT
596 || op->opcode == ZEND_RECV_VARIADIC) && op->op1.num == offset)
597 {
598 return op;
599 }
600 ++op;
601 }
602 ZEND_ASSERT(0 && "Failed to find op");
603 return NULL;
604 }
605
get_default_from_recv(zend_op_array * op_array,uint32_t offset)606 static zval *get_default_from_recv(zend_op_array *op_array, uint32_t offset) {
607 zend_op *recv = get_recv_op(op_array, offset);
608 if (!recv || recv->opcode != ZEND_RECV_INIT) {
609 return NULL;
610 }
611
612 return RT_CONSTANT(recv, recv->op2);
613 }
614
format_default_value(smart_str * str,zval * value,zend_class_entry * scope)615 static int format_default_value(smart_str *str, zval *value, zend_class_entry *scope) {
616 zval zv;
617 ZVAL_COPY(&zv, value);
618 if (UNEXPECTED(zval_update_constant_ex(&zv, scope) == FAILURE)) {
619 zval_ptr_dtor(&zv);
620 return FAILURE;
621 }
622
623 if (Z_TYPE(zv) == IS_TRUE) {
624 smart_str_appends(str, "true");
625 } else if (Z_TYPE(zv) == IS_FALSE) {
626 smart_str_appends(str, "false");
627 } else if (Z_TYPE(zv) == IS_NULL) {
628 smart_str_appends(str, "NULL");
629 } else if (Z_TYPE(zv) == IS_STRING) {
630 smart_str_appendc(str, '\'');
631 smart_str_appendl(str, Z_STRVAL(zv), MIN(Z_STRLEN(zv), 15));
632 if (Z_STRLEN(zv) > 15) {
633 smart_str_appends(str, "...");
634 }
635 smart_str_appendc(str, '\'');
636 } else if (Z_TYPE(zv) == IS_ARRAY) {
637 smart_str_appends(str, "Array");
638 } else {
639 zend_string *tmp_zv_str;
640 zend_string *zv_str = zval_get_tmp_string(&zv, &tmp_zv_str);
641 smart_str_append(str, zv_str);
642 zend_tmp_string_release(tmp_zv_str);
643 }
644 zval_ptr_dtor(&zv);
645 return SUCCESS;
646 }
647
has_internal_arg_info(const zend_function * fptr)648 static inline zend_bool has_internal_arg_info(const zend_function *fptr) {
649 return fptr->type == ZEND_INTERNAL_FUNCTION
650 && !(fptr->common.fn_flags & ZEND_ACC_USER_ARG_INFO);
651 }
652
653 /* {{{ _parameter_string */
_parameter_string(smart_str * str,zend_function * fptr,struct _zend_arg_info * arg_info,uint32_t offset,zend_bool required,char * indent)654 static void _parameter_string(smart_str *str, zend_function *fptr, struct _zend_arg_info *arg_info, uint32_t offset, zend_bool required, char* indent)
655 {
656 smart_str_append_printf(str, "Parameter #%d [ ", offset);
657 if (!required) {
658 smart_str_append_printf(str, "<optional> ");
659 } else {
660 smart_str_append_printf(str, "<required> ");
661 }
662 if (ZEND_TYPE_IS_SET(arg_info->type)) {
663 zend_string *type_str = zend_type_to_string(arg_info->type);
664 smart_str_append(str, type_str);
665 smart_str_appendc(str, ' ');
666 zend_string_release(type_str);
667 }
668 if (ZEND_ARG_SEND_MODE(arg_info)) {
669 smart_str_appendc(str, '&');
670 }
671 if (ZEND_ARG_IS_VARIADIC(arg_info)) {
672 smart_str_appends(str, "...");
673 }
674 smart_str_append_printf(str, "$%s", has_internal_arg_info(fptr)
675 ? ((zend_internal_arg_info*)arg_info)->name : ZSTR_VAL(arg_info->name));
676
677 if (!required && !ZEND_ARG_IS_VARIADIC(arg_info)) {
678 if (fptr->type == ZEND_INTERNAL_FUNCTION) {
679 smart_str_appends(str, " = ");
680 /* TODO: We don't have a way to fetch the default value for an internal function
681 * with userland arg info. */
682 if (has_internal_arg_info(fptr)
683 && ((zend_internal_arg_info*)arg_info)->default_value) {
684 smart_str_appends(str, ((zend_internal_arg_info*)arg_info)->default_value);
685 } else {
686 smart_str_appends(str, "<default>");
687 }
688 } else {
689 zval *default_value = get_default_from_recv((zend_op_array*)fptr, offset);
690 if (default_value) {
691 smart_str_appends(str, " = ");
692 if (format_default_value(str, default_value, fptr->common.scope) == FAILURE) {
693 return;
694 }
695 }
696 }
697 }
698 smart_str_appends(str, " ]");
699 }
700 /* }}} */
701
702 /* {{{ _function_parameter_string */
_function_parameter_string(smart_str * str,zend_function * fptr,char * indent)703 static void _function_parameter_string(smart_str *str, zend_function *fptr, char* indent)
704 {
705 struct _zend_arg_info *arg_info = fptr->common.arg_info;
706 uint32_t i, num_args, num_required = fptr->common.required_num_args;
707
708 if (!arg_info) {
709 return;
710 }
711
712 num_args = fptr->common.num_args;
713 if (fptr->common.fn_flags & ZEND_ACC_VARIADIC) {
714 num_args++;
715 }
716 smart_str_appendc(str, '\n');
717 smart_str_append_printf(str, "%s- Parameters [%d] {\n", indent, num_args);
718 for (i = 0; i < num_args; i++) {
719 smart_str_append_printf(str, "%s ", indent);
720 _parameter_string(str, fptr, arg_info, i, i < num_required, indent);
721 smart_str_appendc(str, '\n');
722 arg_info++;
723 }
724 smart_str_append_printf(str, "%s}\n", indent);
725 }
726 /* }}} */
727
728 /* {{{ _function_closure_string */
_function_closure_string(smart_str * str,zend_function * fptr,char * indent)729 static void _function_closure_string(smart_str *str, zend_function *fptr, char* indent)
730 {
731 uint32_t i, count;
732 zend_string *key;
733 HashTable *static_variables;
734
735 if (fptr->type != ZEND_USER_FUNCTION || !fptr->op_array.static_variables) {
736 return;
737 }
738
739 static_variables = ZEND_MAP_PTR_GET(fptr->op_array.static_variables_ptr);
740 count = zend_hash_num_elements(static_variables);
741
742 if (!count) {
743 return;
744 }
745
746 smart_str_append_printf(str, "\n");
747 smart_str_append_printf(str, "%s- Bound Variables [%d] {\n", indent, zend_hash_num_elements(static_variables));
748 i = 0;
749 ZEND_HASH_FOREACH_STR_KEY(static_variables, key) {
750 smart_str_append_printf(str, "%s Variable #%d [ $%s ]\n", indent, i++, ZSTR_VAL(key));
751 } ZEND_HASH_FOREACH_END();
752 smart_str_append_printf(str, "%s}\n", indent);
753 }
754 /* }}} */
755
756 /* {{{ _function_string */
_function_string(smart_str * str,zend_function * fptr,zend_class_entry * scope,char * indent)757 static void _function_string(smart_str *str, zend_function *fptr, zend_class_entry *scope, char* indent)
758 {
759 smart_str param_indent = {0};
760 zend_function *overwrites;
761 zend_string *lc_name;
762
763 /* TBD: Repair indenting of doc comment (or is this to be done in the parser?)
764 * What's "wrong" is that any whitespace before the doc comment start is
765 * swallowed, leading to an unaligned comment.
766 */
767 if (fptr->type == ZEND_USER_FUNCTION && fptr->op_array.doc_comment) {
768 smart_str_append_printf(str, "%s%s\n", indent, ZSTR_VAL(fptr->op_array.doc_comment));
769 }
770
771 smart_str_appendl(str, indent, strlen(indent));
772 smart_str_append_printf(str, fptr->common.fn_flags & ZEND_ACC_CLOSURE ? "Closure [ " : (fptr->common.scope ? "Method [ " : "Function [ "));
773 smart_str_append_printf(str, (fptr->type == ZEND_USER_FUNCTION) ? "<user" : "<internal");
774 if (fptr->common.fn_flags & ZEND_ACC_DEPRECATED) {
775 smart_str_appends(str, ", deprecated");
776 }
777 if (fptr->type == ZEND_INTERNAL_FUNCTION && ((zend_internal_function*)fptr)->module) {
778 smart_str_append_printf(str, ":%s", ((zend_internal_function*)fptr)->module->name);
779 }
780
781 if (scope && fptr->common.scope) {
782 if (fptr->common.scope != scope) {
783 smart_str_append_printf(str, ", inherits %s", ZSTR_VAL(fptr->common.scope->name));
784 } else if (fptr->common.scope->parent) {
785 lc_name = zend_string_tolower(fptr->common.function_name);
786 if ((overwrites = zend_hash_find_ptr(&fptr->common.scope->parent->function_table, lc_name)) != NULL) {
787 if (fptr->common.scope != overwrites->common.scope) {
788 smart_str_append_printf(str, ", overwrites %s", ZSTR_VAL(overwrites->common.scope->name));
789 }
790 }
791 zend_string_release_ex(lc_name, 0);
792 }
793 }
794 if (fptr->common.prototype && fptr->common.prototype->common.scope) {
795 smart_str_append_printf(str, ", prototype %s", ZSTR_VAL(fptr->common.prototype->common.scope->name));
796 }
797 if (fptr->common.fn_flags & ZEND_ACC_CTOR) {
798 smart_str_appends(str, ", ctor");
799 }
800 smart_str_appends(str, "> ");
801
802 if (fptr->common.fn_flags & ZEND_ACC_ABSTRACT) {
803 smart_str_appends(str, "abstract ");
804 }
805 if (fptr->common.fn_flags & ZEND_ACC_FINAL) {
806 smart_str_appends(str, "final ");
807 }
808 if (fptr->common.fn_flags & ZEND_ACC_STATIC) {
809 smart_str_appends(str, "static ");
810 }
811
812 if (fptr->common.scope) {
813 /* These are mutually exclusive */
814 switch (fptr->common.fn_flags & ZEND_ACC_PPP_MASK) {
815 case ZEND_ACC_PUBLIC:
816 smart_str_appends(str, "public ");
817 break;
818 case ZEND_ACC_PRIVATE:
819 smart_str_appends(str, "private ");
820 break;
821 case ZEND_ACC_PROTECTED:
822 smart_str_appends(str, "protected ");
823 break;
824 default:
825 smart_str_appends(str, "<visibility error> ");
826 break;
827 }
828 smart_str_appends(str, "method ");
829 } else {
830 smart_str_appends(str, "function ");
831 }
832
833 if (fptr->op_array.fn_flags & ZEND_ACC_RETURN_REFERENCE) {
834 smart_str_appendc(str, '&');
835 }
836 smart_str_append_printf(str, "%s ] {\n", ZSTR_VAL(fptr->common.function_name));
837 /* The information where a function is declared is only available for user classes */
838 if (fptr->type == ZEND_USER_FUNCTION) {
839 smart_str_append_printf(str, "%s @@ %s %d - %d\n", indent,
840 ZSTR_VAL(fptr->op_array.filename),
841 fptr->op_array.line_start,
842 fptr->op_array.line_end);
843 }
844 smart_str_append_printf(¶m_indent, "%s ", indent);
845 smart_str_0(¶m_indent);
846 if (fptr->common.fn_flags & ZEND_ACC_CLOSURE) {
847 _function_closure_string(str, fptr, ZSTR_VAL(param_indent.s));
848 }
849 _function_parameter_string(str, fptr, ZSTR_VAL(param_indent.s));
850 smart_str_free(¶m_indent);
851 if (fptr->op_array.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
852 smart_str_append_printf(str, " %s- Return [ ", indent);
853 if (ZEND_TYPE_IS_SET(fptr->common.arg_info[-1].type)) {
854 zend_string *type_str = zend_type_to_string(fptr->common.arg_info[-1].type);
855 smart_str_append_printf(str, "%s ", ZSTR_VAL(type_str));
856 zend_string_release(type_str);
857 }
858 smart_str_appends(str, "]\n");
859 }
860 smart_str_append_printf(str, "%s}\n", indent);
861 }
862 /* }}} */
863
property_get_default(zend_property_info * prop_info)864 static zval *property_get_default(zend_property_info *prop_info) {
865 zend_class_entry *ce = prop_info->ce;
866 if (prop_info->flags & ZEND_ACC_STATIC) {
867 zval *prop = &ce->default_static_members_table[prop_info->offset];
868 ZVAL_DEINDIRECT(prop);
869 return prop;
870 } else {
871 return &ce->default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
872 }
873 }
874
875 /* {{{ _property_string */
_property_string(smart_str * str,zend_property_info * prop,const char * prop_name,char * indent)876 static void _property_string(smart_str *str, zend_property_info *prop, const char *prop_name, char* indent)
877 {
878 smart_str_append_printf(str, "%sProperty [ ", indent);
879 if (!prop) {
880 smart_str_append_printf(str, "<dynamic> public $%s", prop_name);
881 } else {
882 /* These are mutually exclusive */
883 switch (prop->flags & ZEND_ACC_PPP_MASK) {
884 case ZEND_ACC_PUBLIC:
885 smart_str_appends(str, "public ");
886 break;
887 case ZEND_ACC_PRIVATE:
888 smart_str_appends(str, "private ");
889 break;
890 case ZEND_ACC_PROTECTED:
891 smart_str_appends(str, "protected ");
892 break;
893 }
894 if (prop->flags & ZEND_ACC_STATIC) {
895 smart_str_appends(str, "static ");
896 }
897 if (ZEND_TYPE_IS_SET(prop->type)) {
898 zend_string *type_str = zend_type_to_string(prop->type);
899 smart_str_append(str, type_str);
900 smart_str_appendc(str, ' ');
901 zend_string_release(type_str);
902 }
903 if (!prop_name) {
904 const char *class_name;
905 zend_unmangle_property_name(prop->name, &class_name, &prop_name);
906 }
907 smart_str_append_printf(str, "$%s", prop_name);
908
909 zval *default_value = property_get_default(prop);
910 if (!Z_ISUNDEF_P(default_value)) {
911 smart_str_appends(str, " = ");
912 if (format_default_value(str, default_value, prop->ce) == FAILURE) {
913 return;
914 }
915 }
916 }
917
918 smart_str_appends(str, " ]\n");
919 }
920 /* }}} */
921
_extension_ini_string(zend_ini_entry * ini_entry,smart_str * str,char * indent,int number)922 static void _extension_ini_string(zend_ini_entry *ini_entry, smart_str *str, char *indent, int number) /* {{{ */
923 {
924 char *comma = "";
925
926 if (number == ini_entry->module_number) {
927 smart_str_append_printf(str, " %sEntry [ %s <", indent, ZSTR_VAL(ini_entry->name));
928 if (ini_entry->modifiable == ZEND_INI_ALL) {
929 smart_str_appends(str, "ALL");
930 } else {
931 if (ini_entry->modifiable & ZEND_INI_USER) {
932 smart_str_appends(str, "USER");
933 comma = ",";
934 }
935 if (ini_entry->modifiable & ZEND_INI_PERDIR) {
936 smart_str_append_printf(str, "%sPERDIR", comma);
937 comma = ",";
938 }
939 if (ini_entry->modifiable & ZEND_INI_SYSTEM) {
940 smart_str_append_printf(str, "%sSYSTEM", comma);
941 }
942 }
943
944 smart_str_appends(str, "> ]\n");
945 smart_str_append_printf(str, " %s Current = '%s'\n", indent, ini_entry->value ? ZSTR_VAL(ini_entry->value) : "");
946 if (ini_entry->modified) {
947 smart_str_append_printf(str, " %s Default = '%s'\n", indent, ini_entry->orig_value ? ZSTR_VAL(ini_entry->orig_value) : "");
948 }
949 smart_str_append_printf(str, " %s}\n", indent);
950 }
951 }
952 /* }}} */
953
_extension_class_string(zend_class_entry * ce,zend_string * key,smart_str * str,char * indent,zend_module_entry * module,int * num_classes)954 static void _extension_class_string(zend_class_entry *ce, zend_string *key, smart_str *str, char *indent, zend_module_entry *module, int *num_classes) /* {{{ */
955 {
956 if (ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module && !strcasecmp(ce->info.internal.module->name, module->name)) {
957 /* dump class if it is not an alias */
958 if (zend_string_equals_ci(ce->name, key)) {
959 smart_str_append_printf(str, "\n");
960 _class_string(str, ce, NULL, indent);
961 (*num_classes)++;
962 }
963 }
964 }
965 /* }}} */
966
_extension_string(smart_str * str,zend_module_entry * module,char * indent)967 static void _extension_string(smart_str *str, zend_module_entry *module, char *indent) /* {{{ */
968 {
969 smart_str_append_printf(str, "%sExtension [ ", indent);
970 if (module->type == MODULE_PERSISTENT) {
971 smart_str_appends(str, "<persistent>");
972 }
973 if (module->type == MODULE_TEMPORARY) {
974 smart_str_appends(str, "<temporary>" );
975 }
976 smart_str_append_printf(str, " extension #%d %s version %s ] {\n",
977 module->module_number, module->name,
978 (module->version == NO_VERSION_YET) ? "<no_version>" : module->version);
979
980 if (module->deps) {
981 const zend_module_dep* dep = module->deps;
982
983 smart_str_appends(str, "\n - Dependencies {\n");
984
985 while(dep->name) {
986 smart_str_append_printf(str, "%s Dependency [ %s (", indent, dep->name);
987
988 switch(dep->type) {
989 case MODULE_DEP_REQUIRED:
990 smart_str_appends(str, "Required");
991 break;
992 case MODULE_DEP_CONFLICTS:
993 smart_str_appends(str, "Conflicts");
994 break;
995 case MODULE_DEP_OPTIONAL:
996 smart_str_appends(str, "Optional");
997 break;
998 default:
999 smart_str_appends(str, "Error"); /* shouldn't happen */
1000 break;
1001 }
1002
1003 if (dep->rel) {
1004 smart_str_append_printf(str, " %s", dep->rel);
1005 }
1006 if (dep->version) {
1007 smart_str_append_printf(str, " %s", dep->version);
1008 }
1009 smart_str_appends(str, ") ]\n");
1010 dep++;
1011 }
1012 smart_str_append_printf(str, "%s }\n", indent);
1013 }
1014
1015 {
1016 smart_str str_ini = {0};
1017 zend_ini_entry *ini_entry;
1018 ZEND_HASH_FOREACH_PTR(EG(ini_directives), ini_entry) {
1019 _extension_ini_string(ini_entry, &str_ini, indent, module->module_number);
1020 } ZEND_HASH_FOREACH_END();
1021 if (smart_str_get_len(&str_ini) > 0) {
1022 smart_str_append_printf(str, "\n - INI {\n");
1023 smart_str_append_smart_str(str, &str_ini);
1024 smart_str_append_printf(str, "%s }\n", indent);
1025 }
1026 smart_str_free(&str_ini);
1027 }
1028
1029 {
1030 smart_str str_constants = {0};
1031 zend_constant *constant;
1032 int num_constants = 0;
1033
1034 ZEND_HASH_FOREACH_PTR(EG(zend_constants), constant) {
1035 if (ZEND_CONSTANT_MODULE_NUMBER(constant) == module->module_number) {
1036 _const_string(&str_constants, ZSTR_VAL(constant->name), &constant->value, indent);
1037 num_constants++;
1038 }
1039 } ZEND_HASH_FOREACH_END();
1040
1041 if (num_constants) {
1042 smart_str_append_printf(str, "\n - Constants [%d] {\n", num_constants);
1043 smart_str_append_smart_str(str, &str_constants);
1044 smart_str_append_printf(str, "%s }\n", indent);
1045 }
1046 smart_str_free(&str_constants);
1047 }
1048
1049 {
1050 zend_function *fptr;
1051 int first = 1;
1052
1053 ZEND_HASH_FOREACH_PTR(CG(function_table), fptr) {
1054 if (fptr->common.type==ZEND_INTERNAL_FUNCTION
1055 && fptr->internal_function.module == module) {
1056 if (first) {
1057 smart_str_append_printf(str, "\n - Functions {\n");
1058 first = 0;
1059 }
1060 _function_string(str, fptr, NULL, " ");
1061 }
1062 } ZEND_HASH_FOREACH_END();
1063 if (!first) {
1064 smart_str_append_printf(str, "%s }\n", indent);
1065 }
1066 }
1067
1068 {
1069 zend_string *sub_indent = strpprintf(0, "%s ", indent);
1070 smart_str str_classes = {0};
1071 zend_string *key;
1072 zend_class_entry *ce;
1073 int num_classes = 0;
1074
1075 ZEND_HASH_FOREACH_STR_KEY_PTR(EG(class_table), key, ce) {
1076 _extension_class_string(ce, key, &str_classes, ZSTR_VAL(sub_indent), module, &num_classes);
1077 } ZEND_HASH_FOREACH_END();
1078 if (num_classes) {
1079 smart_str_append_printf(str, "\n - Classes [%d] {", num_classes);
1080 smart_str_append_smart_str(str, &str_classes);
1081 smart_str_append_printf(str, "%s }\n", indent);
1082 }
1083 smart_str_free(&str_classes);
1084 zend_string_release_ex(sub_indent, 0);
1085 }
1086
1087 smart_str_append_printf(str, "%s}\n", indent);
1088 }
1089 /* }}} */
1090
1091 /* {{{ reflection_attribute_factory */
reflection_attribute_factory(zval * object,HashTable * attributes,zend_attribute * data,zend_class_entry * scope,uint32_t target,zend_string * filename)1092 static void reflection_attribute_factory(zval *object, HashTable *attributes, zend_attribute *data,
1093 zend_class_entry *scope, uint32_t target, zend_string *filename)
1094 {
1095 reflection_object *intern;
1096 attribute_reference *reference;
1097
1098 reflection_instantiate(reflection_attribute_ptr, object);
1099 intern = Z_REFLECTION_P(object);
1100 reference = (attribute_reference*) emalloc(sizeof(attribute_reference));
1101 reference->attributes = attributes;
1102 reference->data = data;
1103 reference->scope = scope;
1104 reference->filename = filename ? zend_string_copy(filename) : NULL;
1105 reference->target = target;
1106 intern->ptr = reference;
1107 intern->ref_type = REF_TYPE_ATTRIBUTE;
1108 }
1109 /* }}} */
1110
read_attributes(zval * ret,HashTable * attributes,zend_class_entry * scope,uint32_t offset,uint32_t target,zend_string * name,zend_class_entry * base,zend_string * filename)1111 static int read_attributes(zval *ret, HashTable *attributes, zend_class_entry *scope,
1112 uint32_t offset, uint32_t target, zend_string *name, zend_class_entry *base, zend_string *filename) /* {{{ */
1113 {
1114 ZEND_ASSERT(attributes != NULL);
1115
1116 zend_attribute *attr;
1117 zval tmp;
1118
1119 if (name) {
1120 // Name based filtering using lowercased key.
1121 zend_string *filter = zend_string_tolower(name);
1122
1123 ZEND_HASH_FOREACH_PTR(attributes, attr) {
1124 if (attr->offset == offset && zend_string_equals(attr->lcname, filter)) {
1125 reflection_attribute_factory(&tmp, attributes, attr, scope, target, filename);
1126 add_next_index_zval(ret, &tmp);
1127 }
1128 } ZEND_HASH_FOREACH_END();
1129
1130 zend_string_release(filter);
1131 return SUCCESS;
1132 }
1133
1134 ZEND_HASH_FOREACH_PTR(attributes, attr) {
1135 if (attr->offset != offset) {
1136 continue;
1137 }
1138
1139 if (base) {
1140 // Base type filtering.
1141 zend_class_entry *ce = zend_lookup_class_ex(attr->name, attr->lcname, 0);
1142
1143 if (ce == NULL) {
1144 // Bailout on error, otherwise ignore unavailable class.
1145 if (EG(exception)) {
1146 return FAILURE;
1147 }
1148
1149 continue;
1150 }
1151
1152 if (!instanceof_function(ce, base)) {
1153 continue;
1154 }
1155 }
1156
1157 reflection_attribute_factory(&tmp, attributes, attr, scope, target, filename);
1158 add_next_index_zval(ret, &tmp);
1159 } ZEND_HASH_FOREACH_END();
1160
1161 return SUCCESS;
1162 }
1163 /* }}} */
1164
reflect_attributes(INTERNAL_FUNCTION_PARAMETERS,HashTable * attributes,uint32_t offset,zend_class_entry * scope,uint32_t target,zend_string * filename)1165 static void reflect_attributes(INTERNAL_FUNCTION_PARAMETERS, HashTable *attributes,
1166 uint32_t offset, zend_class_entry *scope, uint32_t target, zend_string *filename) /* {{{ */
1167 {
1168 zend_string *name = NULL;
1169 zend_long flags = 0;
1170 zend_class_entry *base = NULL;
1171
1172 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S!l", &name, &flags) == FAILURE) {
1173 RETURN_THROWS();
1174 }
1175
1176 if (flags & ~REFLECTION_ATTRIBUTE_IS_INSTANCEOF) {
1177 zend_argument_value_error(2, "must be a valid attribute filter flag");
1178 RETURN_THROWS();
1179 }
1180
1181 if (name && (flags & REFLECTION_ATTRIBUTE_IS_INSTANCEOF)) {
1182 if (NULL == (base = zend_lookup_class(name))) {
1183 if (!EG(exception)) {
1184 zend_throw_error(NULL, "Class \"%s\" not found", ZSTR_VAL(name));
1185 }
1186
1187 RETURN_THROWS();
1188 }
1189
1190 name = NULL;
1191 }
1192
1193 if (!attributes) {
1194 RETURN_EMPTY_ARRAY();
1195 }
1196
1197 array_init(return_value);
1198
1199 if (FAILURE == read_attributes(return_value, attributes, scope, offset, target, name, base, filename)) {
1200 RETURN_THROWS();
1201 }
1202 }
1203 /* }}} */
1204
_zend_extension_string(smart_str * str,zend_extension * extension,char * indent)1205 static void _zend_extension_string(smart_str *str, zend_extension *extension, char *indent) /* {{{ */
1206 {
1207 smart_str_append_printf(str, "%sZend Extension [ %s ", indent, extension->name);
1208
1209 if (extension->version) {
1210 smart_str_append_printf(str, "%s ", extension->version);
1211 }
1212 if (extension->copyright) {
1213 smart_str_append_printf(str, "%s ", extension->copyright);
1214 }
1215 if (extension->author) {
1216 smart_str_append_printf(str, "by %s ", extension->author);
1217 }
1218 if (extension->URL) {
1219 smart_str_append_printf(str, "<%s> ", extension->URL);
1220 }
1221
1222 smart_str_appends(str, "]\n");
1223 }
1224 /* }}} */
1225
1226 /* {{{ _function_check_flag */
_function_check_flag(INTERNAL_FUNCTION_PARAMETERS,int mask)1227 static void _function_check_flag(INTERNAL_FUNCTION_PARAMETERS, int mask)
1228 {
1229 reflection_object *intern;
1230 zend_function *mptr;
1231
1232 if (zend_parse_parameters_none() == FAILURE) {
1233 RETURN_THROWS();
1234 }
1235 GET_REFLECTION_OBJECT_PTR(mptr);
1236 RETURN_BOOL(mptr->common.fn_flags & mask);
1237 }
1238 /* }}} */
1239
1240 /* {{{ zend_reflection_class_factory */
zend_reflection_class_factory(zend_class_entry * ce,zval * object)1241 PHPAPI void zend_reflection_class_factory(zend_class_entry *ce, zval *object)
1242 {
1243 reflection_object *intern;
1244
1245 reflection_instantiate(reflection_class_ptr, object);
1246 intern = Z_REFLECTION_P(object);
1247 intern->ptr = ce;
1248 intern->ref_type = REF_TYPE_OTHER;
1249 intern->ce = ce;
1250 ZVAL_STR_COPY(reflection_prop_name(object), ce->name);
1251 }
1252 /* }}} */
1253
1254 /* {{{ reflection_extension_factory */
reflection_extension_factory(zval * object,const char * name_str)1255 static void reflection_extension_factory(zval *object, const char *name_str)
1256 {
1257 reflection_object *intern;
1258 size_t name_len = strlen(name_str);
1259 zend_string *lcname;
1260 struct _zend_module_entry *module;
1261
1262 lcname = zend_string_alloc(name_len, 0);
1263 zend_str_tolower_copy(ZSTR_VAL(lcname), name_str, name_len);
1264 module = zend_hash_find_ptr(&module_registry, lcname);
1265 zend_string_efree(lcname);
1266 if (!module) {
1267 return;
1268 }
1269
1270 reflection_instantiate(reflection_extension_ptr, object);
1271 intern = Z_REFLECTION_P(object);
1272 intern->ptr = module;
1273 intern->ref_type = REF_TYPE_OTHER;
1274 intern->ce = NULL;
1275 ZVAL_STRINGL(reflection_prop_name(object), module->name, name_len);
1276 }
1277 /* }}} */
1278
1279 /* {{{ reflection_parameter_factory */
reflection_parameter_factory(zend_function * fptr,zval * closure_object,struct _zend_arg_info * arg_info,uint32_t offset,zend_bool required,zval * object)1280 static void reflection_parameter_factory(zend_function *fptr, zval *closure_object, struct _zend_arg_info *arg_info, uint32_t offset, zend_bool required, zval *object)
1281 {
1282 reflection_object *intern;
1283 parameter_reference *reference;
1284 zval *prop_name;
1285
1286 reflection_instantiate(reflection_parameter_ptr, object);
1287 intern = Z_REFLECTION_P(object);
1288 reference = (parameter_reference*) emalloc(sizeof(parameter_reference));
1289 reference->arg_info = arg_info;
1290 reference->offset = offset;
1291 reference->required = required;
1292 reference->fptr = fptr;
1293 intern->ptr = reference;
1294 intern->ref_type = REF_TYPE_PARAMETER;
1295 intern->ce = fptr->common.scope;
1296 if (closure_object) {
1297 ZVAL_OBJ_COPY(&intern->obj, Z_OBJ_P(closure_object));
1298 }
1299
1300 prop_name = reflection_prop_name(object);
1301 if (has_internal_arg_info(fptr)) {
1302 ZVAL_STRING(prop_name, ((zend_internal_arg_info*)arg_info)->name);
1303 } else {
1304 ZVAL_STR_COPY(prop_name, arg_info->name);
1305 }
1306 }
1307 /* }}} */
1308
1309 /* For backwards compatibility reasons, we need to return T|null style unions
1310 * as a ReflectionNamedType. Here we determine what counts as a union type and
1311 * what doesn't. */
is_union_type(zend_type type)1312 static zend_bool is_union_type(zend_type type) {
1313 if (ZEND_TYPE_HAS_LIST(type)) {
1314 return 1;
1315 }
1316 uint32_t type_mask_without_null = ZEND_TYPE_PURE_MASK_WITHOUT_NULL(type);
1317 if (ZEND_TYPE_HAS_CLASS(type)) {
1318 return type_mask_without_null != 0;
1319 }
1320 if (type_mask_without_null == MAY_BE_BOOL) {
1321 return 0;
1322 }
1323 /* Check that only one bit is set. */
1324 return (type_mask_without_null & (type_mask_without_null - 1)) != 0;
1325 }
1326
1327 /* {{{ reflection_type_factory */
reflection_type_factory(zend_type type,zval * object,zend_bool legacy_behavior)1328 static void reflection_type_factory(zend_type type, zval *object, zend_bool legacy_behavior)
1329 {
1330 reflection_object *intern;
1331 type_reference *reference;
1332 zend_bool is_union = is_union_type(type);
1333 zend_bool is_mixed = ZEND_TYPE_PURE_MASK(type) == MAY_BE_ANY;
1334
1335 reflection_instantiate(is_union && !is_mixed ? reflection_union_type_ptr : reflection_named_type_ptr, object);
1336 intern = Z_REFLECTION_P(object);
1337 reference = (type_reference*) emalloc(sizeof(type_reference));
1338 reference->type = type;
1339 reference->legacy_behavior = legacy_behavior && !is_union && !is_mixed;
1340 intern->ptr = reference;
1341 intern->ref_type = REF_TYPE_TYPE;
1342
1343 /* Property types may be resolved during the lifetime of the ReflectionType.
1344 * If we reference a string, make sure it doesn't get released. However, only
1345 * do this for the top-level type, as resolutions inside type lists will be
1346 * fully visible to us (we'd have to do a fully copy of the type if we wanted
1347 * to prevent that). */
1348 if (ZEND_TYPE_HAS_NAME(type)) {
1349 zend_string_addref(ZEND_TYPE_NAME(type));
1350 }
1351 }
1352 /* }}} */
1353
1354 /* {{{ reflection_function_factory */
reflection_function_factory(zend_function * function,zval * closure_object,zval * object)1355 static void reflection_function_factory(zend_function *function, zval *closure_object, zval *object)
1356 {
1357 reflection_object *intern;
1358 reflection_instantiate(reflection_function_ptr, object);
1359 intern = Z_REFLECTION_P(object);
1360 intern->ptr = function;
1361 intern->ref_type = REF_TYPE_FUNCTION;
1362 intern->ce = NULL;
1363 if (closure_object) {
1364 ZVAL_OBJ_COPY(&intern->obj, Z_OBJ_P(closure_object));
1365 }
1366 ZVAL_STR_COPY(reflection_prop_name(object), function->common.function_name);
1367 }
1368 /* }}} */
1369
1370 /* {{{ reflection_method_factory */
reflection_method_factory(zend_class_entry * ce,zend_function * method,zval * closure_object,zval * object)1371 static void reflection_method_factory(zend_class_entry *ce, zend_function *method, zval *closure_object, zval *object)
1372 {
1373 reflection_object *intern;
1374
1375 reflection_instantiate(reflection_method_ptr, object);
1376 intern = Z_REFLECTION_P(object);
1377 intern->ptr = method;
1378 intern->ref_type = REF_TYPE_FUNCTION;
1379 intern->ce = ce;
1380 if (closure_object) {
1381 ZVAL_OBJ_COPY(&intern->obj, Z_OBJ_P(closure_object));
1382 }
1383
1384 ZVAL_STR_COPY(reflection_prop_name(object), method->common.function_name);
1385 ZVAL_STR_COPY(reflection_prop_class(object), method->common.scope->name);
1386 }
1387 /* }}} */
1388
1389 /* {{{ reflection_property_factory */
reflection_property_factory(zend_class_entry * ce,zend_string * name,zend_property_info * prop,zval * object)1390 static void reflection_property_factory(zend_class_entry *ce, zend_string *name, zend_property_info *prop, zval *object)
1391 {
1392 reflection_object *intern;
1393 property_reference *reference;
1394
1395 reflection_instantiate(reflection_property_ptr, object);
1396 intern = Z_REFLECTION_P(object);
1397 reference = (property_reference*) emalloc(sizeof(property_reference));
1398 reference->prop = prop;
1399 reference->unmangled_name = zend_string_copy(name);
1400 intern->ptr = reference;
1401 intern->ref_type = REF_TYPE_PROPERTY;
1402 intern->ce = ce;
1403 intern->ignore_visibility = 0;
1404 ZVAL_STR_COPY(reflection_prop_name(object), name);
1405 ZVAL_STR_COPY(reflection_prop_class(object), prop ? prop->ce->name : ce->name);
1406 }
1407 /* }}} */
1408
reflection_property_factory_str(zend_class_entry * ce,const char * name_str,size_t name_len,zend_property_info * prop,zval * object)1409 static void reflection_property_factory_str(zend_class_entry *ce, const char *name_str, size_t name_len, zend_property_info *prop, zval *object)
1410 {
1411 zend_string *name = zend_string_init(name_str, name_len, 0);
1412 reflection_property_factory(ce, name, prop, object);
1413 zend_string_release(name);
1414 }
1415
1416 /* {{{ reflection_class_constant_factory */
reflection_class_constant_factory(zend_string * name_str,zend_class_constant * constant,zval * object)1417 static void reflection_class_constant_factory(zend_string *name_str, zend_class_constant *constant, zval *object)
1418 {
1419 reflection_object *intern;
1420
1421 reflection_instantiate(reflection_class_constant_ptr, object);
1422 intern = Z_REFLECTION_P(object);
1423 intern->ptr = constant;
1424 intern->ref_type = REF_TYPE_CLASS_CONSTANT;
1425 intern->ce = constant->ce;
1426 intern->ignore_visibility = 0;
1427
1428 ZVAL_STR_COPY(reflection_prop_name(object), name_str);
1429 ZVAL_STR_COPY(reflection_prop_class(object), constant->ce->name);
1430 }
1431 /* }}} */
1432
get_parameter_default(zval * result,parameter_reference * param)1433 static int get_parameter_default(zval *result, parameter_reference *param) {
1434 if (param->fptr->type == ZEND_INTERNAL_FUNCTION) {
1435 if (param->fptr->common.fn_flags & ZEND_ACC_USER_ARG_INFO) {
1436 /* We don't have a way to determine the default value for this case right now. */
1437 return FAILURE;
1438 }
1439 return zend_get_default_from_internal_arg_info(
1440 result, (zend_internal_arg_info *) param->arg_info);
1441 } else {
1442 zval *default_value = get_default_from_recv((zend_op_array *) param->fptr, param->offset);
1443 if (!default_value) {
1444 return FAILURE;
1445 }
1446
1447 ZVAL_COPY(result, default_value);
1448 return SUCCESS;
1449 }
1450 }
1451
1452 /* {{{ Preventing __clone from being called */
ZEND_METHOD(ReflectionClass,__clone)1453 ZEND_METHOD(ReflectionClass, __clone)
1454 {
1455 /* Should never be executable */
1456 _DO_THROW("Cannot clone object using __clone()");
1457 }
1458 /* }}} */
1459
1460 /* {{{ Returns an array of modifier names */
ZEND_METHOD(Reflection,getModifierNames)1461 ZEND_METHOD(Reflection, getModifierNames)
1462 {
1463 zend_long modifiers;
1464
1465 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &modifiers) == FAILURE) {
1466 RETURN_THROWS();
1467 }
1468
1469 array_init(return_value);
1470
1471 if (modifiers & (ZEND_ACC_ABSTRACT | ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
1472 add_next_index_stringl(return_value, "abstract", sizeof("abstract")-1);
1473 }
1474 if (modifiers & ZEND_ACC_FINAL) {
1475 add_next_index_stringl(return_value, "final", sizeof("final")-1);
1476 }
1477
1478 /* These are mutually exclusive */
1479 switch (modifiers & ZEND_ACC_PPP_MASK) {
1480 case ZEND_ACC_PUBLIC:
1481 add_next_index_stringl(return_value, "public", sizeof("public")-1);
1482 break;
1483 case ZEND_ACC_PRIVATE:
1484 add_next_index_stringl(return_value, "private", sizeof("private")-1);
1485 break;
1486 case ZEND_ACC_PROTECTED:
1487 add_next_index_stringl(return_value, "protected", sizeof("protected")-1);
1488 break;
1489 }
1490
1491 if (modifiers & ZEND_ACC_STATIC) {
1492 add_next_index_stringl(return_value, "static", sizeof("static")-1);
1493 }
1494 }
1495 /* }}} */
1496
1497 /* {{{ Constructor. Throws an Exception in case the given function does not exist */
ZEND_METHOD(ReflectionFunction,__construct)1498 ZEND_METHOD(ReflectionFunction, __construct)
1499 {
1500 zval *object;
1501 zend_object *closure_obj = NULL;
1502 reflection_object *intern;
1503 zend_function *fptr;
1504 zend_string *fname, *lcname;
1505
1506 object = ZEND_THIS;
1507 intern = Z_REFLECTION_P(object);
1508
1509 ZEND_PARSE_PARAMETERS_START(1, 1)
1510 Z_PARAM_OBJ_OF_CLASS_OR_STR(closure_obj, zend_ce_closure, fname)
1511 ZEND_PARSE_PARAMETERS_END();
1512
1513 if (closure_obj) {
1514 fptr = (zend_function*)zend_get_closure_method_def(closure_obj);
1515 } else {
1516 if (UNEXPECTED(ZSTR_VAL(fname)[0] == '\\')) {
1517 /* Ignore leading "\" */
1518 ALLOCA_FLAG(use_heap)
1519 ZSTR_ALLOCA_ALLOC(lcname, ZSTR_LEN(fname) - 1, use_heap);
1520 zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(fname) + 1, ZSTR_LEN(fname) - 1);
1521 fptr = zend_fetch_function(lcname);
1522 ZSTR_ALLOCA_FREE(lcname, use_heap);
1523 } else {
1524 lcname = zend_string_tolower(fname);
1525 fptr = zend_fetch_function(lcname);
1526 zend_string_release(lcname);
1527 }
1528
1529 if (fptr == NULL) {
1530 zend_throw_exception_ex(reflection_exception_ptr, 0,
1531 "Function %s() does not exist", ZSTR_VAL(fname));
1532 RETURN_THROWS();
1533 }
1534 }
1535
1536 if (intern->ptr) {
1537 zval_ptr_dtor(&intern->obj);
1538 zval_ptr_dtor(reflection_prop_name(object));
1539 }
1540
1541 ZVAL_STR_COPY(reflection_prop_name(object), fptr->common.function_name);
1542 intern->ptr = fptr;
1543 intern->ref_type = REF_TYPE_FUNCTION;
1544 if (closure_obj) {
1545 ZVAL_OBJ_COPY(&intern->obj, closure_obj);
1546 } else {
1547 ZVAL_UNDEF(&intern->obj);
1548 }
1549 intern->ce = NULL;
1550 }
1551 /* }}} */
1552
1553 /* {{{ Returns a string representation */
ZEND_METHOD(ReflectionFunction,__toString)1554 ZEND_METHOD(ReflectionFunction, __toString)
1555 {
1556 reflection_object *intern;
1557 zend_function *fptr;
1558 smart_str str = {0};
1559
1560 if (zend_parse_parameters_none() == FAILURE) {
1561 RETURN_THROWS();
1562 }
1563 GET_REFLECTION_OBJECT_PTR(fptr);
1564 _function_string(&str, fptr, intern->ce, "");
1565 RETURN_STR(smart_str_extract(&str));
1566 }
1567 /* }}} */
1568
1569 /* {{{ Returns this function's name */
ZEND_METHOD(ReflectionFunctionAbstract,getName)1570 ZEND_METHOD(ReflectionFunctionAbstract, getName)
1571 {
1572 reflection_object *intern;
1573 zend_function *fptr;
1574
1575 if (zend_parse_parameters_none() == FAILURE) {
1576 RETURN_THROWS();
1577 }
1578
1579 GET_REFLECTION_OBJECT_PTR(fptr);
1580 RETURN_STR_COPY(fptr->common.function_name);
1581 }
1582 /* }}} */
1583
1584 /* {{{ Returns whether this is a closure */
ZEND_METHOD(ReflectionFunctionAbstract,isClosure)1585 ZEND_METHOD(ReflectionFunctionAbstract, isClosure)
1586 {
1587 reflection_object *intern;
1588 zend_function *fptr;
1589
1590 if (zend_parse_parameters_none() == FAILURE) {
1591 RETURN_THROWS();
1592 }
1593 GET_REFLECTION_OBJECT_PTR(fptr);
1594 RETURN_BOOL(fptr->common.fn_flags & ZEND_ACC_CLOSURE);
1595 }
1596 /* }}} */
1597
1598 /* {{{ Returns this pointer bound to closure */
ZEND_METHOD(ReflectionFunctionAbstract,getClosureThis)1599 ZEND_METHOD(ReflectionFunctionAbstract, getClosureThis)
1600 {
1601 reflection_object *intern;
1602 zval* closure_this;
1603
1604 if (zend_parse_parameters_none() == FAILURE) {
1605 RETURN_THROWS();
1606 }
1607 GET_REFLECTION_OBJECT();
1608 if (!Z_ISUNDEF(intern->obj)) {
1609 closure_this = zend_get_closure_this_ptr(&intern->obj);
1610 if (!Z_ISUNDEF_P(closure_this)) {
1611 RETURN_OBJ_COPY(Z_OBJ_P(closure_this));
1612 }
1613 }
1614 }
1615 /* }}} */
1616
1617 /* {{{ Returns the scope associated to the closure */
ZEND_METHOD(ReflectionFunctionAbstract,getClosureScopeClass)1618 ZEND_METHOD(ReflectionFunctionAbstract, getClosureScopeClass)
1619 {
1620 reflection_object *intern;
1621 const zend_function *closure_func;
1622
1623 if (zend_parse_parameters_none() == FAILURE) {
1624 RETURN_THROWS();
1625 }
1626 GET_REFLECTION_OBJECT();
1627 if (!Z_ISUNDEF(intern->obj)) {
1628 closure_func = zend_get_closure_method_def(Z_OBJ(intern->obj));
1629 if (closure_func && closure_func->common.scope) {
1630 zend_reflection_class_factory(closure_func->common.scope, return_value);
1631 }
1632 }
1633 }
1634 /* }}} */
1635
1636 /* {{{ Returns a dynamically created closure for the function */
ZEND_METHOD(ReflectionFunction,getClosure)1637 ZEND_METHOD(ReflectionFunction, getClosure)
1638 {
1639 reflection_object *intern;
1640 zend_function *fptr;
1641
1642 if (zend_parse_parameters_none() == FAILURE) {
1643 RETURN_THROWS();
1644 }
1645 GET_REFLECTION_OBJECT_PTR(fptr);
1646
1647 if (!Z_ISUNDEF(intern->obj)) {
1648 /* Closures are immutable objects */
1649 RETURN_OBJ_COPY(Z_OBJ(intern->obj));
1650 } else {
1651 zend_create_fake_closure(return_value, fptr, NULL, NULL, NULL);
1652 }
1653 }
1654 /* }}} */
1655
1656 /* {{{ Returns whether this is an internal function */
ZEND_METHOD(ReflectionFunctionAbstract,isInternal)1657 ZEND_METHOD(ReflectionFunctionAbstract, isInternal)
1658 {
1659 reflection_object *intern;
1660 zend_function *fptr;
1661
1662 if (zend_parse_parameters_none() == FAILURE) {
1663 RETURN_THROWS();
1664 }
1665 GET_REFLECTION_OBJECT_PTR(fptr);
1666 RETURN_BOOL(fptr->type == ZEND_INTERNAL_FUNCTION);
1667 }
1668 /* }}} */
1669
1670 /* {{{ Returns whether this is a user-defined function */
ZEND_METHOD(ReflectionFunctionAbstract,isUserDefined)1671 ZEND_METHOD(ReflectionFunctionAbstract, isUserDefined)
1672 {
1673 reflection_object *intern;
1674 zend_function *fptr;
1675
1676 if (zend_parse_parameters_none() == FAILURE) {
1677 RETURN_THROWS();
1678 }
1679 GET_REFLECTION_OBJECT_PTR(fptr);
1680 RETURN_BOOL(fptr->type == ZEND_USER_FUNCTION);
1681 }
1682 /* }}} */
1683
1684 /* {{{ Returns whether this function has been disabled or not */
ZEND_METHOD(ReflectionFunction,isDisabled)1685 ZEND_METHOD(ReflectionFunction, isDisabled)
1686 {
1687 if (zend_parse_parameters_none() == FAILURE) {
1688 RETURN_THROWS();
1689 }
1690
1691 /* A disabled function cannot be queried using Reflection. */
1692 RETURN_FALSE;
1693 }
1694 /* }}} */
1695
1696 /* {{{ Returns the filename of the file this function was declared in */
ZEND_METHOD(ReflectionFunctionAbstract,getFileName)1697 ZEND_METHOD(ReflectionFunctionAbstract, getFileName)
1698 {
1699 reflection_object *intern;
1700 zend_function *fptr;
1701
1702 if (zend_parse_parameters_none() == FAILURE) {
1703 RETURN_THROWS();
1704 }
1705 GET_REFLECTION_OBJECT_PTR(fptr);
1706 if (fptr->type == ZEND_USER_FUNCTION) {
1707 RETURN_STR_COPY(fptr->op_array.filename);
1708 }
1709 RETURN_FALSE;
1710 }
1711 /* }}} */
1712
1713 /* {{{ Returns the line this function's declaration starts at */
ZEND_METHOD(ReflectionFunctionAbstract,getStartLine)1714 ZEND_METHOD(ReflectionFunctionAbstract, getStartLine)
1715 {
1716 reflection_object *intern;
1717 zend_function *fptr;
1718
1719 if (zend_parse_parameters_none() == FAILURE) {
1720 RETURN_THROWS();
1721 }
1722 GET_REFLECTION_OBJECT_PTR(fptr);
1723 if (fptr->type == ZEND_USER_FUNCTION) {
1724 RETURN_LONG(fptr->op_array.line_start);
1725 }
1726 RETURN_FALSE;
1727 }
1728 /* }}} */
1729
1730 /* {{{ Returns the line this function's declaration ends at */
ZEND_METHOD(ReflectionFunctionAbstract,getEndLine)1731 ZEND_METHOD(ReflectionFunctionAbstract, getEndLine)
1732 {
1733 reflection_object *intern;
1734 zend_function *fptr;
1735
1736 if (zend_parse_parameters_none() == FAILURE) {
1737 RETURN_THROWS();
1738 }
1739 GET_REFLECTION_OBJECT_PTR(fptr);
1740 if (fptr->type == ZEND_USER_FUNCTION) {
1741 RETURN_LONG(fptr->op_array.line_end);
1742 }
1743 RETURN_FALSE;
1744 }
1745 /* }}} */
1746
1747 /* {{{ Returns the doc comment for this function */
ZEND_METHOD(ReflectionFunctionAbstract,getDocComment)1748 ZEND_METHOD(ReflectionFunctionAbstract, getDocComment)
1749 {
1750 reflection_object *intern;
1751 zend_function *fptr;
1752
1753 if (zend_parse_parameters_none() == FAILURE) {
1754 RETURN_THROWS();
1755 }
1756 GET_REFLECTION_OBJECT_PTR(fptr);
1757 if (fptr->type == ZEND_USER_FUNCTION && fptr->op_array.doc_comment) {
1758 RETURN_STR_COPY(fptr->op_array.doc_comment);
1759 }
1760 RETURN_FALSE;
1761 }
1762 /* }}} */
1763
1764 /* {{{ Returns the attributes of this function */
ZEND_METHOD(ReflectionFunctionAbstract,getAttributes)1765 ZEND_METHOD(ReflectionFunctionAbstract, getAttributes)
1766 {
1767 reflection_object *intern;
1768 zend_function *fptr;
1769 uint32_t target;
1770
1771 GET_REFLECTION_OBJECT_PTR(fptr);
1772
1773 if (fptr->common.scope) {
1774 target = ZEND_ATTRIBUTE_TARGET_METHOD;
1775 } else {
1776 target = ZEND_ATTRIBUTE_TARGET_FUNCTION;
1777 }
1778
1779 reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU,
1780 fptr->common.attributes, 0, fptr->common.scope, target,
1781 fptr->type == ZEND_USER_FUNCTION ? fptr->op_array.filename : NULL);
1782 }
1783 /* }}} */
1784
1785 /* {{{ Returns an associative array containing this function's static variables and their values */
ZEND_METHOD(ReflectionFunctionAbstract,getStaticVariables)1786 ZEND_METHOD(ReflectionFunctionAbstract, getStaticVariables)
1787 {
1788 reflection_object *intern;
1789 zend_function *fptr;
1790 zval *val;
1791
1792 if (zend_parse_parameters_none() == FAILURE) {
1793 RETURN_THROWS();
1794 }
1795 GET_REFLECTION_OBJECT_PTR(fptr);
1796
1797 /* Return an empty array in case no static variables exist */
1798 if (fptr->type == ZEND_USER_FUNCTION && fptr->op_array.static_variables != NULL) {
1799 HashTable *ht;
1800
1801 array_init(return_value);
1802 ht = ZEND_MAP_PTR_GET(fptr->op_array.static_variables_ptr);
1803 if (!ht) {
1804 ZEND_ASSERT(fptr->op_array.fn_flags & ZEND_ACC_IMMUTABLE);
1805 ht = zend_array_dup(fptr->op_array.static_variables);
1806 ZEND_MAP_PTR_SET(fptr->op_array.static_variables_ptr, ht);
1807 }
1808 ZEND_HASH_FOREACH_VAL(ht, val) {
1809 if (UNEXPECTED(zval_update_constant_ex(val, fptr->common.scope) != SUCCESS)) {
1810 RETURN_THROWS();
1811 }
1812 } ZEND_HASH_FOREACH_END();
1813 zend_hash_copy(Z_ARRVAL_P(return_value), ht, zval_add_ref);
1814 } else {
1815 RETURN_EMPTY_ARRAY();
1816 }
1817 }
1818 /* }}} */
1819
1820 /* {{{ Invokes the function */
ZEND_METHOD(ReflectionFunction,invoke)1821 ZEND_METHOD(ReflectionFunction, invoke)
1822 {
1823 zval retval;
1824 zval *params;
1825 int result, num_args;
1826 HashTable *named_params;
1827 zend_fcall_info fci;
1828 zend_fcall_info_cache fcc;
1829 reflection_object *intern;
1830 zend_function *fptr;
1831
1832 ZEND_PARSE_PARAMETERS_START(0, -1)
1833 Z_PARAM_VARIADIC_WITH_NAMED(params, num_args, named_params)
1834 ZEND_PARSE_PARAMETERS_END();
1835
1836 GET_REFLECTION_OBJECT_PTR(fptr);
1837
1838 fci.size = sizeof(fci);
1839 ZVAL_UNDEF(&fci.function_name);
1840 fci.object = NULL;
1841 fci.retval = &retval;
1842 fci.param_count = num_args;
1843 fci.params = params;
1844 fci.named_params = named_params;
1845
1846 fcc.function_handler = fptr;
1847 fcc.called_scope = NULL;
1848 fcc.object = NULL;
1849
1850 if (!Z_ISUNDEF(intern->obj)) {
1851 Z_OBJ_HT(intern->obj)->get_closure(
1852 Z_OBJ(intern->obj), &fcc.called_scope, &fcc.function_handler, &fcc.object, 0);
1853 }
1854
1855 result = zend_call_function(&fci, &fcc);
1856
1857 if (result == FAILURE) {
1858 zend_throw_exception_ex(reflection_exception_ptr, 0,
1859 "Invocation of function %s() failed", ZSTR_VAL(fptr->common.function_name));
1860 RETURN_THROWS();
1861 }
1862
1863 if (Z_TYPE(retval) != IS_UNDEF) {
1864 if (Z_ISREF(retval)) {
1865 zend_unwrap_reference(&retval);
1866 }
1867 ZVAL_COPY_VALUE(return_value, &retval);
1868 }
1869 }
1870 /* }}} */
1871
1872 /* {{{ Invokes the function and pass its arguments as array. */
ZEND_METHOD(ReflectionFunction,invokeArgs)1873 ZEND_METHOD(ReflectionFunction, invokeArgs)
1874 {
1875 zval retval;
1876 int result;
1877 zend_fcall_info fci;
1878 zend_fcall_info_cache fcc;
1879 reflection_object *intern;
1880 zend_function *fptr;
1881 HashTable *params;
1882
1883 GET_REFLECTION_OBJECT_PTR(fptr);
1884
1885 if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", ¶ms) == FAILURE) {
1886 RETURN_THROWS();
1887 }
1888
1889 fci.size = sizeof(fci);
1890 ZVAL_UNDEF(&fci.function_name);
1891 fci.object = NULL;
1892 fci.retval = &retval;
1893 fci.param_count = 0;
1894 fci.params = NULL;
1895 fci.named_params = params;
1896
1897 fcc.function_handler = fptr;
1898 fcc.called_scope = NULL;
1899 fcc.object = NULL;
1900
1901 if (!Z_ISUNDEF(intern->obj)) {
1902 Z_OBJ_HT(intern->obj)->get_closure(
1903 Z_OBJ(intern->obj), &fcc.called_scope, &fcc.function_handler, &fcc.object, 0);
1904 }
1905
1906 result = zend_call_function(&fci, &fcc);
1907
1908 if (result == FAILURE) {
1909 zend_throw_exception_ex(reflection_exception_ptr, 0,
1910 "Invocation of function %s() failed", ZSTR_VAL(fptr->common.function_name));
1911 RETURN_THROWS();
1912 }
1913
1914 if (Z_TYPE(retval) != IS_UNDEF) {
1915 if (Z_ISREF(retval)) {
1916 zend_unwrap_reference(&retval);
1917 }
1918 ZVAL_COPY_VALUE(return_value, &retval);
1919 }
1920 }
1921 /* }}} */
1922
1923 /* {{{ Gets whether this function returns a reference */
ZEND_METHOD(ReflectionFunctionAbstract,returnsReference)1924 ZEND_METHOD(ReflectionFunctionAbstract, returnsReference)
1925 {
1926 reflection_object *intern;
1927 zend_function *fptr;
1928
1929 GET_REFLECTION_OBJECT_PTR(fptr);
1930
1931 if (zend_parse_parameters_none() == FAILURE) {
1932 RETURN_THROWS();
1933 }
1934
1935 RETURN_BOOL((fptr->op_array.fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0);
1936 }
1937 /* }}} */
1938
1939 /* {{{ Gets the number of parameters */
ZEND_METHOD(ReflectionFunctionAbstract,getNumberOfParameters)1940 ZEND_METHOD(ReflectionFunctionAbstract, getNumberOfParameters)
1941 {
1942 reflection_object *intern;
1943 zend_function *fptr;
1944 uint32_t num_args;
1945
1946 GET_REFLECTION_OBJECT_PTR(fptr);
1947
1948 if (zend_parse_parameters_none() == FAILURE) {
1949 RETURN_THROWS();
1950 }
1951
1952 num_args = fptr->common.num_args;
1953 if (fptr->common.fn_flags & ZEND_ACC_VARIADIC) {
1954 num_args++;
1955 }
1956
1957 RETURN_LONG(num_args);
1958 }
1959 /* }}} */
1960
1961 /* {{{ Gets the number of required parameters */
ZEND_METHOD(ReflectionFunctionAbstract,getNumberOfRequiredParameters)1962 ZEND_METHOD(ReflectionFunctionAbstract, getNumberOfRequiredParameters)
1963 {
1964 reflection_object *intern;
1965 zend_function *fptr;
1966
1967 GET_REFLECTION_OBJECT_PTR(fptr);
1968
1969 if (zend_parse_parameters_none() == FAILURE) {
1970 RETURN_THROWS();
1971 }
1972
1973 RETURN_LONG(fptr->common.required_num_args);
1974 }
1975 /* }}} */
1976
1977 /* {{{ Returns an array of parameter objects for this function */
ZEND_METHOD(ReflectionFunctionAbstract,getParameters)1978 ZEND_METHOD(ReflectionFunctionAbstract, getParameters)
1979 {
1980 reflection_object *intern;
1981 zend_function *fptr;
1982 uint32_t i, num_args;
1983 struct _zend_arg_info *arg_info;
1984
1985 GET_REFLECTION_OBJECT_PTR(fptr);
1986
1987 if (zend_parse_parameters_none() == FAILURE) {
1988 RETURN_THROWS();
1989 }
1990
1991 arg_info= fptr->common.arg_info;
1992 num_args = fptr->common.num_args;
1993 if (fptr->common.fn_flags & ZEND_ACC_VARIADIC) {
1994 num_args++;
1995 }
1996
1997 if (!num_args) {
1998 RETURN_EMPTY_ARRAY();
1999 }
2000
2001 array_init(return_value);
2002 for (i = 0; i < num_args; i++) {
2003 zval parameter;
2004
2005 reflection_parameter_factory(
2006 _copy_function(fptr),
2007 Z_ISUNDEF(intern->obj) ? NULL : &intern->obj,
2008 arg_info,
2009 i,
2010 i < fptr->common.required_num_args,
2011 ¶meter
2012 );
2013 add_next_index_zval(return_value, ¶meter);
2014
2015 arg_info++;
2016 }
2017 }
2018 /* }}} */
2019
2020 /* {{{ Returns NULL or the extension the function belongs to */
ZEND_METHOD(ReflectionFunctionAbstract,getExtension)2021 ZEND_METHOD(ReflectionFunctionAbstract, getExtension)
2022 {
2023 reflection_object *intern;
2024 zend_function *fptr;
2025 zend_internal_function *internal;
2026
2027 GET_REFLECTION_OBJECT_PTR(fptr);
2028
2029 if (zend_parse_parameters_none() == FAILURE) {
2030 RETURN_THROWS();
2031 }
2032
2033 if (fptr->type != ZEND_INTERNAL_FUNCTION) {
2034 RETURN_NULL();
2035 }
2036
2037 internal = (zend_internal_function *)fptr;
2038 if (internal->module) {
2039 reflection_extension_factory(return_value, internal->module->name);
2040 } else {
2041 RETURN_NULL();
2042 }
2043 }
2044 /* }}} */
2045
2046 /* {{{ Returns false or the name of the extension the function belongs to */
ZEND_METHOD(ReflectionFunctionAbstract,getExtensionName)2047 ZEND_METHOD(ReflectionFunctionAbstract, getExtensionName)
2048 {
2049 reflection_object *intern;
2050 zend_function *fptr;
2051 zend_internal_function *internal;
2052
2053 GET_REFLECTION_OBJECT_PTR(fptr);
2054
2055 if (zend_parse_parameters_none() == FAILURE) {
2056 RETURN_THROWS();
2057 }
2058
2059 if (fptr->type != ZEND_INTERNAL_FUNCTION) {
2060 RETURN_FALSE;
2061 }
2062
2063 internal = (zend_internal_function *)fptr;
2064 if (internal->module) {
2065 RETURN_STRING(internal->module->name);
2066 } else {
2067 RETURN_FALSE;
2068 }
2069 }
2070 /* }}} */
2071
2072 /* {{{ */
ZEND_METHOD(ReflectionGenerator,__construct)2073 ZEND_METHOD(ReflectionGenerator, __construct)
2074 {
2075 zval *generator, *object;
2076 reflection_object *intern;
2077 zend_execute_data *ex;
2078
2079 object = ZEND_THIS;
2080 intern = Z_REFLECTION_P(object);
2081
2082 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &generator, zend_ce_generator) == FAILURE) {
2083 RETURN_THROWS();
2084 }
2085
2086 ex = ((zend_generator *) Z_OBJ_P(generator))->execute_data;
2087 if (!ex) {
2088 _DO_THROW("Cannot create ReflectionGenerator based on a terminated Generator");
2089 RETURN_THROWS();
2090 }
2091
2092 if (intern->ce) {
2093 zval_ptr_dtor(&intern->obj);
2094 }
2095
2096 intern->ref_type = REF_TYPE_GENERATOR;
2097 ZVAL_OBJ_COPY(&intern->obj, Z_OBJ_P(generator));
2098 intern->ce = zend_ce_generator;
2099 }
2100 /* }}} */
2101
2102 #define REFLECTION_CHECK_VALID_GENERATOR(ex) \
2103 if (!ex) { \
2104 _DO_THROW("Cannot fetch information from a terminated Generator"); \
2105 RETURN_THROWS(); \
2106 }
2107
2108 /* {{{ */
ZEND_METHOD(ReflectionGenerator,getTrace)2109 ZEND_METHOD(ReflectionGenerator, getTrace)
2110 {
2111 zend_long options = DEBUG_BACKTRACE_PROVIDE_OBJECT;
2112 zend_generator *generator = (zend_generator *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
2113 zend_generator *root_generator;
2114 zend_execute_data *ex_backup = EG(current_execute_data);
2115 zend_execute_data *ex = generator->execute_data;
2116 zend_execute_data *root_prev = NULL, *cur_prev;
2117
2118 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &options) == FAILURE) {
2119 RETURN_THROWS();
2120 }
2121
2122 REFLECTION_CHECK_VALID_GENERATOR(ex)
2123
2124 root_generator = zend_generator_get_current(generator);
2125
2126 cur_prev = generator->execute_data->prev_execute_data;
2127 if (generator == root_generator) {
2128 generator->execute_data->prev_execute_data = NULL;
2129 } else {
2130 root_prev = root_generator->execute_data->prev_execute_data;
2131 generator->execute_fake.prev_execute_data = NULL;
2132 root_generator->execute_data->prev_execute_data = &generator->execute_fake;
2133 }
2134
2135 EG(current_execute_data) = root_generator->execute_data;
2136 zend_fetch_debug_backtrace(return_value, 0, options, 0);
2137 EG(current_execute_data) = ex_backup;
2138
2139 root_generator->execute_data->prev_execute_data = root_prev;
2140 generator->execute_data->prev_execute_data = cur_prev;
2141 }
2142 /* }}} */
2143
2144 /* {{{ */
ZEND_METHOD(ReflectionGenerator,getExecutingLine)2145 ZEND_METHOD(ReflectionGenerator, getExecutingLine)
2146 {
2147 zend_generator *generator = (zend_generator *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
2148 zend_execute_data *ex = generator->execute_data;
2149
2150 if (zend_parse_parameters_none() == FAILURE) {
2151 RETURN_THROWS();
2152 }
2153
2154 REFLECTION_CHECK_VALID_GENERATOR(ex)
2155
2156 ZVAL_LONG(return_value, ex->opline->lineno);
2157 }
2158 /* }}} */
2159
2160 /* {{{ */
ZEND_METHOD(ReflectionGenerator,getExecutingFile)2161 ZEND_METHOD(ReflectionGenerator, getExecutingFile)
2162 {
2163 zend_generator *generator = (zend_generator *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
2164 zend_execute_data *ex = generator->execute_data;
2165
2166 if (zend_parse_parameters_none() == FAILURE) {
2167 RETURN_THROWS();
2168 }
2169
2170 REFLECTION_CHECK_VALID_GENERATOR(ex)
2171
2172 ZVAL_STR_COPY(return_value, ex->func->op_array.filename);
2173 }
2174 /* }}} */
2175
2176 /* {{{ */
ZEND_METHOD(ReflectionGenerator,getFunction)2177 ZEND_METHOD(ReflectionGenerator, getFunction)
2178 {
2179 zend_generator *generator = (zend_generator *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
2180 zend_execute_data *ex = generator->execute_data;
2181
2182 if (zend_parse_parameters_none() == FAILURE) {
2183 RETURN_THROWS();
2184 }
2185
2186 REFLECTION_CHECK_VALID_GENERATOR(ex)
2187
2188 if (ex->func->common.fn_flags & ZEND_ACC_CLOSURE) {
2189 zval closure;
2190 ZVAL_OBJ(&closure, ZEND_CLOSURE_OBJECT(ex->func));
2191 reflection_function_factory(ex->func, &closure, return_value);
2192 } else if (ex->func->op_array.scope) {
2193 reflection_method_factory(ex->func->op_array.scope, ex->func, NULL, return_value);
2194 } else {
2195 reflection_function_factory(ex->func, NULL, return_value);
2196 }
2197 }
2198 /* }}} */
2199
2200 /* {{{ */
ZEND_METHOD(ReflectionGenerator,getThis)2201 ZEND_METHOD(ReflectionGenerator, getThis)
2202 {
2203 zend_generator *generator = (zend_generator *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
2204 zend_execute_data *ex = generator->execute_data;
2205
2206 if (zend_parse_parameters_none() == FAILURE) {
2207 RETURN_THROWS();
2208 }
2209
2210 REFLECTION_CHECK_VALID_GENERATOR(ex)
2211
2212 if (Z_TYPE(ex->This) == IS_OBJECT) {
2213 RETURN_OBJ_COPY(Z_OBJ(ex->This));
2214 } else {
2215 RETURN_NULL();
2216 }
2217 }
2218 /* }}} */
2219
2220 /* {{{ */
ZEND_METHOD(ReflectionGenerator,getExecutingGenerator)2221 ZEND_METHOD(ReflectionGenerator, getExecutingGenerator)
2222 {
2223 zend_generator *generator = (zend_generator *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
2224 zend_execute_data *ex = generator->execute_data;
2225 zend_generator *current;
2226
2227 if (zend_parse_parameters_none() == FAILURE) {
2228 RETURN_THROWS();
2229 }
2230
2231 REFLECTION_CHECK_VALID_GENERATOR(ex)
2232
2233 current = zend_generator_get_current(generator);
2234 RETURN_OBJ_COPY(¤t->std);
2235 }
2236 /* }}} */
2237
2238 /* {{{ Constructor. Throws an Exception in case the given method does not exist */
ZEND_METHOD(ReflectionParameter,__construct)2239 ZEND_METHOD(ReflectionParameter, __construct)
2240 {
2241 parameter_reference *ref;
2242 zval *reference;
2243 zend_string *arg_name = NULL;
2244 zend_long position;
2245 zval *object;
2246 zval *prop_name;
2247 reflection_object *intern;
2248 zend_function *fptr;
2249 struct _zend_arg_info *arg_info;
2250 uint32_t num_args;
2251 zend_class_entry *ce = NULL;
2252 zend_bool is_closure = 0;
2253
2254 ZEND_PARSE_PARAMETERS_START(2, 2)
2255 Z_PARAM_ZVAL(reference)
2256 Z_PARAM_STR_OR_LONG(arg_name, position)
2257 ZEND_PARSE_PARAMETERS_END();
2258
2259 object = ZEND_THIS;
2260 intern = Z_REFLECTION_P(object);
2261
2262 /* First, find the function */
2263 switch (Z_TYPE_P(reference)) {
2264 case IS_STRING:
2265 {
2266 zend_string *lcname = zend_string_tolower(Z_STR_P(reference));
2267 fptr = zend_hash_find_ptr(EG(function_table), lcname);
2268 zend_string_release(lcname);
2269 if (!fptr) {
2270 zend_throw_exception_ex(reflection_exception_ptr, 0,
2271 "Function %s() does not exist", Z_STRVAL_P(reference));
2272 RETURN_THROWS();
2273 }
2274 ce = fptr->common.scope;
2275 }
2276 break;
2277
2278 case IS_ARRAY: {
2279 zval *classref;
2280 zval *method;
2281 zend_string *name, *lcname;
2282
2283 if (((classref = zend_hash_index_find(Z_ARRVAL_P(reference), 0)) == NULL)
2284 || ((method = zend_hash_index_find(Z_ARRVAL_P(reference), 1)) == NULL))
2285 {
2286 _DO_THROW("Expected array($object, $method) or array($classname, $method)");
2287 RETURN_THROWS();
2288 }
2289
2290 if (Z_TYPE_P(classref) == IS_OBJECT) {
2291 ce = Z_OBJCE_P(classref);
2292 } else {
2293 name = zval_try_get_string(classref);
2294 if (UNEXPECTED(!name)) {
2295 return;
2296 }
2297 if ((ce = zend_lookup_class(name)) == NULL) {
2298 zend_throw_exception_ex(reflection_exception_ptr, 0,
2299 "Class \"%s\" does not exist", ZSTR_VAL(name));
2300 zend_string_release(name);
2301 RETURN_THROWS();
2302 }
2303 zend_string_release(name);
2304 }
2305
2306 name = zval_try_get_string(method);
2307 if (UNEXPECTED(!name)) {
2308 return;
2309 }
2310
2311 lcname = zend_string_tolower(name);
2312 if (Z_TYPE_P(classref) == IS_OBJECT && is_closure_invoke(ce, lcname)
2313 && (fptr = zend_get_closure_invoke_method(Z_OBJ_P(classref))) != NULL)
2314 {
2315 /* nothing to do. don't set is_closure since is the invoke handler,
2316 not the closure itself */
2317 } else if ((fptr = zend_hash_find_ptr(&ce->function_table, lcname)) == NULL) {
2318 zend_throw_exception_ex(reflection_exception_ptr, 0,
2319 "Method %s::%s() does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(name));
2320 zend_string_release(name);
2321 zend_string_release(lcname);
2322 RETURN_THROWS();
2323 }
2324 zend_string_release(name);
2325 zend_string_release(lcname);
2326 }
2327 break;
2328
2329 case IS_OBJECT: {
2330 ce = Z_OBJCE_P(reference);
2331
2332 if (instanceof_function(ce, zend_ce_closure)) {
2333 fptr = (zend_function *)zend_get_closure_method_def(Z_OBJ_P(reference));
2334 Z_ADDREF_P(reference);
2335 is_closure = 1;
2336 } else if ((fptr = zend_hash_find_ptr(&ce->function_table, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE))) == NULL) {
2337 zend_throw_exception_ex(reflection_exception_ptr, 0,
2338 "Method %s::%s() does not exist", ZSTR_VAL(ce->name), ZEND_INVOKE_FUNC_NAME);
2339 RETURN_THROWS();
2340 }
2341 }
2342 break;
2343
2344 default:
2345 zend_argument_error(reflection_exception_ptr, 1, "must be a string, an array(class, method), or a callable object, %s given", zend_zval_type_name(reference));
2346 RETURN_THROWS();
2347 }
2348
2349 /* Now, search for the parameter */
2350 arg_info = fptr->common.arg_info;
2351 num_args = fptr->common.num_args;
2352 if (fptr->common.fn_flags & ZEND_ACC_VARIADIC) {
2353 num_args++;
2354 }
2355 if (arg_name != NULL) {
2356 uint32_t i;
2357 position = -1;
2358
2359 if (has_internal_arg_info(fptr)) {
2360 for (i = 0; i < num_args; i++) {
2361 if (arg_info[i].name) {
2362 if (strcmp(((zend_internal_arg_info*)arg_info)[i].name, ZSTR_VAL(arg_name)) == 0) {
2363 position = i;
2364 break;
2365 }
2366 }
2367 }
2368 } else {
2369 for (i = 0; i < num_args; i++) {
2370 if (arg_info[i].name) {
2371 if (zend_string_equals(arg_name, arg_info[i].name)) {
2372 position = i;
2373 break;
2374 }
2375 }
2376 }
2377 }
2378 if (position == -1) {
2379 _DO_THROW("The parameter specified by its name could not be found");
2380 goto failure;
2381 }
2382 } else {
2383 if (position < 0) {
2384 zend_argument_value_error(2, "must be greater than or equal to 0");
2385 goto failure;
2386 }
2387 if (position >= num_args) {
2388 _DO_THROW("The parameter specified by its offset could not be found");
2389 goto failure;
2390 }
2391 }
2392
2393 ref = (parameter_reference*) emalloc(sizeof(parameter_reference));
2394 ref->arg_info = &arg_info[position];
2395 ref->offset = (uint32_t)position;
2396 ref->required = (uint32_t)position < fptr->common.required_num_args;
2397 ref->fptr = fptr;
2398 /* TODO: copy fptr */
2399 intern->ptr = ref;
2400 intern->ref_type = REF_TYPE_PARAMETER;
2401 intern->ce = ce;
2402 if (reference && is_closure) {
2403 ZVAL_COPY_VALUE(&intern->obj, reference);
2404 }
2405
2406 prop_name = reflection_prop_name(object);
2407 if (has_internal_arg_info(fptr)) {
2408 ZVAL_STRING(prop_name, ((zend_internal_arg_info*)arg_info)[position].name);
2409 } else {
2410 ZVAL_STR_COPY(prop_name, arg_info[position].name);
2411 }
2412 return;
2413
2414 failure:
2415 if (fptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
2416 zend_string_release_ex(fptr->common.function_name, 0);
2417 zend_free_trampoline(fptr);
2418 }
2419 if (is_closure) {
2420 zval_ptr_dtor(reference);
2421 }
2422 RETURN_THROWS();
2423 }
2424 /* }}} */
2425
2426 /* {{{ Returns a string representation */
ZEND_METHOD(ReflectionParameter,__toString)2427 ZEND_METHOD(ReflectionParameter, __toString)
2428 {
2429 reflection_object *intern;
2430 parameter_reference *param;
2431 smart_str str = {0};
2432
2433 if (zend_parse_parameters_none() == FAILURE) {
2434 RETURN_THROWS();
2435 }
2436 GET_REFLECTION_OBJECT_PTR(param);
2437 _parameter_string(&str, param->fptr, param->arg_info, param->offset, param->required, "");
2438 RETURN_STR(smart_str_extract(&str));
2439 }
2440
2441 /* }}} */
2442
2443 /* {{{ Returns this parameters's name */
ZEND_METHOD(ReflectionParameter,getName)2444 ZEND_METHOD(ReflectionParameter, getName)
2445 {
2446 reflection_object *intern;
2447 parameter_reference *param;
2448
2449 if (zend_parse_parameters_none() == FAILURE) {
2450 RETURN_THROWS();
2451 }
2452
2453 GET_REFLECTION_OBJECT_PTR(param);
2454 if (has_internal_arg_info(param->fptr)) {
2455 RETURN_STRING(((zend_internal_arg_info *) param->arg_info)->name);
2456 } else {
2457 RETURN_STR_COPY(param->arg_info->name);
2458 }
2459 }
2460 /* }}} */
2461
2462 /* {{{ Returns the ReflectionFunction for the function of this parameter */
ZEND_METHOD(ReflectionParameter,getDeclaringFunction)2463 ZEND_METHOD(ReflectionParameter, getDeclaringFunction)
2464 {
2465 reflection_object *intern;
2466 parameter_reference *param;
2467
2468 if (zend_parse_parameters_none() == FAILURE) {
2469 RETURN_THROWS();
2470 }
2471 GET_REFLECTION_OBJECT_PTR(param);
2472
2473 if (!param->fptr->common.scope) {
2474 reflection_function_factory(_copy_function(param->fptr), Z_ISUNDEF(intern->obj)? NULL : &intern->obj, return_value);
2475 } else {
2476 reflection_method_factory(param->fptr->common.scope, _copy_function(param->fptr), Z_ISUNDEF(intern->obj)? NULL : &intern->obj, return_value);
2477 }
2478 }
2479 /* }}} */
2480
2481 /* {{{ Returns in which class this parameter is defined (not the type of the parameter) */
ZEND_METHOD(ReflectionParameter,getDeclaringClass)2482 ZEND_METHOD(ReflectionParameter, getDeclaringClass)
2483 {
2484 reflection_object *intern;
2485 parameter_reference *param;
2486
2487 if (zend_parse_parameters_none() == FAILURE) {
2488 RETURN_THROWS();
2489 }
2490 GET_REFLECTION_OBJECT_PTR(param);
2491
2492 if (param->fptr->common.scope) {
2493 zend_reflection_class_factory(param->fptr->common.scope, return_value);
2494 }
2495 }
2496 /* }}} */
2497
2498 /* {{{ Returns this parameters's class hint or NULL if there is none */
ZEND_METHOD(ReflectionParameter,getClass)2499 ZEND_METHOD(ReflectionParameter, getClass)
2500 {
2501 reflection_object *intern;
2502 parameter_reference *param;
2503 zend_class_entry *ce;
2504
2505 if (zend_parse_parameters_none() == FAILURE) {
2506 RETURN_THROWS();
2507 }
2508 GET_REFLECTION_OBJECT_PTR(param);
2509
2510 // TODO: This is going to return null for union types, which is rather odd.
2511 if (ZEND_TYPE_HAS_NAME(param->arg_info->type)) {
2512 /* Class name is stored as a string, we might also get "self" or "parent"
2513 * - For "self", simply use the function scope. If scope is NULL then
2514 * the function is global and thus self does not make any sense
2515 *
2516 * - For "parent", use the function scope's parent. If scope is NULL then
2517 * the function is global and thus parent does not make any sense.
2518 * If the parent is NULL then the class does not extend anything and
2519 * thus parent does not make any sense, either.
2520 *
2521 * TODO: Think about moving these checks to the compiler or some sort of
2522 * lint-mode.
2523 */
2524 zend_string *class_name;
2525
2526 class_name = ZEND_TYPE_NAME(param->arg_info->type);
2527 if (0 == zend_binary_strcasecmp(ZSTR_VAL(class_name), ZSTR_LEN(class_name), "self", sizeof("self")- 1)) {
2528 ce = param->fptr->common.scope;
2529 if (!ce) {
2530 zend_throw_exception_ex(reflection_exception_ptr, 0,
2531 "Parameter uses \"self\" as type but function is not a class member");
2532 RETURN_THROWS();
2533 }
2534 } else if (0 == zend_binary_strcasecmp(ZSTR_VAL(class_name), ZSTR_LEN(class_name), "parent", sizeof("parent")- 1)) {
2535 ce = param->fptr->common.scope;
2536 if (!ce) {
2537 zend_throw_exception_ex(reflection_exception_ptr, 0,
2538 "Parameter uses \"parent\" as type but function is not a class member");
2539 RETURN_THROWS();
2540 }
2541 if (!ce->parent) {
2542 zend_throw_exception_ex(reflection_exception_ptr, 0,
2543 "Parameter uses \"parent\" as type although class does not have a parent");
2544 RETURN_THROWS();
2545 }
2546 ce = ce->parent;
2547 } else {
2548 ce = zend_lookup_class(class_name);
2549 if (!ce) {
2550 zend_throw_exception_ex(reflection_exception_ptr, 0,
2551 "Class \"%s\" does not exist", ZSTR_VAL(class_name));
2552 RETURN_THROWS();
2553 }
2554 }
2555 zend_reflection_class_factory(ce, return_value);
2556 }
2557 }
2558 /* }}} */
2559
2560 /* {{{ Returns whether parameter has a type */
ZEND_METHOD(ReflectionParameter,hasType)2561 ZEND_METHOD(ReflectionParameter, hasType)
2562 {
2563 reflection_object *intern;
2564 parameter_reference *param;
2565
2566 if (zend_parse_parameters_none() == FAILURE) {
2567 RETURN_THROWS();
2568 }
2569 GET_REFLECTION_OBJECT_PTR(param);
2570
2571 RETVAL_BOOL(ZEND_TYPE_IS_SET(param->arg_info->type));
2572 }
2573 /* }}} */
2574
2575 /* {{{ Returns the type associated with the parameter */
ZEND_METHOD(ReflectionParameter,getType)2576 ZEND_METHOD(ReflectionParameter, getType)
2577 {
2578 reflection_object *intern;
2579 parameter_reference *param;
2580
2581 if (zend_parse_parameters_none() == FAILURE) {
2582 RETURN_THROWS();
2583 }
2584 GET_REFLECTION_OBJECT_PTR(param);
2585
2586 if (!ZEND_TYPE_IS_SET(param->arg_info->type)) {
2587 RETURN_NULL();
2588 }
2589 reflection_type_factory(param->arg_info->type, return_value, 1);
2590 }
2591 /* }}} */
2592
2593 /* {{{ Returns whether parameter MUST be an array */
ZEND_METHOD(ReflectionParameter,isArray)2594 ZEND_METHOD(ReflectionParameter, isArray)
2595 {
2596 reflection_object *intern;
2597 parameter_reference *param;
2598 uint32_t type_mask;
2599
2600 if (zend_parse_parameters_none() == FAILURE) {
2601 RETURN_THROWS();
2602 }
2603 GET_REFLECTION_OBJECT_PTR(param);
2604
2605 type_mask = ZEND_TYPE_PURE_MASK_WITHOUT_NULL(param->arg_info->type);
2606 RETVAL_BOOL(type_mask == MAY_BE_ARRAY);
2607 }
2608 /* }}} */
2609
2610 /* {{{ Returns whether parameter MUST be callable */
ZEND_METHOD(ReflectionParameter,isCallable)2611 ZEND_METHOD(ReflectionParameter, isCallable)
2612 {
2613 reflection_object *intern;
2614 parameter_reference *param;
2615 uint32_t type_mask;
2616
2617 if (zend_parse_parameters_none() == FAILURE) {
2618 RETURN_THROWS();
2619 }
2620 GET_REFLECTION_OBJECT_PTR(param);
2621
2622 type_mask = ZEND_TYPE_PURE_MASK_WITHOUT_NULL(param->arg_info->type);
2623 RETVAL_BOOL(type_mask == MAY_BE_CALLABLE);
2624 }
2625 /* }}} */
2626
2627 /* {{{ Returns whether NULL is allowed as this parameters's value */
ZEND_METHOD(ReflectionParameter,allowsNull)2628 ZEND_METHOD(ReflectionParameter, allowsNull)
2629 {
2630 reflection_object *intern;
2631 parameter_reference *param;
2632
2633 if (zend_parse_parameters_none() == FAILURE) {
2634 RETURN_THROWS();
2635 }
2636 GET_REFLECTION_OBJECT_PTR(param);
2637
2638 RETVAL_BOOL(!ZEND_TYPE_IS_SET(param->arg_info->type)
2639 || ZEND_TYPE_ALLOW_NULL(param->arg_info->type));
2640 }
2641 /* }}} */
2642
2643 /* {{{ Returns whether this parameters is passed to by reference */
ZEND_METHOD(ReflectionParameter,isPassedByReference)2644 ZEND_METHOD(ReflectionParameter, isPassedByReference)
2645 {
2646 reflection_object *intern;
2647 parameter_reference *param;
2648
2649 if (zend_parse_parameters_none() == FAILURE) {
2650 RETURN_THROWS();
2651 }
2652 GET_REFLECTION_OBJECT_PTR(param);
2653
2654 RETVAL_BOOL(ZEND_ARG_SEND_MODE(param->arg_info));
2655 }
2656 /* }}} */
2657
2658 /* {{{ Returns whether this parameter can be passed by value */
ZEND_METHOD(ReflectionParameter,canBePassedByValue)2659 ZEND_METHOD(ReflectionParameter, canBePassedByValue)
2660 {
2661 reflection_object *intern;
2662 parameter_reference *param;
2663
2664 if (zend_parse_parameters_none() == FAILURE) {
2665 RETURN_THROWS();
2666 }
2667 GET_REFLECTION_OBJECT_PTR(param);
2668
2669 /* true if it's ZEND_SEND_BY_VAL or ZEND_SEND_PREFER_REF */
2670 RETVAL_BOOL(ZEND_ARG_SEND_MODE(param->arg_info) != ZEND_SEND_BY_REF);
2671 }
2672 /* }}} */
2673
2674 /* {{{ Get parameter attributes. */
ZEND_METHOD(ReflectionParameter,getAttributes)2675 ZEND_METHOD(ReflectionParameter, getAttributes)
2676 {
2677 reflection_object *intern;
2678 parameter_reference *param;
2679
2680 GET_REFLECTION_OBJECT_PTR(param);
2681
2682 HashTable *attributes = param->fptr->common.attributes;
2683 zend_class_entry *scope = param->fptr->common.scope;
2684
2685 reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU,
2686 attributes, param->offset + 1, scope, ZEND_ATTRIBUTE_TARGET_PARAMETER,
2687 param->fptr->type == ZEND_USER_FUNCTION ? param->fptr->op_array.filename : NULL);
2688 }
2689
2690 /* {{{ Returns whether this parameter is an optional parameter */
ZEND_METHOD(ReflectionParameter,getPosition)2691 ZEND_METHOD(ReflectionParameter, getPosition)
2692 {
2693 reflection_object *intern;
2694 parameter_reference *param;
2695
2696 if (zend_parse_parameters_none() == FAILURE) {
2697 RETURN_THROWS();
2698 }
2699 GET_REFLECTION_OBJECT_PTR(param);
2700
2701 RETVAL_LONG(param->offset);
2702 }
2703 /* }}} */
2704
2705 /* {{{ Returns whether this parameter is an optional parameter */
ZEND_METHOD(ReflectionParameter,isOptional)2706 ZEND_METHOD(ReflectionParameter, isOptional)
2707 {
2708 reflection_object *intern;
2709 parameter_reference *param;
2710
2711 if (zend_parse_parameters_none() == FAILURE) {
2712 RETURN_THROWS();
2713 }
2714 GET_REFLECTION_OBJECT_PTR(param);
2715
2716 RETVAL_BOOL(!param->required);
2717 }
2718 /* }}} */
2719
2720 /* {{{ Returns whether the default value of this parameter is available */
ZEND_METHOD(ReflectionParameter,isDefaultValueAvailable)2721 ZEND_METHOD(ReflectionParameter, isDefaultValueAvailable)
2722 {
2723 reflection_object *intern;
2724 parameter_reference *param;
2725
2726 if (zend_parse_parameters_none() == FAILURE) {
2727 RETURN_THROWS();
2728 }
2729
2730 GET_REFLECTION_OBJECT_PTR(param);
2731
2732 if (param->fptr->type == ZEND_INTERNAL_FUNCTION) {
2733 RETURN_BOOL(!(param->fptr->common.fn_flags & ZEND_ACC_USER_ARG_INFO)
2734 && ((zend_internal_arg_info*) (param->arg_info))->default_value);
2735 } else {
2736 zval *default_value = get_default_from_recv((zend_op_array *)param->fptr, param->offset);
2737 RETURN_BOOL(default_value != NULL);
2738 }
2739 }
2740 /* }}} */
2741
2742 /* {{{ Returns the default value of this parameter or throws an exception */
ZEND_METHOD(ReflectionParameter,getDefaultValue)2743 ZEND_METHOD(ReflectionParameter, getDefaultValue)
2744 {
2745 reflection_object *intern;
2746 parameter_reference *param;
2747
2748 if (zend_parse_parameters_none() == FAILURE) {
2749 RETURN_THROWS();
2750 }
2751
2752 GET_REFLECTION_OBJECT_PTR(param);
2753
2754 if (get_parameter_default(return_value, param) == FAILURE) {
2755 zend_throw_exception_ex(reflection_exception_ptr, 0,
2756 "Internal error: Failed to retrieve the default value");
2757 RETURN_THROWS();
2758 }
2759
2760 if (Z_TYPE_P(return_value) == IS_CONSTANT_AST) {
2761 zval_update_constant_ex(return_value, param->fptr->common.scope);
2762 }
2763 }
2764 /* }}} */
2765
2766 /* {{{ Returns whether the default value of this parameter is constant */
ZEND_METHOD(ReflectionParameter,isDefaultValueConstant)2767 ZEND_METHOD(ReflectionParameter, isDefaultValueConstant)
2768 {
2769 reflection_object *intern;
2770 parameter_reference *param;
2771
2772 if (zend_parse_parameters_none() == FAILURE) {
2773 RETURN_THROWS();
2774 }
2775
2776 GET_REFLECTION_OBJECT_PTR(param);
2777
2778 zval default_value;
2779 if (get_parameter_default(&default_value, param) == FAILURE) {
2780 zend_throw_exception_ex(reflection_exception_ptr, 0,
2781 "Internal error: Failed to retrieve the default value");
2782 RETURN_THROWS();
2783 }
2784
2785 if (Z_TYPE(default_value) == IS_CONSTANT_AST) {
2786 zend_ast *ast = Z_ASTVAL(default_value);
2787 RETVAL_BOOL(ast->kind == ZEND_AST_CONSTANT || ast->kind == ZEND_AST_CONSTANT_CLASS);
2788 } else {
2789 RETVAL_FALSE;
2790 }
2791
2792 zval_ptr_dtor_nogc(&default_value);
2793 }
2794 /* }}} */
2795
2796 /* {{{ Returns the default value's constant name if default value is constant or null */
ZEND_METHOD(ReflectionParameter,getDefaultValueConstantName)2797 ZEND_METHOD(ReflectionParameter, getDefaultValueConstantName)
2798 {
2799 reflection_object *intern;
2800 parameter_reference *param;
2801
2802 if (zend_parse_parameters_none() == FAILURE) {
2803 RETURN_THROWS();
2804 }
2805
2806 GET_REFLECTION_OBJECT_PTR(param);
2807
2808 zval default_value;
2809 if (get_parameter_default(&default_value, param) == FAILURE) {
2810 zend_throw_exception_ex(reflection_exception_ptr, 0,
2811 "Internal error: Failed to retrieve the default value");
2812 RETURN_THROWS();
2813 }
2814
2815 if (Z_TYPE(default_value) != IS_CONSTANT_AST) {
2816 zval_ptr_dtor_nogc(&default_value);
2817 RETURN_NULL();
2818 }
2819
2820 zend_ast *ast = Z_ASTVAL(default_value);
2821 if (ast->kind == ZEND_AST_CONSTANT) {
2822 RETVAL_STR_COPY(zend_ast_get_constant_name(ast));
2823 } else if (ast->kind == ZEND_AST_CONSTANT_CLASS) {
2824 RETVAL_STRINGL("__CLASS__", sizeof("__CLASS__")-1);
2825 } else {
2826 RETVAL_NULL();
2827 }
2828 zval_ptr_dtor_nogc(&default_value);
2829 }
2830
2831 /* {{{ Returns whether this parameter is a variadic parameter */
ZEND_METHOD(ReflectionParameter,isVariadic)2832 ZEND_METHOD(ReflectionParameter, isVariadic)
2833 {
2834 reflection_object *intern;
2835 parameter_reference *param;
2836
2837 if (zend_parse_parameters_none() == FAILURE) {
2838 RETURN_THROWS();
2839 }
2840 GET_REFLECTION_OBJECT_PTR(param);
2841
2842 RETVAL_BOOL(ZEND_ARG_IS_VARIADIC(param->arg_info));
2843 }
2844 /* }}} */
2845
2846 /* {{{ Returns this constructor parameter has been promoted to a property */
ZEND_METHOD(ReflectionParameter,isPromoted)2847 ZEND_METHOD(ReflectionParameter, isPromoted)
2848 {
2849 reflection_object *intern;
2850 parameter_reference *param;
2851
2852 if (zend_parse_parameters_none() == FAILURE) {
2853 RETURN_THROWS();
2854 }
2855 GET_REFLECTION_OBJECT_PTR(param);
2856
2857 RETVAL_BOOL(ZEND_ARG_IS_PROMOTED(param->arg_info));
2858 }
2859 /* }}} */
2860
2861 /* {{{ Returns whether parameter MAY be null */
ZEND_METHOD(ReflectionType,allowsNull)2862 ZEND_METHOD(ReflectionType, allowsNull)
2863 {
2864 reflection_object *intern;
2865 type_reference *param;
2866
2867 if (zend_parse_parameters_none() == FAILURE) {
2868 RETURN_THROWS();
2869 }
2870 GET_REFLECTION_OBJECT_PTR(param);
2871
2872 RETVAL_BOOL(ZEND_TYPE_ALLOW_NULL(param->type));
2873 }
2874 /* }}} */
2875
zend_type_to_string_without_null(zend_type type)2876 static zend_string *zend_type_to_string_without_null(zend_type type) {
2877 ZEND_TYPE_FULL_MASK(type) &= ~MAY_BE_NULL;
2878 return zend_type_to_string(type);
2879 }
2880
2881 /* {{{ Return the text of the type hint */
ZEND_METHOD(ReflectionType,__toString)2882 ZEND_METHOD(ReflectionType, __toString)
2883 {
2884 reflection_object *intern;
2885 type_reference *param;
2886
2887 if (zend_parse_parameters_none() == FAILURE) {
2888 RETURN_THROWS();
2889 }
2890 GET_REFLECTION_OBJECT_PTR(param);
2891
2892 RETURN_STR(zend_type_to_string(param->type));
2893 }
2894 /* }}} */
2895
2896 /* {{{ Return the name of the type */
ZEND_METHOD(ReflectionNamedType,getName)2897 ZEND_METHOD(ReflectionNamedType, getName)
2898 {
2899 reflection_object *intern;
2900 type_reference *param;
2901
2902 if (zend_parse_parameters_none() == FAILURE) {
2903 RETURN_THROWS();
2904 }
2905 GET_REFLECTION_OBJECT_PTR(param);
2906
2907 if (param->legacy_behavior) {
2908 RETURN_STR(zend_type_to_string_without_null(param->type));
2909 }
2910 RETURN_STR(zend_type_to_string(param->type));
2911 }
2912 /* }}} */
2913
2914 /* {{{ Returns whether type is a builtin type */
ZEND_METHOD(ReflectionNamedType,isBuiltin)2915 ZEND_METHOD(ReflectionNamedType, isBuiltin)
2916 {
2917 reflection_object *intern;
2918 type_reference *param;
2919
2920 if (zend_parse_parameters_none() == FAILURE) {
2921 RETURN_THROWS();
2922 }
2923 GET_REFLECTION_OBJECT_PTR(param);
2924
2925 /* Treat "static" as a class type for the purposes of reflection. */
2926 RETVAL_BOOL(ZEND_TYPE_IS_ONLY_MASK(param->type)
2927 && !(ZEND_TYPE_FULL_MASK(param->type) & MAY_BE_STATIC));
2928 }
2929 /* }}} */
2930
append_type(zval * return_value,zend_type type)2931 static void append_type(zval *return_value, zend_type type) {
2932 zval reflection_type;
2933 reflection_type_factory(type, &reflection_type, 0);
2934 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &reflection_type);
2935 }
2936
append_type_mask(zval * return_value,uint32_t type_mask)2937 static void append_type_mask(zval *return_value, uint32_t type_mask) {
2938 append_type(return_value, (zend_type) ZEND_TYPE_INIT_MASK(type_mask));
2939 }
2940
2941 /* {{{ Returns the types that are part of this union type */
ZEND_METHOD(ReflectionUnionType,getTypes)2942 ZEND_METHOD(ReflectionUnionType, getTypes)
2943 {
2944 reflection_object *intern;
2945 type_reference *param;
2946 uint32_t type_mask;
2947
2948 if (zend_parse_parameters_none() == FAILURE) {
2949 RETURN_THROWS();
2950 }
2951 GET_REFLECTION_OBJECT_PTR(param);
2952
2953 array_init(return_value);
2954 if (ZEND_TYPE_HAS_LIST(param->type)) {
2955 zend_type *list_type;
2956 ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(param->type), list_type) {
2957 append_type(return_value, *list_type);
2958 } ZEND_TYPE_LIST_FOREACH_END();
2959 } else if (ZEND_TYPE_HAS_NAME(param->type)) {
2960 append_type(return_value,
2961 (zend_type) ZEND_TYPE_INIT_CLASS(ZEND_TYPE_NAME(param->type), 0, 0));
2962 } else if (ZEND_TYPE_HAS_CE(param->type)) {
2963 append_type(return_value,
2964 (zend_type) ZEND_TYPE_INIT_CE(ZEND_TYPE_CE(param->type), 0, 0));
2965 }
2966
2967 type_mask = ZEND_TYPE_PURE_MASK(param->type);
2968 ZEND_ASSERT(!(type_mask & MAY_BE_VOID));
2969 if (type_mask & MAY_BE_STATIC) {
2970 append_type_mask(return_value, MAY_BE_STATIC);
2971 }
2972 if (type_mask & MAY_BE_CALLABLE) {
2973 append_type_mask(return_value, MAY_BE_CALLABLE);
2974 }
2975 if (type_mask & MAY_BE_ITERABLE) {
2976 append_type_mask(return_value, MAY_BE_ITERABLE);
2977 }
2978 if (type_mask & MAY_BE_OBJECT) {
2979 append_type_mask(return_value, MAY_BE_OBJECT);
2980 }
2981 if (type_mask & MAY_BE_ARRAY) {
2982 append_type_mask(return_value, MAY_BE_ARRAY);
2983 }
2984 if (type_mask & MAY_BE_STRING) {
2985 append_type_mask(return_value, MAY_BE_STRING);
2986 }
2987 if (type_mask & MAY_BE_LONG) {
2988 append_type_mask(return_value, MAY_BE_LONG);
2989 }
2990 if (type_mask & MAY_BE_DOUBLE) {
2991 append_type_mask(return_value, MAY_BE_DOUBLE);
2992 }
2993 if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL) {
2994 append_type_mask(return_value, MAY_BE_BOOL);
2995 } else if (type_mask & MAY_BE_FALSE) {
2996 append_type_mask(return_value, MAY_BE_FALSE);
2997 }
2998 if (type_mask & MAY_BE_NULL) {
2999 append_type_mask(return_value, MAY_BE_NULL);
3000 }
3001 }
3002 /* }}} */
3003
3004 /* {{{ Constructor. Throws an Exception in case the given method does not exist */
ZEND_METHOD(ReflectionMethod,__construct)3005 ZEND_METHOD(ReflectionMethod, __construct)
3006 {
3007 zend_object *arg1_obj;
3008 zend_string *arg1_str;
3009 zend_string *arg2_str = NULL;
3010
3011 zend_object *orig_obj = NULL;
3012 zend_class_entry *ce = NULL;
3013 zend_string *class_name = NULL;
3014 char *method_name;
3015 size_t method_name_len;
3016 char *lcname;
3017
3018 zval *object;
3019 reflection_object *intern;
3020 zend_function *mptr;
3021
3022 ZEND_PARSE_PARAMETERS_START(1, 2)
3023 Z_PARAM_OBJ_OR_STR(arg1_obj, arg1_str)
3024 Z_PARAM_OPTIONAL
3025 Z_PARAM_STR_OR_NULL(arg2_str)
3026 ZEND_PARSE_PARAMETERS_END();
3027
3028 if (arg1_obj) {
3029 if (!arg2_str) {
3030 zend_argument_value_error(2, "cannot be null when argument #1 ($objectOrMethod) is an object");
3031 RETURN_THROWS();
3032 }
3033
3034 orig_obj = arg1_obj;
3035 ce = arg1_obj->ce;
3036 method_name = ZSTR_VAL(arg2_str);
3037 method_name_len = ZSTR_LEN(arg2_str);
3038 } else if (arg2_str) {
3039 class_name = zend_string_copy(arg1_str);
3040 method_name = ZSTR_VAL(arg2_str);
3041 method_name_len = ZSTR_LEN(arg2_str);
3042 } else {
3043 char *tmp;
3044 size_t tmp_len;
3045 char *name = ZSTR_VAL(arg1_str);
3046
3047 if ((tmp = strstr(name, "::")) == NULL) {
3048 zend_argument_error(reflection_exception_ptr, 1, "must be a valid method name");
3049 RETURN_THROWS();
3050 }
3051 tmp_len = tmp - name;
3052
3053 class_name = zend_string_init(name, tmp_len, 0);
3054 method_name = tmp + 2;
3055 method_name_len = ZSTR_LEN(arg1_str) - tmp_len - 2;
3056 }
3057
3058 if (class_name) {
3059 if ((ce = zend_lookup_class(class_name)) == NULL) {
3060 if (!EG(exception)) {
3061 zend_throw_exception_ex(reflection_exception_ptr, 0, "Class \"%s\" does not exist", ZSTR_VAL(class_name));
3062 }
3063 zend_string_release(class_name);
3064 RETURN_THROWS();
3065 }
3066
3067 zend_string_release(class_name);
3068 }
3069
3070 object = ZEND_THIS;
3071 intern = Z_REFLECTION_P(object);
3072
3073 lcname = zend_str_tolower_dup(method_name, method_name_len);
3074
3075 if (ce == zend_ce_closure && orig_obj && (method_name_len == sizeof(ZEND_INVOKE_FUNC_NAME)-1)
3076 && memcmp(lcname, ZEND_INVOKE_FUNC_NAME, sizeof(ZEND_INVOKE_FUNC_NAME)-1) == 0
3077 && (mptr = zend_get_closure_invoke_method(orig_obj)) != NULL)
3078 {
3079 /* do nothing, mptr already set */
3080 } else if ((mptr = zend_hash_str_find_ptr(&ce->function_table, lcname, method_name_len)) == NULL) {
3081 efree(lcname);
3082 zend_throw_exception_ex(reflection_exception_ptr, 0,
3083 "Method %s::%s() does not exist", ZSTR_VAL(ce->name), method_name);
3084 RETURN_THROWS();
3085 }
3086 efree(lcname);
3087
3088 ZVAL_STR_COPY(reflection_prop_name(object), mptr->common.function_name);
3089 ZVAL_STR_COPY(reflection_prop_class(object), mptr->common.scope->name);
3090 intern->ptr = mptr;
3091 intern->ref_type = REF_TYPE_FUNCTION;
3092 intern->ce = ce;
3093 }
3094 /* }}} */
3095
3096 /* {{{ Returns a string representation */
ZEND_METHOD(ReflectionMethod,__toString)3097 ZEND_METHOD(ReflectionMethod, __toString)
3098 {
3099 reflection_object *intern;
3100 zend_function *mptr;
3101 smart_str str = {0};
3102
3103 if (zend_parse_parameters_none() == FAILURE) {
3104 RETURN_THROWS();
3105 }
3106 GET_REFLECTION_OBJECT_PTR(mptr);
3107 _function_string(&str, mptr, intern->ce, "");
3108 RETURN_STR(smart_str_extract(&str));
3109 }
3110 /* }}} */
3111
3112 /* {{{ Invokes the function */
ZEND_METHOD(ReflectionMethod,getClosure)3113 ZEND_METHOD(ReflectionMethod, getClosure)
3114 {
3115 reflection_object *intern;
3116 zval *obj = NULL;
3117 zend_function *mptr;
3118
3119 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|o!", &obj) == FAILURE) {
3120 RETURN_THROWS();
3121 }
3122
3123 GET_REFLECTION_OBJECT_PTR(mptr);
3124
3125 if (mptr->common.fn_flags & ZEND_ACC_STATIC) {
3126 zend_create_fake_closure(return_value, mptr, mptr->common.scope, mptr->common.scope, NULL);
3127 } else {
3128 if (!obj) {
3129 zend_argument_value_error(1, "cannot be null for non-static methods");
3130 RETURN_THROWS();
3131 }
3132
3133 if (!instanceof_function(Z_OBJCE_P(obj), mptr->common.scope)) {
3134 _DO_THROW("Given object is not an instance of the class this method was declared in");
3135 RETURN_THROWS();
3136 }
3137
3138 /* This is an original closure object and __invoke is to be called. */
3139 if (Z_OBJCE_P(obj) == zend_ce_closure &&
3140 (mptr->internal_function.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE))
3141 {
3142 RETURN_OBJ_COPY(Z_OBJ_P(obj));
3143 } else {
3144 zend_create_fake_closure(return_value, mptr, mptr->common.scope, Z_OBJCE_P(obj), obj);
3145 }
3146 }
3147 }
3148 /* }}} */
3149
3150 /* {{{ reflection_method_invoke */
reflection_method_invoke(INTERNAL_FUNCTION_PARAMETERS,int variadic)3151 static void reflection_method_invoke(INTERNAL_FUNCTION_PARAMETERS, int variadic)
3152 {
3153 zval retval;
3154 zval *params = NULL, *object;
3155 HashTable *named_params = NULL;
3156 reflection_object *intern;
3157 zend_function *mptr;
3158 int argc = 0, result;
3159 zend_fcall_info fci;
3160 zend_fcall_info_cache fcc;
3161 zend_class_entry *obj_ce;
3162
3163 GET_REFLECTION_OBJECT_PTR(mptr);
3164
3165 if (mptr->common.fn_flags & ZEND_ACC_ABSTRACT) {
3166 zend_throw_exception_ex(reflection_exception_ptr, 0,
3167 "Trying to invoke abstract method %s::%s()",
3168 ZSTR_VAL(mptr->common.scope->name), ZSTR_VAL(mptr->common.function_name));
3169 RETURN_THROWS();
3170 }
3171
3172 if (!(mptr->common.fn_flags & ZEND_ACC_PUBLIC) && intern->ignore_visibility == 0) {
3173 zend_throw_exception_ex(reflection_exception_ptr, 0,
3174 "Trying to invoke %s method %s::%s() from scope %s",
3175 mptr->common.fn_flags & ZEND_ACC_PROTECTED ? "protected" : "private",
3176 ZSTR_VAL(mptr->common.scope->name), ZSTR_VAL(mptr->common.function_name),
3177 ZSTR_VAL(Z_OBJCE_P(ZEND_THIS)->name));
3178 RETURN_THROWS();
3179 }
3180
3181 if (variadic) {
3182 ZEND_PARSE_PARAMETERS_START(1, -1)
3183 Z_PARAM_OBJECT_OR_NULL(object)
3184 Z_PARAM_VARIADIC_WITH_NAMED(params, argc, named_params)
3185 ZEND_PARSE_PARAMETERS_END();
3186 } else {
3187 if (zend_parse_parameters(ZEND_NUM_ARGS(), "o!h", &object, &named_params) == FAILURE) {
3188 RETURN_THROWS();
3189 }
3190 }
3191
3192 /* In case this is a static method, we shouldn't pass an object_ptr
3193 * (which is used as calling context aka $this). We can thus ignore the
3194 * first parameter.
3195 *
3196 * Else, we verify that the given object is an instance of the class.
3197 */
3198 if (mptr->common.fn_flags & ZEND_ACC_STATIC) {
3199 object = NULL;
3200 obj_ce = mptr->common.scope;
3201 } else {
3202 if (!object) {
3203 zend_throw_exception_ex(reflection_exception_ptr, 0,
3204 "Trying to invoke non static method %s::%s() without an object",
3205 ZSTR_VAL(mptr->common.scope->name), ZSTR_VAL(mptr->common.function_name));
3206 RETURN_THROWS();
3207 }
3208
3209 obj_ce = Z_OBJCE_P(object);
3210
3211 if (!instanceof_function(obj_ce, mptr->common.scope)) {
3212 if (!variadic) {
3213 efree(params);
3214 }
3215 _DO_THROW("Given object is not an instance of the class this method was declared in");
3216 RETURN_THROWS();
3217 }
3218 }
3219
3220 fci.size = sizeof(fci);
3221 ZVAL_UNDEF(&fci.function_name);
3222 fci.object = object ? Z_OBJ_P(object) : NULL;
3223 fci.retval = &retval;
3224 fci.param_count = argc;
3225 fci.params = params;
3226 fci.named_params = named_params;
3227
3228 fcc.function_handler = mptr;
3229 fcc.called_scope = intern->ce;
3230 fcc.object = object ? Z_OBJ_P(object) : NULL;
3231
3232 /*
3233 * Copy the zend_function when calling via handler (e.g. Closure::__invoke())
3234 */
3235 if ((mptr->internal_function.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
3236 fcc.function_handler = _copy_function(mptr);
3237 }
3238
3239 result = zend_call_function(&fci, &fcc);
3240
3241 if (result == FAILURE) {
3242 zend_throw_exception_ex(reflection_exception_ptr, 0,
3243 "Invocation of method %s::%s() failed", ZSTR_VAL(mptr->common.scope->name), ZSTR_VAL(mptr->common.function_name));
3244 RETURN_THROWS();
3245 }
3246
3247 if (Z_TYPE(retval) != IS_UNDEF) {
3248 if (Z_ISREF(retval)) {
3249 zend_unwrap_reference(&retval);
3250 }
3251 ZVAL_COPY_VALUE(return_value, &retval);
3252 }
3253 }
3254 /* }}} */
3255
3256 /* {{{ Invokes the method. */
ZEND_METHOD(ReflectionMethod,invoke)3257 ZEND_METHOD(ReflectionMethod, invoke)
3258 {
3259 reflection_method_invoke(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
3260 }
3261 /* }}} */
3262
3263 /* {{{ Invokes the function and pass its arguments as array. */
ZEND_METHOD(ReflectionMethod,invokeArgs)3264 ZEND_METHOD(ReflectionMethod, invokeArgs)
3265 {
3266 reflection_method_invoke(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
3267 }
3268 /* }}} */
3269
3270 /* {{{ Returns whether this method is final */
ZEND_METHOD(ReflectionMethod,isFinal)3271 ZEND_METHOD(ReflectionMethod, isFinal)
3272 {
3273 _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_FINAL);
3274 }
3275 /* }}} */
3276
3277 /* {{{ Returns whether this method is abstract */
ZEND_METHOD(ReflectionMethod,isAbstract)3278 ZEND_METHOD(ReflectionMethod, isAbstract)
3279 {
3280 _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_ABSTRACT);
3281 }
3282 /* }}} */
3283
3284 /* {{{ Returns whether this method is public */
ZEND_METHOD(ReflectionMethod,isPublic)3285 ZEND_METHOD(ReflectionMethod, isPublic)
3286 {
3287 _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PUBLIC);
3288 }
3289 /* }}} */
3290
3291 /* {{{ Returns whether this method is private */
ZEND_METHOD(ReflectionMethod,isPrivate)3292 ZEND_METHOD(ReflectionMethod, isPrivate)
3293 {
3294 _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PRIVATE);
3295 }
3296 /* }}} */
3297
3298 /* {{{ Returns whether this method is protected */
ZEND_METHOD(ReflectionMethod,isProtected)3299 ZEND_METHOD(ReflectionMethod, isProtected)
3300 {
3301 _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PROTECTED);
3302 }
3303 /* }}} */
3304
3305 /* {{{ Returns whether this method is static */
ZEND_METHOD(ReflectionMethod,isStatic)3306 ZEND_METHOD(ReflectionMethod, isStatic)
3307 {
3308 _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_STATIC);
3309 }
3310 /* }}} */
3311
3312 /* {{{ Returns whether this function is deprecated */
ZEND_METHOD(ReflectionFunctionAbstract,isDeprecated)3313 ZEND_METHOD(ReflectionFunctionAbstract, isDeprecated)
3314 {
3315 _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_DEPRECATED);
3316 }
3317 /* }}} */
3318
3319 /* {{{ Returns whether this function is a generator */
ZEND_METHOD(ReflectionFunctionAbstract,isGenerator)3320 ZEND_METHOD(ReflectionFunctionAbstract, isGenerator)
3321 {
3322 _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_GENERATOR);
3323 }
3324 /* }}} */
3325
3326 /* {{{ Returns whether this function is variadic */
ZEND_METHOD(ReflectionFunctionAbstract,isVariadic)3327 ZEND_METHOD(ReflectionFunctionAbstract, isVariadic)
3328 {
3329 _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_VARIADIC);
3330 }
3331 /* }}} */
3332
3333 /* {{{ Returns whether this function is defined in namespace */
ZEND_METHOD(ReflectionFunctionAbstract,inNamespace)3334 ZEND_METHOD(ReflectionFunctionAbstract, inNamespace)
3335 {
3336 reflection_object *intern;
3337 zend_function *fptr;
3338
3339 if (zend_parse_parameters_none() == FAILURE) {
3340 RETURN_THROWS();
3341 }
3342
3343 GET_REFLECTION_OBJECT_PTR(fptr);
3344
3345 zend_string *name = fptr->common.function_name;
3346 const char *backslash = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
3347 RETURN_BOOL(backslash && backslash > ZSTR_VAL(name));
3348 }
3349 /* }}} */
3350
3351 /* {{{ Returns the name of namespace where this function is defined */
ZEND_METHOD(ReflectionFunctionAbstract,getNamespaceName)3352 ZEND_METHOD(ReflectionFunctionAbstract, getNamespaceName)
3353 {
3354 reflection_object *intern;
3355 zend_function *fptr;
3356
3357 if (zend_parse_parameters_none() == FAILURE) {
3358 RETURN_THROWS();
3359 }
3360
3361 GET_REFLECTION_OBJECT_PTR(fptr);
3362
3363 zend_string *name = fptr->common.function_name;
3364 const char *backslash = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
3365 if (backslash && backslash > ZSTR_VAL(name)) {
3366 RETURN_STRINGL(ZSTR_VAL(name), backslash - ZSTR_VAL(name));
3367 }
3368 RETURN_EMPTY_STRING();
3369 }
3370 /* }}} */
3371
3372 /* {{{ Returns the short name of the function (without namespace part) */
ZEND_METHOD(ReflectionFunctionAbstract,getShortName)3373 ZEND_METHOD(ReflectionFunctionAbstract, getShortName)
3374 {
3375 reflection_object *intern;
3376 zend_function *fptr;
3377
3378 if (zend_parse_parameters_none() == FAILURE) {
3379 RETURN_THROWS();
3380 }
3381
3382 GET_REFLECTION_OBJECT_PTR(fptr);
3383
3384 zend_string *name = fptr->common.function_name;
3385 const char *backslash = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
3386 if (backslash && backslash > ZSTR_VAL(name)) {
3387 RETURN_STRINGL(backslash + 1, ZSTR_LEN(name) - (backslash - ZSTR_VAL(name) + 1));
3388 }
3389 RETURN_STR_COPY(name);
3390 }
3391 /* }}} */
3392
3393 /* {{{ Return whether the function has a return type */
ZEND_METHOD(ReflectionFunctionAbstract,hasReturnType)3394 ZEND_METHOD(ReflectionFunctionAbstract, hasReturnType)
3395 {
3396 reflection_object *intern;
3397 zend_function *fptr;
3398
3399 if (zend_parse_parameters_none() == FAILURE) {
3400 RETURN_THROWS();
3401 }
3402
3403 GET_REFLECTION_OBJECT_PTR(fptr);
3404
3405 RETVAL_BOOL(fptr->op_array.fn_flags & ZEND_ACC_HAS_RETURN_TYPE);
3406 }
3407 /* }}} */
3408
3409 /* {{{ Returns the return type associated with the function */
ZEND_METHOD(ReflectionFunctionAbstract,getReturnType)3410 ZEND_METHOD(ReflectionFunctionAbstract, getReturnType)
3411 {
3412 reflection_object *intern;
3413 zend_function *fptr;
3414
3415 if (zend_parse_parameters_none() == FAILURE) {
3416 RETURN_THROWS();
3417 }
3418
3419 GET_REFLECTION_OBJECT_PTR(fptr);
3420
3421 if (!(fptr->op_array.fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
3422 RETURN_NULL();
3423 }
3424
3425 reflection_type_factory(fptr->common.arg_info[-1].type, return_value, 1);
3426 }
3427 /* }}} */
3428
3429 /* {{{ Returns whether this method is the constructor */
ZEND_METHOD(ReflectionMethod,isConstructor)3430 ZEND_METHOD(ReflectionMethod, isConstructor)
3431 {
3432 reflection_object *intern;
3433 zend_function *mptr;
3434
3435 if (zend_parse_parameters_none() == FAILURE) {
3436 RETURN_THROWS();
3437 }
3438 GET_REFLECTION_OBJECT_PTR(mptr);
3439 /* we need to check if the ctor is the ctor of the class level we we
3440 * looking at since we might be looking at an inherited old style ctor
3441 * defined in base class. */
3442 RETURN_BOOL((mptr->common.fn_flags & ZEND_ACC_CTOR) && intern->ce->constructor && intern->ce->constructor->common.scope == mptr->common.scope);
3443 }
3444 /* }}} */
3445
3446 /* {{{ Returns whether this method is a destructor */
ZEND_METHOD(ReflectionMethod,isDestructor)3447 ZEND_METHOD(ReflectionMethod, isDestructor)
3448 {
3449 reflection_object *intern;
3450 zend_function *mptr;
3451
3452 if (zend_parse_parameters_none() == FAILURE) {
3453 RETURN_THROWS();
3454 }
3455 GET_REFLECTION_OBJECT_PTR(mptr);
3456 RETURN_BOOL(zend_string_equals_literal_ci(
3457 mptr->common.function_name, ZEND_DESTRUCTOR_FUNC_NAME));
3458 }
3459 /* }}} */
3460
3461 /* {{{ Returns a bitfield of the access modifiers for this method */
ZEND_METHOD(ReflectionMethod,getModifiers)3462 ZEND_METHOD(ReflectionMethod, getModifiers)
3463 {
3464 reflection_object *intern;
3465 zend_function *mptr;
3466 uint32_t keep_flags = ZEND_ACC_PPP_MASK
3467 | ZEND_ACC_STATIC | ZEND_ACC_ABSTRACT | ZEND_ACC_FINAL;
3468
3469 if (zend_parse_parameters_none() == FAILURE) {
3470 RETURN_THROWS();
3471 }
3472 GET_REFLECTION_OBJECT_PTR(mptr);
3473
3474 RETURN_LONG((mptr->common.fn_flags & keep_flags));
3475 }
3476 /* }}} */
3477
3478 /* {{{ Get the declaring class */
ZEND_METHOD(ReflectionMethod,getDeclaringClass)3479 ZEND_METHOD(ReflectionMethod, getDeclaringClass)
3480 {
3481 reflection_object *intern;
3482 zend_function *mptr;
3483
3484 GET_REFLECTION_OBJECT_PTR(mptr);
3485
3486 if (zend_parse_parameters_none() == FAILURE) {
3487 RETURN_THROWS();
3488 }
3489
3490 zend_reflection_class_factory(mptr->common.scope, return_value);
3491 }
3492 /* }}} */
3493
3494 /* {{{ Get the prototype */
ZEND_METHOD(ReflectionMethod,getPrototype)3495 ZEND_METHOD(ReflectionMethod, getPrototype)
3496 {
3497 reflection_object *intern;
3498 zend_function *mptr;
3499
3500 GET_REFLECTION_OBJECT_PTR(mptr);
3501
3502 if (zend_parse_parameters_none() == FAILURE) {
3503 RETURN_THROWS();
3504 }
3505
3506 if (!mptr->common.prototype) {
3507 zend_throw_exception_ex(reflection_exception_ptr, 0,
3508 "Method %s::%s does not have a prototype", ZSTR_VAL(intern->ce->name), ZSTR_VAL(mptr->common.function_name));
3509 RETURN_THROWS();
3510 }
3511
3512 reflection_method_factory(mptr->common.prototype->common.scope, mptr->common.prototype, NULL, return_value);
3513 }
3514 /* }}} */
3515
3516 /* {{{ Sets whether non-public methods can be invoked */
ZEND_METHOD(ReflectionMethod,setAccessible)3517 ZEND_METHOD(ReflectionMethod, setAccessible)
3518 {
3519 reflection_object *intern;
3520 zend_bool visible;
3521
3522 if (zend_parse_parameters(ZEND_NUM_ARGS(), "b", &visible) == FAILURE) {
3523 RETURN_THROWS();
3524 }
3525
3526 intern = Z_REFLECTION_P(ZEND_THIS);
3527
3528 intern->ignore_visibility = visible;
3529 }
3530 /* }}} */
3531
3532 /* {{{ Constructor. Throws an Exception in case the given class constant does not exist */
ZEND_METHOD(ReflectionClassConstant,__construct)3533 ZEND_METHOD(ReflectionClassConstant, __construct)
3534 {
3535 zval *object;
3536 zend_string *classname_str;
3537 zend_object *classname_obj;
3538 zend_string *constname;
3539 reflection_object *intern;
3540 zend_class_entry *ce;
3541 zend_class_constant *constant = NULL;
3542
3543 ZEND_PARSE_PARAMETERS_START(2, 2)
3544 Z_PARAM_OBJ_OR_STR(classname_obj, classname_str)
3545 Z_PARAM_STR(constname)
3546 ZEND_PARSE_PARAMETERS_END();
3547
3548 if (classname_obj) {
3549 ce = classname_obj->ce;
3550 } else {
3551 if ((ce = zend_lookup_class(classname_str)) == NULL) {
3552 zend_throw_exception_ex(reflection_exception_ptr, 0, "Class \"%s\" does not exist", ZSTR_VAL(classname_str));
3553 RETURN_THROWS();
3554 }
3555 }
3556
3557 object = ZEND_THIS;
3558 intern = Z_REFLECTION_P(object);
3559
3560 if ((constant = zend_hash_find_ptr(&ce->constants_table, constname)) == NULL) {
3561 zend_throw_exception_ex(reflection_exception_ptr, 0, "Constant %s::%s does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(constname));
3562 RETURN_THROWS();
3563 }
3564
3565 intern->ptr = constant;
3566 intern->ref_type = REF_TYPE_CLASS_CONSTANT;
3567 intern->ce = constant->ce;
3568 intern->ignore_visibility = 0;
3569 ZVAL_STR_COPY(reflection_prop_name(object), constname);
3570 ZVAL_STR_COPY(reflection_prop_class(object), constant->ce->name);
3571 }
3572 /* }}} */
3573
3574 /* {{{ Returns a string representation */
ZEND_METHOD(ReflectionClassConstant,__toString)3575 ZEND_METHOD(ReflectionClassConstant, __toString)
3576 {
3577 reflection_object *intern;
3578 zend_class_constant *ref;
3579 smart_str str = {0};
3580 zval name;
3581
3582 if (zend_parse_parameters_none() == FAILURE) {
3583 RETURN_THROWS();
3584 }
3585 GET_REFLECTION_OBJECT_PTR(ref);
3586 _default_get_name(ZEND_THIS, &name);
3587 _class_const_string(&str, Z_STRVAL(name), ref, "");
3588 zval_ptr_dtor(&name);
3589 RETURN_STR(smart_str_extract(&str));
3590 }
3591 /* }}} */
3592
3593 /* {{{ Returns the constant' name */
ZEND_METHOD(ReflectionClassConstant,getName)3594 ZEND_METHOD(ReflectionClassConstant, getName)
3595 {
3596 if (zend_parse_parameters_none() == FAILURE) {
3597 RETURN_THROWS();
3598 }
3599 _default_get_name(ZEND_THIS, return_value);
3600 }
3601 /* }}} */
3602
_class_constant_check_flag(INTERNAL_FUNCTION_PARAMETERS,int mask)3603 static void _class_constant_check_flag(INTERNAL_FUNCTION_PARAMETERS, int mask) /* {{{ */
3604 {
3605 reflection_object *intern;
3606 zend_class_constant *ref;
3607
3608 if (zend_parse_parameters_none() == FAILURE) {
3609 RETURN_THROWS();
3610 }
3611 GET_REFLECTION_OBJECT_PTR(ref);
3612 RETURN_BOOL(Z_ACCESS_FLAGS(ref->value) & mask);
3613 }
3614 /* }}} */
3615
3616 /* {{{ Returns whether this constant is public */
ZEND_METHOD(ReflectionClassConstant,isPublic)3617 ZEND_METHOD(ReflectionClassConstant, isPublic)
3618 {
3619 _class_constant_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PUBLIC);
3620 }
3621 /* }}} */
3622
3623 /* {{{ Returns whether this constant is private */
ZEND_METHOD(ReflectionClassConstant,isPrivate)3624 ZEND_METHOD(ReflectionClassConstant, isPrivate)
3625 {
3626 _class_constant_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PRIVATE);
3627 }
3628 /* }}} */
3629
3630 /* {{{ Returns whether this constant is protected */
ZEND_METHOD(ReflectionClassConstant,isProtected)3631 ZEND_METHOD(ReflectionClassConstant, isProtected)
3632 {
3633 _class_constant_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PROTECTED);
3634 }
3635 /* }}} */
3636
3637 /* {{{ Returns a bitfield of the access modifiers for this constant */
ZEND_METHOD(ReflectionClassConstant,getModifiers)3638 ZEND_METHOD(ReflectionClassConstant, getModifiers)
3639 {
3640 reflection_object *intern;
3641 zend_class_constant *ref;
3642
3643 if (zend_parse_parameters_none() == FAILURE) {
3644 RETURN_THROWS();
3645 }
3646 GET_REFLECTION_OBJECT_PTR(ref);
3647
3648 RETURN_LONG(Z_ACCESS_FLAGS(ref->value));
3649 }
3650 /* }}} */
3651
3652 /* {{{ Returns this constant's value */
ZEND_METHOD(ReflectionClassConstant,getValue)3653 ZEND_METHOD(ReflectionClassConstant, getValue)
3654 {
3655 reflection_object *intern;
3656 zend_class_constant *ref;
3657
3658 if (zend_parse_parameters_none() == FAILURE) {
3659 RETURN_THROWS();
3660 }
3661 GET_REFLECTION_OBJECT_PTR(ref);
3662
3663 ZVAL_COPY_OR_DUP(return_value, &ref->value);
3664 if (Z_TYPE_P(return_value) == IS_CONSTANT_AST) {
3665 zval_update_constant_ex(return_value, ref->ce);
3666 }
3667 }
3668 /* }}} */
3669
3670 /* {{{ Get the declaring class */
ZEND_METHOD(ReflectionClassConstant,getDeclaringClass)3671 ZEND_METHOD(ReflectionClassConstant, getDeclaringClass)
3672 {
3673 reflection_object *intern;
3674 zend_class_constant *ref;
3675
3676 if (zend_parse_parameters_none() == FAILURE) {
3677 RETURN_THROWS();
3678 }
3679 GET_REFLECTION_OBJECT_PTR(ref);
3680
3681 zend_reflection_class_factory(ref->ce, return_value);
3682 }
3683 /* }}} */
3684
3685 /* {{{ Returns the doc comment for this constant */
ZEND_METHOD(ReflectionClassConstant,getDocComment)3686 ZEND_METHOD(ReflectionClassConstant, getDocComment)
3687 {
3688 reflection_object *intern;
3689 zend_class_constant *ref;
3690
3691 if (zend_parse_parameters_none() == FAILURE) {
3692 RETURN_THROWS();
3693 }
3694 GET_REFLECTION_OBJECT_PTR(ref);
3695 if (ref->doc_comment) {
3696 RETURN_STR_COPY(ref->doc_comment);
3697 }
3698 RETURN_FALSE;
3699 }
3700 /* }}} */
3701
3702 /* {{{ Returns the attributes of this constant */
ZEND_METHOD(ReflectionClassConstant,getAttributes)3703 ZEND_METHOD(ReflectionClassConstant, getAttributes)
3704 {
3705 reflection_object *intern;
3706 zend_class_constant *ref;
3707
3708 GET_REFLECTION_OBJECT_PTR(ref);
3709
3710 reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU,
3711 ref->attributes, 0, ref->ce, ZEND_ATTRIBUTE_TARGET_CLASS_CONST,
3712 ref->ce->type == ZEND_USER_CLASS ? ref->ce->info.user.filename : NULL);
3713 }
3714 /* }}} */
3715
3716 /* {{{ reflection_class_object_ctor */
reflection_class_object_ctor(INTERNAL_FUNCTION_PARAMETERS,int is_object)3717 static void reflection_class_object_ctor(INTERNAL_FUNCTION_PARAMETERS, int is_object)
3718 {
3719 zval *object;
3720 zend_string *arg_class = NULL;
3721 zend_object *arg_obj;
3722 reflection_object *intern;
3723 zend_class_entry *ce;
3724
3725 if (is_object) {
3726 ZEND_PARSE_PARAMETERS_START(1, 1)
3727 Z_PARAM_OBJ(arg_obj)
3728 ZEND_PARSE_PARAMETERS_END();
3729 } else {
3730 ZEND_PARSE_PARAMETERS_START(1, 1)
3731 Z_PARAM_OBJ_OR_STR(arg_obj, arg_class)
3732 ZEND_PARSE_PARAMETERS_END();
3733 }
3734
3735 object = ZEND_THIS;
3736 intern = Z_REFLECTION_P(object);
3737
3738 if (arg_obj) {
3739 ZVAL_STR_COPY(reflection_prop_name(object), arg_obj->ce->name);
3740 intern->ptr = arg_obj->ce;
3741 if (is_object) {
3742 ZVAL_OBJ_COPY(&intern->obj, arg_obj);
3743 }
3744 } else {
3745 if ((ce = zend_lookup_class(arg_class)) == NULL) {
3746 if (!EG(exception)) {
3747 zend_throw_exception_ex(reflection_exception_ptr, -1, "Class \"%s\" does not exist", ZSTR_VAL(arg_class));
3748 }
3749 RETURN_THROWS();
3750 }
3751
3752 ZVAL_STR_COPY(reflection_prop_name(object), ce->name);
3753 intern->ptr = ce;
3754 }
3755 intern->ref_type = REF_TYPE_OTHER;
3756 }
3757 /* }}} */
3758
3759 /* {{{ Constructor. Takes a string or an instance as an argument */
ZEND_METHOD(ReflectionClass,__construct)3760 ZEND_METHOD(ReflectionClass, __construct)
3761 {
3762 reflection_class_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
3763 }
3764 /* }}} */
3765
3766 /* {{{ add_class_vars */
add_class_vars(zend_class_entry * ce,zend_bool statics,zval * return_value)3767 static void add_class_vars(zend_class_entry *ce, zend_bool statics, zval *return_value)
3768 {
3769 zend_property_info *prop_info;
3770 zval *prop, prop_copy;
3771 zend_string *key;
3772
3773 ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->properties_info, key, prop_info) {
3774 if (((prop_info->flags & ZEND_ACC_PRIVATE) &&
3775 prop_info->ce != ce)) {
3776 continue;
3777 }
3778
3779 zend_bool is_static = (prop_info->flags & ZEND_ACC_STATIC) != 0;
3780 if (statics != is_static) {
3781 continue;
3782 }
3783
3784 prop = property_get_default(prop_info);
3785 if (Z_ISUNDEF_P(prop)) {
3786 continue;
3787 }
3788
3789 /* copy: enforce read only access */
3790 ZVAL_DEREF(prop);
3791 ZVAL_COPY_OR_DUP(&prop_copy, prop);
3792
3793 /* this is necessary to make it able to work with default array
3794 * properties, returned to user */
3795 if (Z_TYPE(prop_copy) == IS_CONSTANT_AST) {
3796 if (UNEXPECTED(zval_update_constant_ex(&prop_copy, ce) != SUCCESS)) {
3797 return;
3798 }
3799 }
3800
3801 zend_hash_update(Z_ARRVAL_P(return_value), key, &prop_copy);
3802 } ZEND_HASH_FOREACH_END();
3803 }
3804 /* }}} */
3805
3806 /* {{{ Returns an associative array containing all static property values of the class */
ZEND_METHOD(ReflectionClass,getStaticProperties)3807 ZEND_METHOD(ReflectionClass, getStaticProperties)
3808 {
3809 reflection_object *intern;
3810 zend_class_entry *ce;
3811 zend_property_info *prop_info;
3812 zval *prop;
3813 zend_string *key;
3814
3815 if (zend_parse_parameters_none() == FAILURE) {
3816 RETURN_THROWS();
3817 }
3818
3819 GET_REFLECTION_OBJECT_PTR(ce);
3820
3821 if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
3822 RETURN_THROWS();
3823 }
3824
3825 if (ce->default_static_members_count && !CE_STATIC_MEMBERS(ce)) {
3826 zend_class_init_statics(ce);
3827 }
3828
3829 array_init(return_value);
3830
3831 ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->properties_info, key, prop_info) {
3832 if (((prop_info->flags & ZEND_ACC_PRIVATE) &&
3833 prop_info->ce != ce)) {
3834 continue;
3835 }
3836 if ((prop_info->flags & ZEND_ACC_STATIC) == 0) {
3837 continue;
3838 }
3839
3840 prop = &CE_STATIC_MEMBERS(ce)[prop_info->offset];
3841 ZVAL_DEINDIRECT(prop);
3842
3843 if (ZEND_TYPE_IS_SET(prop_info->type) && Z_ISUNDEF_P(prop)) {
3844 continue;
3845 }
3846
3847 /* enforce read only access */
3848 ZVAL_DEREF(prop);
3849 Z_TRY_ADDREF_P(prop);
3850
3851 zend_hash_update(Z_ARRVAL_P(return_value), key, prop);
3852 } ZEND_HASH_FOREACH_END();
3853 }
3854 /* }}} */
3855
3856 /* {{{ Returns the value of a static property */
ZEND_METHOD(ReflectionClass,getStaticPropertyValue)3857 ZEND_METHOD(ReflectionClass, getStaticPropertyValue)
3858 {
3859 reflection_object *intern;
3860 zend_class_entry *ce, *old_scope;
3861 zend_string *name;
3862 zval *prop, *def_value = NULL;
3863
3864 if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|z", &name, &def_value) == FAILURE) {
3865 RETURN_THROWS();
3866 }
3867
3868 GET_REFLECTION_OBJECT_PTR(ce);
3869
3870 if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
3871 RETURN_THROWS();
3872 }
3873
3874 old_scope = EG(fake_scope);
3875 EG(fake_scope) = ce;
3876 prop = zend_std_get_static_property(ce, name, BP_VAR_IS);
3877 EG(fake_scope) = old_scope;
3878
3879 if (!prop) {
3880 if (def_value) {
3881 ZVAL_COPY(return_value, def_value);
3882 } else {
3883 zend_throw_exception_ex(reflection_exception_ptr, 0,
3884 "Property %s::$%s does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(name));
3885 }
3886 return;
3887 } else {
3888 ZVAL_COPY_DEREF(return_value, prop);
3889 }
3890 }
3891 /* }}} */
3892
3893 /* {{{ Sets the value of a static property */
ZEND_METHOD(ReflectionClass,setStaticPropertyValue)3894 ZEND_METHOD(ReflectionClass, setStaticPropertyValue)
3895 {
3896 reflection_object *intern;
3897 zend_class_entry *ce, *old_scope;
3898 zend_property_info *prop_info;
3899 zend_string *name;
3900 zval *variable_ptr, *value;
3901
3902 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sz", &name, &value) == FAILURE) {
3903 RETURN_THROWS();
3904 }
3905
3906 GET_REFLECTION_OBJECT_PTR(ce);
3907
3908 if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
3909 RETURN_THROWS();
3910 }
3911 old_scope = EG(fake_scope);
3912 EG(fake_scope) = ce;
3913 variable_ptr = zend_std_get_static_property_with_info(ce, name, BP_VAR_W, &prop_info);
3914 EG(fake_scope) = old_scope;
3915 if (!variable_ptr) {
3916 zend_clear_exception();
3917 zend_throw_exception_ex(reflection_exception_ptr, 0,
3918 "Class %s does not have a property named %s", ZSTR_VAL(ce->name), ZSTR_VAL(name));
3919 RETURN_THROWS();
3920 }
3921
3922 if (Z_ISREF_P(variable_ptr)) {
3923 zend_reference *ref = Z_REF_P(variable_ptr);
3924 variable_ptr = Z_REFVAL_P(variable_ptr);
3925
3926 if (!zend_verify_ref_assignable_zval(ref, value, 0)) {
3927 return;
3928 }
3929 }
3930
3931 if (ZEND_TYPE_IS_SET(prop_info->type) && !zend_verify_property_type(prop_info, value, 0)) {
3932 return;
3933 }
3934
3935 zval_ptr_dtor(variable_ptr);
3936 ZVAL_COPY(variable_ptr, value);
3937
3938 }
3939 /* }}} */
3940
3941 /* {{{ Returns an associative array containing copies of all default property values of the class */
ZEND_METHOD(ReflectionClass,getDefaultProperties)3942 ZEND_METHOD(ReflectionClass, getDefaultProperties)
3943 {
3944 reflection_object *intern;
3945 zend_class_entry *ce;
3946
3947 if (zend_parse_parameters_none() == FAILURE) {
3948 RETURN_THROWS();
3949 }
3950 GET_REFLECTION_OBJECT_PTR(ce);
3951 array_init(return_value);
3952 if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
3953 RETURN_THROWS();
3954 }
3955 add_class_vars(ce, 1, return_value);
3956 add_class_vars(ce, 0, return_value);
3957 }
3958 /* }}} */
3959
3960 /* {{{ Returns a string representation */
ZEND_METHOD(ReflectionClass,__toString)3961 ZEND_METHOD(ReflectionClass, __toString)
3962 {
3963 reflection_object *intern;
3964 zend_class_entry *ce;
3965 smart_str str = {0};
3966
3967 if (zend_parse_parameters_none() == FAILURE) {
3968 RETURN_THROWS();
3969 }
3970 GET_REFLECTION_OBJECT_PTR(ce);
3971 _class_string(&str, ce, &intern->obj, "");
3972 RETURN_STR(smart_str_extract(&str));
3973 }
3974 /* }}} */
3975
3976 /* {{{ Returns the class' name */
ZEND_METHOD(ReflectionClass,getName)3977 ZEND_METHOD(ReflectionClass, getName)
3978 {
3979 reflection_object *intern;
3980 zend_class_entry *ce;
3981
3982 if (zend_parse_parameters_none() == FAILURE) {
3983 RETURN_THROWS();
3984 }
3985
3986 GET_REFLECTION_OBJECT_PTR(ce);
3987 RETURN_STR_COPY(ce->name);
3988 }
3989 /* }}} */
3990
3991 /* {{{ Returns whether this class is an internal class */
ZEND_METHOD(ReflectionClass,isInternal)3992 ZEND_METHOD(ReflectionClass, isInternal)
3993 {
3994 reflection_object *intern;
3995 zend_class_entry *ce;
3996
3997 if (zend_parse_parameters_none() == FAILURE) {
3998 RETURN_THROWS();
3999 }
4000 GET_REFLECTION_OBJECT_PTR(ce);
4001 RETURN_BOOL(ce->type == ZEND_INTERNAL_CLASS);
4002 }
4003 /* }}} */
4004
4005 /* {{{ Returns whether this class is user-defined */
ZEND_METHOD(ReflectionClass,isUserDefined)4006 ZEND_METHOD(ReflectionClass, isUserDefined)
4007 {
4008 reflection_object *intern;
4009 zend_class_entry *ce;
4010
4011 if (zend_parse_parameters_none() == FAILURE) {
4012 RETURN_THROWS();
4013 }
4014 GET_REFLECTION_OBJECT_PTR(ce);
4015 RETURN_BOOL(ce->type == ZEND_USER_CLASS);
4016 }
4017 /* }}} */
4018
4019 /* {{{ Returns whether this class is anonymous */
ZEND_METHOD(ReflectionClass,isAnonymous)4020 ZEND_METHOD(ReflectionClass, isAnonymous)
4021 {
4022 reflection_object *intern;
4023 zend_class_entry *ce;
4024
4025 if (zend_parse_parameters_none() == FAILURE) {
4026 RETURN_THROWS();
4027 }
4028 GET_REFLECTION_OBJECT_PTR(ce);
4029 RETURN_BOOL(ce->ce_flags & ZEND_ACC_ANON_CLASS);
4030 }
4031 /* }}} */
4032
4033 /* {{{ Returns the filename of the file this class was declared in */
ZEND_METHOD(ReflectionClass,getFileName)4034 ZEND_METHOD(ReflectionClass, getFileName)
4035 {
4036 reflection_object *intern;
4037 zend_class_entry *ce;
4038
4039 if (zend_parse_parameters_none() == FAILURE) {
4040 RETURN_THROWS();
4041 }
4042 GET_REFLECTION_OBJECT_PTR(ce);
4043 if (ce->type == ZEND_USER_CLASS) {
4044 RETURN_STR_COPY(ce->info.user.filename);
4045 }
4046 RETURN_FALSE;
4047 }
4048 /* }}} */
4049
4050 /* {{{ Returns the line this class' declaration starts at */
ZEND_METHOD(ReflectionClass,getStartLine)4051 ZEND_METHOD(ReflectionClass, getStartLine)
4052 {
4053 reflection_object *intern;
4054 zend_class_entry *ce;
4055
4056 if (zend_parse_parameters_none() == FAILURE) {
4057 RETURN_THROWS();
4058 }
4059 GET_REFLECTION_OBJECT_PTR(ce);
4060 if (ce->type == ZEND_USER_CLASS) {
4061 RETURN_LONG(ce->info.user.line_start);
4062 }
4063 RETURN_FALSE;
4064 }
4065 /* }}} */
4066
4067 /* {{{ Returns the line this class' declaration ends at */
ZEND_METHOD(ReflectionClass,getEndLine)4068 ZEND_METHOD(ReflectionClass, getEndLine)
4069 {
4070 reflection_object *intern;
4071 zend_class_entry *ce;
4072
4073 if (zend_parse_parameters_none() == FAILURE) {
4074 RETURN_THROWS();
4075 }
4076 GET_REFLECTION_OBJECT_PTR(ce);
4077 if (ce->type == ZEND_USER_CLASS) {
4078 RETURN_LONG(ce->info.user.line_end);
4079 }
4080 RETURN_FALSE;
4081 }
4082 /* }}} */
4083
4084 /* {{{ Returns the doc comment for this class */
ZEND_METHOD(ReflectionClass,getDocComment)4085 ZEND_METHOD(ReflectionClass, getDocComment)
4086 {
4087 reflection_object *intern;
4088 zend_class_entry *ce;
4089
4090 if (zend_parse_parameters_none() == FAILURE) {
4091 RETURN_THROWS();
4092 }
4093 GET_REFLECTION_OBJECT_PTR(ce);
4094 if (ce->type == ZEND_USER_CLASS && ce->info.user.doc_comment) {
4095 RETURN_STR_COPY(ce->info.user.doc_comment);
4096 }
4097 RETURN_FALSE;
4098 }
4099 /* }}} */
4100
4101 /* {{{ Returns the attributes for this class */
ZEND_METHOD(ReflectionClass,getAttributes)4102 ZEND_METHOD(ReflectionClass, getAttributes)
4103 {
4104 reflection_object *intern;
4105 zend_class_entry *ce;
4106
4107 GET_REFLECTION_OBJECT_PTR(ce);
4108
4109 reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU,
4110 ce->attributes, 0, ce, ZEND_ATTRIBUTE_TARGET_CLASS,
4111 ce->type == ZEND_USER_CLASS ? ce->info.user.filename : NULL);
4112 }
4113 /* }}} */
4114
4115 /* {{{ Returns the class' constructor if there is one, NULL otherwise */
ZEND_METHOD(ReflectionClass,getConstructor)4116 ZEND_METHOD(ReflectionClass, getConstructor)
4117 {
4118 reflection_object *intern;
4119 zend_class_entry *ce;
4120
4121 if (zend_parse_parameters_none() == FAILURE) {
4122 RETURN_THROWS();
4123 }
4124 GET_REFLECTION_OBJECT_PTR(ce);
4125
4126 if (ce->constructor) {
4127 reflection_method_factory(ce, ce->constructor, NULL, return_value);
4128 } else {
4129 RETURN_NULL();
4130 }
4131 }
4132 /* }}} */
4133
4134 /* {{{ Returns whether a method exists or not */
ZEND_METHOD(ReflectionClass,hasMethod)4135 ZEND_METHOD(ReflectionClass, hasMethod)
4136 {
4137 reflection_object *intern;
4138 zend_class_entry *ce;
4139 zend_string *name, *lc_name;
4140
4141 if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
4142 RETURN_THROWS();
4143 }
4144
4145 GET_REFLECTION_OBJECT_PTR(ce);
4146 lc_name = zend_string_tolower(name);
4147 RETVAL_BOOL(zend_hash_exists(&ce->function_table, lc_name) || is_closure_invoke(ce, lc_name));
4148 zend_string_release(lc_name);
4149 }
4150 /* }}} */
4151
4152 /* {{{ Returns the class' method specified by its name */
ZEND_METHOD(ReflectionClass,getMethod)4153 ZEND_METHOD(ReflectionClass, getMethod)
4154 {
4155 reflection_object *intern;
4156 zend_class_entry *ce;
4157 zend_function *mptr;
4158 zval obj_tmp;
4159 zend_string *name, *lc_name;
4160
4161 if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
4162 RETURN_THROWS();
4163 }
4164
4165 GET_REFLECTION_OBJECT_PTR(ce);
4166 lc_name = zend_string_tolower(name);
4167 if (!Z_ISUNDEF(intern->obj) && is_closure_invoke(ce, lc_name)
4168 && (mptr = zend_get_closure_invoke_method(Z_OBJ(intern->obj))) != NULL)
4169 {
4170 /* don't assign closure_object since we only reflect the invoke handler
4171 method and not the closure definition itself */
4172 reflection_method_factory(ce, mptr, NULL, return_value);
4173 } else if (Z_ISUNDEF(intern->obj) && is_closure_invoke(ce, lc_name)
4174 && object_init_ex(&obj_tmp, ce) == SUCCESS && (mptr = zend_get_closure_invoke_method(Z_OBJ(obj_tmp))) != NULL) {
4175 /* don't assign closure_object since we only reflect the invoke handler
4176 method and not the closure definition itself */
4177 reflection_method_factory(ce, mptr, NULL, return_value);
4178 zval_ptr_dtor(&obj_tmp);
4179 } else if ((mptr = zend_hash_find_ptr(&ce->function_table, lc_name)) != NULL) {
4180 reflection_method_factory(ce, mptr, NULL, return_value);
4181 } else {
4182 zend_throw_exception_ex(reflection_exception_ptr, 0,
4183 "Method %s::%s() does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(name));
4184 }
4185 zend_string_release(lc_name);
4186 }
4187 /* }}} */
4188
4189 /* {{{ _addmethod */
_addmethod(zend_function * mptr,zend_class_entry * ce,zval * retval,zend_long filter)4190 static void _addmethod(zend_function *mptr, zend_class_entry *ce, zval *retval, zend_long filter)
4191 {
4192 if ((mptr->common.fn_flags & ZEND_ACC_PRIVATE) && mptr->common.scope != ce) {
4193 return;
4194 }
4195
4196 if (mptr->common.fn_flags & filter) {
4197 zval method;
4198 reflection_method_factory(ce, mptr, NULL, &method);
4199 add_next_index_zval(retval, &method);
4200 }
4201 }
4202 /* }}} */
4203
4204 /* {{{ Returns an array of this class' methods */
ZEND_METHOD(ReflectionClass,getMethods)4205 ZEND_METHOD(ReflectionClass, getMethods)
4206 {
4207 reflection_object *intern;
4208 zend_class_entry *ce;
4209 zend_function *mptr;
4210 zend_long filter;
4211 zend_bool filter_is_null = 1;
4212
4213 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l!", &filter, &filter_is_null) == FAILURE) {
4214 RETURN_THROWS();
4215 }
4216
4217 if (filter_is_null) {
4218 filter = ZEND_ACC_PPP_MASK | ZEND_ACC_ABSTRACT | ZEND_ACC_FINAL | ZEND_ACC_STATIC;
4219 }
4220
4221 GET_REFLECTION_OBJECT_PTR(ce);
4222
4223 array_init(return_value);
4224 ZEND_HASH_FOREACH_PTR(&ce->function_table, mptr) {
4225 _addmethod(mptr, ce, return_value, filter);
4226 } ZEND_HASH_FOREACH_END();
4227
4228 if (instanceof_function(ce, zend_ce_closure)) {
4229 zend_bool has_obj = Z_TYPE(intern->obj) != IS_UNDEF;
4230 zval obj_tmp;
4231 zend_object *obj;
4232 if (!has_obj) {
4233 object_init_ex(&obj_tmp, ce);
4234 obj = Z_OBJ(obj_tmp);
4235 } else {
4236 obj = Z_OBJ(intern->obj);
4237 }
4238 zend_function *closure = zend_get_closure_invoke_method(obj);
4239 if (closure) {
4240 _addmethod(closure, ce, return_value, filter);
4241 }
4242 if (!has_obj) {
4243 zval_ptr_dtor(&obj_tmp);
4244 }
4245 }
4246 }
4247 /* }}} */
4248
4249 /* {{{ Returns whether a property exists or not */
ZEND_METHOD(ReflectionClass,hasProperty)4250 ZEND_METHOD(ReflectionClass, hasProperty)
4251 {
4252 reflection_object *intern;
4253 zend_property_info *property_info;
4254 zend_class_entry *ce;
4255 zend_string *name;
4256
4257 if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
4258 RETURN_THROWS();
4259 }
4260
4261 GET_REFLECTION_OBJECT_PTR(ce);
4262 if ((property_info = zend_hash_find_ptr(&ce->properties_info, name)) != NULL) {
4263 if ((property_info->flags & ZEND_ACC_PRIVATE) && property_info->ce != ce) {
4264 RETURN_FALSE;
4265 }
4266 RETURN_TRUE;
4267 } else {
4268 if (Z_TYPE(intern->obj) != IS_UNDEF) {
4269 if (Z_OBJ_HANDLER(intern->obj, has_property)(Z_OBJ(intern->obj), name, 2, NULL)) {
4270 RETURN_TRUE;
4271 }
4272 }
4273 RETURN_FALSE;
4274 }
4275 }
4276 /* }}} */
4277
4278 /* {{{ Returns the class' property specified by its name */
ZEND_METHOD(ReflectionClass,getProperty)4279 ZEND_METHOD(ReflectionClass, getProperty)
4280 {
4281 reflection_object *intern;
4282 zend_class_entry *ce, *ce2;
4283 zend_property_info *property_info;
4284 zend_string *name, *classname;
4285 char *tmp, *str_name;
4286 size_t classname_len, str_name_len;
4287
4288 if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
4289 RETURN_THROWS();
4290 }
4291
4292 GET_REFLECTION_OBJECT_PTR(ce);
4293 if ((property_info = zend_hash_find_ptr(&ce->properties_info, name)) != NULL) {
4294 if (!(property_info->flags & ZEND_ACC_PRIVATE) || property_info->ce == ce) {
4295 reflection_property_factory(ce, name, property_info, return_value);
4296 return;
4297 }
4298 } else if (Z_TYPE(intern->obj) != IS_UNDEF) {
4299 /* Check for dynamic properties */
4300 if (zend_hash_exists(Z_OBJ_HT(intern->obj)->get_properties(Z_OBJ(intern->obj)), name)) {
4301 reflection_property_factory(ce, name, NULL, return_value);
4302 return;
4303 }
4304 }
4305 str_name = ZSTR_VAL(name);
4306 if ((tmp = strstr(ZSTR_VAL(name), "::")) != NULL) {
4307 classname_len = tmp - ZSTR_VAL(name);
4308 classname = zend_string_alloc(classname_len, 0);
4309 zend_str_tolower_copy(ZSTR_VAL(classname), ZSTR_VAL(name), classname_len);
4310 ZSTR_VAL(classname)[classname_len] = '\0';
4311 str_name_len = ZSTR_LEN(name) - (classname_len + 2);
4312 str_name = tmp + 2;
4313
4314 ce2 = zend_lookup_class(classname);
4315 if (!ce2) {
4316 if (!EG(exception)) {
4317 zend_throw_exception_ex(reflection_exception_ptr, -1, "Class \"%s\" does not exist", ZSTR_VAL(classname));
4318 }
4319 zend_string_release_ex(classname, 0);
4320 RETURN_THROWS();
4321 }
4322 zend_string_release_ex(classname, 0);
4323
4324 if (!instanceof_function(ce, ce2)) {
4325 zend_throw_exception_ex(reflection_exception_ptr, -1, "Fully qualified property name %s::$%s does not specify a base class of %s", ZSTR_VAL(ce2->name), str_name, ZSTR_VAL(ce->name));
4326 RETURN_THROWS();
4327 }
4328 ce = ce2;
4329
4330 property_info = zend_hash_str_find_ptr(&ce->properties_info, str_name, str_name_len);
4331 if (property_info != NULL
4332 && (!(property_info->flags & ZEND_ACC_PRIVATE)
4333 || property_info->ce == ce)) {
4334 reflection_property_factory_str(ce, str_name, str_name_len, property_info, return_value);
4335 return;
4336 }
4337 }
4338 zend_throw_exception_ex(reflection_exception_ptr, 0, "Property %s::$%s does not exist", ZSTR_VAL(ce->name), str_name);
4339 }
4340 /* }}} */
4341
4342 /* {{{ _addproperty */
_addproperty(zend_property_info * pptr,zend_string * key,zend_class_entry * ce,zval * retval,long filter)4343 static void _addproperty(zend_property_info *pptr, zend_string *key, zend_class_entry *ce, zval *retval, long filter)
4344 {
4345 if ((pptr->flags & ZEND_ACC_PRIVATE) && pptr->ce != ce) {
4346 return;
4347 }
4348
4349 if (pptr->flags & filter) {
4350 zval property;
4351 reflection_property_factory(ce, key, pptr, &property);
4352 add_next_index_zval(retval, &property);
4353 }
4354 }
4355 /* }}} */
4356
4357 /* {{{ _adddynproperty */
_adddynproperty(zval * ptr,zend_string * key,zend_class_entry * ce,zval * retval)4358 static void _adddynproperty(zval *ptr, zend_string *key, zend_class_entry *ce, zval *retval)
4359 {
4360 zval property;
4361
4362 /* under some circumstances, the properties hash table may contain numeric
4363 * properties (e.g. when casting from array). This is a WON'T FIX bug, at
4364 * least for the moment. Ignore these */
4365 if (key == NULL) {
4366 return;
4367 }
4368
4369 /* Not a dynamic property */
4370 if (Z_TYPE_P(ptr) == IS_INDIRECT) {
4371 return;
4372 }
4373
4374 reflection_property_factory(ce, key, NULL, &property);
4375 add_next_index_zval(retval, &property);
4376 }
4377 /* }}} */
4378
4379 /* {{{ Returns an array of this class' properties */
ZEND_METHOD(ReflectionClass,getProperties)4380 ZEND_METHOD(ReflectionClass, getProperties)
4381 {
4382 reflection_object *intern;
4383 zend_class_entry *ce;
4384 zend_string *key;
4385 zend_property_info *prop_info;
4386 zend_long filter;
4387 zend_bool filter_is_null = 1;
4388
4389 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l!", &filter, &filter_is_null) == FAILURE) {
4390 RETURN_THROWS();
4391 }
4392
4393 if (filter_is_null) {
4394 filter = ZEND_ACC_PPP_MASK | ZEND_ACC_STATIC;
4395 }
4396
4397 GET_REFLECTION_OBJECT_PTR(ce);
4398
4399 array_init(return_value);
4400 ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->properties_info, key, prop_info) {
4401 _addproperty(prop_info, key, ce, return_value, filter);
4402 } ZEND_HASH_FOREACH_END();
4403
4404 if (Z_TYPE(intern->obj) != IS_UNDEF && (filter & ZEND_ACC_PUBLIC) != 0) {
4405 HashTable *properties = Z_OBJ_HT(intern->obj)->get_properties(Z_OBJ(intern->obj));
4406 zval *prop;
4407 ZEND_HASH_FOREACH_STR_KEY_VAL(properties, key, prop) {
4408 _adddynproperty(prop, key, ce, return_value);
4409 } ZEND_HASH_FOREACH_END();
4410 }
4411 }
4412 /* }}} */
4413
4414 /* {{{ Returns whether a constant exists or not */
ZEND_METHOD(ReflectionClass,hasConstant)4415 ZEND_METHOD(ReflectionClass, hasConstant)
4416 {
4417 reflection_object *intern;
4418 zend_class_entry *ce;
4419 zend_string *name;
4420
4421 if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
4422 RETURN_THROWS();
4423 }
4424
4425 GET_REFLECTION_OBJECT_PTR(ce);
4426 if (zend_hash_exists(&ce->constants_table, name)) {
4427 RETURN_TRUE;
4428 } else {
4429 RETURN_FALSE;
4430 }
4431 }
4432 /* }}} */
4433
4434 /* {{{ Returns an associative array containing this class' constants and their values */
ZEND_METHOD(ReflectionClass,getConstants)4435 ZEND_METHOD(ReflectionClass, getConstants)
4436 {
4437 reflection_object *intern;
4438 zend_class_entry *ce;
4439 zend_string *key;
4440 zend_class_constant *constant;
4441 zval val;
4442 zend_long filter;
4443 zend_bool filter_is_null = 1;
4444
4445 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l!", &filter, &filter_is_null) == FAILURE) {
4446 RETURN_THROWS();
4447 }
4448
4449 if (filter_is_null) {
4450 filter = ZEND_ACC_PPP_MASK;
4451 }
4452
4453 GET_REFLECTION_OBJECT_PTR(ce);
4454
4455 array_init(return_value);
4456 ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->constants_table, key, constant) {
4457 if (UNEXPECTED(zval_update_constant_ex(&constant->value, ce) != SUCCESS)) {
4458 RETURN_THROWS();
4459 }
4460
4461 if (Z_ACCESS_FLAGS(constant->value) & filter) {
4462 ZVAL_COPY_OR_DUP(&val, &constant->value);
4463 zend_hash_add_new(Z_ARRVAL_P(return_value), key, &val);
4464 }
4465 } ZEND_HASH_FOREACH_END();
4466 }
4467 /* }}} */
4468
4469 /* {{{ Returns an associative array containing this class' constants as ReflectionClassConstant objects */
ZEND_METHOD(ReflectionClass,getReflectionConstants)4470 ZEND_METHOD(ReflectionClass, getReflectionConstants)
4471 {
4472 reflection_object *intern;
4473 zend_class_entry *ce;
4474 zend_string *name;
4475 zend_class_constant *constant;
4476 zend_long filter;
4477 zend_bool filter_is_null = 1;
4478
4479 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l!", &filter, &filter_is_null) == FAILURE) {
4480 RETURN_THROWS();
4481 }
4482
4483 if (filter_is_null) {
4484 filter = ZEND_ACC_PPP_MASK;
4485 }
4486
4487 GET_REFLECTION_OBJECT_PTR(ce);
4488
4489 array_init(return_value);
4490 ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->constants_table, name, constant) {
4491 if (Z_ACCESS_FLAGS(constant->value) & filter) {
4492 zval class_const;
4493 reflection_class_constant_factory(name, constant, &class_const);
4494 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &class_const);
4495 }
4496 } ZEND_HASH_FOREACH_END();
4497 }
4498 /* }}} */
4499
4500 /* {{{ Returns the class' constant specified by its name */
ZEND_METHOD(ReflectionClass,getConstant)4501 ZEND_METHOD(ReflectionClass, getConstant)
4502 {
4503 reflection_object *intern;
4504 zend_class_entry *ce;
4505 zend_class_constant *c;
4506 zend_string *name;
4507
4508 if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
4509 RETURN_THROWS();
4510 }
4511
4512 GET_REFLECTION_OBJECT_PTR(ce);
4513 ZEND_HASH_FOREACH_PTR(&ce->constants_table, c) {
4514 if (UNEXPECTED(zval_update_constant_ex(&c->value, ce) != SUCCESS)) {
4515 RETURN_THROWS();
4516 }
4517 } ZEND_HASH_FOREACH_END();
4518 if ((c = zend_hash_find_ptr(&ce->constants_table, name)) == NULL) {
4519 RETURN_FALSE;
4520 }
4521 ZVAL_COPY_OR_DUP(return_value, &c->value);
4522 }
4523 /* }}} */
4524
4525 /* {{{ Returns the class' constant as ReflectionClassConstant objects */
ZEND_METHOD(ReflectionClass,getReflectionConstant)4526 ZEND_METHOD(ReflectionClass, getReflectionConstant)
4527 {
4528 reflection_object *intern;
4529 zend_class_entry *ce;
4530 zend_class_constant *constant;
4531 zend_string *name;
4532
4533 GET_REFLECTION_OBJECT_PTR(ce);
4534 if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
4535 RETURN_THROWS();
4536 }
4537
4538 if ((constant = zend_hash_find_ptr(&ce->constants_table, name)) == NULL) {
4539 RETURN_FALSE;
4540 }
4541 reflection_class_constant_factory(name, constant, return_value);
4542 }
4543 /* }}} */
4544
4545 /* {{{ _class_check_flag */
_class_check_flag(INTERNAL_FUNCTION_PARAMETERS,int mask)4546 static void _class_check_flag(INTERNAL_FUNCTION_PARAMETERS, int mask)
4547 {
4548 reflection_object *intern;
4549 zend_class_entry *ce;
4550
4551 if (zend_parse_parameters_none() == FAILURE) {
4552 RETURN_THROWS();
4553 }
4554 GET_REFLECTION_OBJECT_PTR(ce);
4555 RETVAL_BOOL(ce->ce_flags & mask);
4556 }
4557 /* }}} */
4558
4559 /* {{{ Returns whether this class is instantiable */
ZEND_METHOD(ReflectionClass,isInstantiable)4560 ZEND_METHOD(ReflectionClass, isInstantiable)
4561 {
4562 reflection_object *intern;
4563 zend_class_entry *ce;
4564
4565 if (zend_parse_parameters_none() == FAILURE) {
4566 RETURN_THROWS();
4567 }
4568 GET_REFLECTION_OBJECT_PTR(ce);
4569 if (ce->ce_flags & (ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT | ZEND_ACC_EXPLICIT_ABSTRACT_CLASS | ZEND_ACC_IMPLICIT_ABSTRACT_CLASS)) {
4570 RETURN_FALSE;
4571 }
4572
4573 /* Basically, the class is instantiable. Though, if there is a constructor
4574 * and it is not publicly accessible, it isn't! */
4575 if (!ce->constructor) {
4576 RETURN_TRUE;
4577 }
4578
4579 RETURN_BOOL(ce->constructor->common.fn_flags & ZEND_ACC_PUBLIC);
4580 }
4581 /* }}} */
4582
4583 /* {{{ Returns whether this class is cloneable */
ZEND_METHOD(ReflectionClass,isCloneable)4584 ZEND_METHOD(ReflectionClass, isCloneable)
4585 {
4586 reflection_object *intern;
4587 zend_class_entry *ce;
4588 zval obj;
4589
4590 if (zend_parse_parameters_none() == FAILURE) {
4591 RETURN_THROWS();
4592 }
4593 GET_REFLECTION_OBJECT_PTR(ce);
4594 if (ce->ce_flags & (ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT | ZEND_ACC_EXPLICIT_ABSTRACT_CLASS | ZEND_ACC_IMPLICIT_ABSTRACT_CLASS)) {
4595 RETURN_FALSE;
4596 }
4597 if (!Z_ISUNDEF(intern->obj)) {
4598 if (ce->clone) {
4599 RETURN_BOOL(ce->clone->common.fn_flags & ZEND_ACC_PUBLIC);
4600 } else {
4601 RETURN_BOOL(Z_OBJ_HANDLER(intern->obj, clone_obj) != NULL);
4602 }
4603 } else {
4604 if (ce->clone) {
4605 RETURN_BOOL(ce->clone->common.fn_flags & ZEND_ACC_PUBLIC);
4606 } else {
4607 if (UNEXPECTED(object_init_ex(&obj, ce) != SUCCESS)) {
4608 return;
4609 }
4610 /* We're not calling the constructor, so don't call the destructor either. */
4611 zend_object_store_ctor_failed(Z_OBJ(obj));
4612 RETVAL_BOOL(Z_OBJ_HANDLER(obj, clone_obj) != NULL);
4613 zval_ptr_dtor(&obj);
4614 }
4615 }
4616 }
4617 /* }}} */
4618
4619 /* {{{ Returns whether this is an interface or a class */
ZEND_METHOD(ReflectionClass,isInterface)4620 ZEND_METHOD(ReflectionClass, isInterface)
4621 {
4622 _class_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_INTERFACE);
4623 }
4624 /* }}} */
4625
4626 /* {{{ Returns whether this is a trait */
ZEND_METHOD(ReflectionClass,isTrait)4627 ZEND_METHOD(ReflectionClass, isTrait)
4628 {
4629 _class_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_TRAIT);
4630 }
4631 /* }}} */
4632
4633 /* {{{ Returns whether this class is final */
ZEND_METHOD(ReflectionClass,isFinal)4634 ZEND_METHOD(ReflectionClass, isFinal)
4635 {
4636 _class_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_FINAL);
4637 }
4638 /* }}} */
4639
4640 /* {{{ Returns whether this class is abstract */
ZEND_METHOD(ReflectionClass,isAbstract)4641 ZEND_METHOD(ReflectionClass, isAbstract)
4642 {
4643 _class_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS);
4644 }
4645 /* }}} */
4646
4647 /* {{{ Returns a bitfield of the access modifiers for this class */
ZEND_METHOD(ReflectionClass,getModifiers)4648 ZEND_METHOD(ReflectionClass, getModifiers)
4649 {
4650 reflection_object *intern;
4651 zend_class_entry *ce;
4652 uint32_t keep_flags = ZEND_ACC_FINAL
4653 | ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
4654
4655 if (zend_parse_parameters_none() == FAILURE) {
4656 RETURN_THROWS();
4657 }
4658 GET_REFLECTION_OBJECT_PTR(ce);
4659
4660 RETURN_LONG((ce->ce_flags & keep_flags));
4661 }
4662 /* }}} */
4663
4664 /* {{{ Returns whether the given object is an instance of this class */
ZEND_METHOD(ReflectionClass,isInstance)4665 ZEND_METHOD(ReflectionClass, isInstance)
4666 {
4667 reflection_object *intern;
4668 zend_class_entry *ce;
4669 zval *object;
4670
4671 if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &object) == FAILURE) {
4672 RETURN_THROWS();
4673 }
4674 GET_REFLECTION_OBJECT_PTR(ce);
4675 RETURN_BOOL(instanceof_function(Z_OBJCE_P(object), ce));
4676 }
4677 /* }}} */
4678
4679 /* {{{ Returns an instance of this class */
ZEND_METHOD(ReflectionClass,newInstance)4680 ZEND_METHOD(ReflectionClass, newInstance)
4681 {
4682 reflection_object *intern;
4683 zend_class_entry *ce, *old_scope;
4684 zend_function *constructor;
4685
4686 GET_REFLECTION_OBJECT_PTR(ce);
4687
4688 if (UNEXPECTED(object_init_ex(return_value, ce) != SUCCESS)) {
4689 return;
4690 }
4691
4692 old_scope = EG(fake_scope);
4693 EG(fake_scope) = ce;
4694 constructor = Z_OBJ_HT_P(return_value)->get_constructor(Z_OBJ_P(return_value));
4695 EG(fake_scope) = old_scope;
4696
4697 /* Run the constructor if there is one */
4698 if (constructor) {
4699 zval *params;
4700 int num_args;
4701 HashTable *named_params;
4702
4703 if (!(constructor->common.fn_flags & ZEND_ACC_PUBLIC)) {
4704 zend_throw_exception_ex(reflection_exception_ptr, 0, "Access to non-public constructor of class %s", ZSTR_VAL(ce->name));
4705 zval_ptr_dtor(return_value);
4706 RETURN_NULL();
4707 }
4708
4709 ZEND_PARSE_PARAMETERS_START(0, -1)
4710 Z_PARAM_VARIADIC_WITH_NAMED(params, num_args, named_params)
4711 ZEND_PARSE_PARAMETERS_END();
4712
4713 zend_call_known_function(
4714 constructor, Z_OBJ_P(return_value), Z_OBJCE_P(return_value), NULL,
4715 num_args, params, named_params);
4716
4717 if (EG(exception)) {
4718 zend_object_store_ctor_failed(Z_OBJ_P(return_value));
4719 }
4720 } else if (ZEND_NUM_ARGS()) {
4721 zend_throw_exception_ex(reflection_exception_ptr, 0, "Class %s does not have a constructor, so you cannot pass any constructor arguments", ZSTR_VAL(ce->name));
4722 }
4723 }
4724 /* }}} */
4725
4726 /* {{{ Returns an instance of this class without invoking its constructor */
ZEND_METHOD(ReflectionClass,newInstanceWithoutConstructor)4727 ZEND_METHOD(ReflectionClass, newInstanceWithoutConstructor)
4728 {
4729 reflection_object *intern;
4730 zend_class_entry *ce;
4731
4732 GET_REFLECTION_OBJECT_PTR(ce);
4733
4734 if (zend_parse_parameters_none() == FAILURE) {
4735 RETURN_THROWS();
4736 }
4737
4738 if (ce->type == ZEND_INTERNAL_CLASS
4739 && ce->create_object != NULL && (ce->ce_flags & ZEND_ACC_FINAL)) {
4740 zend_throw_exception_ex(reflection_exception_ptr, 0, "Class %s is an internal class marked as final that cannot be instantiated without invoking its constructor", ZSTR_VAL(ce->name));
4741 RETURN_THROWS();
4742 }
4743
4744 object_init_ex(return_value, ce);
4745 }
4746 /* }}} */
4747
4748 /* {{{ Returns an instance of this class */
ZEND_METHOD(ReflectionClass,newInstanceArgs)4749 ZEND_METHOD(ReflectionClass, newInstanceArgs)
4750 {
4751 reflection_object *intern;
4752 zend_class_entry *ce, *old_scope;
4753 int argc = 0;
4754 HashTable *args = NULL;
4755 zend_function *constructor;
4756
4757 GET_REFLECTION_OBJECT_PTR(ce);
4758
4759 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|h", &args) == FAILURE) {
4760 RETURN_THROWS();
4761 }
4762
4763 if (args) {
4764 argc = zend_hash_num_elements(args);
4765 }
4766
4767 if (UNEXPECTED(object_init_ex(return_value, ce) != SUCCESS)) {
4768 return;
4769 }
4770
4771 old_scope = EG(fake_scope);
4772 EG(fake_scope) = ce;
4773 constructor = Z_OBJ_HT_P(return_value)->get_constructor(Z_OBJ_P(return_value));
4774 EG(fake_scope) = old_scope;
4775
4776 /* Run the constructor if there is one */
4777 if (constructor) {
4778 if (!(constructor->common.fn_flags & ZEND_ACC_PUBLIC)) {
4779 zend_throw_exception_ex(reflection_exception_ptr, 0, "Access to non-public constructor of class %s", ZSTR_VAL(ce->name));
4780 zval_ptr_dtor(return_value);
4781 RETURN_NULL();
4782 }
4783
4784 zend_call_known_function(
4785 constructor, Z_OBJ_P(return_value), Z_OBJCE_P(return_value), NULL, 0, NULL, args);
4786
4787 if (EG(exception)) {
4788 zend_object_store_ctor_failed(Z_OBJ_P(return_value));
4789 }
4790 } else if (argc) {
4791 zend_throw_exception_ex(reflection_exception_ptr, 0, "Class %s does not have a constructor, so you cannot pass any constructor arguments", ZSTR_VAL(ce->name));
4792 }
4793 }
4794 /* }}} */
4795
4796 /* {{{ Returns an array of interfaces this class implements */
ZEND_METHOD(ReflectionClass,getInterfaces)4797 ZEND_METHOD(ReflectionClass, getInterfaces)
4798 {
4799 reflection_object *intern;
4800 zend_class_entry *ce;
4801
4802 if (zend_parse_parameters_none() == FAILURE) {
4803 RETURN_THROWS();
4804 }
4805 GET_REFLECTION_OBJECT_PTR(ce);
4806
4807 if (ce->num_interfaces) {
4808 uint32_t i;
4809
4810 ZEND_ASSERT(ce->ce_flags & ZEND_ACC_LINKED);
4811 array_init(return_value);
4812 for (i=0; i < ce->num_interfaces; i++) {
4813 zval interface;
4814 zend_reflection_class_factory(ce->interfaces[i], &interface);
4815 zend_hash_update(Z_ARRVAL_P(return_value), ce->interfaces[i]->name, &interface);
4816 }
4817 } else {
4818 RETURN_EMPTY_ARRAY();
4819 }
4820 }
4821 /* }}} */
4822
4823 /* {{{ Returns an array of names of interfaces this class implements */
ZEND_METHOD(ReflectionClass,getInterfaceNames)4824 ZEND_METHOD(ReflectionClass, getInterfaceNames)
4825 {
4826 reflection_object *intern;
4827 zend_class_entry *ce;
4828 uint32_t i;
4829
4830 if (zend_parse_parameters_none() == FAILURE) {
4831 RETURN_THROWS();
4832 }
4833 GET_REFLECTION_OBJECT_PTR(ce);
4834
4835 if (!ce->num_interfaces) {
4836 /* Return an empty array if this class implements no interfaces */
4837 RETURN_EMPTY_ARRAY();
4838 }
4839
4840 ZEND_ASSERT(ce->ce_flags & ZEND_ACC_LINKED);
4841 array_init(return_value);
4842
4843 for (i=0; i < ce->num_interfaces; i++) {
4844 add_next_index_str(return_value, zend_string_copy(ce->interfaces[i]->name));
4845 }
4846 }
4847 /* }}} */
4848
4849 /* {{{ Returns an array of traits used by this class */
ZEND_METHOD(ReflectionClass,getTraits)4850 ZEND_METHOD(ReflectionClass, getTraits)
4851 {
4852 reflection_object *intern;
4853 zend_class_entry *ce;
4854 uint32_t i;
4855
4856 if (zend_parse_parameters_none() == FAILURE) {
4857 RETURN_THROWS();
4858 }
4859 GET_REFLECTION_OBJECT_PTR(ce);
4860
4861 if (!ce->num_traits) {
4862 RETURN_EMPTY_ARRAY();
4863 }
4864
4865 array_init(return_value);
4866
4867 for (i=0; i < ce->num_traits; i++) {
4868 zval trait;
4869 zend_class_entry *trait_ce;
4870
4871 trait_ce = zend_fetch_class_by_name(ce->trait_names[i].name,
4872 ce->trait_names[i].lc_name, ZEND_FETCH_CLASS_TRAIT);
4873 ZEND_ASSERT(trait_ce);
4874 zend_reflection_class_factory(trait_ce, &trait);
4875 zend_hash_update(Z_ARRVAL_P(return_value), ce->trait_names[i].name, &trait);
4876 }
4877 }
4878 /* }}} */
4879
4880 /* {{{ Returns an array of names of traits used by this class */
ZEND_METHOD(ReflectionClass,getTraitNames)4881 ZEND_METHOD(ReflectionClass, getTraitNames)
4882 {
4883 reflection_object *intern;
4884 zend_class_entry *ce;
4885 uint32_t i;
4886
4887 if (zend_parse_parameters_none() == FAILURE) {
4888 RETURN_THROWS();
4889 }
4890 GET_REFLECTION_OBJECT_PTR(ce);
4891
4892 if (!ce->num_traits) {
4893 RETURN_EMPTY_ARRAY();
4894 }
4895
4896 array_init(return_value);
4897
4898 for (i=0; i < ce->num_traits; i++) {
4899 add_next_index_str(return_value, zend_string_copy(ce->trait_names[i].name));
4900 }
4901 }
4902 /* }}} */
4903
4904 /* {{{ Returns an array of trait aliases */
ZEND_METHOD(ReflectionClass,getTraitAliases)4905 ZEND_METHOD(ReflectionClass, getTraitAliases)
4906 {
4907 reflection_object *intern;
4908 zend_class_entry *ce;
4909
4910 if (zend_parse_parameters_none() == FAILURE) {
4911 RETURN_THROWS();
4912 }
4913 GET_REFLECTION_OBJECT_PTR(ce);
4914
4915
4916 if (ce->trait_aliases) {
4917 uint32_t i = 0;
4918
4919 array_init(return_value);
4920 while (ce->trait_aliases[i]) {
4921 zend_string *mname;
4922 zend_trait_method_reference *cur_ref = &ce->trait_aliases[i]->trait_method;
4923
4924 if (ce->trait_aliases[i]->alias) {
4925
4926 mname = zend_string_alloc(ZSTR_LEN(cur_ref->class_name) + ZSTR_LEN(cur_ref->method_name) + 2, 0);
4927 snprintf(ZSTR_VAL(mname), ZSTR_LEN(mname) + 1, "%s::%s", ZSTR_VAL(cur_ref->class_name), ZSTR_VAL(cur_ref->method_name));
4928 add_assoc_str_ex(return_value, ZSTR_VAL(ce->trait_aliases[i]->alias), ZSTR_LEN(ce->trait_aliases[i]->alias), mname);
4929 }
4930 i++;
4931 }
4932 } else {
4933 RETURN_EMPTY_ARRAY();
4934 }
4935 }
4936 /* }}} */
4937
4938 /* {{{ Returns the class' parent class, or, if none exists, FALSE */
ZEND_METHOD(ReflectionClass,getParentClass)4939 ZEND_METHOD(ReflectionClass, getParentClass)
4940 {
4941 reflection_object *intern;
4942 zend_class_entry *ce;
4943
4944 if (zend_parse_parameters_none() == FAILURE) {
4945 RETURN_THROWS();
4946 }
4947 GET_REFLECTION_OBJECT_PTR(ce);
4948
4949 if (ce->parent) {
4950 zend_reflection_class_factory(ce->parent, return_value);
4951 } else {
4952 RETURN_FALSE;
4953 }
4954 }
4955 /* }}} */
4956
4957 /* {{{ Returns whether this class is a subclass of another class */
ZEND_METHOD(ReflectionClass,isSubclassOf)4958 ZEND_METHOD(ReflectionClass, isSubclassOf)
4959 {
4960 reflection_object *intern, *argument;
4961 zend_class_entry *ce, *class_ce;
4962 zend_string *class_str;
4963 zend_object *class_obj;
4964
4965 ZEND_PARSE_PARAMETERS_START(1, 1)
4966 Z_PARAM_OBJ_OF_CLASS_OR_STR(class_obj, reflection_class_ptr, class_str)
4967 ZEND_PARSE_PARAMETERS_END();
4968
4969 if (class_obj) {
4970 argument = reflection_object_from_obj(class_obj);
4971 if (argument->ptr == NULL) {
4972 zend_throw_error(NULL, "Internal error: Failed to retrieve the argument's reflection object");
4973 RETURN_THROWS();
4974 }
4975
4976 class_ce = argument->ptr;
4977 } else {
4978 if ((class_ce = zend_lookup_class(class_str)) == NULL) {
4979 zend_throw_exception_ex(reflection_exception_ptr, 0, "Class \"%s\" does not exist", ZSTR_VAL(class_str));
4980 RETURN_THROWS();
4981 }
4982 }
4983
4984 GET_REFLECTION_OBJECT_PTR(ce);
4985
4986 RETURN_BOOL((ce != class_ce && instanceof_function(ce, class_ce)));
4987 }
4988 /* }}} */
4989
4990 /* {{{ Returns whether this class is a subclass of another class */
ZEND_METHOD(ReflectionClass,implementsInterface)4991 ZEND_METHOD(ReflectionClass, implementsInterface)
4992 {
4993 reflection_object *intern, *argument;
4994 zend_string *interface_str;
4995 zend_class_entry *ce, *interface_ce;
4996 zend_object *interface_obj;
4997
4998 ZEND_PARSE_PARAMETERS_START(1, 1)
4999 Z_PARAM_OBJ_OF_CLASS_OR_STR(interface_obj, reflection_class_ptr, interface_str)
5000 ZEND_PARSE_PARAMETERS_END();
5001
5002 if (interface_obj) {
5003 argument = reflection_object_from_obj(interface_obj);
5004 if (argument->ptr == NULL) {
5005 zend_throw_error(NULL, "Internal error: Failed to retrieve the argument's reflection object");
5006 RETURN_THROWS();
5007 }
5008
5009 interface_ce = argument->ptr;
5010 } else {
5011 if ((interface_ce = zend_lookup_class(interface_str)) == NULL) {
5012 zend_throw_exception_ex(reflection_exception_ptr, 0, "Interface \"%s\" does not exist", ZSTR_VAL(interface_str));
5013 RETURN_THROWS();
5014 }
5015 }
5016
5017 if (!(interface_ce->ce_flags & ZEND_ACC_INTERFACE)) {
5018 zend_throw_exception_ex(reflection_exception_ptr, 0, "%s is not an interface", ZSTR_VAL(interface_ce->name));
5019 RETURN_THROWS();
5020 }
5021
5022 GET_REFLECTION_OBJECT_PTR(ce);
5023
5024 RETURN_BOOL(instanceof_function(ce, interface_ce));
5025 }
5026 /* }}} */
5027
5028 /* {{{ Returns whether this class is iterable (can be used inside foreach) */
ZEND_METHOD(ReflectionClass,isIterable)5029 ZEND_METHOD(ReflectionClass, isIterable)
5030 {
5031 reflection_object *intern;
5032 zend_class_entry *ce;
5033
5034 if (zend_parse_parameters_none() == FAILURE) {
5035 RETURN_THROWS();
5036 }
5037
5038 GET_REFLECTION_OBJECT_PTR(ce);
5039
5040 if (ce->ce_flags & (ZEND_ACC_INTERFACE | ZEND_ACC_IMPLICIT_ABSTRACT_CLASS |
5041 ZEND_ACC_TRAIT | ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
5042 RETURN_FALSE;
5043 }
5044
5045 RETURN_BOOL(ce->get_iterator || instanceof_function(ce, zend_ce_traversable));
5046 }
5047 /* }}} */
5048
5049 /* {{{ Returns NULL or the extension the class belongs to */
ZEND_METHOD(ReflectionClass,getExtension)5050 ZEND_METHOD(ReflectionClass, getExtension)
5051 {
5052 reflection_object *intern;
5053 zend_class_entry *ce;
5054
5055 if (zend_parse_parameters_none() == FAILURE) {
5056 RETURN_THROWS();
5057 }
5058
5059 GET_REFLECTION_OBJECT_PTR(ce);
5060
5061 if ((ce->type == ZEND_INTERNAL_CLASS) && ce->info.internal.module) {
5062 reflection_extension_factory(return_value, ce->info.internal.module->name);
5063 }
5064 }
5065 /* }}} */
5066
5067 /* {{{ Returns false or the name of the extension the class belongs to */
ZEND_METHOD(ReflectionClass,getExtensionName)5068 ZEND_METHOD(ReflectionClass, getExtensionName)
5069 {
5070 reflection_object *intern;
5071 zend_class_entry *ce;
5072
5073 if (zend_parse_parameters_none() == FAILURE) {
5074 RETURN_THROWS();
5075 }
5076
5077 GET_REFLECTION_OBJECT_PTR(ce);
5078
5079 if ((ce->type == ZEND_INTERNAL_CLASS) && ce->info.internal.module) {
5080 RETURN_STRING(ce->info.internal.module->name);
5081 } else {
5082 RETURN_FALSE;
5083 }
5084 }
5085 /* }}} */
5086
5087 /* {{{ Returns whether this class is defined in namespace */
ZEND_METHOD(ReflectionClass,inNamespace)5088 ZEND_METHOD(ReflectionClass, inNamespace)
5089 {
5090 reflection_object *intern;
5091 zend_class_entry *ce;
5092
5093 if (zend_parse_parameters_none() == FAILURE) {
5094 RETURN_THROWS();
5095 }
5096
5097 GET_REFLECTION_OBJECT_PTR(ce);
5098
5099 zend_string *name = ce->name;
5100 const char *backslash = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
5101 RETURN_BOOL(backslash && backslash > ZSTR_VAL(name));
5102 }
5103 /* }}} */
5104
5105 /* {{{ Returns the name of namespace where this class is defined */
ZEND_METHOD(ReflectionClass,getNamespaceName)5106 ZEND_METHOD(ReflectionClass, getNamespaceName)
5107 {
5108 reflection_object *intern;
5109 zend_class_entry *ce;
5110
5111 if (zend_parse_parameters_none() == FAILURE) {
5112 RETURN_THROWS();
5113 }
5114
5115 GET_REFLECTION_OBJECT_PTR(ce);
5116
5117 zend_string *name = ce->name;
5118 const char *backslash = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
5119 if (backslash && backslash > ZSTR_VAL(name)) {
5120 RETURN_STRINGL(ZSTR_VAL(name), backslash - ZSTR_VAL(name));
5121 }
5122 RETURN_EMPTY_STRING();
5123 }
5124 /* }}} */
5125
5126 /* {{{ Returns the short name of the class (without namespace part) */
ZEND_METHOD(ReflectionClass,getShortName)5127 ZEND_METHOD(ReflectionClass, getShortName)
5128 {
5129 reflection_object *intern;
5130 zend_class_entry *ce;
5131
5132 if (zend_parse_parameters_none() == FAILURE) {
5133 RETURN_THROWS();
5134 }
5135
5136 GET_REFLECTION_OBJECT_PTR(ce);
5137
5138 zend_string *name = ce->name;
5139 const char *backslash = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
5140 if (backslash && backslash > ZSTR_VAL(name)) {
5141 RETURN_STRINGL(backslash + 1, ZSTR_LEN(name) - (backslash - ZSTR_VAL(name) + 1));
5142 }
5143 RETURN_STR_COPY(name);
5144 }
5145 /* }}} */
5146
5147 /* {{{ Constructor. Takes an instance as an argument */
ZEND_METHOD(ReflectionObject,__construct)5148 ZEND_METHOD(ReflectionObject, __construct)
5149 {
5150 reflection_class_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
5151 }
5152 /* }}} */
5153
5154 /* {{{ Constructor. Throws an Exception in case the given property does not exist */
ZEND_METHOD(ReflectionProperty,__construct)5155 ZEND_METHOD(ReflectionProperty, __construct)
5156 {
5157 zend_string *classname_str;
5158 zend_object *classname_obj;
5159 zend_string *name;
5160 int dynam_prop = 0;
5161 zval *object;
5162 reflection_object *intern;
5163 zend_class_entry *ce;
5164 zend_property_info *property_info = NULL;
5165 property_reference *reference;
5166
5167 ZEND_PARSE_PARAMETERS_START(2, 2)
5168 Z_PARAM_OBJ_OR_STR(classname_obj, classname_str)
5169 Z_PARAM_STR(name)
5170 ZEND_PARSE_PARAMETERS_END();
5171
5172 object = ZEND_THIS;
5173 intern = Z_REFLECTION_P(object);
5174
5175 if (classname_obj) {
5176 ce = classname_obj->ce;
5177 } else {
5178 if ((ce = zend_lookup_class(classname_str)) == NULL) {
5179 zend_throw_exception_ex(reflection_exception_ptr, 0, "Class \"%s\" does not exist", ZSTR_VAL(classname_str));
5180 RETURN_THROWS();
5181 }
5182 }
5183
5184 property_info = zend_hash_find_ptr(&ce->properties_info, name);
5185 if (property_info == NULL
5186 || ((property_info->flags & ZEND_ACC_PRIVATE)
5187 && property_info->ce != ce)) {
5188 /* Check for dynamic properties */
5189 if (property_info == NULL && classname_obj) {
5190 if (zend_hash_exists(classname_obj->handlers->get_properties(classname_obj), name)) {
5191 dynam_prop = 1;
5192 }
5193 }
5194 if (dynam_prop == 0) {
5195 zend_throw_exception_ex(reflection_exception_ptr, 0, "Property %s::$%s does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(name));
5196 RETURN_THROWS();
5197 }
5198 }
5199
5200 ZVAL_STR_COPY(reflection_prop_name(object), name);
5201 if (dynam_prop == 0) {
5202 ZVAL_STR_COPY(reflection_prop_class(object), property_info->ce->name);
5203 } else {
5204 ZVAL_STR_COPY(reflection_prop_class(object), ce->name);
5205 }
5206
5207 reference = (property_reference*) emalloc(sizeof(property_reference));
5208 reference->prop = dynam_prop ? NULL : property_info;
5209 reference->unmangled_name = zend_string_copy(name);
5210 intern->ptr = reference;
5211 intern->ref_type = REF_TYPE_PROPERTY;
5212 intern->ce = ce;
5213 intern->ignore_visibility = 0;
5214 }
5215 /* }}} */
5216
5217 /* {{{ Returns a string representation */
ZEND_METHOD(ReflectionProperty,__toString)5218 ZEND_METHOD(ReflectionProperty, __toString)
5219 {
5220 reflection_object *intern;
5221 property_reference *ref;
5222 smart_str str = {0};
5223
5224 if (zend_parse_parameters_none() == FAILURE) {
5225 RETURN_THROWS();
5226 }
5227 GET_REFLECTION_OBJECT_PTR(ref);
5228 _property_string(&str, ref->prop, ZSTR_VAL(ref->unmangled_name), "");
5229 RETURN_STR(smart_str_extract(&str));
5230 }
5231 /* }}} */
5232
5233 /* {{{ Returns the class' name */
ZEND_METHOD(ReflectionProperty,getName)5234 ZEND_METHOD(ReflectionProperty, getName)
5235 {
5236 reflection_object *intern;
5237 property_reference *ref;
5238
5239 if (zend_parse_parameters_none() == FAILURE) {
5240 RETURN_THROWS();
5241 }
5242
5243 GET_REFLECTION_OBJECT_PTR(ref);
5244 RETURN_STR_COPY(ref->unmangled_name);
5245 }
5246 /* }}} */
5247
_property_check_flag(INTERNAL_FUNCTION_PARAMETERS,int mask)5248 static void _property_check_flag(INTERNAL_FUNCTION_PARAMETERS, int mask) /* {{{ */
5249 {
5250 reflection_object *intern;
5251 property_reference *ref;
5252
5253 if (zend_parse_parameters_none() == FAILURE) {
5254 RETURN_THROWS();
5255 }
5256 GET_REFLECTION_OBJECT_PTR(ref);
5257 RETURN_BOOL(prop_get_flags(ref) & mask);
5258 }
5259 /* }}} */
5260
5261 /* {{{ Returns whether this property is public */
ZEND_METHOD(ReflectionProperty,isPublic)5262 ZEND_METHOD(ReflectionProperty, isPublic)
5263 {
5264 _property_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PUBLIC);
5265 }
5266 /* }}} */
5267
5268 /* {{{ Returns whether this property is private */
ZEND_METHOD(ReflectionProperty,isPrivate)5269 ZEND_METHOD(ReflectionProperty, isPrivate)
5270 {
5271 _property_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PRIVATE);
5272 }
5273 /* }}} */
5274
5275 /* {{{ Returns whether this property is protected */
ZEND_METHOD(ReflectionProperty,isProtected)5276 ZEND_METHOD(ReflectionProperty, isProtected)
5277 {
5278 _property_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PROTECTED);
5279 }
5280 /* }}} */
5281
5282 /* {{{ Returns whether this property is static */
ZEND_METHOD(ReflectionProperty,isStatic)5283 ZEND_METHOD(ReflectionProperty, isStatic)
5284 {
5285 _property_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_STATIC);
5286 }
5287 /* }}} */
5288
5289 /* {{{ Returns whether this property is default (declared at compilation time). */
ZEND_METHOD(ReflectionProperty,isDefault)5290 ZEND_METHOD(ReflectionProperty, isDefault)
5291 {
5292 reflection_object *intern;
5293 property_reference *ref;
5294
5295 if (zend_parse_parameters_none() == FAILURE) {
5296 RETURN_THROWS();
5297 }
5298 GET_REFLECTION_OBJECT_PTR(ref);
5299 RETURN_BOOL(ref->prop != NULL);
5300 }
5301 /* }}} */
5302
5303 /* {{{ Returns whether this property has been promoted from a constructor */
ZEND_METHOD(ReflectionProperty,isPromoted)5304 ZEND_METHOD(ReflectionProperty, isPromoted)
5305 {
5306 _property_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PROMOTED);
5307 }
5308 /* }}} */
5309
5310 /* {{{ Returns a bitfield of the access modifiers for this property */
ZEND_METHOD(ReflectionProperty,getModifiers)5311 ZEND_METHOD(ReflectionProperty, getModifiers)
5312 {
5313 reflection_object *intern;
5314 property_reference *ref;
5315 uint32_t keep_flags = ZEND_ACC_PPP_MASK | ZEND_ACC_STATIC;
5316
5317 if (zend_parse_parameters_none() == FAILURE) {
5318 RETURN_THROWS();
5319 }
5320 GET_REFLECTION_OBJECT_PTR(ref);
5321
5322 RETURN_LONG(prop_get_flags(ref) & keep_flags);
5323 }
5324 /* }}} */
5325
5326 /* {{{ Returns this property's value */
ZEND_METHOD(ReflectionProperty,getValue)5327 ZEND_METHOD(ReflectionProperty, getValue)
5328 {
5329 reflection_object *intern;
5330 property_reference *ref;
5331 zval *object = NULL;
5332 zval *member_p = NULL;
5333
5334 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|o!", &object) == FAILURE) {
5335 RETURN_THROWS();
5336 }
5337
5338 GET_REFLECTION_OBJECT_PTR(ref);
5339
5340 if (!(prop_get_flags(ref) & ZEND_ACC_PUBLIC) && intern->ignore_visibility == 0) {
5341 zend_throw_exception_ex(reflection_exception_ptr, 0,
5342 "Cannot access non-public property %s::$%s",
5343 ZSTR_VAL(intern->ce->name), ZSTR_VAL(ref->unmangled_name));
5344 RETURN_THROWS();
5345 }
5346
5347 if (prop_get_flags(ref) & ZEND_ACC_STATIC) {
5348 member_p = zend_read_static_property_ex(intern->ce, ref->unmangled_name, 0);
5349 if (member_p) {
5350 ZVAL_COPY_DEREF(return_value, member_p);
5351 }
5352 } else {
5353 zval rv;
5354
5355 if (!object) {
5356 zend_argument_type_error(1, "must be provided for instance properties");
5357 RETURN_THROWS();
5358 }
5359
5360 /* TODO: Should this always use intern->ce? */
5361 if (!instanceof_function(Z_OBJCE_P(object), ref->prop ? ref->prop->ce : intern->ce)) {
5362 _DO_THROW("Given object is not an instance of the class this property was declared in");
5363 RETURN_THROWS();
5364 }
5365
5366 member_p = zend_read_property_ex(intern->ce, Z_OBJ_P(object), ref->unmangled_name, 0, &rv);
5367 if (member_p != &rv) {
5368 ZVAL_COPY_DEREF(return_value, member_p);
5369 } else {
5370 if (Z_ISREF_P(member_p)) {
5371 zend_unwrap_reference(member_p);
5372 }
5373 ZVAL_COPY_VALUE(return_value, member_p);
5374 }
5375 }
5376 }
5377 /* }}} */
5378
5379 /* {{{ Sets this property's value */
ZEND_METHOD(ReflectionProperty,setValue)5380 ZEND_METHOD(ReflectionProperty, setValue)
5381 {
5382 reflection_object *intern;
5383 property_reference *ref;
5384 zval *object;
5385 zval *value;
5386 zval *tmp;
5387
5388 GET_REFLECTION_OBJECT_PTR(ref);
5389
5390 if (!(prop_get_flags(ref) & ZEND_ACC_PUBLIC) && intern->ignore_visibility == 0) {
5391 zend_throw_exception_ex(reflection_exception_ptr, 0,
5392 "Cannot access non-public property %s::$%s",
5393 ZSTR_VAL(intern->ce->name), ZSTR_VAL(ref->unmangled_name));
5394 RETURN_THROWS();
5395 }
5396
5397 if (prop_get_flags(ref) & ZEND_ACC_STATIC) {
5398 if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "z", &value) == FAILURE) {
5399 if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &tmp, &value) == FAILURE) {
5400 RETURN_THROWS();
5401 }
5402 }
5403
5404 zend_update_static_property_ex(intern->ce, ref->unmangled_name, value);
5405 } else {
5406 if (zend_parse_parameters(ZEND_NUM_ARGS(), "oz", &object, &value) == FAILURE) {
5407 RETURN_THROWS();
5408 }
5409
5410 zend_update_property_ex(intern->ce, Z_OBJ_P(object), ref->unmangled_name, value);
5411 }
5412 }
5413 /* }}} */
5414
5415 /* {{{ Returns this property's value */
ZEND_METHOD(ReflectionProperty,isInitialized)5416 ZEND_METHOD(ReflectionProperty, isInitialized)
5417 {
5418 reflection_object *intern;
5419 property_reference *ref;
5420 zval *object = NULL;
5421 zval *member_p = NULL;
5422
5423 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|o!", &object) == FAILURE) {
5424 RETURN_THROWS();
5425 }
5426
5427 GET_REFLECTION_OBJECT_PTR(ref);
5428
5429 if (!(prop_get_flags(ref) & ZEND_ACC_PUBLIC) && intern->ignore_visibility == 0) {
5430 zend_throw_exception_ex(reflection_exception_ptr, 0,
5431 "Cannot access non-public property %s::$%s",
5432 ZSTR_VAL(intern->ce->name), ZSTR_VAL(ref->unmangled_name));
5433 RETURN_THROWS();
5434 }
5435
5436 if (prop_get_flags(ref) & ZEND_ACC_STATIC) {
5437 member_p = zend_read_static_property_ex(intern->ce, ref->unmangled_name, 1);
5438 if (member_p) {
5439 RETURN_BOOL(!Z_ISUNDEF_P(member_p));
5440 }
5441 RETURN_FALSE;
5442 } else {
5443 zend_class_entry *old_scope;
5444 int retval;
5445
5446 if (!object) {
5447 zend_argument_type_error(1, "must be provided for instance properties");
5448 RETURN_THROWS();
5449 }
5450
5451 /* TODO: Should this always use intern->ce? */
5452 if (!instanceof_function(Z_OBJCE_P(object), ref->prop ? ref->prop->ce : intern->ce)) {
5453 _DO_THROW("Given object is not an instance of the class this property was declared in");
5454 RETURN_THROWS();
5455 }
5456
5457 old_scope = EG(fake_scope);
5458 EG(fake_scope) = intern->ce;
5459 retval = Z_OBJ_HT_P(object)->has_property(Z_OBJ_P(object), ref->unmangled_name, ZEND_PROPERTY_EXISTS, NULL);
5460 EG(fake_scope) = old_scope;
5461
5462 RETVAL_BOOL(retval);
5463 }
5464 }
5465 /* }}} */
5466
5467 /* {{{ Get the declaring class */
ZEND_METHOD(ReflectionProperty,getDeclaringClass)5468 ZEND_METHOD(ReflectionProperty, getDeclaringClass)
5469 {
5470 reflection_object *intern;
5471 property_reference *ref;
5472 zend_class_entry *ce;
5473
5474 if (zend_parse_parameters_none() == FAILURE) {
5475 RETURN_THROWS();
5476 }
5477 GET_REFLECTION_OBJECT_PTR(ref);
5478
5479 ce = ref->prop ? ref->prop->ce : intern->ce;
5480 zend_reflection_class_factory(ce, return_value);
5481 }
5482 /* }}} */
5483
5484 /* {{{ Returns the doc comment for this property */
ZEND_METHOD(ReflectionProperty,getDocComment)5485 ZEND_METHOD(ReflectionProperty, getDocComment)
5486 {
5487 reflection_object *intern;
5488 property_reference *ref;
5489
5490 if (zend_parse_parameters_none() == FAILURE) {
5491 RETURN_THROWS();
5492 }
5493 GET_REFLECTION_OBJECT_PTR(ref);
5494 if (ref->prop && ref->prop->doc_comment) {
5495 RETURN_STR_COPY(ref->prop->doc_comment);
5496 }
5497 RETURN_FALSE;
5498 }
5499 /* }}} */
5500
5501 /* {{{ Returns the attributes of this property */
ZEND_METHOD(ReflectionProperty,getAttributes)5502 ZEND_METHOD(ReflectionProperty, getAttributes)
5503 {
5504 reflection_object *intern;
5505 property_reference *ref;
5506
5507 GET_REFLECTION_OBJECT_PTR(ref);
5508
5509 if (ref->prop == NULL) {
5510 RETURN_EMPTY_ARRAY();
5511 }
5512
5513 reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU,
5514 ref->prop->attributes, 0, ref->prop->ce, ZEND_ATTRIBUTE_TARGET_PROPERTY,
5515 ref->prop->ce->type == ZEND_USER_CLASS ? ref->prop->ce->info.user.filename : NULL);
5516 }
5517 /* }}} */
5518
5519 /* {{{ Sets whether non-public properties can be requested */
ZEND_METHOD(ReflectionProperty,setAccessible)5520 ZEND_METHOD(ReflectionProperty, setAccessible)
5521 {
5522 reflection_object *intern;
5523 zend_bool visible;
5524
5525 if (zend_parse_parameters(ZEND_NUM_ARGS(), "b", &visible) == FAILURE) {
5526 RETURN_THROWS();
5527 }
5528
5529 intern = Z_REFLECTION_P(ZEND_THIS);
5530
5531 intern->ignore_visibility = visible;
5532 }
5533 /* }}} */
5534
5535 /* {{{ Returns the type associated with the property */
ZEND_METHOD(ReflectionProperty,getType)5536 ZEND_METHOD(ReflectionProperty, getType)
5537 {
5538 reflection_object *intern;
5539 property_reference *ref;
5540
5541 if (zend_parse_parameters_none() == FAILURE) {
5542 RETURN_THROWS();
5543 }
5544
5545 GET_REFLECTION_OBJECT_PTR(ref);
5546
5547 if (!ref->prop || !ZEND_TYPE_IS_SET(ref->prop->type)) {
5548 RETURN_NULL();
5549 }
5550
5551 reflection_type_factory(ref->prop->type, return_value, 1);
5552 }
5553 /* }}} */
5554
5555 /* {{{ Returns whether property has a type */
ZEND_METHOD(ReflectionProperty,hasType)5556 ZEND_METHOD(ReflectionProperty, hasType)
5557 {
5558 reflection_object *intern;
5559 property_reference *ref;
5560
5561 if (zend_parse_parameters_none() == FAILURE) {
5562 RETURN_THROWS();
5563 }
5564
5565 GET_REFLECTION_OBJECT_PTR(ref);
5566
5567 RETVAL_BOOL(ref->prop && ZEND_TYPE_IS_SET(ref->prop->type));
5568 }
5569 /* }}} */
5570
5571 /* {{{ Returns whether property has a default value */
ZEND_METHOD(ReflectionProperty,hasDefaultValue)5572 ZEND_METHOD(ReflectionProperty, hasDefaultValue)
5573 {
5574 reflection_object *intern;
5575 property_reference *ref;
5576 zend_property_info *prop_info;
5577 zval *prop;
5578
5579 if (zend_parse_parameters_none() == FAILURE) {
5580 RETURN_THROWS();
5581 }
5582
5583 GET_REFLECTION_OBJECT_PTR(ref);
5584
5585 prop_info = ref->prop;
5586
5587 if (prop_info == NULL) {
5588 RETURN_FALSE;
5589 }
5590
5591 prop = property_get_default(prop_info);
5592 RETURN_BOOL(!Z_ISUNDEF_P(prop));
5593 }
5594 /* }}} */
5595
5596 /* {{{ Returns the default value of a property */
ZEND_METHOD(ReflectionProperty,getDefaultValue)5597 ZEND_METHOD(ReflectionProperty, getDefaultValue)
5598 {
5599 reflection_object *intern;
5600 property_reference *ref;
5601 zend_property_info *prop_info;
5602 zval *prop;
5603
5604 if (zend_parse_parameters_none() == FAILURE) {
5605 RETURN_THROWS();
5606 }
5607
5608 GET_REFLECTION_OBJECT_PTR(ref);
5609
5610 prop_info = ref->prop;
5611
5612 if (prop_info == NULL) {
5613 return; // throw exception?
5614 }
5615
5616 prop = property_get_default(prop_info);
5617 if (Z_ISUNDEF_P(prop)) {
5618 return;
5619 }
5620
5621 /* copy: enforce read only access */
5622 ZVAL_DEREF(prop);
5623 ZVAL_COPY_OR_DUP(return_value, prop);
5624
5625 /* this is necessary to make it able to work with default array
5626 * properties, returned to user */
5627 if (Z_TYPE_P(return_value) == IS_CONSTANT_AST) {
5628 if (UNEXPECTED(zval_update_constant_ex(return_value, prop_info->ce) != SUCCESS)) {
5629 RETURN_THROWS();
5630 }
5631 }
5632 }
5633 /* }}} */
5634
5635 /* {{{ Constructor. Throws an Exception in case the given extension does not exist */
ZEND_METHOD(ReflectionExtension,__construct)5636 ZEND_METHOD(ReflectionExtension, __construct)
5637 {
5638 zval *object;
5639 char *lcname;
5640 reflection_object *intern;
5641 zend_module_entry *module;
5642 char *name_str;
5643 size_t name_len;
5644 ALLOCA_FLAG(use_heap)
5645
5646 if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name_str, &name_len) == FAILURE) {
5647 RETURN_THROWS();
5648 }
5649
5650 object = ZEND_THIS;
5651 intern = Z_REFLECTION_P(object);
5652 lcname = do_alloca(name_len + 1, use_heap);
5653 zend_str_tolower_copy(lcname, name_str, name_len);
5654 if ((module = zend_hash_str_find_ptr(&module_registry, lcname, name_len)) == NULL) {
5655 free_alloca(lcname, use_heap);
5656 zend_throw_exception_ex(reflection_exception_ptr, 0,
5657 "Extension \"%s\" does not exist", name_str);
5658 RETURN_THROWS();
5659 }
5660 free_alloca(lcname, use_heap);
5661 ZVAL_STRING(reflection_prop_name(object), module->name);
5662 intern->ptr = module;
5663 intern->ref_type = REF_TYPE_OTHER;
5664 intern->ce = NULL;
5665 }
5666 /* }}} */
5667
5668 /* {{{ Returns a string representation */
ZEND_METHOD(ReflectionExtension,__toString)5669 ZEND_METHOD(ReflectionExtension, __toString)
5670 {
5671 reflection_object *intern;
5672 zend_module_entry *module;
5673 smart_str str = {0};
5674
5675 if (zend_parse_parameters_none() == FAILURE) {
5676 RETURN_THROWS();
5677 }
5678 GET_REFLECTION_OBJECT_PTR(module);
5679 _extension_string(&str, module, "");
5680 RETURN_STR(smart_str_extract(&str));
5681 }
5682 /* }}} */
5683
5684 /* {{{ Returns this extension's name */
ZEND_METHOD(ReflectionExtension,getName)5685 ZEND_METHOD(ReflectionExtension, getName)
5686 {
5687 reflection_object *intern;
5688 zend_module_entry *module;
5689
5690 if (zend_parse_parameters_none() == FAILURE) {
5691 RETURN_THROWS();
5692 }
5693
5694 GET_REFLECTION_OBJECT_PTR(module);
5695 RETURN_STRING(module->name);
5696 }
5697 /* }}} */
5698
5699 /* {{{ Returns this extension's version */
ZEND_METHOD(ReflectionExtension,getVersion)5700 ZEND_METHOD(ReflectionExtension, getVersion)
5701 {
5702 reflection_object *intern;
5703 zend_module_entry *module;
5704
5705 if (zend_parse_parameters_none() == FAILURE) {
5706 RETURN_THROWS();
5707 }
5708 GET_REFLECTION_OBJECT_PTR(module);
5709
5710 /* An extension does not necessarily have a version number */
5711 if (module->version == NO_VERSION_YET) {
5712 RETURN_NULL();
5713 } else {
5714 RETURN_STRING(module->version);
5715 }
5716 }
5717 /* }}} */
5718
5719 /* {{{ Returns an array of this extension's functions */
ZEND_METHOD(ReflectionExtension,getFunctions)5720 ZEND_METHOD(ReflectionExtension, getFunctions)
5721 {
5722 reflection_object *intern;
5723 zend_module_entry *module;
5724 zval function;
5725 zend_function *fptr;
5726
5727 if (zend_parse_parameters_none() == FAILURE) {
5728 RETURN_THROWS();
5729 }
5730 GET_REFLECTION_OBJECT_PTR(module);
5731
5732 array_init(return_value);
5733 ZEND_HASH_FOREACH_PTR(CG(function_table), fptr) {
5734 if (fptr->common.type==ZEND_INTERNAL_FUNCTION
5735 && fptr->internal_function.module == module) {
5736 reflection_function_factory(fptr, NULL, &function);
5737 zend_hash_update(Z_ARRVAL_P(return_value), fptr->common.function_name, &function);
5738 }
5739 } ZEND_HASH_FOREACH_END();
5740 }
5741 /* }}} */
5742
5743 /* {{{ Returns an associative array containing this extension's constants and their values */
ZEND_METHOD(ReflectionExtension,getConstants)5744 ZEND_METHOD(ReflectionExtension, getConstants)
5745 {
5746 reflection_object *intern;
5747 zend_module_entry *module;
5748 zend_constant *constant;
5749
5750 if (zend_parse_parameters_none() == FAILURE) {
5751 RETURN_THROWS();
5752 }
5753 GET_REFLECTION_OBJECT_PTR(module);
5754
5755 array_init(return_value);
5756 ZEND_HASH_FOREACH_PTR(EG(zend_constants), constant) {
5757 if (module->module_number == ZEND_CONSTANT_MODULE_NUMBER(constant)) {
5758 zval const_val;
5759 ZVAL_COPY_OR_DUP(&const_val, &constant->value);
5760 zend_hash_update(Z_ARRVAL_P(return_value), constant->name, &const_val);
5761 }
5762 } ZEND_HASH_FOREACH_END();
5763 }
5764 /* }}} */
5765
5766 /* {{{ _addinientry */
_addinientry(zend_ini_entry * ini_entry,zval * retval,int number)5767 static void _addinientry(zend_ini_entry *ini_entry, zval *retval, int number)
5768 {
5769 if (number == ini_entry->module_number) {
5770 zval zv;
5771 if (ini_entry->value) {
5772 ZVAL_STR_COPY(&zv, ini_entry->value);
5773 } else {
5774 ZVAL_NULL(&zv);
5775 }
5776 zend_symtable_update(Z_ARRVAL_P(retval), ini_entry->name, &zv);
5777 }
5778 }
5779 /* }}} */
5780
5781 /* {{{ Returns an associative array containing this extension's INI entries and their values */
ZEND_METHOD(ReflectionExtension,getINIEntries)5782 ZEND_METHOD(ReflectionExtension, getINIEntries)
5783 {
5784 reflection_object *intern;
5785 zend_module_entry *module;
5786 zend_ini_entry *ini_entry;
5787
5788 if (zend_parse_parameters_none() == FAILURE) {
5789 RETURN_THROWS();
5790 }
5791 GET_REFLECTION_OBJECT_PTR(module);
5792
5793 array_init(return_value);
5794 ZEND_HASH_FOREACH_PTR(EG(ini_directives), ini_entry) {
5795 _addinientry(ini_entry, return_value, module->module_number);
5796 } ZEND_HASH_FOREACH_END();
5797 }
5798 /* }}} */
5799
5800 /* {{{ add_extension_class */
add_extension_class(zend_class_entry * ce,zend_string * key,zval * class_array,zend_module_entry * module,zend_bool add_reflection_class)5801 static void add_extension_class(zend_class_entry *ce, zend_string *key, zval *class_array, zend_module_entry *module, zend_bool add_reflection_class)
5802 {
5803 if (ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module && !strcasecmp(ce->info.internal.module->name, module->name)) {
5804 zend_string *name;
5805
5806 if (!zend_string_equals_ci(ce->name, key)) {
5807 /* This is a class alias, use alias name */
5808 name = key;
5809 } else {
5810 /* Use class name */
5811 name = ce->name;
5812 }
5813 if (add_reflection_class) {
5814 zval zclass;
5815 zend_reflection_class_factory(ce, &zclass);
5816 zend_hash_update(Z_ARRVAL_P(class_array), name, &zclass);
5817 } else {
5818 add_next_index_str(class_array, zend_string_copy(name));
5819 }
5820 }
5821 }
5822 /* }}} */
5823
5824 /* {{{ Returns an array containing ReflectionClass objects for all classes of this extension */
ZEND_METHOD(ReflectionExtension,getClasses)5825 ZEND_METHOD(ReflectionExtension, getClasses)
5826 {
5827 reflection_object *intern;
5828 zend_module_entry *module;
5829 zend_string *key;
5830 zend_class_entry *ce;
5831
5832 if (zend_parse_parameters_none() == FAILURE) {
5833 RETURN_THROWS();
5834 }
5835 GET_REFLECTION_OBJECT_PTR(module);
5836
5837 array_init(return_value);
5838 ZEND_HASH_FOREACH_STR_KEY_PTR(EG(class_table), key, ce) {
5839 add_extension_class(ce, key, return_value, module, 1);
5840 } ZEND_HASH_FOREACH_END();
5841 }
5842 /* }}} */
5843
5844 /* {{{ Returns an array containing all names of all classes of this extension */
ZEND_METHOD(ReflectionExtension,getClassNames)5845 ZEND_METHOD(ReflectionExtension, getClassNames)
5846 {
5847 reflection_object *intern;
5848 zend_module_entry *module;
5849 zend_string *key;
5850 zend_class_entry *ce;
5851
5852 if (zend_parse_parameters_none() == FAILURE) {
5853 RETURN_THROWS();
5854 }
5855 GET_REFLECTION_OBJECT_PTR(module);
5856
5857 array_init(return_value);
5858 ZEND_HASH_FOREACH_STR_KEY_PTR(EG(class_table), key, ce) {
5859 add_extension_class(ce, key, return_value, module, 0);
5860 } ZEND_HASH_FOREACH_END();
5861 }
5862 /* }}} */
5863
5864 /* {{{ Returns an array containing all names of all extensions this extension depends on */
ZEND_METHOD(ReflectionExtension,getDependencies)5865 ZEND_METHOD(ReflectionExtension, getDependencies)
5866 {
5867 reflection_object *intern;
5868 zend_module_entry *module;
5869 const zend_module_dep *dep;
5870
5871 if (zend_parse_parameters_none() == FAILURE) {
5872 RETURN_THROWS();
5873 }
5874 GET_REFLECTION_OBJECT_PTR(module);
5875
5876 dep = module->deps;
5877
5878 if (!dep)
5879 {
5880 RETURN_EMPTY_ARRAY();
5881 }
5882
5883 array_init(return_value);
5884 while(dep->name) {
5885 zend_string *relation;
5886 char *rel_type;
5887 size_t len = 0;
5888
5889 switch(dep->type) {
5890 case MODULE_DEP_REQUIRED:
5891 rel_type = "Required";
5892 len += sizeof("Required") - 1;
5893 break;
5894 case MODULE_DEP_CONFLICTS:
5895 rel_type = "Conflicts";
5896 len += sizeof("Conflicts") - 1;
5897 break;
5898 case MODULE_DEP_OPTIONAL:
5899 rel_type = "Optional";
5900 len += sizeof("Optional") - 1;
5901 break;
5902 default:
5903 rel_type = "Error"; /* shouldn't happen */
5904 len += sizeof("Error") - 1;
5905 break;
5906 }
5907
5908 if (dep->rel) {
5909 len += strlen(dep->rel) + 1;
5910 }
5911
5912 if (dep->version) {
5913 len += strlen(dep->version) + 1;
5914 }
5915
5916 relation = zend_string_alloc(len, 0);
5917 snprintf(ZSTR_VAL(relation), ZSTR_LEN(relation) + 1, "%s%s%s%s%s",
5918 rel_type,
5919 dep->rel ? " " : "",
5920 dep->rel ? dep->rel : "",
5921 dep->version ? " " : "",
5922 dep->version ? dep->version : "");
5923 add_assoc_str(return_value, dep->name, relation);
5924 dep++;
5925 }
5926 }
5927 /* }}} */
5928
5929 /* {{{ Prints phpinfo block for the extension */
ZEND_METHOD(ReflectionExtension,info)5930 ZEND_METHOD(ReflectionExtension, info)
5931 {
5932 reflection_object *intern;
5933 zend_module_entry *module;
5934
5935 if (zend_parse_parameters_none() == FAILURE) {
5936 RETURN_THROWS();
5937 }
5938 GET_REFLECTION_OBJECT_PTR(module);
5939
5940 php_info_print_module(module);
5941 }
5942 /* }}} */
5943
5944 /* {{{ Returns whether this extension is persistent */
ZEND_METHOD(ReflectionExtension,isPersistent)5945 ZEND_METHOD(ReflectionExtension, isPersistent)
5946 {
5947 reflection_object *intern;
5948 zend_module_entry *module;
5949
5950 if (zend_parse_parameters_none() == FAILURE) {
5951 RETURN_THROWS();
5952 }
5953 GET_REFLECTION_OBJECT_PTR(module);
5954
5955 RETURN_BOOL(module->type == MODULE_PERSISTENT);
5956 }
5957 /* }}} */
5958
5959 /* {{{ Returns whether this extension is temporary */
ZEND_METHOD(ReflectionExtension,isTemporary)5960 ZEND_METHOD(ReflectionExtension, isTemporary)
5961 {
5962 reflection_object *intern;
5963 zend_module_entry *module;
5964
5965 if (zend_parse_parameters_none() == FAILURE) {
5966 RETURN_THROWS();
5967 }
5968 GET_REFLECTION_OBJECT_PTR(module);
5969
5970 RETURN_BOOL(module->type == MODULE_TEMPORARY);
5971 }
5972 /* }}} */
5973
5974 /* {{{ Constructor. Throws an Exception in case the given Zend extension does not exist */
ZEND_METHOD(ReflectionZendExtension,__construct)5975 ZEND_METHOD(ReflectionZendExtension, __construct)
5976 {
5977 zval *object;
5978 reflection_object *intern;
5979 zend_extension *extension;
5980 char *name_str;
5981 size_t name_len;
5982
5983 if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name_str, &name_len) == FAILURE) {
5984 RETURN_THROWS();
5985 }
5986
5987 object = ZEND_THIS;
5988 intern = Z_REFLECTION_P(object);
5989
5990 extension = zend_get_extension(name_str);
5991 if (!extension) {
5992 zend_throw_exception_ex(reflection_exception_ptr, 0,
5993 "Zend Extension \"%s\" does not exist", name_str);
5994 RETURN_THROWS();
5995 }
5996 ZVAL_STRING(reflection_prop_name(object), extension->name);
5997 intern->ptr = extension;
5998 intern->ref_type = REF_TYPE_OTHER;
5999 intern->ce = NULL;
6000 }
6001 /* }}} */
6002
6003 /* {{{ Returns a string representation */
ZEND_METHOD(ReflectionZendExtension,__toString)6004 ZEND_METHOD(ReflectionZendExtension, __toString)
6005 {
6006 reflection_object *intern;
6007 zend_extension *extension;
6008 smart_str str = {0};
6009
6010 if (zend_parse_parameters_none() == FAILURE) {
6011 RETURN_THROWS();
6012 }
6013 GET_REFLECTION_OBJECT_PTR(extension);
6014 _zend_extension_string(&str, extension, "");
6015 RETURN_STR(smart_str_extract(&str));
6016 }
6017 /* }}} */
6018
6019 /* {{{ Returns the name of this Zend extension */
ZEND_METHOD(ReflectionZendExtension,getName)6020 ZEND_METHOD(ReflectionZendExtension, getName)
6021 {
6022 reflection_object *intern;
6023 zend_extension *extension;
6024
6025 if (zend_parse_parameters_none() == FAILURE) {
6026 RETURN_THROWS();
6027 }
6028 GET_REFLECTION_OBJECT_PTR(extension);
6029
6030 RETURN_STRING(extension->name);
6031 }
6032 /* }}} */
6033
6034 /* {{{ Returns the version information of this Zend extension */
ZEND_METHOD(ReflectionZendExtension,getVersion)6035 ZEND_METHOD(ReflectionZendExtension, getVersion)
6036 {
6037 reflection_object *intern;
6038 zend_extension *extension;
6039
6040 if (zend_parse_parameters_none() == FAILURE) {
6041 RETURN_THROWS();
6042 }
6043 GET_REFLECTION_OBJECT_PTR(extension);
6044
6045 if (extension->version) {
6046 RETURN_STRING(extension->version);
6047 } else {
6048 RETURN_EMPTY_STRING();
6049 }
6050 }
6051 /* }}} */
6052
6053 /* {{{ Returns the name of this Zend extension's author */
ZEND_METHOD(ReflectionZendExtension,getAuthor)6054 ZEND_METHOD(ReflectionZendExtension, getAuthor)
6055 {
6056 reflection_object *intern;
6057 zend_extension *extension;
6058
6059 if (zend_parse_parameters_none() == FAILURE) {
6060 RETURN_THROWS();
6061 }
6062 GET_REFLECTION_OBJECT_PTR(extension);
6063
6064 if (extension->author) {
6065 RETURN_STRING(extension->author);
6066 } else {
6067 RETURN_EMPTY_STRING();
6068 }
6069 }
6070 /* }}} */
6071
6072 /* {{{ Returns this Zend extension's URL*/
ZEND_METHOD(ReflectionZendExtension,getURL)6073 ZEND_METHOD(ReflectionZendExtension, getURL)
6074 {
6075 reflection_object *intern;
6076 zend_extension *extension;
6077
6078 if (zend_parse_parameters_none() == FAILURE) {
6079 RETURN_THROWS();
6080 }
6081 GET_REFLECTION_OBJECT_PTR(extension);
6082
6083 if (extension->URL) {
6084 RETURN_STRING(extension->URL);
6085 } else {
6086 RETURN_EMPTY_STRING();
6087 }
6088 }
6089 /* }}} */
6090
6091 /* {{{ Returns this Zend extension's copyright information */
ZEND_METHOD(ReflectionZendExtension,getCopyright)6092 ZEND_METHOD(ReflectionZendExtension, getCopyright)
6093 {
6094 reflection_object *intern;
6095 zend_extension *extension;
6096
6097 if (zend_parse_parameters_none() == FAILURE) {
6098 RETURN_THROWS();
6099 }
6100 GET_REFLECTION_OBJECT_PTR(extension);
6101
6102 if (extension->copyright) {
6103 RETURN_STRING(extension->copyright);
6104 } else {
6105 RETURN_EMPTY_STRING();
6106 }
6107 }
6108 /* }}} */
6109
6110 /* {{{ Dummy constructor -- always throws ReflectionExceptions. */
ZEND_METHOD(ReflectionReference,__construct)6111 ZEND_METHOD(ReflectionReference, __construct)
6112 {
6113 _DO_THROW(
6114 "Cannot directly instantiate ReflectionReference. "
6115 "Use ReflectionReference::fromArrayElement() instead"
6116 );
6117 }
6118 /* }}} */
6119
is_ignorable_reference(HashTable * ht,zval * ref)6120 static zend_bool is_ignorable_reference(HashTable *ht, zval *ref) {
6121 if (Z_REFCOUNT_P(ref) != 1) {
6122 return 0;
6123 }
6124
6125 /* Directly self-referential arrays are treated as proper references
6126 * in zend_array_dup() despite rc=1. */
6127 return Z_TYPE_P(Z_REFVAL_P(ref)) != IS_ARRAY || Z_ARRVAL_P(Z_REFVAL_P(ref)) != ht;
6128 }
6129
6130 /* {{{ Create ReflectionReference for array item. Returns null if not a reference. */
ZEND_METHOD(ReflectionReference,fromArrayElement)6131 ZEND_METHOD(ReflectionReference, fromArrayElement)
6132 {
6133 HashTable *ht;
6134 zval *item;
6135 zend_string *string_key = NULL;
6136 zend_long int_key = 0;
6137 reflection_object *intern;
6138
6139 ZEND_PARSE_PARAMETERS_START(2, 2)
6140 Z_PARAM_ARRAY_HT(ht)
6141 Z_PARAM_STR_OR_LONG(string_key, int_key)
6142 ZEND_PARSE_PARAMETERS_END();
6143
6144 if (string_key) {
6145 item = zend_hash_find(ht, string_key);
6146 } else {
6147 item = zend_hash_index_find(ht, int_key);
6148 }
6149
6150 if (!item) {
6151 _DO_THROW("Array key not found");
6152 RETURN_THROWS();
6153 }
6154
6155 if (Z_TYPE_P(item) != IS_REFERENCE || is_ignorable_reference(ht, item)) {
6156 RETURN_NULL();
6157 }
6158
6159 object_init_ex(return_value, reflection_reference_ptr);
6160 intern = Z_REFLECTION_P(return_value);
6161 ZVAL_COPY(&intern->obj, item);
6162 intern->ref_type = REF_TYPE_OTHER;
6163 }
6164 /* }}} */
6165
6166 /* {{{ Returns a unique identifier for the reference.
6167 * The format of the return value is unspecified and may change. */
ZEND_METHOD(ReflectionReference,getId)6168 ZEND_METHOD(ReflectionReference, getId)
6169 {
6170 reflection_object *intern;
6171 unsigned char digest[20];
6172 PHP_SHA1_CTX context;
6173
6174 if (zend_parse_parameters_none() == FAILURE) {
6175 RETURN_THROWS();
6176 }
6177
6178 intern = Z_REFLECTION_P(getThis());
6179 if (Z_TYPE(intern->obj) != IS_REFERENCE) {
6180 _DO_THROW("Corrupted ReflectionReference object");
6181 RETURN_THROWS();
6182 }
6183
6184 if (!REFLECTION_G(key_initialized)) {
6185 if (php_random_bytes_throw(&REFLECTION_G(key_initialized), 16) == FAILURE) {
6186 RETURN_THROWS();
6187 }
6188
6189 REFLECTION_G(key_initialized) = 1;
6190 }
6191
6192 /* SHA1(ref || key) to avoid directly exposing memory addresses. */
6193 PHP_SHA1Init(&context);
6194 PHP_SHA1Update(&context, (unsigned char *) &Z_REF(intern->obj), sizeof(zend_reference *));
6195 PHP_SHA1Update(&context, REFLECTION_G(key), REFLECTION_KEY_LEN);
6196 PHP_SHA1Final(digest, &context);
6197
6198 RETURN_STRINGL((char *) digest, sizeof(digest));
6199 }
6200 /* }}} */
6201
ZEND_METHOD(ReflectionAttribute,__construct)6202 ZEND_METHOD(ReflectionAttribute, __construct)
6203 {
6204 }
6205
ZEND_METHOD(ReflectionAttribute,__clone)6206 ZEND_METHOD(ReflectionAttribute, __clone)
6207 {
6208 /* Should never be executable */
6209 _DO_THROW("Cannot clone object using __clone()");
6210 }
6211
6212 /* {{{ * Returns the name of the attribute */
ZEND_METHOD(ReflectionAttribute,getName)6213 ZEND_METHOD(ReflectionAttribute, getName)
6214 {
6215 reflection_object *intern;
6216 attribute_reference *attr;
6217
6218 if (zend_parse_parameters_none() == FAILURE) {
6219 RETURN_THROWS();
6220 }
6221 GET_REFLECTION_OBJECT_PTR(attr);
6222
6223 RETURN_STR_COPY(attr->data->name);
6224 }
6225 /* }}} */
6226
6227 /* {{{ * Returns the target of the attribute */
ZEND_METHOD(ReflectionAttribute,getTarget)6228 ZEND_METHOD(ReflectionAttribute, getTarget)
6229 {
6230 reflection_object *intern;
6231 attribute_reference *attr;
6232
6233 if (zend_parse_parameters_none() == FAILURE) {
6234 RETURN_THROWS();
6235 }
6236 GET_REFLECTION_OBJECT_PTR(attr);
6237
6238 RETURN_LONG(attr->target);
6239 }
6240 /* }}} */
6241
6242 /* {{{ * Returns true if the attribute is repeated */
ZEND_METHOD(ReflectionAttribute,isRepeated)6243 ZEND_METHOD(ReflectionAttribute, isRepeated)
6244 {
6245 reflection_object *intern;
6246 attribute_reference *attr;
6247
6248 if (zend_parse_parameters_none() == FAILURE) {
6249 RETURN_THROWS();
6250 }
6251 GET_REFLECTION_OBJECT_PTR(attr);
6252
6253 RETURN_BOOL(zend_is_attribute_repeated(attr->attributes, attr->data));
6254 }
6255 /* }}} */
6256
6257 /* {{{ * Returns the arguments passed to the attribute */
ZEND_METHOD(ReflectionAttribute,getArguments)6258 ZEND_METHOD(ReflectionAttribute, getArguments)
6259 {
6260 reflection_object *intern;
6261 attribute_reference *attr;
6262
6263 zval tmp;
6264 uint32_t i;
6265
6266 if (zend_parse_parameters_none() == FAILURE) {
6267 RETURN_THROWS();
6268 }
6269 GET_REFLECTION_OBJECT_PTR(attr);
6270
6271 array_init(return_value);
6272
6273 for (i = 0; i < attr->data->argc; i++) {
6274 if (FAILURE == zend_get_attribute_value(&tmp, attr->data, i, attr->scope)) {
6275 RETURN_THROWS();
6276 }
6277
6278 if (attr->data->args[i].name) {
6279 /* We ensured at compile-time that there are no duplicate parameter names. */
6280 zend_hash_add_new(Z_ARRVAL_P(return_value), attr->data->args[i].name, &tmp);
6281 } else {
6282 add_next_index_zval(return_value, &tmp);
6283 }
6284 }
6285 }
6286 /* }}} */
6287
call_attribute_constructor(zend_attribute * attr,zend_class_entry * ce,zend_object * obj,zval * args,uint32_t argc,HashTable * named_params,zend_string * filename)6288 static int call_attribute_constructor(
6289 zend_attribute *attr, zend_class_entry *ce, zend_object *obj,
6290 zval *args, uint32_t argc, HashTable *named_params, zend_string *filename)
6291 {
6292 zend_function *ctor = ce->constructor;
6293 zend_execute_data *prev_execute_data, dummy_frame;
6294 zend_function dummy_func;
6295 zend_op dummy_opline;
6296 ZEND_ASSERT(ctor != NULL);
6297
6298 if (!(ctor->common.fn_flags & ZEND_ACC_PUBLIC)) {
6299 zend_throw_error(NULL, "Attribute constructor of class %s must be public", ZSTR_VAL(ce->name));
6300 return FAILURE;
6301 }
6302
6303 if (filename) {
6304 /* Set up dummy call frame that makes it look like the attribute was invoked
6305 * from where it occurs in the code. */
6306 memset(&dummy_frame, 0, sizeof(zend_execute_data));
6307 memset(&dummy_func, 0, sizeof(zend_function));
6308 memset(&dummy_opline, 0, sizeof(zend_op));
6309
6310 prev_execute_data = EG(current_execute_data);
6311 dummy_frame.prev_execute_data = prev_execute_data;
6312 dummy_frame.func = &dummy_func;
6313 dummy_frame.opline = &dummy_opline;
6314
6315 dummy_func.type = ZEND_USER_FUNCTION;
6316 dummy_func.common.fn_flags =
6317 attr->flags & ZEND_ATTRIBUTE_STRICT_TYPES ? ZEND_ACC_STRICT_TYPES : 0;
6318 dummy_func.op_array.filename = filename;
6319
6320 dummy_opline.opcode = ZEND_DO_FCALL;
6321 dummy_opline.lineno = attr->lineno;
6322
6323 EG(current_execute_data) = &dummy_frame;
6324 }
6325
6326 zend_call_known_function(ctor, obj, obj->ce, NULL, argc, args, named_params);
6327
6328 if (filename) {
6329 EG(current_execute_data) = prev_execute_data;
6330 }
6331
6332 if (EG(exception)) {
6333 zend_object_store_ctor_failed(obj);
6334 return FAILURE;
6335 }
6336
6337 return SUCCESS;
6338 }
6339
attribute_ctor_cleanup(zval * obj,zval * args,uint32_t argc,HashTable * named_params)6340 static void attribute_ctor_cleanup(
6341 zval *obj, zval *args, uint32_t argc, HashTable *named_params) /* {{{ */
6342 {
6343 if (obj) {
6344 zval_ptr_dtor(obj);
6345 }
6346
6347 if (args) {
6348 uint32_t i;
6349
6350 for (i = 0; i < argc; i++) {
6351 zval_ptr_dtor(&args[i]);
6352 }
6353
6354 efree(args);
6355 }
6356
6357 if (named_params) {
6358 zend_array_destroy(named_params);
6359 }
6360 }
6361 /* }}} */
6362
6363 /* {{{ * Returns the attribute as an object */
ZEND_METHOD(ReflectionAttribute,newInstance)6364 ZEND_METHOD(ReflectionAttribute, newInstance)
6365 {
6366 reflection_object *intern;
6367 attribute_reference *attr;
6368 zend_attribute *marker;
6369
6370 zend_class_entry *ce;
6371 zval obj;
6372
6373 zval *args = NULL;
6374 HashTable *named_params = NULL;
6375
6376 if (zend_parse_parameters_none() == FAILURE) {
6377 RETURN_THROWS();
6378 }
6379
6380 GET_REFLECTION_OBJECT_PTR(attr);
6381
6382 if (NULL == (ce = zend_lookup_class(attr->data->name))) {
6383 zend_throw_error(NULL, "Attribute class \"%s\" not found", ZSTR_VAL(attr->data->name));
6384 RETURN_THROWS();
6385 }
6386
6387 if (NULL == (marker = zend_get_attribute_str(ce->attributes, ZEND_STRL("attribute")))) {
6388 zend_throw_error(NULL, "Attempting to use non-attribute class \"%s\" as attribute", ZSTR_VAL(attr->data->name));
6389 RETURN_THROWS();
6390 }
6391
6392 if (ce->type == ZEND_USER_CLASS) {
6393 uint32_t flags = ZEND_ATTRIBUTE_TARGET_ALL;
6394
6395 if (marker->argc > 0) {
6396 zval tmp;
6397
6398 if (FAILURE == zend_get_attribute_value(&tmp, marker, 0, ce)) {
6399 RETURN_THROWS();
6400 }
6401
6402 flags = (uint32_t) Z_LVAL(tmp);
6403 }
6404
6405 if (!(attr->target & flags)) {
6406 zend_string *location = zend_get_attribute_target_names(attr->target);
6407 zend_string *allowed = zend_get_attribute_target_names(flags);
6408
6409 zend_throw_error(NULL, "Attribute \"%s\" cannot target %s (allowed targets: %s)",
6410 ZSTR_VAL(attr->data->name), ZSTR_VAL(location), ZSTR_VAL(allowed)
6411 );
6412
6413 zend_string_release(location);
6414 zend_string_release(allowed);
6415
6416 RETURN_THROWS();
6417 }
6418
6419 if (!(flags & ZEND_ATTRIBUTE_IS_REPEATABLE)) {
6420 if (zend_is_attribute_repeated(attr->attributes, attr->data)) {
6421 zend_throw_error(NULL, "Attribute \"%s\" must not be repeated", ZSTR_VAL(attr->data->name));
6422 RETURN_THROWS();
6423 }
6424 }
6425 }
6426
6427 if (SUCCESS != object_init_ex(&obj, ce)) {
6428 RETURN_THROWS();
6429 }
6430
6431 uint32_t argc = 0;
6432 if (attr->data->argc) {
6433 args = emalloc(attr->data->argc * sizeof(zval));
6434
6435 for (uint32_t i = 0; i < attr->data->argc; i++) {
6436 zval val;
6437 if (FAILURE == zend_get_attribute_value(&val, attr->data, i, attr->scope)) {
6438 attribute_ctor_cleanup(&obj, args, argc, named_params);
6439 RETURN_THROWS();
6440 }
6441 if (attr->data->args[i].name) {
6442 if (!named_params) {
6443 named_params = zend_new_array(0);
6444 }
6445 zend_hash_add_new(named_params, attr->data->args[i].name, &val);
6446 } else {
6447 ZVAL_COPY_VALUE(&args[i], &val);
6448 argc++;
6449 }
6450 }
6451 }
6452
6453 if (ce->constructor) {
6454 if (FAILURE == call_attribute_constructor(attr->data, ce, Z_OBJ(obj), args, argc, named_params, attr->filename)) {
6455 attribute_ctor_cleanup(&obj, args, argc, named_params);
6456 RETURN_THROWS();
6457 }
6458 } else if (argc || named_params) {
6459 attribute_ctor_cleanup(&obj, args, argc, named_params);
6460 zend_throw_error(NULL, "Attribute class %s does not have a constructor, cannot pass arguments", ZSTR_VAL(ce->name));
6461 RETURN_THROWS();
6462 }
6463
6464 attribute_ctor_cleanup(NULL, args, argc, named_params);
6465
6466 RETURN_COPY_VALUE(&obj);
6467 }
6468
6469 /* {{{ _reflection_write_property */
_reflection_write_property(zend_object * object,zend_string * name,zval * value,void ** cache_slot)6470 static zval *_reflection_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot)
6471 {
6472 if (zend_hash_exists(&object->ce->properties_info, name)
6473 && (zend_string_equals_literal(name, "name") || zend_string_equals_literal(name, "class")))
6474 {
6475 zend_throw_exception_ex(reflection_exception_ptr, 0,
6476 "Cannot set read-only property %s::$%s", ZSTR_VAL(object->ce->name), ZSTR_VAL(name));
6477 return &EG(uninitialized_zval);
6478 }
6479 else
6480 {
6481 return zend_std_write_property(object, name, value, cache_slot);
6482 }
6483 }
6484 /* }}} */
6485
reflection_init_class_handlers(zend_class_entry * ce)6486 static void reflection_init_class_handlers(zend_class_entry *ce) {
6487 ce->create_object = reflection_objects_new;
6488 ce->serialize = zend_class_serialize_deny;
6489 ce->unserialize = zend_class_unserialize_deny;
6490 }
6491
PHP_MINIT_FUNCTION(reflection)6492 PHP_MINIT_FUNCTION(reflection) /* {{{ */
6493 {
6494 zend_class_entry _reflection_entry;
6495
6496 memcpy(&reflection_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
6497 reflection_object_handlers.offset = XtOffsetOf(reflection_object, zo);
6498 reflection_object_handlers.free_obj = reflection_free_objects_storage;
6499 reflection_object_handlers.clone_obj = NULL;
6500 reflection_object_handlers.write_property = _reflection_write_property;
6501 reflection_object_handlers.get_gc = reflection_get_gc;
6502
6503 INIT_CLASS_ENTRY(_reflection_entry, "ReflectionException", class_ReflectionException_methods);
6504 reflection_exception_ptr = zend_register_internal_class_ex(&_reflection_entry, zend_ce_exception);
6505
6506 INIT_CLASS_ENTRY(_reflection_entry, "Reflection", class_Reflection_methods);
6507 reflection_ptr = zend_register_internal_class(&_reflection_entry);
6508
6509 INIT_CLASS_ENTRY(_reflection_entry, "Reflector", class_Reflector_methods);
6510 reflector_ptr = zend_register_internal_interface(&_reflection_entry);
6511 zend_class_implements(reflector_ptr, 1, zend_ce_stringable);
6512
6513 INIT_CLASS_ENTRY(_reflection_entry, "ReflectionFunctionAbstract", class_ReflectionFunctionAbstract_methods);
6514 reflection_init_class_handlers(&_reflection_entry);
6515 reflection_function_abstract_ptr = zend_register_internal_class(&_reflection_entry);
6516 zend_class_implements(reflection_function_abstract_ptr, 1, reflector_ptr);
6517 zend_declare_property_string(reflection_function_abstract_ptr, "name", sizeof("name")-1, "", ZEND_ACC_ABSTRACT);
6518
6519 INIT_CLASS_ENTRY(_reflection_entry, "ReflectionFunction", class_ReflectionFunction_methods);
6520 reflection_init_class_handlers(&_reflection_entry);
6521 reflection_function_ptr = zend_register_internal_class_ex(&_reflection_entry, reflection_function_abstract_ptr);
6522 zend_declare_property_string(reflection_function_ptr, "name", sizeof("name")-1, "", ZEND_ACC_PUBLIC);
6523
6524 REGISTER_REFLECTION_CLASS_CONST_LONG(function, "IS_DEPRECATED", ZEND_ACC_DEPRECATED);
6525
6526 INIT_CLASS_ENTRY(_reflection_entry, "ReflectionGenerator", class_ReflectionGenerator_methods);
6527 reflection_init_class_handlers(&_reflection_entry);
6528 reflection_generator_ptr = zend_register_internal_class(&_reflection_entry);
6529 reflection_generator_ptr->ce_flags |= ZEND_ACC_FINAL;
6530
6531 INIT_CLASS_ENTRY(_reflection_entry, "ReflectionParameter", class_ReflectionParameter_methods);
6532 reflection_init_class_handlers(&_reflection_entry);
6533 reflection_parameter_ptr = zend_register_internal_class(&_reflection_entry);
6534 zend_class_implements(reflection_parameter_ptr, 1, reflector_ptr);
6535 zend_declare_property_string(reflection_parameter_ptr, "name", sizeof("name")-1, "", ZEND_ACC_PUBLIC);
6536
6537 INIT_CLASS_ENTRY(_reflection_entry, "ReflectionType", class_ReflectionType_methods);
6538 reflection_init_class_handlers(&_reflection_entry);
6539 reflection_type_ptr = zend_register_internal_class(&_reflection_entry);
6540 reflection_type_ptr->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
6541 zend_class_implements(reflection_type_ptr, 1, zend_ce_stringable);
6542
6543 INIT_CLASS_ENTRY(_reflection_entry, "ReflectionNamedType", class_ReflectionNamedType_methods);
6544 reflection_init_class_handlers(&_reflection_entry);
6545 reflection_named_type_ptr = zend_register_internal_class_ex(&_reflection_entry, reflection_type_ptr);
6546
6547 INIT_CLASS_ENTRY(_reflection_entry, "ReflectionUnionType", class_ReflectionUnionType_methods);
6548 reflection_init_class_handlers(&_reflection_entry);
6549 reflection_union_type_ptr = zend_register_internal_class_ex(&_reflection_entry, reflection_type_ptr);
6550
6551 INIT_CLASS_ENTRY(_reflection_entry, "ReflectionMethod", class_ReflectionMethod_methods);
6552 reflection_init_class_handlers(&_reflection_entry);
6553 reflection_method_ptr = zend_register_internal_class_ex(&_reflection_entry, reflection_function_abstract_ptr);
6554 zend_declare_property_string(reflection_method_ptr, "name", sizeof("name")-1, "", ZEND_ACC_PUBLIC);
6555 zend_declare_property_string(reflection_method_ptr, "class", sizeof("class")-1, "", ZEND_ACC_PUBLIC);
6556
6557 REGISTER_REFLECTION_CLASS_CONST_LONG(method, "IS_STATIC", ZEND_ACC_STATIC);
6558 REGISTER_REFLECTION_CLASS_CONST_LONG(method, "IS_PUBLIC", ZEND_ACC_PUBLIC);
6559 REGISTER_REFLECTION_CLASS_CONST_LONG(method, "IS_PROTECTED", ZEND_ACC_PROTECTED);
6560 REGISTER_REFLECTION_CLASS_CONST_LONG(method, "IS_PRIVATE", ZEND_ACC_PRIVATE);
6561 REGISTER_REFLECTION_CLASS_CONST_LONG(method, "IS_ABSTRACT", ZEND_ACC_ABSTRACT);
6562 REGISTER_REFLECTION_CLASS_CONST_LONG(method, "IS_FINAL", ZEND_ACC_FINAL);
6563
6564 INIT_CLASS_ENTRY(_reflection_entry, "ReflectionClass", class_ReflectionClass_methods);
6565 reflection_init_class_handlers(&_reflection_entry);
6566 reflection_class_ptr = zend_register_internal_class(&_reflection_entry);
6567 zend_class_implements(reflection_class_ptr, 1, reflector_ptr);
6568 zend_declare_property_string(reflection_class_ptr, "name", sizeof("name")-1, "", ZEND_ACC_PUBLIC);
6569
6570 /* IS_IMPLICIT_ABSTRACT is not longer used */
6571 REGISTER_REFLECTION_CLASS_CONST_LONG(class, "IS_IMPLICIT_ABSTRACT", ZEND_ACC_IMPLICIT_ABSTRACT_CLASS);
6572 REGISTER_REFLECTION_CLASS_CONST_LONG(class, "IS_EXPLICIT_ABSTRACT", ZEND_ACC_EXPLICIT_ABSTRACT_CLASS);
6573 REGISTER_REFLECTION_CLASS_CONST_LONG(class, "IS_FINAL", ZEND_ACC_FINAL);
6574
6575 INIT_CLASS_ENTRY(_reflection_entry, "ReflectionObject", class_ReflectionObject_methods);
6576 reflection_init_class_handlers(&_reflection_entry);
6577 reflection_object_ptr = zend_register_internal_class_ex(&_reflection_entry, reflection_class_ptr);
6578
6579 INIT_CLASS_ENTRY(_reflection_entry, "ReflectionProperty", class_ReflectionProperty_methods);
6580 reflection_init_class_handlers(&_reflection_entry);
6581 reflection_property_ptr = zend_register_internal_class(&_reflection_entry);
6582 zend_class_implements(reflection_property_ptr, 1, reflector_ptr);
6583 zend_declare_property_string(reflection_property_ptr, "name", sizeof("name")-1, "", ZEND_ACC_PUBLIC);
6584 zend_declare_property_string(reflection_property_ptr, "class", sizeof("class")-1, "", ZEND_ACC_PUBLIC);
6585
6586 REGISTER_REFLECTION_CLASS_CONST_LONG(property, "IS_STATIC", ZEND_ACC_STATIC);
6587 REGISTER_REFLECTION_CLASS_CONST_LONG(property, "IS_PUBLIC", ZEND_ACC_PUBLIC);
6588 REGISTER_REFLECTION_CLASS_CONST_LONG(property, "IS_PROTECTED", ZEND_ACC_PROTECTED);
6589 REGISTER_REFLECTION_CLASS_CONST_LONG(property, "IS_PRIVATE", ZEND_ACC_PRIVATE);
6590
6591 INIT_CLASS_ENTRY(_reflection_entry, "ReflectionClassConstant", class_ReflectionClassConstant_methods);
6592 reflection_init_class_handlers(&_reflection_entry);
6593 reflection_class_constant_ptr = zend_register_internal_class(&_reflection_entry);
6594 zend_class_implements(reflection_class_constant_ptr, 1, reflector_ptr);
6595 zend_declare_property_string(reflection_class_constant_ptr, "name", sizeof("name")-1, "", ZEND_ACC_PUBLIC);
6596 zend_declare_property_string(reflection_class_constant_ptr, "class", sizeof("class")-1, "", ZEND_ACC_PUBLIC);
6597
6598 REGISTER_REFLECTION_CLASS_CONST_LONG(class_constant, "IS_PUBLIC", ZEND_ACC_PUBLIC);
6599 REGISTER_REFLECTION_CLASS_CONST_LONG(class_constant, "IS_PROTECTED", ZEND_ACC_PROTECTED);
6600 REGISTER_REFLECTION_CLASS_CONST_LONG(class_constant, "IS_PRIVATE", ZEND_ACC_PRIVATE);
6601
6602 INIT_CLASS_ENTRY(_reflection_entry, "ReflectionExtension", class_ReflectionExtension_methods);
6603 reflection_init_class_handlers(&_reflection_entry);
6604 reflection_extension_ptr = zend_register_internal_class(&_reflection_entry);
6605 zend_class_implements(reflection_extension_ptr, 1, reflector_ptr);
6606 zend_declare_property_string(reflection_extension_ptr, "name", sizeof("name")-1, "", ZEND_ACC_PUBLIC);
6607
6608 INIT_CLASS_ENTRY(_reflection_entry, "ReflectionZendExtension", class_ReflectionZendExtension_methods);
6609 reflection_init_class_handlers(&_reflection_entry);
6610 reflection_zend_extension_ptr = zend_register_internal_class(&_reflection_entry);
6611 zend_class_implements(reflection_zend_extension_ptr, 1, reflector_ptr);
6612 zend_declare_property_string(reflection_zend_extension_ptr, "name", sizeof("name")-1, "", ZEND_ACC_PUBLIC);
6613
6614 INIT_CLASS_ENTRY(_reflection_entry, "ReflectionReference", class_ReflectionReference_methods);
6615 reflection_init_class_handlers(&_reflection_entry);
6616 _reflection_entry.ce_flags |= ZEND_ACC_FINAL;
6617 reflection_reference_ptr = zend_register_internal_class(&_reflection_entry);
6618
6619 INIT_CLASS_ENTRY(_reflection_entry, "ReflectionAttribute", class_ReflectionAttribute_methods);
6620 reflection_init_class_handlers(&_reflection_entry);
6621 reflection_attribute_ptr = zend_register_internal_class(&_reflection_entry);
6622
6623 REGISTER_REFLECTION_CLASS_CONST_LONG(attribute, "IS_INSTANCEOF", REFLECTION_ATTRIBUTE_IS_INSTANCEOF);
6624
6625 REFLECTION_G(key_initialized) = 0;
6626
6627 return SUCCESS;
6628 } /* }}} */
6629
PHP_MINFO_FUNCTION(reflection)6630 PHP_MINFO_FUNCTION(reflection) /* {{{ */
6631 {
6632 php_info_print_table_start();
6633 php_info_print_table_row(2, "Reflection", "enabled");
6634 php_info_print_table_end();
6635 } /* }}} */
6636
6637 zend_module_entry reflection_module_entry = { /* {{{ */
6638 STANDARD_MODULE_HEADER,
6639 "Reflection",
6640 NULL,
6641 PHP_MINIT(reflection),
6642 NULL,
6643 NULL,
6644 NULL,
6645 PHP_MINFO(reflection),
6646 PHP_REFLECTION_VERSION,
6647 ZEND_MODULE_GLOBALS(reflection),
6648 NULL,
6649 NULL,
6650 NULL,
6651 STANDARD_MODULE_PROPERTIES_EX
6652 }; /* }}} */
6653