2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2011 Derick Rethans |
6 +----------------------------------------------------------------------+
7 | Redistribution and use in source and binary forms, with or without |
8 | modification, are permitted provided that the conditions mentioned |
9 | in the accompanying LICENSE file are met (BSD-3-Clause). |
10 +----------------------------------------------------------------------+
11 | Author: Derick Rethans <derick@derickrethans.nl> |
12 +----------------------------------------------------------------------+
21 #include "ext/standard/php_var.h"
22 #include "ext/standard/php_string.h"
23 #include "ext/standard/php_smart_str.h"
24 #include "ext/spl/spl_exceptions.h"
26 #include "Zend/zend_object_handlers.h"
27 #include "Zend/zend_interfaces.h"
28 #include "Zend/zend_exceptions.h"
31 #define Z_ADDREF_P(pz) (pz)->refcount++
34 #ifndef E_USER_DEPRECATED
35 #define E_USER_DEPRECATED (1<<14L)
38 #define FREE_DTOR(z) \
42 #if PHP_VERSION_ID >= 50300
43 #define APPLY_TSRMLS_DC TSRMLS_DC
44 #define APPLY_TSRMLS_CC TSRMLS_CC
45 #define APPLY_TSRMLS_FETCH()
47 #define APPLY_TSRMLS_DC
48 #define APPLY_TSRMLS_CC
49 #define APPLY_TSRMLS_FETCH() TSRMLS_FETCH()
52 ZEND_BEGIN_ARG_INFO_EX(twig_template_get_attribute_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 6)
53 ZEND_ARG_INFO(0, template)
54 ZEND_ARG_INFO(0, object)
55 ZEND_ARG_INFO(0, item)
56 ZEND_ARG_INFO(0, arguments)
57 ZEND_ARG_INFO(0, type)
58 ZEND_ARG_INFO(0, isDefinedTest)
62 #define PHP_FE_END { NULL, NULL, NULL}
65 static const zend_function_entry twig_functions[] = {
66 PHP_FE(twig_template_get_attributes, twig_template_get_attribute_args)
70 PHP_RSHUTDOWN_FUNCTION(twig)
73 CG(unclean_shutdown) = 0; /* get rid of PHPUnit's exit() and report memleaks */
78 zend_module_entry twig_module_entry = {
79 STANDARD_MODULE_HEADER,
88 STANDARD_MODULE_PROPERTIES
92 #ifdef COMPILE_DL_TWIG
96 static int TWIG_ARRAY_KEY_EXISTS(zval *array, zval *key)
98 if (Z_TYPE_P(array) != IS_ARRAY) {
102 switch (Z_TYPE_P(key)) {
104 return zend_hash_exists(Z_ARRVAL_P(array), "", 1);
108 convert_to_long(key);
110 return zend_hash_index_exists(Z_ARRVAL_P(array), Z_LVAL_P(key));
113 convert_to_string(key);
114 return zend_symtable_exists(Z_ARRVAL_P(array), Z_STRVAL_P(key), Z_STRLEN_P(key) + 1);
118 static int TWIG_INSTANCE_OF(zval *object, zend_class_entry *interface TSRMLS_DC)
120 if (Z_TYPE_P(object) != IS_OBJECT) {
123 return instanceof_function(Z_OBJCE_P(object), interface TSRMLS_CC);
126 static int TWIG_INSTANCE_OF_USERLAND(zval *object, char *interface TSRMLS_DC)
128 zend_class_entry **pce;
129 if (Z_TYPE_P(object) != IS_OBJECT) {
132 if (zend_lookup_class(interface, strlen(interface), &pce TSRMLS_CC) == FAILURE) {
135 return instanceof_function(Z_OBJCE_P(object), *pce TSRMLS_CC);
138 static zval *TWIG_GET_ARRAYOBJECT_ELEMENT(zval *object, zval *offset TSRMLS_DC)
140 zend_class_entry *ce = Z_OBJCE_P(object);
143 if (Z_TYPE_P(object) == IS_OBJECT) {
144 SEPARATE_ARG_IF_REF(offset);
145 zend_call_method_with_1_params(&object, ce, NULL, "offsetget", &retval, offset);
147 zval_ptr_dtor(&offset);
150 if (!EG(exception)) {
151 zend_error(E_ERROR, "Undefined offset for object of type %s used as array.", ce->name);
161 static int TWIG_ISSET_ARRAYOBJECT_ELEMENT(zval *object, zval *offset TSRMLS_DC)
163 zend_class_entry *ce = Z_OBJCE_P(object);
166 if (Z_TYPE_P(object) == IS_OBJECT) {
167 SEPARATE_ARG_IF_REF(offset);
168 zend_call_method_with_1_params(&object, ce, NULL, "offsetexists", &retval, offset);
170 zval_ptr_dtor(&offset);
173 if (!EG(exception)) {
174 zend_error(E_ERROR, "Undefined offset for object of type %s used as array.", ce->name);
179 return (retval && Z_TYPE_P(retval) == IS_BOOL && Z_LVAL_P(retval));
184 static char *TWIG_STRTOLOWER(const char *str, int str_len)
188 item_dup = estrndup(str, str_len);
189 php_strtolower(item_dup, str_len);
193 static zval *TWIG_CALL_USER_FUNC_ARRAY(zval *object, char *function, zval *arguments TSRMLS_DC)
205 table = HASH_OF(arguments);
206 args = safe_emalloc(sizeof(zval **), table->nNumOfElements, 0);
208 zend_hash_internal_pointer_reset_ex(table, &pos);
210 while (zend_hash_get_current_data_ex(table, (void **)&args[i], &pos) == SUCCESS) {
212 zend_hash_move_forward_ex(table, &pos);
214 arg_count = table->nNumOfElements;
217 MAKE_STD_ZVAL(zfunction);
218 ZVAL_STRING(zfunction, function, 1);
219 fci.size = sizeof(fci);
220 fci.function_table = EG(function_table);
221 fci.function_name = zfunction;
222 fci.symbol_table = NULL;
223 #if PHP_VERSION_ID >= 50300
224 fci.object_ptr = object;
226 fci.object_pp = &object;
228 fci.retval_ptr_ptr = &retval_ptr;
229 fci.param_count = arg_count;
231 fci.no_separation = 0;
233 if (zend_call_function(&fci, NULL TSRMLS_CC) == FAILURE) {
234 ALLOC_INIT_ZVAL(retval_ptr);
235 ZVAL_BOOL(retval_ptr, 0);
241 FREE_DTOR(zfunction);
245 static int TWIG_CALL_BOOLEAN(zval *object, char *functionName TSRMLS_DC)
250 ret = TWIG_CALL_USER_FUNC_ARRAY(object, functionName, NULL TSRMLS_CC);
256 static zval *TWIG_GET_STATIC_PROPERTY(zval *class, char *prop_name TSRMLS_DC)
259 zend_class_entry *ce;
261 if (class == NULL || Z_TYPE_P(class) != IS_OBJECT) {
265 ce = zend_get_class_entry(class TSRMLS_CC);
266 #if PHP_VERSION_ID >= 50400
267 tmp_zval = zend_std_get_static_property(ce, prop_name, strlen(prop_name), 0, NULL TSRMLS_CC);
269 tmp_zval = zend_std_get_static_property(ce, prop_name, strlen(prop_name), 0 TSRMLS_CC);
274 static zval *TWIG_GET_ARRAY_ELEMENT_ZVAL(zval *class, zval *prop_name TSRMLS_DC)
278 if (class == NULL || Z_TYPE_P(class) != IS_ARRAY) {
279 if (class != NULL && Z_TYPE_P(class) == IS_OBJECT && TWIG_INSTANCE_OF(class, zend_ce_arrayaccess TSRMLS_CC)) {
280 // array access object
281 return TWIG_GET_ARRAYOBJECT_ELEMENT(class, prop_name TSRMLS_CC);
286 switch(Z_TYPE_P(prop_name)) {
288 zend_hash_find(HASH_OF(class), "", 1, (void**) &tmp_zval);
293 convert_to_long(prop_name);
295 zend_hash_index_find(HASH_OF(class), Z_LVAL_P(prop_name), (void **) &tmp_zval);
299 zend_symtable_find(HASH_OF(class), Z_STRVAL_P(prop_name), Z_STRLEN_P(prop_name) + 1, (void**) &tmp_zval);
306 static zval *TWIG_GET_ARRAY_ELEMENT(zval *class, char *prop_name, int prop_name_length TSRMLS_DC)
310 if (class == NULL/* || Z_TYPE_P(class) != IS_ARRAY*/) {
314 if (class != NULL && Z_TYPE_P(class) == IS_OBJECT && TWIG_INSTANCE_OF(class, zend_ce_arrayaccess TSRMLS_CC)) {
315 // array access object
319 ALLOC_INIT_ZVAL(tmp_name_zval);
320 ZVAL_STRING(tmp_name_zval, prop_name, 1);
321 tmp_ret_zval = TWIG_GET_ARRAYOBJECT_ELEMENT(class, tmp_name_zval TSRMLS_CC);
322 FREE_DTOR(tmp_name_zval);
326 if (zend_symtable_find(HASH_OF(class), prop_name, prop_name_length+1, (void**)&tmp_zval) == SUCCESS) {
332 static zval *TWIG_PROPERTY(zval *object, zval *propname TSRMLS_DC)
336 if (Z_OBJ_HT_P(object)->read_property) {
337 #if PHP_VERSION_ID >= 50400
338 tmp = Z_OBJ_HT_P(object)->read_property(object, propname, BP_VAR_IS, NULL TSRMLS_CC);
340 tmp = Z_OBJ_HT_P(object)->read_property(object, propname, BP_VAR_IS TSRMLS_CC);
342 if (tmp == EG(uninitialized_zval_ptr)) {
349 static int TWIG_HAS_PROPERTY(zval *object, zval *propname TSRMLS_DC)
351 if (Z_OBJ_HT_P(object)->has_property) {
352 #if PHP_VERSION_ID >= 50400
353 return Z_OBJ_HT_P(object)->has_property(object, propname, 0, NULL TSRMLS_CC);
355 return Z_OBJ_HT_P(object)->has_property(object, propname, 0 TSRMLS_CC);
361 static int TWIG_HAS_DYNAMIC_PROPERTY(zval *object, char *prop, int prop_len TSRMLS_DC)
363 if (Z_OBJ_HT_P(object)->get_properties) {
364 return zend_hash_quick_exists(
365 Z_OBJ_HT_P(object)->get_properties(object TSRMLS_CC), // the properties hash
366 prop, // property name
367 prop_len + 1, // property length
368 zend_get_hash_value(prop, prop_len + 1) // hash value
374 static zval *TWIG_PROPERTY_CHAR(zval *object, char *propname TSRMLS_DC)
376 zval *tmp_name_zval, *tmp;
378 ALLOC_INIT_ZVAL(tmp_name_zval);
379 ZVAL_STRING(tmp_name_zval, propname, 1);
380 tmp = TWIG_PROPERTY(object, tmp_name_zval TSRMLS_CC);
381 FREE_DTOR(tmp_name_zval);
385 static zval *TWIG_CALL_S(zval *object, char *method, char *arg0 TSRMLS_DC)
393 MAKE_STD_ZVAL(argument);
394 ZVAL_STRING(argument, arg0, 1);
397 MAKE_STD_ZVAL(zfunction);
398 ZVAL_STRING(zfunction, method, 1);
399 fci.size = sizeof(fci);
400 fci.function_table = EG(function_table);
401 fci.function_name = zfunction;
402 fci.symbol_table = NULL;
403 #if PHP_VERSION_ID >= 50300
404 fci.object_ptr = object;
406 fci.object_pp = &object;
408 fci.retval_ptr_ptr = &retval_ptr;
411 fci.no_separation = 0;
413 if (zend_call_function(&fci, NULL TSRMLS_CC) == FAILURE) {
414 FREE_DTOR(zfunction);
415 zval_ptr_dtor(&argument);
418 FREE_DTOR(zfunction);
419 zval_ptr_dtor(&argument);
423 static int TWIG_CALL_SB(zval *object, char *method, char *arg0 TSRMLS_DC)
428 retval_ptr = TWIG_CALL_S(object, method, arg0 TSRMLS_CC);
429 success = (retval_ptr && (Z_TYPE_P(retval_ptr) == IS_BOOL) && Z_LVAL_P(retval_ptr));
432 zval_ptr_dtor(&retval_ptr);
438 static int TWIG_CALL_ZZ(zval *object, char *method, zval *arg1, zval *arg2 TSRMLS_DC)
449 MAKE_STD_ZVAL(zfunction);
450 ZVAL_STRING(zfunction, method, 1);
451 fci.size = sizeof(fci);
452 fci.function_table = EG(function_table);
453 fci.function_name = zfunction;
454 fci.symbol_table = NULL;
455 #if PHP_VERSION_ID >= 50300
456 fci.object_ptr = object;
458 fci.object_pp = &object;
460 fci.retval_ptr_ptr = &retval_ptr;
463 fci.no_separation = 0;
465 if (zend_call_function(&fci, NULL TSRMLS_CC) == FAILURE) {
466 FREE_DTOR(zfunction);
470 FREE_DTOR(zfunction);
472 success = (retval_ptr && (Z_TYPE_P(retval_ptr) == IS_BOOL) && Z_LVAL_P(retval_ptr));
474 zval_ptr_dtor(&retval_ptr);
480 #ifndef Z_SET_REFCOUNT_P
481 # define Z_SET_REFCOUNT_P(pz, rc) pz->refcount = rc
482 # define Z_UNSET_ISREF_P(pz) pz->is_ref = 0
485 static void TWIG_NEW(zval *object, char *class, zval *arg0, zval *arg1 TSRMLS_DC)
487 zend_class_entry **pce;
489 if (zend_lookup_class(class, strlen(class), &pce TSRMLS_CC) == FAILURE) {
493 Z_TYPE_P(object) = IS_OBJECT;
494 object_init_ex(object, *pce);
495 Z_SET_REFCOUNT_P(object, 1);
496 Z_UNSET_ISREF_P(object);
498 TWIG_CALL_ZZ(object, "__construct", arg0, arg1 TSRMLS_CC);
501 static int twig_add_array_key_to_string(void *pDest APPLY_TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
505 APPLY_TSRMLS_FETCH();
507 buf = va_arg(args, smart_str*);
508 joiner = va_arg(args, char*);
511 smart_str_appends(buf, joiner);
514 if (hash_key->nKeyLength == 0) {
515 smart_str_append_long(buf, (long) hash_key->h);
518 int key_len, tmp_len;
519 key = php_addcslashes(hash_key->arKey, hash_key->nKeyLength - 1, &key_len, 0, "'\\", 2 TSRMLS_CC);
520 tmp_str = php_str_to_str_ex(key, key_len, "\0", 1, "' . \"\\0\" . '", 12, &tmp_len, 0, NULL);
522 smart_str_appendl(buf, tmp_str, tmp_len);
530 static char *TWIG_IMPLODE_ARRAY_KEYS(char *joiner, zval *array TSRMLS_DC)
532 smart_str collector = { 0, 0, 0 };
534 smart_str_appendl(&collector, "", 0);
535 zend_hash_apply_with_arguments(HASH_OF(array) APPLY_TSRMLS_CC, twig_add_array_key_to_string, 2, &collector, joiner);
536 smart_str_0(&collector);
541 static void TWIG_RUNTIME_ERROR(zval *template TSRMLS_DC, char *message, ...)
545 zend_class_entry **pce;
552 zval *constructor_args[3];
553 zval *constructor_retval;
555 if (zend_lookup_class("Twig_Error_Runtime", strlen("Twig_Error_Runtime"), &pce TSRMLS_CC) == FAILURE) {
559 va_start(args, message);
560 vspprintf(&buffer, 0, message, args);
564 object_init_ex(ex, *pce);
566 // Call Twig_Error constructor
567 MAKE_STD_ZVAL(constructor);
568 MAKE_STD_ZVAL(zmessage);
569 MAKE_STD_ZVAL(lineno);
570 MAKE_STD_ZVAL(filename);
571 MAKE_STD_ZVAL(filename_func);
572 MAKE_STD_ZVAL(constructor_retval);
574 ZVAL_STRINGL(constructor, "__construct", sizeof("__construct")-1, 1);
575 ZVAL_STRING(zmessage, buffer, 1);
576 ZVAL_LONG(lineno, -1);
578 // Get template filename
579 ZVAL_STRINGL(filename_func, "getTemplateName", sizeof("getTemplateName")-1, 1);
580 call_user_function(EG(function_table), &template, filename_func, filename, 0, 0 TSRMLS_CC);
582 constructor_args[0] = zmessage;
583 constructor_args[1] = lineno;
584 constructor_args[2] = filename;
585 call_user_function(EG(function_table), &ex, constructor, constructor_retval, 3, constructor_args TSRMLS_CC);
587 zval_ptr_dtor(&constructor_retval);
588 zval_ptr_dtor(&zmessage);
589 zval_ptr_dtor(&lineno);
590 zval_ptr_dtor(&filename);
591 FREE_DTOR(constructor);
592 FREE_DTOR(filename_func);
595 zend_throw_exception_object(ex TSRMLS_CC);
598 static char *TWIG_GET_CLASS_NAME(zval *object TSRMLS_DC)
601 zend_uint class_name_len;
603 if (Z_TYPE_P(object) != IS_OBJECT) {
606 #if PHP_API_VERSION >= 20100412
607 zend_get_object_classname(object, (const char **) &class_name, &class_name_len TSRMLS_CC);
609 zend_get_object_classname(object, &class_name, &class_name_len TSRMLS_CC);
614 static int twig_add_method_to_class(void *pDest APPLY_TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
616 zend_class_entry *ce;
620 zend_function *mptr = (zend_function *) pDest;
621 APPLY_TSRMLS_FETCH();
623 if (!(mptr->common.fn_flags & ZEND_ACC_PUBLIC)) {
627 ce = *va_arg(args, zend_class_entry**);
628 retval = va_arg(args, zval*);
630 item_len = strlen(mptr->common.function_name);
631 item = estrndup(mptr->common.function_name, item_len);
632 php_strtolower(item, item_len);
634 if (strcmp("getenvironment", item) == 0) {
635 zend_class_entry **twig_template_ce;
636 if (zend_lookup_class("Twig_Template", strlen("Twig_Template"), &twig_template_ce TSRMLS_CC) == FAILURE) {
639 if (instanceof_function(ce, *twig_template_ce TSRMLS_CC)) {
644 add_assoc_stringl_ex(retval, item, item_len+1, item, item_len, 0);
649 static int twig_add_property_to_class(void *pDest APPLY_TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
651 zend_class_entry *ce;
653 char *class_name, *prop_name;
654 zend_property_info *pptr = (zend_property_info *) pDest;
655 APPLY_TSRMLS_FETCH();
657 if (!(pptr->flags & ZEND_ACC_PUBLIC) || (pptr->flags & ZEND_ACC_STATIC)) {
661 ce = *va_arg(args, zend_class_entry**);
662 retval = va_arg(args, zval*);
664 #if PHP_API_VERSION >= 20100412
665 zend_unmangle_property_name(pptr->name, pptr->name_length, (const char **) &class_name, (const char **) &prop_name);
667 zend_unmangle_property_name(pptr->name, pptr->name_length, &class_name, &prop_name);
670 add_assoc_string(retval, prop_name, prop_name, 1);
675 static void twig_add_class_to_cache(zval *cache, zval *object, char *class_name TSRMLS_DC)
677 zval *class_info, *class_methods, *class_properties;
678 zend_class_entry *class_ce;
680 class_ce = zend_get_class_entry(object TSRMLS_CC);
682 ALLOC_INIT_ZVAL(class_info);
683 ALLOC_INIT_ZVAL(class_methods);
684 ALLOC_INIT_ZVAL(class_properties);
685 array_init(class_info);
686 array_init(class_methods);
687 array_init(class_properties);
688 // add all methods to self::cache[$class]['methods']
689 zend_hash_apply_with_arguments(&class_ce->function_table APPLY_TSRMLS_CC, twig_add_method_to_class, 2, &class_ce, class_methods);
690 zend_hash_apply_with_arguments(&class_ce->properties_info APPLY_TSRMLS_CC, twig_add_property_to_class, 2, &class_ce, class_properties);
692 add_assoc_zval(class_info, "methods", class_methods);
693 add_assoc_zval(class_info, "properties", class_properties);
694 add_assoc_zval(cache, class_name, class_info);
697 /* {{{ proto mixed twig_template_get_attributes(TwigTemplate template, mixed object, mixed item, array arguments, string type, boolean isDefinedTest, boolean ignoreStrictCheck)
698 A C implementation of TwigTemplate::getAttribute() */
699 PHP_FUNCTION(twig_template_get_attributes)
705 zval *zitem, ztmpitem;
706 zval *arguments = NULL;
710 zend_bool isDefinedTest = 0;
711 zend_bool ignoreStrictCheck = 0;
713 zval *tmp_self_cache;
714 char *class_name = NULL;
718 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ozz|asbb", &template, &object, &zitem, &arguments, &type, &type_len, &isDefinedTest, &ignoreStrictCheck) == FAILURE) {
722 // convert the item to a string
724 zval_copy_ctor(&ztmpitem);
725 convert_to_string(&ztmpitem);
726 item_len = Z_STRLEN(ztmpitem);
727 item = estrndup(Z_STRVAL(ztmpitem), item_len);
728 zval_dtor(&ztmpitem);
736 if (Twig_Template::METHOD_CALL !== $type) {
737 $arrayItem = is_bool($item) || is_float($item) ? (int) $item : $item;
739 if ((is_array($object) && array_key_exists($arrayItem, $object))
740 || ($object instanceof ArrayAccess && isset($object[$arrayItem]))
742 if ($isDefinedTest) {
746 return $object[$arrayItem];
751 if (strcmp("method", type) != 0) {
752 if ((TWIG_ARRAY_KEY_EXISTS(object, zitem))
753 || (TWIG_INSTANCE_OF(object, zend_ce_arrayaccess TSRMLS_CC) && TWIG_ISSET_ARRAYOBJECT_ELEMENT(object, zitem TSRMLS_CC))
761 ret = TWIG_GET_ARRAY_ELEMENT_ZVAL(object, zitem TSRMLS_CC);
764 ret = &EG(uninitialized_zval);
766 RETVAL_ZVAL(ret, 1, 0);
774 if (Twig_Template::ARRAY_CALL === $type) {
775 if ($isDefinedTest) {
778 if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
782 if (strcmp("array", type) == 0 || Z_TYPE_P(object) != IS_OBJECT) {
787 if (ignoreStrictCheck || !TWIG_CALL_BOOLEAN(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "isStrictVariables" TSRMLS_CC)) {
792 if ($object instanceof ArrayAccess) {
793 $message = sprintf('Key "%s" in object with ArrayAccess of class "%s" does not exist', $arrayItem, get_class($object));
794 } elseif (is_object($object)) {
795 $message = sprintf('Impossible to access a key "%s" on an object of class "%s" that does not implement ArrayAccess interface', $item, get_class($object));
796 } elseif (is_array($object)) {
797 if (empty($object)) {
798 $message = sprintf('Key "%s" does not exist as the array is empty', $arrayItem);
800 $message = sprintf('Key "%s" for array with keys "%s" does not exist', $arrayItem, implode(', ', array_keys($object)));
802 } elseif (Twig_Template::ARRAY_CALL === $type) {
803 if (null === $object) {
804 $message = sprintf('Impossible to access a key ("%s") on a null variable', $item);
806 $message = sprintf('Impossible to access a key ("%s") on a %s variable ("%s")', $item, gettype($object), $object);
808 } elseif (null === $object) {
809 $message = sprintf('Impossible to access an attribute ("%s") on a null variable', $item);
811 $message = sprintf('Impossible to access an attribute ("%s") on a %s variable ("%s")', $item, gettype($object), $object);
813 throw new Twig_Error_Runtime($message, -1, $this->getTemplateName());
817 if (TWIG_INSTANCE_OF(object, zend_ce_arrayaccess TSRMLS_CC)) {
818 TWIG_RUNTIME_ERROR(template TSRMLS_CC, "Key \"%s\" in object with ArrayAccess of class \"%s\" does not exist.", item, TWIG_GET_CLASS_NAME(object TSRMLS_CC));
819 } else if (Z_TYPE_P(object) == IS_OBJECT) {
820 TWIG_RUNTIME_ERROR(template TSRMLS_CC, "Impossible to access a key \"%s\" on an object of class \"%s\" that does not implement ArrayAccess interface.", item, TWIG_GET_CLASS_NAME(object TSRMLS_CC));
821 } else if (Z_TYPE_P(object) == IS_ARRAY) {
822 if (0 == zend_hash_num_elements(Z_ARRVAL_P(object))) {
823 TWIG_RUNTIME_ERROR(template TSRMLS_CC, "Key \"%s\" does not exist as the array is empty.", item);
825 char *array_keys = TWIG_IMPLODE_ARRAY_KEYS(", ", object TSRMLS_CC);
826 TWIG_RUNTIME_ERROR(template TSRMLS_CC, "Key \"%s\" for array with keys \"%s\" does not exist.", item, array_keys);
830 char *type_name = zend_zval_type_name(object);
832 if (Z_TYPE_P(object) == IS_NULL) {
833 convert_to_string(object);
834 TWIG_RUNTIME_ERROR(template TSRMLS_CC,
835 (strcmp("array", type) == 0)
836 ? "Impossible to access a key (\"%s\") on a %s variable."
837 : "Impossible to access an attribute (\"%s\") on a %s variable.",
840 convert_to_string(object);
841 TWIG_RUNTIME_ERROR(template TSRMLS_CC,
842 (strcmp("array", type) == 0)
843 ? "Impossible to access a key (\"%s\") on a %s variable (\"%s\")."
844 : "Impossible to access an attribute (\"%s\") on a %s variable (\"%s\").",
845 item, type_name, Z_STRVAL_P(object));
847 zval_ptr_dtor(&object);
855 if (!is_object($object)) {
856 if ($isDefinedTest) {
861 if (Z_TYPE_P(object) != IS_OBJECT) {
867 if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
871 if (null === $object) {
872 $message = sprintf('Impossible to invoke a method ("%s") on a null variable', $item);
874 $message = sprintf('Impossible to invoke a method ("%s") on a %s variable ("%s")', $item, gettype($object), $object);
877 throw new Twig_Error_Runtime($message, -1, $this->getTemplateName());
880 if (ignoreStrictCheck || !TWIG_CALL_BOOLEAN(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "isStrictVariables" TSRMLS_CC)) {
885 type_name = zend_zval_type_name(object);
887 if (Z_TYPE_P(object) == IS_NULL) {
888 convert_to_string_ex(&object);
890 TWIG_RUNTIME_ERROR(template TSRMLS_CC, "Impossible to invoke a method (\"%s\") on a %s variable.", item, type_name);
892 convert_to_string_ex(&object);
894 TWIG_RUNTIME_ERROR(template TSRMLS_CC, "Impossible to invoke a method (\"%s\") on a %s variable (\"%s\").", item, type_name, Z_STRVAL_P(object));
897 zval_ptr_dtor(&object);
902 $class = get_class($object);
905 class_name = TWIG_GET_CLASS_NAME(object TSRMLS_CC);
906 tmp_self_cache = TWIG_GET_STATIC_PROPERTY(template, "cache" TSRMLS_CC);
907 tmp_class = TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name, strlen(class_name) TSRMLS_CC);
910 twig_add_class_to_cache(tmp_self_cache, object, class_name TSRMLS_CC);
911 tmp_class = TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name, strlen(class_name) TSRMLS_CC);
917 if (Twig_Template::METHOD_CALL !== $type && !$object instanceof Twig_Template) {
918 if (isset($object->$item) || array_key_exists((string) $item, $object)) {
919 if ($isDefinedTest) {
923 if ($this->env->hasExtension('Twig_Extension_Sandbox')) {
924 $this->env->getExtension('Twig_Extension_Sandbox')->checkPropertyAllowed($object, $item);
927 return $object->$item;
931 if (strcmp("method", type) != 0 && !TWIG_INSTANCE_OF_USERLAND(object, "Twig_Template" TSRMLS_CC)) {
932 zval *tmp_properties, *tmp_item;
934 tmp_properties = TWIG_GET_ARRAY_ELEMENT(tmp_class, "properties", strlen("properties") TSRMLS_CC);
935 tmp_item = TWIG_GET_ARRAY_ELEMENT(tmp_properties, item, item_len TSRMLS_CC);
937 if (tmp_item || TWIG_HAS_PROPERTY(object, zitem TSRMLS_CC) || TWIG_HAS_DYNAMIC_PROPERTY(object, item, item_len TSRMLS_CC)) {
942 if (TWIG_CALL_SB(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "hasExtension", "Twig_Extension_Sandbox" TSRMLS_CC)) {
943 TWIG_CALL_ZZ(TWIG_CALL_S(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "getExtension", "Twig_Extension_Sandbox" TSRMLS_CC), "checkPropertyAllowed", object, zitem TSRMLS_CC);
950 ret = TWIG_PROPERTY(object, zitem TSRMLS_CC);
952 RETURN_ZVAL(ret, 1, 0);
957 if (!isset(self::$cache[$class]['methods'])) {
958 if ($object instanceof self) {
959 $ref = new ReflectionClass($class);
962 foreach ($ref->getMethods(ReflectionMethod::IS_PUBLIC) as $refMethod) {
963 $methodName = strtolower($refMethod->name);
965 // Accessing the environment from templates is forbidden to prevent untrusted changes to the environment
966 if ('getenvironment' !== $methodName) {
967 $methods[$methodName] = true;
971 self::$cache[$class]['methods'] = $methods;
973 self::$cache[$class]['methods'] = array_change_key_case(array_flip(get_class_methods($object)));
978 $lcItem = strtolower($item);
979 if (isset(self::$cache[$class]['methods'][$lcItem])) {
980 $method = (string) $item;
981 } elseif (isset(self::$cache[$class]['methods']['get'.$lcItem])) {
982 $method = 'get'.$item;
983 } elseif (isset(self::$cache[$class]['methods']['is'.$lcItem])) {
984 $method = 'is'.$item;
985 } elseif (isset(self::$cache[$class]['methods']['__call'])) {
986 $method = (string) $item;
991 char *lcItem = TWIG_STRTOLOWER(item, item_len);
994 char *methodForDeprecation = NULL;
995 char *tmp_method_name_get;
996 char *tmp_method_name_is;
1000 lcItem_length = strlen(lcItem);
1001 tmp_method_name_get = emalloc(4 + lcItem_length);
1002 tmp_method_name_is = emalloc(3 + lcItem_length);
1004 sprintf(tmp_method_name_get, "get%s", lcItem);
1005 sprintf(tmp_method_name_is, "is%s", lcItem);
1007 tmp_methods = TWIG_GET_ARRAY_ELEMENT(tmp_class, "methods", strlen("methods") TSRMLS_CC);
1008 methodForDeprecation = emalloc(item_len + 1);
1009 sprintf(methodForDeprecation, "%s", item);
1011 if (TWIG_GET_ARRAY_ELEMENT(tmp_methods, lcItem, lcItem_length TSRMLS_CC)) {
1013 } else if (TWIG_GET_ARRAY_ELEMENT(tmp_methods, tmp_method_name_get, lcItem_length + 3 TSRMLS_CC)) {
1014 method = tmp_method_name_get;
1015 } else if (TWIG_GET_ARRAY_ELEMENT(tmp_methods, tmp_method_name_is, lcItem_length + 2 TSRMLS_CC)) {
1016 method = tmp_method_name_is;
1017 } else if (TWIG_GET_ARRAY_ELEMENT(tmp_methods, "__call", 6 TSRMLS_CC)) {
1022 if ($isDefinedTest) {
1026 if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
1030 throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist.', $item, get_class($object)), -1, $this->getTemplateName());
1033 if ($isDefinedTest) {
1038 efree(tmp_method_name_get);
1039 efree(tmp_method_name_is);
1042 if (isDefinedTest) {
1046 if (ignoreStrictCheck || !TWIG_CALL_BOOLEAN(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "isStrictVariables" TSRMLS_CC)) {
1050 TWIG_RUNTIME_ERROR(template TSRMLS_CC, "Neither the property \"%s\" nor one of the methods \"%s()\", \"get%s()\"/\"is%s()\" or \"__call()\" exist and have public access in class \"%s\".", item, item, item, item, TWIG_GET_CLASS_NAME(object TSRMLS_CC));
1055 if (isDefinedTest) {
1056 efree(tmp_method_name_get);
1057 efree(tmp_method_name_is);
1058 efree(lcItem);efree(item);
1062 if ($this->env->hasExtension('Twig_Extension_Sandbox')) {
1063 $this->env->getExtension('Twig_Extension_Sandbox')->checkMethodAllowed($object, $method);
1066 MAKE_STD_ZVAL(zmethod);
1067 ZVAL_STRING(zmethod, method, 1);
1068 if (TWIG_CALL_SB(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "hasExtension", "Twig_Extension_Sandbox" TSRMLS_CC)) {
1069 TWIG_CALL_ZZ(TWIG_CALL_S(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "getExtension", "Twig_Extension_Sandbox" TSRMLS_CC), "checkMethodAllowed", object, zmethod TSRMLS_CC);
1071 zval_ptr_dtor(&zmethod);
1072 if (EG(exception)) {
1073 efree(tmp_method_name_get);
1074 efree(tmp_method_name_is);
1075 efree(lcItem);efree(item);
1079 // Some objects throw exceptions when they have __call, and the method we try
1080 // to call is not supported. If ignoreStrictCheck is true, we should return null.
1082 $ret = call_user_func_array(array($object, $method), $arguments);
1083 } catch (BadMethodCallException $e) {
1084 if ($call && ($ignoreStrictCheck || !$this->env->isStrictVariables())) {
1090 ret = TWIG_CALL_USER_FUNC_ARRAY(object, method, arguments TSRMLS_CC);
1091 if (EG(exception) && TWIG_INSTANCE_OF(EG(exception), spl_ce_BadMethodCallException TSRMLS_CC)) {
1092 if (ignoreStrictCheck || !TWIG_CALL_BOOLEAN(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "isStrictVariables" TSRMLS_CC)) {
1093 efree(tmp_method_name_get);
1094 efree(tmp_method_name_is);
1095 efree(lcItem);efree(item);
1096 zend_clear_exception(TSRMLS_C);
1101 efree(tmp_method_name_get);
1102 efree(tmp_method_name_is);
1105 // @deprecated in 1.28
1106 if ($object instanceof Twig_TemplateInterface) {
1107 $self = $object->getTemplateName() === $this->getTemplateName();
1108 $message = sprintf('Calling "%s" on template "%s" from template "%s" is deprecated since version 1.28 and won\'t be supported anymore in 2.0.', $item, $object->getTemplateName(), $this->getTemplateName());
1109 if ('renderBlock' === $method || 'displayBlock' === $method) {
1110 $message .= sprintf(' Use block("%s"%s) instead).', $arguments[0], $self ? '' : ', template');
1111 } elseif ('hasBlock' === $method) {
1112 $message .= sprintf(' Use "block("%s"%s) is defined" instead).', $arguments[0], $self ? '' : ', template');
1113 } elseif ('render' === $method || 'display' === $method) {
1114 $message .= sprintf(' Use include("%s") instead).', $object->getTemplateName());
1116 @trigger_error($message, E_USER_DEPRECATED);
1118 return $ret === '' ? '' : new Twig_Markup($ret, $this->env->getCharset());
1124 // ret can be null, if e.g. the called method throws an exception
1126 if (TWIG_INSTANCE_OF_USERLAND(object, "Twig_TemplateInterface" TSRMLS_CC)) {
1128 int old_error_reporting;
1129 zval *object_filename;
1130 zval *this_filename;
1131 zval *filename_func;
1132 char *deprecation_message_complement = NULL;
1133 char *deprecation_message = NULL;
1135 MAKE_STD_ZVAL(object_filename);
1136 MAKE_STD_ZVAL(this_filename);
1137 MAKE_STD_ZVAL(filename_func);
1139 // Get templates names
1140 ZVAL_STRINGL(filename_func, "getTemplateName", sizeof("getTemplateName")-1, 1);
1141 call_user_function(EG(function_table), &object, filename_func, object_filename, 0, 0 TSRMLS_CC);
1142 ZVAL_STRINGL(filename_func, "getTemplateName", sizeof("getTemplateName")-1, 1);
1143 call_user_function(EG(function_table), &template, filename_func, this_filename, 0, 0 TSRMLS_CC);
1145 self = (strcmp(Z_STRVAL_P(object_filename), Z_STRVAL_P(this_filename)) == 0);
1147 if (strcmp(methodForDeprecation, "renderBlock") == 0 || strcmp(methodForDeprecation, "displayBlock") == 0) {
1149 zend_hash_index_find(HASH_OF(arguments), 0, (void **) &arg0);
1151 &deprecation_message_complement,
1152 " Use block(\"%s\"%s) instead).",
1154 self ? "" : ", template"
1156 } else if (strcmp(methodForDeprecation, "hasBlock") == 0) {
1158 zend_hash_index_find(HASH_OF(arguments), 0, (void **) &arg0);
1160 &deprecation_message_complement,
1161 " Use \"block(\"%s\"%s) is defined\" instead).",
1163 self ? "" : ", template"
1165 } else if (strcmp(methodForDeprecation, "render") == 0 || strcmp(methodForDeprecation, "display") == 0) {
1167 &deprecation_message_complement,
1168 " Use include(\"%s\") instead).",
1169 Z_STRVAL_P(object_filename)
1172 deprecation_message_complement = (char*)calloc(0, sizeof(char));
1176 &deprecation_message,
1177 "Calling \"%s\" on template \"%s\" from template \"%s\" is deprecated since version 1.28 and won't be supported anymore in 2.0.%s",
1178 methodForDeprecation,
1179 Z_STRVAL_P(object_filename),
1180 Z_STRVAL_P(this_filename),
1181 deprecation_message_complement
1184 old_error_reporting = EG(error_reporting);
1185 EG(error_reporting) = 0;
1186 zend_error(E_USER_DEPRECATED, "%s", deprecation_message);
1187 EG(error_reporting) = old_error_reporting;
1189 FREE_DTOR(filename_func)
1190 FREE_DTOR(object_filename)
1191 FREE_DTOR(this_filename)
1192 free(deprecation_message);
1193 free(deprecation_message_complement);
1195 if (Z_STRLEN_P(ret) != 0) {
1196 zval *charset = TWIG_CALL_USER_FUNC_ARRAY(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "getCharset", NULL TSRMLS_CC);
1197 TWIG_NEW(return_value, "Twig_Markup", ret, charset TSRMLS_CC);
1198 zval_ptr_dtor(&charset);
1200 zval_ptr_dtor(&ret);
1202 efree(methodForDeprecation);
1207 RETVAL_ZVAL(ret, 1, 0);
1209 zval_ptr_dtor(&ret);
1213 efree(methodForDeprecation);