From 430aecfdc0da32d4289f2334b938eff0aec46d70 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Mon, 21 Mar 2011 11:42:25 +0000 Subject: [PATCH 01/73] - Added basic twig extension structure. --- config.m4 | 8 +++++ php_twig.h | 52 +++++++++++++++++++++++++++ twig.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 164 insertions(+) create mode 100644 config.m4 create mode 100644 php_twig.h create mode 100644 twig.c diff --git a/config.m4 b/config.m4 new file mode 100644 index 000000000..83486be4c --- /dev/null +++ b/config.m4 @@ -0,0 +1,8 @@ +dnl config.m4 for extension twig + +PHP_ARG_ENABLE(twig, whether to enable twig support, +[ --enable-twig Enable twig support]) + +if test "$PHP_TWIG" != "no"; then + PHP_NEW_EXTENSION(twig, twig.c, $ext_shared) +fi diff --git a/php_twig.h b/php_twig.h new file mode 100644 index 000000000..01d661032 --- /dev/null +++ b/php_twig.h @@ -0,0 +1,52 @@ +/* + +----------------------------------------------------------------------+ + | Twig Extension | + +----------------------------------------------------------------------+ + | Copyright (c) 2011 Derick Rethans | + +----------------------------------------------------------------------+ + | This source file is subject to version 2.02 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available at through the world-wide-web at | + | http://www.php.net/license/2_02.txt. | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Derick Rethans | + +----------------------------------------------------------------------+ + */ + +#ifndef PHP_TWIG_H +#define PHP_TWIG_H + +#include "php.h" + +extern zend_module_entry twig_module_entry; +#define phpext_twig_ptr &twig_module_entry + +#ifdef PHP_WIN32 +#define PHP_TWIG_API __declspec(dllexport) +#else +#define PHP_TWIG_API +#endif + +#ifdef ZTS +#include "TSRM.h" +#endif + +PHP_MINIT_FUNCTION(twig); +PHP_MSHUTDOWN_FUNCTION(twig); +PHP_RINIT_FUNCTION(twig); +PHP_RSHUTDOWN_FUNCTION(twig); +PHP_MINFO_FUNCTION(twig); + +ZEND_BEGIN_MODULE_GLOBALS(twig) +ZEND_END_MODULE_GLOBALS(twig) + +#ifdef ZTS +#define TWIG_G(v) TSRMG(twig_globals_id, zend_twig_globals *, v) +#else +#define TWIG_G(v) (twig_globals.v) +#endif + +#endif diff --git a/twig.c b/twig.c new file mode 100644 index 000000000..9dc3dc7a9 --- /dev/null +++ b/twig.c @@ -0,0 +1,104 @@ +/* + +----------------------------------------------------------------------+ + | Twig Extension | + +----------------------------------------------------------------------+ + | Copyright (c) 2011 Derick Rethans | + +----------------------------------------------------------------------+ + | This source file is subject to version 2.02 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available at through the world-wide-web at | + | http://www.php.net/license/2_02.txt. | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Derick Rethans | + +----------------------------------------------------------------------+ + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "php.h" +#include "php_ini.h" +#include "ext/standard/info.h" +#include "php_twig.h" + +zend_function_entry twig_functions[] = { + {NULL, NULL, NULL} +}; + + +zend_module_entry twig_module_entry = { +#if ZEND_MODULE_API_NO >= 20010901 + STANDARD_MODULE_HEADER, +#endif + "twig", + twig_functions, + PHP_MINIT(twig), + PHP_MSHUTDOWN(twig), + PHP_RINIT(twig), + PHP_RSHUTDOWN(twig), + PHP_MINFO(twig), +#if ZEND_MODULE_API_NO >= 20010901 + "0.0.1", +#endif + STANDARD_MODULE_PROPERTIES +}; + + +#ifdef COMPILE_DL_TWIG +ZEND_GET_MODULE(twig) +#endif + +ZEND_DECLARE_MODULE_GLOBALS(twig) + +PHP_INI_BEGIN() +PHP_INI_END() + +static void twig_init_globals(zend_twig_globals *twig_globals) +{ +} + + +PHP_MINIT_FUNCTION(twig) +{ + ZEND_INIT_MODULE_GLOBALS(twig, twig_init_globals, NULL); + REGISTER_INI_ENTRIES(); + + return SUCCESS; +} + + +PHP_MSHUTDOWN_FUNCTION(twig) +{ + UNREGISTER_INI_ENTRIES(); + + return SUCCESS; +} + + + +PHP_RINIT_FUNCTION(twig) +{ + return SUCCESS; +} + + + +PHP_RSHUTDOWN_FUNCTION(twig) +{ + return SUCCESS; +} + + +PHP_MINFO_FUNCTION(twig) +{ + php_info_print_table_start(); + php_info_print_table_header(2, "twig support", "enabled"); + php_info_print_table_end(); + + DISPLAY_INI_ENTRIES(); + +} From e19701d75bf11413f5825685f565eb9bf7d24fe1 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Mon, 21 Mar 2011 11:43:00 +0000 Subject: [PATCH 02/73] - Added function declaration and headers. --- php_twig.h | 2 ++ twig.c | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/php_twig.h b/php_twig.h index 01d661032..4958d78f7 100644 --- a/php_twig.h +++ b/php_twig.h @@ -34,6 +34,8 @@ extern zend_module_entry twig_module_entry; #include "TSRM.h" #endif +PHP_FUNCTION(twig_template_get_attributes); + PHP_MINIT_FUNCTION(twig); PHP_MSHUTDOWN_FUNCTION(twig); PHP_RINIT_FUNCTION(twig); diff --git a/twig.c b/twig.c index 9dc3dc7a9..ee1a21d91 100644 --- a/twig.c +++ b/twig.c @@ -25,7 +25,18 @@ #include "ext/standard/info.h" #include "php_twig.h" +ZEND_BEGIN_ARG_INFO_EX(twig_template_get_attribute_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 7) + ZEND_ARG_INFO(0, template) + ZEND_ARG_INFO(0, object) + ZEND_ARG_INFO(0, item) + ZEND_ARG_INFO(0, arguments) + ZEND_ARG_INFO(0, type) + ZEND_ARG_INFO(0, noStrictCheck) + ZEND_ARG_INFO(0, line) +ZEND_END_ARG_INFO() + zend_function_entry twig_functions[] = { + PHP_FE(twig_template_get_attributes, twig_template_get_attribute_args) {NULL, NULL, NULL} }; @@ -102,3 +113,7 @@ PHP_MINFO_FUNCTION(twig) DISPLAY_INI_ENTRIES(); } + +PHP_FUNCTION(twig_template_get_attributes) +{ +} From 979d6ec51b8d53feab7fdca9f250a87b06b27af4 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Mon, 21 Mar 2011 11:43:15 +0000 Subject: [PATCH 03/73] - Added broken down original code. --- twig.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/twig.c b/twig.c index ee1a21d91..838cf4ff1 100644 --- a/twig.c +++ b/twig.c @@ -116,4 +116,96 @@ PHP_MINFO_FUNCTION(twig) PHP_FUNCTION(twig_template_get_attributes) { +/* + // array + if (Twig_TemplateInterface::METHOD_CALL !== $type) { + if ((is_array($object) || is_object($object) && $object instanceof ArrayAccess) && isset($object[$item])) { + return $object[$item]; + } +*/ +/* + if (Twig_TemplateInterface::ARRAY_CALL === $type) { + if (!$this->env->isStrictVariables() || $noStrictCheck) { + return null; + } + + if (is_object($object)) { + throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $item, get_class($object)), $line, $this->getTemplateName()); + // array + } else { + throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $item, implode(', ', array_keys($object))), $line, $this->getTemplateName()); + } + } + } +*/ +/* + if (!is_object($object)) { + if (!$this->env->isStrictVariables() || $noStrictCheck) { + return null; + } + throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, $object), $line, $this->getTemplateName()); + } +*/ +/* + // get some information about the object + $class = get_class($object); + if (!isset(self::$cache[$class])) { + $r = new ReflectionClass($class); + self::$cache[$class] = array('methods' => array(), 'properties' => array()); + foreach ($r->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { + self::$cache[$class]['methods'][strtolower($method->getName())] = true; + } + + foreach ($r->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { + self::$cache[$class]['properties'][$property->getName()] = true; + } + } +*/ +/* + // object property + if (Twig_TemplateInterface::METHOD_CALL !== $type) { + if (isset(self::$cache[$class]['properties'][$item]) || isset($object->$item)) { + if ($this->env->hasExtension('sandbox')) { + $this->env->getExtension('sandbox')->checkPropertyAllowed($object, $item); + } + + return $object->$item; + } + } +*/ +/* + // object method + $lcItem = strtolower($item); + if (isset(self::$cache[$class]['methods'][$lcItem])) { + $method = $item; + } elseif (isset(self::$cache[$class]['methods']['get'.$lcItem])) { + $method = 'get'.$item; + } elseif (isset(self::$cache[$class]['methods']['is'.$lcItem])) { + $method = 'is'.$item; + } elseif (isset(self::$cache[$class]['methods']['__call'])) { + $method = $item; + } else { + if (!$this->env->isStrictVariables() || $noStrictCheck) { + return null; + } + + throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object)), $line, $this->getTemplateName()); + } +*/ +/* + if ($this->env->hasExtension('sandbox')) { + $this->env->getExtension('sandbox')->checkMethodAllowed($object, $method); + } +*/ +/* + $ret = call_user_func_array(array($object, $method), $arguments); +*/ +/* + if ($object instanceof Twig_TemplateInterface) { + return new Twig_Markup($ret); + } +*/ +/* + return $ret; +*/ } From 6edc311faac2818a23cb4fea03da4036a8984db9 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Mon, 21 Mar 2011 11:43:31 +0000 Subject: [PATCH 04/73] - Some scaffolding implementation. --- twig.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/twig.c b/twig.c index 838cf4ff1..34c82d823 100644 --- a/twig.c +++ b/twig.c @@ -114,8 +114,22 @@ PHP_MINFO_FUNCTION(twig) } +/* {{{ proto mixed twig_template_get_attributes(TwigTemplate template, mixed object, mixed item, array arguments, string type, boolean noStrictCheck, integer line) + A C implementation of TwigTemplate::getAttribute() */ PHP_FUNCTION(twig_template_get_attributes) { + zval *template; + zval *object; + zval *item; + zval *arguments; + char *type = NULL; + int type_len = 0; + zend_bool noStrictCheck = 0; + long line = -1; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ozzasbl", &template, &object, &item, &arguments, &type, &type_len, &noStrictCheck, &line) == FAILURE) { + return NULL; + } /* // array if (Twig_TemplateInterface::METHOD_CALL !== $type) { @@ -123,6 +137,9 @@ PHP_FUNCTION(twig_template_get_attributes) return $object[$item]; } */ + if (strcmp(type, "any") == 0) { + if ((IS_ARRAY(object) && ... ) || (IS_OBJECT(object) && instanceof_function(Z_OBJCE_P(object), array_access_ce TSRMLS_CC) && (...))) { + } /* if (Twig_TemplateInterface::ARRAY_CALL === $type) { if (!$this->env->isStrictVariables() || $noStrictCheck) { @@ -138,6 +155,18 @@ PHP_FUNCTION(twig_template_get_attributes) } } */ + if (strcmp(type, "array") == 0) { + if (!CHECKSTRICTVARS || noStrictCheck) { + RETURN_NULL; + } + + if (IS_OBJECT(object)) { + THROW EXCEPTION + } else { + THROW EXCEPTION + } + } + } /* if (!is_object($object)) { if (!$this->env->isStrictVariables() || $noStrictCheck) { @@ -146,6 +175,13 @@ PHP_FUNCTION(twig_template_get_attributes) throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, $object), $line, $this->getTemplateName()); } */ + if (!IS_OBJECT(object)) { + if (!CHECKSTRICTVARS || noStrictCheck) { + RETURN_NULL; + } + THROW EXCEPTION + } + /* // get some information about the object $class = get_class($object); From 75d3e66cba46a9c209b041905bcd50f6ff8efad2 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Fri, 3 Jun 2011 17:37:20 +0100 Subject: [PATCH 05/73] - Updated with new code from Fabien's repository. --- twig.c | 74 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/twig.c b/twig.c index 34c82d823..a31bf8f0a 100644 --- a/twig.c +++ b/twig.c @@ -114,7 +114,7 @@ PHP_MINFO_FUNCTION(twig) } -/* {{{ proto mixed twig_template_get_attributes(TwigTemplate template, mixed object, mixed item, array arguments, string type, boolean noStrictCheck, integer line) +/* {{{ proto mixed twig_template_get_attributes(TwigTemplate template, mixed object, mixed item, array arguments, string type, boolean isDefinedTest) A C implementation of TwigTemplate::getAttribute() */ PHP_FUNCTION(twig_template_get_attributes) { @@ -124,64 +124,56 @@ PHP_FUNCTION(twig_template_get_attributes) zval *arguments; char *type = NULL; int type_len = 0; - zend_bool noStrictCheck = 0; - long line = -1; + zend_bool isDefinedTest = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ozzasbl", &template, &object, &item, &arguments, &type, &type_len, &noStrictCheck, &line) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ozzasbl", &template, &object, &item, &arguments, &type, &type_len, &isDefinedTest) == FAILURE) { return NULL; } + /* // array if (Twig_TemplateInterface::METHOD_CALL !== $type) { - if ((is_array($object) || is_object($object) && $object instanceof ArrayAccess) && isset($object[$item])) { + if ((is_array($object) && array_key_exists($item, $object)) + || ($object instanceof ArrayAccess && isset($object[$item])) + ) { + if ($isDefinedTest) { + return true; + } + return $object[$item]; } */ - if (strcmp(type, "any") == 0) { - if ((IS_ARRAY(object) && ... ) || (IS_OBJECT(object) && instanceof_function(Z_OBJCE_P(object), array_access_ce TSRMLS_CC) && (...))) { - } /* if (Twig_TemplateInterface::ARRAY_CALL === $type) { - if (!$this->env->isStrictVariables() || $noStrictCheck) { + if ($isDefinedTest) { + return false; + } + if (!$this->env->isStrictVariables()) { return null; } if (is_object($object)) { - throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $item, get_class($object)), $line, $this->getTemplateName()); + throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $item, get_class($object))); // array } else { - throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $item, implode(', ', array_keys($object))), $line, $this->getTemplateName()); + throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $item, implode(', ', array_keys($object)))); } } } */ - if (strcmp(type, "array") == 0) { - if (!CHECKSTRICTVARS || noStrictCheck) { - RETURN_NULL; - } - - if (IS_OBJECT(object)) { - THROW EXCEPTION - } else { - THROW EXCEPTION - } - } - } /* if (!is_object($object)) { - if (!$this->env->isStrictVariables() || $noStrictCheck) { + if ($isDefinedTest) { + return false; + } +*/ +/* + if (!$this->env->isStrictVariables()) { return null; } - throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, $object), $line, $this->getTemplateName()); + throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, $object)); } */ - if (!IS_OBJECT(object)) { - if (!CHECKSTRICTVARS || noStrictCheck) { - RETURN_NULL; - } - THROW EXCEPTION - } - /* // get some information about the object $class = get_class($object); @@ -200,7 +192,12 @@ PHP_FUNCTION(twig_template_get_attributes) /* // object property if (Twig_TemplateInterface::METHOD_CALL !== $type) { - if (isset(self::$cache[$class]['properties'][$item]) || isset($object->$item)) { + if (isset(self::$cache[$class]['properties'][$item]) + || isset($object->$item) || array_key_exists($item, $object) + ) { + if ($isDefinedTest) { + return true; + } if ($this->env->hasExtension('sandbox')) { $this->env->getExtension('sandbox')->checkPropertyAllowed($object, $item); } @@ -221,11 +218,16 @@ PHP_FUNCTION(twig_template_get_attributes) } elseif (isset(self::$cache[$class]['methods']['__call'])) { $method = $item; } else { - if (!$this->env->isStrictVariables() || $noStrictCheck) { + if ($isDefinedTest) { + return false; + } + if (!$this->env->isStrictVariables()) { return null; } - - throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object)), $line, $this->getTemplateName()); + throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object))); + } + if ($isDefinedTest) { + return true; } */ /* From bfc6e34f29964f9a180233b032d9061df4f74e81 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Fri, 3 Jun 2011 17:37:53 +0100 Subject: [PATCH 06/73] - Added a .gitignore file --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..1ee84da98 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.sw* From 15de5334c2ae4801dc15e6c3a34fa623f7b769c3 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Mon, 6 Jun 2011 16:16:58 +0100 Subject: [PATCH 07/73] - Added pseudo code --- twig.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/twig.c b/twig.c index a31bf8f0a..566198ecc 100644 --- a/twig.c +++ b/twig.c @@ -143,6 +143,16 @@ PHP_FUNCTION(twig_template_get_attributes) return $object[$item]; } */ + if (strcmp("method", type) == 0) { + if ((Z_TYPE(object) == IS_ARRAY && TWIG_ARRAY_KEY_EXISTS(item, object)) + || (TWIG_INSTANCE_OF(object, "ArrayAccess") && TWIG_ISSET_ARRAY_ELEMENT(object, item)) + ) { + if (isDefinedTest) { + RETURN_TRUE; + } + + TWIG_RETURN_ARRAY_ELEMENT(object, item); + } /* if (Twig_TemplateInterface::ARRAY_CALL === $type) { if ($isDefinedTest) { @@ -151,7 +161,15 @@ PHP_FUNCTION(twig_template_get_attributes) if (!$this->env->isStrictVariables()) { return null; } - +*/ + if (strcmp("array", type) == 0) { + if (isDefinedTest) { + RETURN_FALSE; + } + if (!TWIG_CALL(TWIG_BOOLEAN, TWIG_PROPERTY(getThis(), "env"), "isStrictVariables")) { + RETURN_NULL; + } +/* if (is_object($object)) { throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $item, get_class($object))); // array @@ -161,12 +179,24 @@ PHP_FUNCTION(twig_template_get_attributes) } } */ + if (Z_TYPE_P(object) == IS_OBJECT) { + TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Key \"%s\" in object (with ArrayAccess) of type \"%s\" does not exist", item, TWIG_GET_CLASS(object)); + } else { + TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Key \"%s\" for array with keys \"%s\" does not exist", item, TWIG_IMPLODE(", ", TWIG_ARRAY_KEYS(object))); + } + } + } + /* if (!is_object($object)) { if ($isDefinedTest) { return false; } */ + if (!Z_TYPE_P(object) == IS_OBJECT) { + if (isDefinedTest) { + RETURN_FALSE; + } /* if (!$this->env->isStrictVariables()) { return null; @@ -174,6 +204,11 @@ PHP_FUNCTION(twig_template_get_attributes) throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, $object)); } */ + if (!TWIG_CALL(TWIG_BOOLEAN, TWIG_PROPERTY(getThis(), "env"), "isStrictVariables")) { + RETURN_FALSE; + } + TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Item \"%s\" for \"%s\" does not exist", item, object); + } /* // get some information about the object $class = get_class($object); @@ -206,6 +241,20 @@ PHP_FUNCTION(twig_template_get_attributes) } } */ + if (strcmp("method", type) != 0) { + if (TWIG_ISSET("self::$cache[$class]['properties'][$item]") + || TWIG_ISSET(object, item) || TWIG_ARRAY_KEY_EXISTS(item, object) + ) { + if (isDefinedTest) { + RETURN_TRUE; + } + if (TWIG_CALL(TWIG_PROPERTY(getThis(), "env"), "hasExtension", "sandbox")) { + TWIG_CALL(TWIG_CALL(TWIG_PROPERTY(getThis(), "env"), "getExtension", "sandbox"), "checkPropertyAllowed", object, item); + } + + TWIG_RETURN_OBJPROP_ELEMENT(object, item); + } + } /* // object method $lcItem = strtolower($item); @@ -217,6 +266,18 @@ PHP_FUNCTION(twig_template_get_attributes) $method = 'is'.$item; } elseif (isset(self::$cache[$class]['methods']['__call'])) { $method = $item; +*/ + char *lsItem = TWIG_STRTOLOWER(item); + char *method = NULL; + if (TWIG_ISSET(TWIG_SELF, "cache", "class", "methods", "lcItem")) { + method = item; + } else if (TWIG_ISSET(TWIG_SELF, "cache", "class", "methods", "get" lcItem)) { + method = "get" item; + } else if (TWIG_ISSET(TWIG_SELF, "cache", "class", "methods", "is" lcItem)) { + method = "is" item; + } else if (TWIG_ISSET(TWIG_SELF, "cache", "class", "methods", "__call")) { + method = item; +/* } else { if ($isDefinedTest) { return false; @@ -230,20 +291,41 @@ PHP_FUNCTION(twig_template_get_attributes) return true; } */ + } else { + if (isDefinedTest) { + RETURN_FALSE; + } + if (TWIG_CALL(TWIG_PROPERTY(getThis(), "env", "isStrictVariables"))) { + RETURN_NULL; + } + TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Method \"%s\" for object \"%s\" does not exist", item, TWIG_GET_CLASS(object)); + } + if (isDefinedTest) { + RETURN_FALSE; + } /* if ($this->env->hasExtension('sandbox')) { $this->env->getExtension('sandbox')->checkMethodAllowed($object, $method); } */ + if (TWIG_CALL(TWIG_PROPERTY(getThis(), "env"), "hasExtension", "sandbox")) { + TWIG_CALL(TWIG_CALL(TWIG_PROPERTY(getThis(), "env"), "getExtension", "sandbox"), "checkMethodAllowed", object, method); + } /* $ret = call_user_func_array(array($object, $method), $arguments); */ + zval *ret = TWIG_CALL_USER_FUNC_ARRAY(object, method, arguments); /* if ($object instanceof Twig_TemplateInterface) { return new Twig_Markup($ret); } */ + if (TWIG_INSTANCE_OF(object, "Twig_TemplateInterface")) { + zval *retval = TWIG_NEW("Twig_Markup", ret); + TWIG_RETURN(retval); + } /* return $ret; */ + TWIG_REUTNR(ret); } From fad8e9a7b3e37fa6db81bffc2d0e75ef0b8f6253 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Mon, 6 Jun 2011 16:17:45 +0100 Subject: [PATCH 08/73] - Fixed ARG INFO --- twig.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/twig.c b/twig.c index 566198ecc..a5c0a9f76 100644 --- a/twig.c +++ b/twig.c @@ -25,14 +25,13 @@ #include "ext/standard/info.h" #include "php_twig.h" -ZEND_BEGIN_ARG_INFO_EX(twig_template_get_attribute_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 7) +ZEND_BEGIN_ARG_INFO_EX(twig_template_get_attribute_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 6) ZEND_ARG_INFO(0, template) ZEND_ARG_INFO(0, object) ZEND_ARG_INFO(0, item) ZEND_ARG_INFO(0, arguments) ZEND_ARG_INFO(0, type) - ZEND_ARG_INFO(0, noStrictCheck) - ZEND_ARG_INFO(0, line) + ZEND_ARG_INFO(0, isDefinedTest) ZEND_END_ARG_INFO() zend_function_entry twig_functions[] = { From a2f3c34ba5a5cc8432d8911f36df4c1d17c0eaee Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Tue, 7 Jun 2011 08:59:57 +0100 Subject: [PATCH 09/73] - Added more files to .gitignore --- .gitignore | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/.gitignore b/.gitignore index 1ee84da98..1615930bb 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,26 @@ *.sw* +.deps +Makefile +Makefile.fragments +Makefile.global +Makefile.objects +acinclude.m4 +aclocal.m4 +build/ +config.cache +config.guess +config.h +config.h.in +config.log +config.nice +config.status +config.sub +configure +configure.in +install-sh +libtool +ltmain.sh +missing +mkinstalldirs +run-tests.php +twig.loT From 4de3659c9b57bda159553da3b334062684c2495e Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Tue, 7 Jun 2011 09:22:58 +0100 Subject: [PATCH 10/73] - Reduce compile warnings and errors. - Initialize the two method names. --- twig.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/twig.c b/twig.c index a5c0a9f76..0a53c746a 100644 --- a/twig.c +++ b/twig.c @@ -126,7 +126,7 @@ PHP_FUNCTION(twig_template_get_attributes) zend_bool isDefinedTest = 0; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ozzasbl", &template, &object, &item, &arguments, &type, &type_len, &isDefinedTest) == FAILURE) { - return NULL; + return; } /* @@ -143,7 +143,7 @@ PHP_FUNCTION(twig_template_get_attributes) } */ if (strcmp("method", type) == 0) { - if ((Z_TYPE(object) == IS_ARRAY && TWIG_ARRAY_KEY_EXISTS(item, object)) + if ((Z_TYPE_P(object) == IS_ARRAY && TWIG_ARRAY_KEY_EXISTS(item, object)) || (TWIG_INSTANCE_OF(object, "ArrayAccess") && TWIG_ISSET_ARRAY_ELEMENT(object, item)) ) { if (isDefinedTest) { @@ -166,7 +166,7 @@ PHP_FUNCTION(twig_template_get_attributes) RETURN_FALSE; } if (!TWIG_CALL(TWIG_BOOLEAN, TWIG_PROPERTY(getThis(), "env"), "isStrictVariables")) { - RETURN_NULL; + return; } /* if (is_object($object)) { @@ -268,14 +268,21 @@ PHP_FUNCTION(twig_template_get_attributes) */ char *lsItem = TWIG_STRTOLOWER(item); char *method = NULL; + char *tmp_method_name_get = emalloc(4 + strlen(lsItem)); + char *tmp_method_name_is = emalloc(3 + strlen(lsItem)); + + sprintf(tmp_method_name_get, "get%s", lsItem); + sprintf(tmp_method_name_is, "is%s", lsItem); + efree(lsItem); + if (TWIG_ISSET(TWIG_SELF, "cache", "class", "methods", "lcItem")) { - method = item; - } else if (TWIG_ISSET(TWIG_SELF, "cache", "class", "methods", "get" lcItem)) { - method = "get" item; - } else if (TWIG_ISSET(TWIG_SELF, "cache", "class", "methods", "is" lcItem)) { - method = "is" item; + method = Z_STRVAL_P(item); + } else if (TWIG_ISSET(TWIG_SELF, "cache", "class", "methods", tmp_method_name_get)) { + method = tmp_method_name_get; + } else if (TWIG_ISSET(TWIG_SELF, "cache", "class", "methods", tmp_method_name_is)) { + method = tmp_method_name_is; } else if (TWIG_ISSET(TWIG_SELF, "cache", "class", "methods", "__call")) { - method = item; + method = Z_STRVAL_P(item); /* } else { if ($isDefinedTest) { @@ -295,11 +302,13 @@ PHP_FUNCTION(twig_template_get_attributes) RETURN_FALSE; } if (TWIG_CALL(TWIG_PROPERTY(getThis(), "env", "isStrictVariables"))) { - RETURN_NULL; + return; } TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Method \"%s\" for object \"%s\" does not exist", item, TWIG_GET_CLASS(object)); } if (isDefinedTest) { + efree(tmp_method_name_get); + efree(tmp_method_name_is); RETURN_FALSE; } /* @@ -314,6 +323,8 @@ PHP_FUNCTION(twig_template_get_attributes) $ret = call_user_func_array(array($object, $method), $arguments); */ zval *ret = TWIG_CALL_USER_FUNC_ARRAY(object, method, arguments); + efree(tmp_method_name_get); + efree(tmp_method_name_is); /* if ($object instanceof Twig_TemplateInterface) { return new Twig_Markup($ret); From 19fbee7ef66b97f34dc4bdd147ceffb96607426e Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Tue, 7 Jun 2011 09:29:02 +0100 Subject: [PATCH 11/73] - Implemented TWIG_ARRAY_KEY_EXISTS (for normal arrays only). --- twig.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/twig.c b/twig.c index 0a53c746a..27ad3acca 100644 --- a/twig.c +++ b/twig.c @@ -113,6 +113,20 @@ PHP_MINFO_FUNCTION(twig) } +int TWIG_ARRAY_KEY_EXISTS(zval *array, zval *key) +{ + void *dummy; + + if (Z_TYPE_P(array) != IS_ARRAY) { + return 0; + } + convert_to_string(key); + if (zend_hash_find(Z_ARRVAL_P(array), Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, &dummy) == SUCCESS) { + return 1; + } + return 0; +} + /* {{{ proto mixed twig_template_get_attributes(TwigTemplate template, mixed object, mixed item, array arguments, string type, boolean isDefinedTest) A C implementation of TwigTemplate::getAttribute() */ PHP_FUNCTION(twig_template_get_attributes) @@ -143,7 +157,7 @@ PHP_FUNCTION(twig_template_get_attributes) } */ if (strcmp("method", type) == 0) { - if ((Z_TYPE_P(object) == IS_ARRAY && TWIG_ARRAY_KEY_EXISTS(item, object)) + if ((TWIG_ARRAY_KEY_EXISTS(object, item)) || (TWIG_INSTANCE_OF(object, "ArrayAccess") && TWIG_ISSET_ARRAY_ELEMENT(object, item)) ) { if (isDefinedTest) { @@ -242,7 +256,7 @@ PHP_FUNCTION(twig_template_get_attributes) */ if (strcmp("method", type) != 0) { if (TWIG_ISSET("self::$cache[$class]['properties'][$item]") - || TWIG_ISSET(object, item) || TWIG_ARRAY_KEY_EXISTS(item, object) + || TWIG_ISSET(object, item) || TWIG_ARRAY_KEY_EXISTS(object, item) // FIXME: Array key? is that array access here? ) { if (isDefinedTest) { RETURN_TRUE; From 79f6dc6efc9a43c7d1eef78438883bb803a7bd9f Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Tue, 7 Jun 2011 10:42:17 +0100 Subject: [PATCH 12/73] - Implement TWIG_INSTANCEOF_* --- twig.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/twig.c b/twig.c index 27ad3acca..571a779f7 100644 --- a/twig.c +++ b/twig.c @@ -25,6 +25,8 @@ #include "ext/standard/info.h" #include "php_twig.h" +#include "Zend/zend_interfaces.h" + ZEND_BEGIN_ARG_INFO_EX(twig_template_get_attribute_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 6) ZEND_ARG_INFO(0, template) ZEND_ARG_INFO(0, object) @@ -127,6 +129,26 @@ int TWIG_ARRAY_KEY_EXISTS(zval *array, zval *key) return 0; } +int TWIG_INSTANCE_OF(zval *object, zend_class_entry *interface TSRMLS_DC) +{ + if (Z_TYPE_P(object) != IS_OBJECT) { + return 0; + } + return instanceof_function(Z_OBJCE_P(object), interface TSRMLS_CC); +} + +int TWIG_INSTANCE_OF_USERLAND(zval *object, char *interface TSRMLS_DC) +{ + zend_class_entry **pce; + if (Z_TYPE_P(object) != IS_OBJECT) { + return 0; + } + if (zend_lookup_class(interface, strlen(interface), &pce TSRMLS_CC) == FAILURE) { + return 0; + } + return instanceof_function(Z_OBJCE_P(object), *pce TSRMLS_CC); +} + /* {{{ proto mixed twig_template_get_attributes(TwigTemplate template, mixed object, mixed item, array arguments, string type, boolean isDefinedTest) A C implementation of TwigTemplate::getAttribute() */ PHP_FUNCTION(twig_template_get_attributes) @@ -158,7 +180,7 @@ PHP_FUNCTION(twig_template_get_attributes) */ if (strcmp("method", type) == 0) { if ((TWIG_ARRAY_KEY_EXISTS(object, item)) - || (TWIG_INSTANCE_OF(object, "ArrayAccess") && TWIG_ISSET_ARRAY_ELEMENT(object, item)) + || (TWIG_INSTANCE_OF(object, zend_ce_arrayaccess) && TWIG_ISSET_ARRAY_ELEMENT(object, item)) ) { if (isDefinedTest) { RETURN_TRUE; @@ -344,7 +366,7 @@ PHP_FUNCTION(twig_template_get_attributes) return new Twig_Markup($ret); } */ - if (TWIG_INSTANCE_OF(object, "Twig_TemplateInterface")) { + if (TWIG_INSTANCE_OF_USERLAND(object, "Twig_TemplateInterface")) { zval *retval = TWIG_NEW("Twig_Markup", ret); TWIG_RETURN(retval); } From 6aa1a64c2b0ebca9de72ec78e044cb1d244762e3 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Tue, 7 Jun 2011 14:57:28 +0100 Subject: [PATCH 13/73] - Stubs and more implementations. --- twig.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 83 insertions(+), 10 deletions(-) diff --git a/twig.c b/twig.c index 571a779f7..715c37a07 100644 --- a/twig.c +++ b/twig.c @@ -149,6 +149,75 @@ int TWIG_INSTANCE_OF_USERLAND(zval *object, char *interface TSRMLS_DC) return instanceof_function(Z_OBJCE_P(object), *pce TSRMLS_CC); } +int TWIG_ISSET_ARRAY_ELEMENT(zval *array, zval *item) +{ + return 0; +} + +zval *TWIG_RETURN_ARRAY_ELEMENT(zval *object, zval *item) +{ + +} + +zval *TWIG_PROPERTY(zval *object, char *propname) +{ +} + +int TWIG_CALL_BOOLEAN(zval *property, char *functionName) +{ + +} + +char *TWIG_STRTOLOWER(zval *item) +{ +} + +zval *TWIG_CALL_USER_FUNC_ARRAY(zval *object, char *function, zval *arguments) +{ + zend_fcall_info fci; + zval ***args; + zval *retval; + HashTable *table = HASH_OF(arguments); + HashPosition pos; + int i = 0; + + zval *zfunction; + + args = safe_emalloc(sizeof(zval **), table->nNumOfElements, 0); + + zend_hash_internal_pointer_reset_ex(table, &pos); + + while (zend_hash_get_current_data_ex(table, (void **)&args[i], &pos) == SUCCESS) { + i++; + zend_hash_move_forward_ex(table, &pos); + } + + fci.size = sizeof(fci); + fci.function_table = EG(function_table); + fci.function_name = zfunction; + fci.symbol_table = NULL; + fci.object_ptr = object; + fci.retval_ptr_ptr = &retval; + fci.param_count = table->nNumOfElements; + fci.params = args; + fci.no_separation = 0; + +} + +void TWIG_NEW(zval *object, char *class, zval *value) +{ + zend_class_entry **pce; + + if (zend_lookup_class(class, strlen(class), &pce TSRMLS_CC) == FAILURE) { + return; + } + + Z_TYPE_P(object) = IS_OBJECT; + object_init_ex(object, *pce); + Z_SET_REFCOUNT_P(object, 1); + Z_UNSET_ISREF_P(object); +} + /* {{{ proto mixed twig_template_get_attributes(TwigTemplate template, mixed object, mixed item, array arguments, string type, boolean isDefinedTest) A C implementation of TwigTemplate::getAttribute() */ PHP_FUNCTION(twig_template_get_attributes) @@ -201,7 +270,7 @@ PHP_FUNCTION(twig_template_get_attributes) if (isDefinedTest) { RETURN_FALSE; } - if (!TWIG_CALL(TWIG_BOOLEAN, TWIG_PROPERTY(getThis(), "env"), "isStrictVariables")) { + if (!TWIG_CALL_BOOLEAN(TWIG_PROPERTY(getThis(), "env"), "isStrictVariables")) { return; } /* @@ -239,7 +308,7 @@ PHP_FUNCTION(twig_template_get_attributes) throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, $object)); } */ - if (!TWIG_CALL(TWIG_BOOLEAN, TWIG_PROPERTY(getThis(), "env"), "isStrictVariables")) { + if (!TWIG_CALL_BOOLEAN(TWIG_PROPERTY(getThis(), "env"), "isStrictVariables")) { RETURN_FALSE; } TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Item \"%s\" for \"%s\" does not exist", item, object); @@ -311,13 +380,17 @@ PHP_FUNCTION(twig_template_get_attributes) sprintf(tmp_method_name_is, "is%s", lsItem); efree(lsItem); - if (TWIG_ISSET(TWIG_SELF, "cache", "class", "methods", "lcItem")) { + zval *cache = TWIG_PROPERTY(getThis(), "cache"); + zval *class = TWIG_PROPERTY(cache, "class"); + zval *methods = TWIG_PROPERTY(class, "methods"); + + if (TWIG_ISSET(TWIG_PROPERTY(methods, "lcItem"))) { method = Z_STRVAL_P(item); - } else if (TWIG_ISSET(TWIG_SELF, "cache", "class", "methods", tmp_method_name_get)) { + } else if (TWIG_PROPERTY(methods, tmp_method_name_get)) { method = tmp_method_name_get; - } else if (TWIG_ISSET(TWIG_SELF, "cache", "class", "methods", tmp_method_name_is)) { + } else if (TWIG_PROPERTY(methods, tmp_method_name_is)) { method = tmp_method_name_is; - } else if (TWIG_ISSET(TWIG_SELF, "cache", "class", "methods", "__call")) { + } else if (TWIG_PROPERTY(methods, "__call")) { method = Z_STRVAL_P(item); /* } else { @@ -337,7 +410,8 @@ PHP_FUNCTION(twig_template_get_attributes) if (isDefinedTest) { RETURN_FALSE; } - if (TWIG_CALL(TWIG_PROPERTY(getThis(), "env", "isStrictVariables"))) { + zval *env = TWIG_PROPERTY(getThis(), "env"); + if (TWIG_CALL(TWIG_PROPERTY(env, "isStrictVariables"))) { return; } TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Method \"%s\" for object \"%s\" does not exist", item, TWIG_GET_CLASS(object)); @@ -367,11 +441,10 @@ PHP_FUNCTION(twig_template_get_attributes) } */ if (TWIG_INSTANCE_OF_USERLAND(object, "Twig_TemplateInterface")) { - zval *retval = TWIG_NEW("Twig_Markup", ret); - TWIG_RETURN(retval); + TWIG_NEW(return_value, "Twig_Markup", ret); } /* return $ret; */ - TWIG_REUTNR(ret); + TWIG_RETURN(ret); } From 07e38efa703f0d1caa758be88713a419d527a69a Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Tue, 7 Jun 2011 14:57:54 +0100 Subject: [PATCH 14/73] - Added more generated files to .gitignore --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 1615930bb..56ea76cc2 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,7 @@ missing mkinstalldirs run-tests.php twig.loT +.libs/ +modules/ +twig.la +twig.lo From d08224457f09dc0e6d785b57076c877b25acef40 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Wed, 8 Jun 2011 13:26:48 +0100 Subject: [PATCH 15/73] - Used the template, instead of getThis(), which wouldn't exist. --- twig.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/twig.c b/twig.c index 715c37a07..aa92723fc 100644 --- a/twig.c +++ b/twig.c @@ -270,7 +270,7 @@ PHP_FUNCTION(twig_template_get_attributes) if (isDefinedTest) { RETURN_FALSE; } - if (!TWIG_CALL_BOOLEAN(TWIG_PROPERTY(getThis(), "env"), "isStrictVariables")) { + if (!TWIG_CALL_BOOLEAN(TWIG_PROPERTY(template, "env"), "isStrictVariables")) { return; } /* @@ -308,7 +308,7 @@ PHP_FUNCTION(twig_template_get_attributes) throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, $object)); } */ - if (!TWIG_CALL_BOOLEAN(TWIG_PROPERTY(getThis(), "env"), "isStrictVariables")) { + if (!TWIG_CALL_BOOLEAN(TWIG_PROPERTY(template, "env"), "isStrictVariables")) { RETURN_FALSE; } TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Item \"%s\" for \"%s\" does not exist", item, object); @@ -352,8 +352,8 @@ PHP_FUNCTION(twig_template_get_attributes) if (isDefinedTest) { RETURN_TRUE; } - if (TWIG_CALL(TWIG_PROPERTY(getThis(), "env"), "hasExtension", "sandbox")) { - TWIG_CALL(TWIG_CALL(TWIG_PROPERTY(getThis(), "env"), "getExtension", "sandbox"), "checkPropertyAllowed", object, item); + if (TWIG_CALL(TWIG_PROPERTY(template, "env"), "hasExtension", "sandbox")) { + TWIG_CALL(TWIG_CALL(TWIG_PROPERTY(template, "env"), "getExtension", "sandbox"), "checkPropertyAllowed", object, item); } TWIG_RETURN_OBJPROP_ELEMENT(object, item); @@ -380,7 +380,7 @@ PHP_FUNCTION(twig_template_get_attributes) sprintf(tmp_method_name_is, "is%s", lsItem); efree(lsItem); - zval *cache = TWIG_PROPERTY(getThis(), "cache"); + zval *cache = TWIG_PROPERTY(template, "cache"); zval *class = TWIG_PROPERTY(cache, "class"); zval *methods = TWIG_PROPERTY(class, "methods"); @@ -410,7 +410,7 @@ PHP_FUNCTION(twig_template_get_attributes) if (isDefinedTest) { RETURN_FALSE; } - zval *env = TWIG_PROPERTY(getThis(), "env"); + zval *env = TWIG_PROPERTY(template, "env"); if (TWIG_CALL(TWIG_PROPERTY(env, "isStrictVariables"))) { return; } @@ -426,8 +426,8 @@ PHP_FUNCTION(twig_template_get_attributes) $this->env->getExtension('sandbox')->checkMethodAllowed($object, $method); } */ - if (TWIG_CALL(TWIG_PROPERTY(getThis(), "env"), "hasExtension", "sandbox")) { - TWIG_CALL(TWIG_CALL(TWIG_PROPERTY(getThis(), "env"), "getExtension", "sandbox"), "checkMethodAllowed", object, method); + if (TWIG_CALL(TWIG_PROPERTY(template, "env"), "hasExtension", "sandbox")) { + TWIG_CALL(TWIG_CALL(TWIG_PROPERTY(template, "env"), "getExtension", "sandbox"), "checkMethodAllowed", object, method); } /* $ret = call_user_func_array(array($object, $method), $arguments); From 1b96539ecb069fb3ff20a0c5a819815b6c18a4bf Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Wed, 8 Jun 2011 13:28:48 +0100 Subject: [PATCH 16/73] - The parse parameters line was wrong, it still had the extra one that was removed in Twig itself already. --- twig.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/twig.c b/twig.c index aa92723fc..838c53a62 100644 --- a/twig.c +++ b/twig.c @@ -230,7 +230,7 @@ PHP_FUNCTION(twig_template_get_attributes) int type_len = 0; zend_bool isDefinedTest = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ozzasbl", &template, &object, &item, &arguments, &type, &type_len, &isDefinedTest) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ozzasb", &template, &object, &item, &arguments, &type, &type_len, &isDefinedTest) == FAILURE) { return; } From e133074da18c6d373e62a35457bbd3d398e38c2b Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Wed, 8 Jun 2011 16:24:53 +0100 Subject: [PATCH 17/73] - The extension now loads and the pseudo code is mostly ready. --- twig.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 10 deletions(-) diff --git a/twig.c b/twig.c index 838c53a62..e1b3f84ee 100644 --- a/twig.c +++ b/twig.c @@ -201,7 +201,30 @@ zval *TWIG_CALL_USER_FUNC_ARRAY(zval *object, char *function, zval *arguments) fci.param_count = table->nNumOfElements; fci.params = args; fci.no_separation = 0; +} +zval *TWIG_GET_STATIC_PROPERTY(zval *class, char *prop_name) +{ +} + +zval *TWIG_GET_ARRAY_ELEMENT_ZVAL(zval *class, zval *prop_name) +{ +} + +zval *TWIG_GET_ARRAY_ELEMENT(zval *class, char *prop_name) +{ +} + +int TWIG_CALL_B_0(zval *object, char *method) +{ +} + +zval *TWIG_CALL_S(zval *object, char *method, char *arg0) +{ +} + +zval *TWIG_CALL_ZZ(zval *object, char *method, zval *arg1, zval *arg2) +{ } void TWIG_NEW(zval *object, char *class, zval *value) @@ -328,6 +351,10 @@ PHP_FUNCTION(twig_template_get_attributes) } } */ + char *class_name; + zend_uint class_name_len; + + zend_get_object_classname(object, &class_name, &class_name_len TSRMLS_CC); /* // object property if (Twig_TemplateInterface::METHOD_CALL !== $type) { @@ -346,17 +373,27 @@ PHP_FUNCTION(twig_template_get_attributes) } */ if (strcmp("method", type) != 0) { - if (TWIG_ISSET("self::$cache[$class]['properties'][$item]") - || TWIG_ISSET(object, item) || TWIG_ARRAY_KEY_EXISTS(object, item) // FIXME: Array key? is that array access here? + zval *tmp_self_cache, *tmp_class, *tmp_properties, *tmp_item, *tmp_object_item; + + tmp_self_cache = TWIG_GET_STATIC_PROPERTY(template, "cache"); + tmp_class = TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name); + tmp_properties = TWIG_GET_ARRAY_ELEMENT(tmp_class, "properties"); + tmp_item = TWIG_GET_ARRAY_ELEMENT_ZVAL(tmp_properties, item); + convert_to_string(tmp_item); + + tmp_object_item = TWIG_PROPERTY(object, Z_STRVAL_P(tmp_item)); + + if (tmp_item || tmp_object_item || TWIG_ARRAY_KEY_EXISTS(object, item) // FIXME: Array key? is that array access here? ) { if (isDefinedTest) { RETURN_TRUE; } - if (TWIG_CALL(TWIG_PROPERTY(template, "env"), "hasExtension", "sandbox")) { - TWIG_CALL(TWIG_CALL(TWIG_PROPERTY(template, "env"), "getExtension", "sandbox"), "checkPropertyAllowed", object, item); + if (TWIG_CALL_S(TWIG_PROPERTY(template, "env"), "hasExtension", "sandbox")) { + TWIG_CALL_ZZ(TWIG_CALL_S(TWIG_PROPERTY(template, "env"), "getExtension", "sandbox"), "checkPropertyAllowed", object, item); } - TWIG_RETURN_OBJPROP_ELEMENT(object, item); + convert_to_string(item); + return_value = TWIG_PROPERTY(object, Z_STRVAL_P(item)); // this is obviously wrong } } /* @@ -384,7 +421,7 @@ PHP_FUNCTION(twig_template_get_attributes) zval *class = TWIG_PROPERTY(cache, "class"); zval *methods = TWIG_PROPERTY(class, "methods"); - if (TWIG_ISSET(TWIG_PROPERTY(methods, "lcItem"))) { + if (TWIG_PROPERTY(methods, "lcItem")) { method = Z_STRVAL_P(item); } else if (TWIG_PROPERTY(methods, tmp_method_name_get)) { method = tmp_method_name_get; @@ -411,7 +448,7 @@ PHP_FUNCTION(twig_template_get_attributes) RETURN_FALSE; } zval *env = TWIG_PROPERTY(template, "env"); - if (TWIG_CALL(TWIG_PROPERTY(env, "isStrictVariables"))) { + if (TWIG_CALL_B_0(env, "isStrictVariables")) { return; } TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Method \"%s\" for object \"%s\" does not exist", item, TWIG_GET_CLASS(object)); @@ -426,8 +463,8 @@ PHP_FUNCTION(twig_template_get_attributes) $this->env->getExtension('sandbox')->checkMethodAllowed($object, $method); } */ - if (TWIG_CALL(TWIG_PROPERTY(template, "env"), "hasExtension", "sandbox")) { - TWIG_CALL(TWIG_CALL(TWIG_PROPERTY(template, "env"), "getExtension", "sandbox"), "checkMethodAllowed", object, method); + if (TWIG_CALL_S(TWIG_PROPERTY(template, "env"), "hasExtension", "sandbox")) { + TWIG_CALL_ZZ(TWIG_CALL_S(TWIG_PROPERTY(template, "env"), "getExtension", "sandbox"), "checkMethodAllowed", object, item); } /* $ret = call_user_func_array(array($object, $method), $arguments); @@ -446,5 +483,5 @@ PHP_FUNCTION(twig_template_get_attributes) /* return $ret; */ - TWIG_RETURN(ret); + return_value = ret; // FIXME also wrong } From 2078e50de1c4c2e336f1d9000f4fc08b4d7adcab Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Wed, 29 Jun 2011 12:42:34 +0100 Subject: [PATCH 18/73] - Implemented the strtolower function. --- twig.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/twig.c b/twig.c index e1b3f84ee..6e0dc83d2 100644 --- a/twig.c +++ b/twig.c @@ -168,8 +168,16 @@ int TWIG_CALL_BOOLEAN(zval *property, char *functionName) } -char *TWIG_STRTOLOWER(zval *item) +char *TWIG_STRTOLOWER_ZVAL(zval *item) { + char *item_dup; + + if (Z_TYPE_P(item) != IS_STRING) { + return NULL; + } + item_dup = estrndup(Z_STRVAL_P(item), Z_STRLEN_P(item)); + php_strtolower(item_dup, Z_STRLEN_P(item)); + return item_dup; } zval *TWIG_CALL_USER_FUNC_ARRAY(zval *object, char *function, zval *arguments) From 9d40c9f8c89abfd6d4cf24a602eb3c575acaab21 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Wed, 29 Jun 2011 12:44:10 +0100 Subject: [PATCH 19/73] Implemented TWIG_CALL_USER_FUNC_ARRAY, TWIG_GET_STATIC_PROPERTY, TWIG_GET_ARRAY_ELEMENT_ZVAL and TWIG_GET_ARRAY_ELEMENT. --- twig.c | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/twig.c b/twig.c index 6e0dc83d2..37ea8d00e 100644 --- a/twig.c +++ b/twig.c @@ -184,11 +184,10 @@ zval *TWIG_CALL_USER_FUNC_ARRAY(zval *object, char *function, zval *arguments) { zend_fcall_info fci; zval ***args; - zval *retval; HashTable *table = HASH_OF(arguments); HashPosition pos; int i = 0; - + zval *retval_ptr; zval *zfunction; args = safe_emalloc(sizeof(zval **), table->nNumOfElements, 0); @@ -200,27 +199,67 @@ zval *TWIG_CALL_USER_FUNC_ARRAY(zval *object, char *function, zval *arguments) zend_hash_move_forward_ex(table, &pos); } + MAKE_STD_ZVAL(zfunction); + ZVAL_STRING(zfunction, function, 0); fci.size = sizeof(fci); fci.function_table = EG(function_table); fci.function_name = zfunction; fci.symbol_table = NULL; fci.object_ptr = object; - fci.retval_ptr_ptr = &retval; + fci.retval_ptr_ptr = &retval_ptr; fci.param_count = table->nNumOfElements; fci.params = args; fci.no_separation = 0; + + if (zend_call_function(&fci, NULL TSRMLS_CC) == FAILURE) { + zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Could not execute %s::%s()", zend_get_class_entry(object)->name, function); + } + efree(fci.params); + return retval_ptr; } zval *TWIG_GET_STATIC_PROPERTY(zval *class, char *prop_name) { + zval **tmp_zval; + zend_class_entry *ce; + + if (class == NULL || Z_TYPE_P(class) != IS_OBJECT) { + return NULL; + } + + ce = zend_get_class_entry(class); + tmp_zval = zend_std_get_static_property(ce, prop_name, strlen(prop_name), 0 TSRMLS_CC); + return *tmp_zval; } zval *TWIG_GET_ARRAY_ELEMENT_ZVAL(zval *class, zval *prop_name) { + zval *tmp_zval; + char *tmp_name; + + if (class == NULL || Z_TYPE_P(class) != IS_ARRAY || Z_TYPE_P(prop_name) != IS_STRING) { + return NULL; + } + tmp_name = Z_STRVAL_P(prop_name); + + if (zend_hash_find(HASH_OF(class), tmp_name, strlen(tmp_name)+1, &tmp_zval) == SUCCESS) { + return tmp_zval; + } + return NULL; } zval *TWIG_GET_ARRAY_ELEMENT(zval *class, char *prop_name) { + zval **tmp_zval; + + if (class == NULL || Z_TYPE_P(class) != IS_ARRAY) { + return NULL; + } + + if (zend_hash_find(HASH_OF(class), prop_name, strlen(prop_name)+1, (void**)&tmp_zval) == SUCCESS) { + return *tmp_zval; + } + return NULL; } int TWIG_CALL_B_0(zval *object, char *method) From 7367cf942b995c9098caa21852af0993b216c554 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Wed, 29 Jun 2011 12:46:00 +0100 Subject: [PATCH 20/73] Added the cache fetching method for methods and properties. --- twig.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 4 deletions(-) diff --git a/twig.c b/twig.c index 37ea8d00e..1b2033e7c 100644 --- a/twig.c +++ b/twig.c @@ -282,10 +282,80 @@ void TWIG_NEW(zval *object, char *class, zval *value) return; } - Z_TYPE_P(object) = IS_OBJECT; - object_init_ex(object, *pce); - Z_SET_REFCOUNT_P(object, 1); - Z_UNSET_ISREF_P(object); + Z_TYPE_P(object) = IS_OBJECT; + object_init_ex(object, *pce); + Z_SET_REFCOUNT_P(object, 1); + Z_UNSET_ISREF_P(object); +} + +static void twig_add_method_to_class(zend_function *mptr TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) +{ + zval *retval = va_arg(args, zval*); + + add_next_index_string(retval, mptr->common.function_name, 1); +} + +static int twig_add_property_to_class(zend_property_info *pptr TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) +{ + zend_class_entry *ce = *va_arg(args, zend_class_entry**); + zval *retval = va_arg(args, zval*); + char *class_name, *prop_name; + + if (pptr->flags & ZEND_ACC_SHADOW) { + return 0; + } + + zend_unmangle_property_name(pptr->name, pptr->name_length, &class_name, &prop_name); + + add_next_index_string(retval, prop_name, 1); + + return 0; +} + +/* {{{ _adddynproperty */ +static int twig_add_dyn_property_to_class(zval **pptr TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) +{ + zend_class_entry *ce = *va_arg(args, zend_class_entry**); + zval *retval = va_arg(args, zval*), member; + char *class_name, *prop_name; + + if (hash_key->arKey[0] == '\0') { + return 0; /* non public cannot be dynamic */ + } + + ZVAL_STRINGL(&member, hash_key->arKey, hash_key->nKeyLength-1, 0); + if (zend_get_property_info(ce, &member, 1 TSRMLS_CC) == &EG(std_property_info)) { + zend_unmangle_property_name((&EG(std_property_info))->name, (&EG(std_property_info))->name_length, &class_name, &prop_name); + add_next_index_string(retval, prop_name, 1); + } + return 0; +} + +static void twig_add_class_to_cache(zval *cache, zval *object, char *class_name) +{ + zval *tmp_class, *tmp_properties, *tmp_item, *tmp_object_item = NULL; + zval *class_info, *class_methods, *class_properties; + zend_class_entry *class_ce; + + class_ce = zend_get_class_entry(object); + + ALLOC_INIT_ZVAL(class_info); + ALLOC_INIT_ZVAL(class_methods); + ALLOC_INIT_ZVAL(class_properties); + array_init(class_info); + array_init(class_methods); + array_init(class_properties); + // add all methods to self::cache[$class]['methods'] + zend_hash_apply_with_arguments(&class_ce->function_table TSRMLS_CC, (apply_func_args_t) twig_add_method_to_class, 1, class_methods); + zend_hash_apply_with_arguments(&class_ce->properties_info TSRMLS_CC, (apply_func_args_t) twig_add_property_to_class, 2, &class_ce, class_properties); + + if (object && Z_OBJ_HT_P(object)->get_properties) { + HashTable *properties = Z_OBJ_HT_P(object)->get_properties(object TSRMLS_CC); + zend_hash_apply_with_arguments(properties TSRMLS_CC, (apply_func_args_t) twig_add_dyn_property_to_class, 2, &class_ce, class_properties); + } + add_assoc_zval(class_info, "methods", class_methods); + add_assoc_zval(class_info, "properties", class_properties); + add_assoc_zval(cache, class_name, class_info); } /* {{{ proto mixed twig_template_get_attributes(TwigTemplate template, mixed object, mixed item, array arguments, string type, boolean isDefinedTest) From 8bc1f0268be163804a2053407dedd405b038a6cc Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Wed, 29 Jun 2011 12:46:46 +0100 Subject: [PATCH 21/73] Added the cache creating/fetching method in the code. --- twig.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/twig.c b/twig.c index 1b2033e7c..e33e35be5 100644 --- a/twig.c +++ b/twig.c @@ -470,8 +470,15 @@ PHP_FUNCTION(twig_template_get_attributes) */ char *class_name; zend_uint class_name_len; + zval *tmp_self_cache; zend_get_object_classname(object, &class_name, &class_name_len TSRMLS_CC); + tmp_self_cache = TWIG_GET_STATIC_PROPERTY(template, "cache"); + + if (!TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name)) { + twig_add_class_to_cache(tmp_self_cache, object, class_name); + } + /* // object property if (Twig_TemplateInterface::METHOD_CALL !== $type) { From e1663e8d0bdfb3e0284c1fd9dc662cee9f1952e5 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Wed, 29 Jun 2011 12:47:47 +0100 Subject: [PATCH 22/73] Re-scope variables. --- twig.c | 90 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 49 insertions(+), 41 deletions(-) diff --git a/twig.c b/twig.c index e33e35be5..a072ca32e 100644 --- a/twig.c +++ b/twig.c @@ -366,6 +366,7 @@ PHP_FUNCTION(twig_template_get_attributes) zval *object; zval *item; zval *arguments; + zval *ret; char *type = NULL; int type_len = 0; zend_bool isDefinedTest = 0; @@ -437,6 +438,7 @@ PHP_FUNCTION(twig_template_get_attributes) return false; } */ + if (!Z_TYPE_P(object) == IS_OBJECT) { if (isDefinedTest) { RETURN_FALSE; @@ -497,15 +499,16 @@ PHP_FUNCTION(twig_template_get_attributes) } */ if (strcmp("method", type) != 0) { - zval *tmp_self_cache, *tmp_class, *tmp_properties, *tmp_item, *tmp_object_item; + zval *tmp_class, *tmp_properties, *tmp_item, *tmp_object_item = NULL; - tmp_self_cache = TWIG_GET_STATIC_PROPERTY(template, "cache"); tmp_class = TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name); tmp_properties = TWIG_GET_ARRAY_ELEMENT(tmp_class, "properties"); tmp_item = TWIG_GET_ARRAY_ELEMENT_ZVAL(tmp_properties, item); - convert_to_string(tmp_item); - tmp_object_item = TWIG_PROPERTY(object, Z_STRVAL_P(tmp_item)); + if (tmp_item) { + convert_to_string(tmp_item); + tmp_object_item = TWIG_PROPERTY(object, Z_STRVAL_P(tmp_item)); + } if (tmp_item || tmp_object_item || TWIG_ARRAY_KEY_EXISTS(object, item) // FIXME: Array key? is that array access here? ) { @@ -532,27 +535,27 @@ PHP_FUNCTION(twig_template_get_attributes) } elseif (isset(self::$cache[$class]['methods']['__call'])) { $method = $item; */ - char *lsItem = TWIG_STRTOLOWER(item); - char *method = NULL; - char *tmp_method_name_get = emalloc(4 + strlen(lsItem)); - char *tmp_method_name_is = emalloc(3 + strlen(lsItem)); + { + char *lcItem = TWIG_STRTOLOWER_ZVAL(item); + char *method = NULL; + char *tmp_method_name_get = emalloc(4 + strlen(lcItem)); + char *tmp_method_name_is = emalloc(3 + strlen(lcItem)); + zval *tmp_class, *tmp_methods; - sprintf(tmp_method_name_get, "get%s", lsItem); - sprintf(tmp_method_name_is, "is%s", lsItem); - efree(lsItem); + sprintf(tmp_method_name_get, "get%s", lcItem); + sprintf(tmp_method_name_is, "is%s", lcItem); - zval *cache = TWIG_PROPERTY(template, "cache"); - zval *class = TWIG_PROPERTY(cache, "class"); - zval *methods = TWIG_PROPERTY(class, "methods"); + tmp_class = TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name); + tmp_methods = TWIG_GET_ARRAY_ELEMENT(tmp_class, "methods"); - if (TWIG_PROPERTY(methods, "lcItem")) { - method = Z_STRVAL_P(item); - } else if (TWIG_PROPERTY(methods, tmp_method_name_get)) { - method = tmp_method_name_get; - } else if (TWIG_PROPERTY(methods, tmp_method_name_is)) { - method = tmp_method_name_is; - } else if (TWIG_PROPERTY(methods, "__call")) { - method = Z_STRVAL_P(item); + if (TWIG_PROPERTY(tmp_methods, lcItem)) { + method = Z_STRVAL_P(item); + } else if (TWIG_PROPERTY(tmp_methods, tmp_method_name_get)) { + method = tmp_method_name_get; + } else if (TWIG_PROPERTY(tmp_methods, tmp_method_name_is)) { + method = tmp_method_name_is; + } else if (TWIG_PROPERTY(tmp_methods, "__call")) { + method = Z_STRVAL_P(item); /* } else { if ($isDefinedTest) { @@ -567,35 +570,38 @@ PHP_FUNCTION(twig_template_get_attributes) return true; } */ - } else { + } else { + if (isDefinedTest) { + RETURN_FALSE; + } + zval *env = TWIG_PROPERTY(template, "env"); + if (TWIG_CALL_B_0(env, "isStrictVariables")) { + return; + } + TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Method \"%s\" for object \"%s\" does not exist", item, TWIG_GET_CLASS(object)); + } if (isDefinedTest) { + efree(tmp_method_name_get); + efree(tmp_method_name_is); + efree(lcItem); RETURN_FALSE; } - zval *env = TWIG_PROPERTY(template, "env"); - if (TWIG_CALL_B_0(env, "isStrictVariables")) { - return; - } - TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Method \"%s\" for object \"%s\" does not exist", item, TWIG_GET_CLASS(object)); - } - if (isDefinedTest) { - efree(tmp_method_name_get); - efree(tmp_method_name_is); - RETURN_FALSE; - } /* if ($this->env->hasExtension('sandbox')) { $this->env->getExtension('sandbox')->checkMethodAllowed($object, $method); } */ - if (TWIG_CALL_S(TWIG_PROPERTY(template, "env"), "hasExtension", "sandbox")) { - TWIG_CALL_ZZ(TWIG_CALL_S(TWIG_PROPERTY(template, "env"), "getExtension", "sandbox"), "checkMethodAllowed", object, item); - } + if (TWIG_CALL_S(TWIG_PROPERTY(template, "env"), "hasExtension", "sandbox")) { + TWIG_CALL_ZZ(TWIG_CALL_S(TWIG_PROPERTY(template, "env"), "getExtension", "sandbox"), "checkMethodAllowed", object, item); + } /* $ret = call_user_func_array(array($object, $method), $arguments); */ - zval *ret = TWIG_CALL_USER_FUNC_ARRAY(object, method, arguments); - efree(tmp_method_name_get); - efree(tmp_method_name_is); + ret = TWIG_CALL_USER_FUNC_ARRAY(object, method, arguments); + efree(tmp_method_name_get); + efree(tmp_method_name_is); + efree(lcItem); + } /* if ($object instanceof Twig_TemplateInterface) { return new Twig_Markup($ret); @@ -607,5 +613,7 @@ PHP_FUNCTION(twig_template_get_attributes) /* return $ret; */ - return_value = ret; // FIXME also wrong + if (ret) { + RETVAL_ZVAL(ret, 0, 1); + } } From b918f2ea7ba466d52986b553c9417a295a5f7b8d Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 30 Jun 2011 11:25:57 +0100 Subject: [PATCH 23/73] Implemented TWIG_RETURN_ARRAY_ELEMENT. --- twig.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/twig.c b/twig.c index a072ca32e..f365ab7ed 100644 --- a/twig.c +++ b/twig.c @@ -154,9 +154,18 @@ int TWIG_ISSET_ARRAY_ELEMENT(zval *array, zval *item) return 0; } -zval *TWIG_RETURN_ARRAY_ELEMENT(zval *object, zval *item) +zval *TWIG_RETURN_ARRAY_ELEMENT(zval *array, zval *key) { + void *dummy; + if (Z_TYPE_P(array) != IS_ARRAY) { + return NULL; + } + convert_to_string(key); + if (zend_hash_find(Z_ARRVAL_P(array), Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, &dummy) == SUCCESS) { + return dummy; + } + return NULL; } zval *TWIG_PROPERTY(zval *object, char *propname) From 3da24d9fda80bd2c3e8a36ea5498fa1fd1c1fa74 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 30 Jun 2011 11:26:25 +0100 Subject: [PATCH 24/73] Fixed logic and implemented normal array key return. --- twig.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/twig.c b/twig.c index f365ab7ed..21d8ec8c5 100644 --- a/twig.c +++ b/twig.c @@ -397,15 +397,18 @@ PHP_FUNCTION(twig_template_get_attributes) return $object[$item]; } */ - if (strcmp("method", type) == 0) { + if (strcmp("method", type) != 0) { if ((TWIG_ARRAY_KEY_EXISTS(object, item)) || (TWIG_INSTANCE_OF(object, zend_ce_arrayaccess) && TWIG_ISSET_ARRAY_ELEMENT(object, item)) ) { + zval *ret; + if (isDefinedTest) { RETURN_TRUE; } - TWIG_RETURN_ARRAY_ELEMENT(object, item); + ret = TWIG_RETURN_ARRAY_ELEMENT(object, item); + RETURN_ZVAL(ret, 0, 1); } /* if (Twig_TemplateInterface::ARRAY_CALL === $type) { From 701be8bb57df19b3a50c4891f914f6cbf0585586 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 30 Jun 2011 12:15:40 +0100 Subject: [PATCH 25/73] Implemented TWIG_IMPLODE_ARRAY_KEYS. --- twig.c | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/twig.c b/twig.c index 21d8ec8c5..6c56b1bd0 100644 --- a/twig.c +++ b/twig.c @@ -24,6 +24,7 @@ #include "php_ini.h" #include "ext/standard/info.h" #include "php_twig.h" +#include "ext/standard/php_smart_str.h" #include "Zend/zend_interfaces.h" @@ -297,6 +298,42 @@ void TWIG_NEW(zval *object, char *class, zval *value) Z_UNSET_ISREF_P(object); } +static void twig_add_array_key_to_string(zval **zv TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) +{ + smart_str *buf; + char *joiner; + + buf = va_arg(args, smart_str*); + joiner = va_arg(args, char*); + + if (buf->len != 0) { + smart_str_appends(buf, joiner); + } + + if (hash_key->nKeyLength == 0) { + smart_str_append_long(buf, (long) hash_key->h); + } else { + char *key, *tmp_str; + int key_len, tmp_len; + key = php_addcslashes(hash_key->arKey, hash_key->nKeyLength - 1, &key_len, 0, "'\\", 2 TSRMLS_CC); + tmp_str = php_str_to_str_ex(key, key_len, "\0", 1, "' . \"\\0\" . '", 12, &tmp_len, 0, NULL); + + smart_str_appendl(buf, tmp_str, tmp_len); + efree(key); + efree(tmp_str); + } +} + +char *TWIG_IMPLODE_ARRAY_KEYS(char *joiner, zval *array) +{ + smart_str collector = { 0, 0, 0 }; + + zend_hash_apply_with_arguments(HASH_OF(array) TSRMLS_CC, (apply_func_args_t) twig_add_array_key_to_string, 2, &collector, joiner); + smart_str_0(&collector); + + return collector.c; +} + static void twig_add_method_to_class(zend_function *mptr TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) { zval *retval = va_arg(args, zval*); @@ -439,7 +476,7 @@ PHP_FUNCTION(twig_template_get_attributes) if (Z_TYPE_P(object) == IS_OBJECT) { TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Key \"%s\" in object (with ArrayAccess) of type \"%s\" does not exist", item, TWIG_GET_CLASS(object)); } else { - TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Key \"%s\" for array with keys \"%s\" does not exist", item, TWIG_IMPLODE(", ", TWIG_ARRAY_KEYS(object))); + TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Key \"%s\" for array with keys \"%s\" does not exist", item, TWIG_IMPLODE_ARRAY_KEYS(", ", object)); } } } From 0cc719add3a3dd8183671928a368600ce4a97929 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 30 Jun 2011 13:21:57 +0100 Subject: [PATCH 26/73] Implemented TWIG_THROW_EXCEPTION. --- twig.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/twig.c b/twig.c index 6c56b1bd0..c60c0df84 100644 --- a/twig.c +++ b/twig.c @@ -334,6 +334,25 @@ char *TWIG_IMPLODE_ARRAY_KEYS(char *joiner, zval *array) return collector.c; } +static void TWIG_THROW_EXCEPTION(char *exception_name, char *message, ...) +{ + char *buffer; + va_list args; + + zend_class_entry **pce; + + if (zend_lookup_class(exception_name, strlen(exception_name), &pce TSRMLS_CC) == FAILURE) + { + return; + } + + va_start(args, message); + vspprintf(&buffer, 0, message, args); + va_end(args); + + zend_throw_exception_ex(*pce, 0 TSRMLS_CC, buffer); +} + static void twig_add_method_to_class(zend_function *mptr TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) { zval *retval = va_arg(args, zval*); @@ -474,10 +493,11 @@ PHP_FUNCTION(twig_template_get_attributes) } */ if (Z_TYPE_P(object) == IS_OBJECT) { - TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Key \"%s\" in object (with ArrayAccess) of type \"%s\" does not exist", item, TWIG_GET_CLASS(object)); + TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Key \"%s\" in object (with ArrayAccess) of type \"%s\" does not exist", Z_STRVAL_P(item), TWIG_GET_CLASS(object)); } else { - TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Key \"%s\" for array with keys \"%s\" does not exist", item, TWIG_IMPLODE_ARRAY_KEYS(", ", object)); + TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Key \"%s\" for array with keys \"%s\" does not exist", Z_STRVAL_P(item), TWIG_IMPLODE_ARRAY_KEYS(", ", object)); } + return; } } From 828c113fa2af284a25da1aa0ace88a88148205e1 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 30 Jun 2011 14:24:27 +0100 Subject: [PATCH 27/73] Get rid of TWIG_RETURN_ARRAY_ELEMENT and use TWIG_GET_ARRAY_ELEMENT instead. --- twig.c | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/twig.c b/twig.c index c60c0df84..07b7a6446 100644 --- a/twig.c +++ b/twig.c @@ -155,20 +155,6 @@ int TWIG_ISSET_ARRAY_ELEMENT(zval *array, zval *item) return 0; } -zval *TWIG_RETURN_ARRAY_ELEMENT(zval *array, zval *key) -{ - void *dummy; - - if (Z_TYPE_P(array) != IS_ARRAY) { - return NULL; - } - convert_to_string(key); - if (zend_hash_find(Z_ARRVAL_P(array), Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, &dummy) == SUCCESS) { - return dummy; - } - return NULL; -} - zval *TWIG_PROPERTY(zval *object, char *propname) { } @@ -463,7 +449,7 @@ PHP_FUNCTION(twig_template_get_attributes) RETURN_TRUE; } - ret = TWIG_RETURN_ARRAY_ELEMENT(object, item); + ret = TWIG_GET_ARRAY_ELEMENT(object, Z_STRVAL_P(item)); RETURN_ZVAL(ret, 0, 1); } /* From b891ad909a5f4f5dd2948ede32622a622e1732b6 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 30 Jun 2011 14:25:00 +0100 Subject: [PATCH 28/73] Initialise ret to NULL. --- twig.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/twig.c b/twig.c index 07b7a6446..02dfa2b2b 100644 --- a/twig.c +++ b/twig.c @@ -417,7 +417,7 @@ PHP_FUNCTION(twig_template_get_attributes) zval *object; zval *item; zval *arguments; - zval *ret; + zval *ret = NULL; char *type = NULL; int type_len = 0; zend_bool isDefinedTest = 0; From 6e1dd5617281e0857d768f180711f4dc6efb920e Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 30 Jun 2011 14:25:45 +0100 Subject: [PATCH 29/73] Implement TWIG_GET_CLASS_NAME and make use of it. --- twig.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/twig.c b/twig.c index 02dfa2b2b..96b8e5bd1 100644 --- a/twig.c +++ b/twig.c @@ -339,6 +339,18 @@ static void TWIG_THROW_EXCEPTION(char *exception_name, char *message, ...) zend_throw_exception_ex(*pce, 0 TSRMLS_CC, buffer); } +char *TWIG_GET_CLASS_NAME(zval *object) +{ + char *class_name; + zend_uint class_name_len; + + if (Z_TYPE_P(object) != IS_OBJECT) { + return ""; + } + zend_get_object_classname(object, &class_name, &class_name_len TSRMLS_CC); + return class_name; +} + static void twig_add_method_to_class(zend_function *mptr TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) { zval *retval = va_arg(args, zval*); @@ -479,7 +491,7 @@ PHP_FUNCTION(twig_template_get_attributes) } */ if (Z_TYPE_P(object) == IS_OBJECT) { - TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Key \"%s\" in object (with ArrayAccess) of type \"%s\" does not exist", Z_STRVAL_P(item), TWIG_GET_CLASS(object)); + TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Key \"%s\" in object (with ArrayAccess) of type \"%s\" does not exist", Z_STRVAL_P(item), TWIG_GET_CLASS_NAME(object)); } else { TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Key \"%s\" for array with keys \"%s\" does not exist", Z_STRVAL_P(item), TWIG_IMPLODE_ARRAY_KEYS(", ", object)); } @@ -525,11 +537,9 @@ PHP_FUNCTION(twig_template_get_attributes) } } */ - char *class_name; - zend_uint class_name_len; + char *class_name = TWIG_GET_CLASS_NAME(object); zval *tmp_self_cache; - zend_get_object_classname(object, &class_name, &class_name_len TSRMLS_CC); tmp_self_cache = TWIG_GET_STATIC_PROPERTY(template, "cache"); if (!TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name)) { From f7fc1ffed6fcf2a27d93778e66f236dfb3179862 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 30 Jun 2011 17:01:34 +0100 Subject: [PATCH 30/73] Fold TWIG_GET_ARRAY_ELEMENT and TWIG_PROPERTY into one worker function. --- twig.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/twig.c b/twig.c index 96b8e5bd1..3e602eb06 100644 --- a/twig.c +++ b/twig.c @@ -155,10 +155,6 @@ int TWIG_ISSET_ARRAY_ELEMENT(zval *array, zval *item) return 0; } -zval *TWIG_PROPERTY(zval *object, char *propname) -{ -} - int TWIG_CALL_BOOLEAN(zval *property, char *functionName) { @@ -244,20 +240,29 @@ zval *TWIG_GET_ARRAY_ELEMENT_ZVAL(zval *class, zval *prop_name) return NULL; } -zval *TWIG_GET_ARRAY_ELEMENT(zval *class, char *prop_name) +zval *TWIG_GET_ARRAY_ELEMENT(zval *class, char *prop_name, int prop_name_length) { zval **tmp_zval; - if (class == NULL || Z_TYPE_P(class) != IS_ARRAY) { + if (class == NULL/* || Z_TYPE_P(class) != IS_ARRAY*/) { return NULL; } - if (zend_hash_find(HASH_OF(class), prop_name, strlen(prop_name)+1, (void**)&tmp_zval) == SUCCESS) { + if (zend_hash_find(HASH_OF(class), prop_name, prop_name_length+1, (void**)&tmp_zval) == SUCCESS) { return *tmp_zval; } return NULL; } +zval *TWIG_PROPERTY(zval *object, char *propname) +{ + char *prot_name; + int prot_name_length; + + zend_mangle_property_name(&prot_name, &prot_name_length, "*", 1, propname, strlen(propname), 0); + return TWIG_GET_ARRAY_ELEMENT(object, prot_name, prot_name_length); +} + int TWIG_CALL_B_0(zval *object, char *method) { } @@ -461,8 +466,8 @@ PHP_FUNCTION(twig_template_get_attributes) RETURN_TRUE; } - ret = TWIG_GET_ARRAY_ELEMENT(object, Z_STRVAL_P(item)); - RETURN_ZVAL(ret, 0, 1); + ret = TWIG_GET_ARRAY_ELEMENT(object, Z_STRVAL_P(item), Z_STRLEN_P(item)); + RETURN_ZVAL(ret, 1, 0); } /* if (Twig_TemplateInterface::ARRAY_CALL === $type) { @@ -542,7 +547,7 @@ PHP_FUNCTION(twig_template_get_attributes) tmp_self_cache = TWIG_GET_STATIC_PROPERTY(template, "cache"); - if (!TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name)) { + if (!TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name, strlen(class_name))) { twig_add_class_to_cache(tmp_self_cache, object, class_name); } @@ -566,8 +571,8 @@ PHP_FUNCTION(twig_template_get_attributes) if (strcmp("method", type) != 0) { zval *tmp_class, *tmp_properties, *tmp_item, *tmp_object_item = NULL; - tmp_class = TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name); - tmp_properties = TWIG_GET_ARRAY_ELEMENT(tmp_class, "properties"); + tmp_class = TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name, strlen(class_name)); + tmp_properties = TWIG_GET_ARRAY_ELEMENT(tmp_class, "properties", sizeof("properties")); tmp_item = TWIG_GET_ARRAY_ELEMENT_ZVAL(tmp_properties, item); if (tmp_item) { @@ -610,8 +615,8 @@ PHP_FUNCTION(twig_template_get_attributes) sprintf(tmp_method_name_get, "get%s", lcItem); sprintf(tmp_method_name_is, "is%s", lcItem); - tmp_class = TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name); - tmp_methods = TWIG_GET_ARRAY_ELEMENT(tmp_class, "methods"); + tmp_class = TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name, strlen(class_name)); + tmp_methods = TWIG_GET_ARRAY_ELEMENT(tmp_class, "methods", sizeof("methods")); if (TWIG_PROPERTY(tmp_methods, lcItem)) { method = Z_STRVAL_P(item); From 2b58fec35d03b4812d8499580106c751abe5812a Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 30 Jun 2011 17:02:27 +0100 Subject: [PATCH 31/73] Added support for NULL arguments to CALL_USER_FUNC_ARRAY and made CALL_BOOLEAN use that. --- twig.c | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/twig.c b/twig.c index 3e602eb06..0cb55af30 100644 --- a/twig.c +++ b/twig.c @@ -155,11 +155,6 @@ int TWIG_ISSET_ARRAY_ELEMENT(zval *array, zval *item) return 0; } -int TWIG_CALL_BOOLEAN(zval *property, char *functionName) -{ - -} - char *TWIG_STRTOLOWER_ZVAL(zval *item) { char *item_dup; @@ -175,20 +170,25 @@ char *TWIG_STRTOLOWER_ZVAL(zval *item) zval *TWIG_CALL_USER_FUNC_ARRAY(zval *object, char *function, zval *arguments) { zend_fcall_info fci; - zval ***args; - HashTable *table = HASH_OF(arguments); + zval ***args = NULL; + int arg_count = 0; + HashTable *table; HashPosition pos; int i = 0; zval *retval_ptr; zval *zfunction; - args = safe_emalloc(sizeof(zval **), table->nNumOfElements, 0); + if (arguments) { + table = HASH_OF(arguments); + args = safe_emalloc(sizeof(zval **), table->nNumOfElements, 0); - zend_hash_internal_pointer_reset_ex(table, &pos); + zend_hash_internal_pointer_reset_ex(table, &pos); - while (zend_hash_get_current_data_ex(table, (void **)&args[i], &pos) == SUCCESS) { - i++; - zend_hash_move_forward_ex(table, &pos); + while (zend_hash_get_current_data_ex(table, (void **)&args[i], &pos) == SUCCESS) { + i++; + zend_hash_move_forward_ex(table, &pos); + } + arg_count = table->nNumOfElements; } MAKE_STD_ZVAL(zfunction); @@ -199,7 +199,7 @@ zval *TWIG_CALL_USER_FUNC_ARRAY(zval *object, char *function, zval *arguments) fci.symbol_table = NULL; fci.object_ptr = object; fci.retval_ptr_ptr = &retval_ptr; - fci.param_count = table->nNumOfElements; + fci.param_count = arg_count; fci.params = args; fci.no_separation = 0; @@ -210,6 +210,14 @@ zval *TWIG_CALL_USER_FUNC_ARRAY(zval *object, char *function, zval *arguments) return retval_ptr; } +int TWIG_CALL_BOOLEAN(zval *object, char *functionName) +{ + zval *ret; + + ret = TWIG_CALL_USER_FUNC_ARRAY(object, functionName, NULL); + return Z_LVAL_P(ret); +} + zval *TWIG_GET_STATIC_PROPERTY(zval *class, char *prop_name) { zval **tmp_zval; @@ -667,7 +675,9 @@ PHP_FUNCTION(twig_template_get_attributes) /* $ret = call_user_func_array(array($object, $method), $arguments); */ - ret = TWIG_CALL_USER_FUNC_ARRAY(object, method, arguments); + if (Z_TYPE_P(object) == IS_OBJECT) { + ret = TWIG_CALL_USER_FUNC_ARRAY(object, method, arguments); + } efree(tmp_method_name_get); efree(tmp_method_name_is); efree(lcItem); From 577169c5155c78bd5e74f8891983529149ae7fb1 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 30 Jun 2011 17:03:00 +0100 Subject: [PATCH 32/73] Added missing header and switch arguments for the return around as I got that wrong. --- twig.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/twig.c b/twig.c index 0cb55af30..e1b98efcb 100644 --- a/twig.c +++ b/twig.c @@ -26,6 +26,7 @@ #include "php_twig.h" #include "ext/standard/php_smart_str.h" +#include "Zend/zend_object_handlers.h" #include "Zend/zend_interfaces.h" ZEND_BEGIN_ARG_INFO_EX(twig_template_get_attribute_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 6) @@ -694,6 +695,6 @@ PHP_FUNCTION(twig_template_get_attributes) return $ret; */ if (ret) { - RETVAL_ZVAL(ret, 0, 1); + RETVAL_ZVAL(ret, 1, 0); } } From f42cd14d2dee0a83dfae463ceb5d2d0da15e6b7e Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Mon, 4 Jul 2011 16:06:46 +0100 Subject: [PATCH 33/73] Fix efree(NULL) when no arguments are passed in. --- twig.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/twig.c b/twig.c index e1b98efcb..cf79f542d 100644 --- a/twig.c +++ b/twig.c @@ -207,7 +207,10 @@ zval *TWIG_CALL_USER_FUNC_ARRAY(zval *object, char *function, zval *arguments) if (zend_call_function(&fci, NULL TSRMLS_CC) == FAILURE) { zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Could not execute %s::%s()", zend_get_class_entry(object)->name, function); } - efree(fci.params); + + if (args) { + efree(fci.params); + } return retval_ptr; } From 1b55c97eba94cdd94cadff35387f9b7f051d1990 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Mon, 4 Jul 2011 16:46:33 +0100 Subject: [PATCH 34/73] Only add the object to the cache if there is actually an object. --- twig.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/twig.c b/twig.c index cf79f542d..f3bbbecca 100644 --- a/twig.c +++ b/twig.c @@ -554,13 +554,16 @@ PHP_FUNCTION(twig_template_get_attributes) } } */ - char *class_name = TWIG_GET_CLASS_NAME(object); + char *class_name; zval *tmp_self_cache; - - tmp_self_cache = TWIG_GET_STATIC_PROPERTY(template, "cache"); - if (!TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name, strlen(class_name))) { - twig_add_class_to_cache(tmp_self_cache, object, class_name); + if (Z_TYPE_P(object) == IS_OBJECT) { + class_name = TWIG_GET_CLASS_NAME(object); + tmp_self_cache = TWIG_GET_STATIC_PROPERTY(template, "cache"); + + if (!TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name, strlen(class_name))) { + twig_add_class_to_cache(tmp_self_cache, object, class_name); + } } /* From 078690b4dca36dec9398bfc86d7de00e98438a29 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Mon, 4 Jul 2011 18:02:02 +0100 Subject: [PATCH 35/73] - Remove debugging code. --- twig.c | 1 + 1 file changed, 1 insertion(+) diff --git a/twig.c b/twig.c index f3bbbecca..74ed67ea5 100644 --- a/twig.c +++ b/twig.c @@ -469,6 +469,7 @@ PHP_FUNCTION(twig_template_get_attributes) } */ if (strcmp("method", type) != 0) { +// printf("XXXmethod: %s\n", type); if ((TWIG_ARRAY_KEY_EXISTS(object, item)) || (TWIG_INSTANCE_OF(object, zend_ce_arrayaccess) && TWIG_ISSET_ARRAY_ELEMENT(object, item)) ) { From 803c1a9f839d438988667001e09116011242d2d0 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Mon, 4 Jul 2011 18:02:17 +0100 Subject: [PATCH 36/73] Apparently this is needed for GCC to not optimise it out... --- twig.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/twig.c b/twig.c index 74ed67ea5..d2cc1ea99 100644 --- a/twig.c +++ b/twig.c @@ -524,7 +524,7 @@ PHP_FUNCTION(twig_template_get_attributes) } */ - if (!Z_TYPE_P(object) == IS_OBJECT) { + if (Z_TYPE_P(object) != IS_OBJECT) { if (isDefinedTest) { RETURN_FALSE; } From f196f01704d63a8826bd7b957af242b9cf16d84d Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Mon, 4 Jul 2011 18:02:39 +0100 Subject: [PATCH 37/73] Initialise variable to NULL. --- twig.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/twig.c b/twig.c index d2cc1ea99..832a9b9b8 100644 --- a/twig.c +++ b/twig.c @@ -555,7 +555,7 @@ PHP_FUNCTION(twig_template_get_attributes) } } */ - char *class_name; + char *class_name = NULL; zval *tmp_self_cache; if (Z_TYPE_P(object) == IS_OBJECT) { From f19ec8a090ba270d98e1d92fb621f30c19d45f9c Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Tue, 5 Jul 2011 12:37:08 +0100 Subject: [PATCH 38/73] Implemented first bit for ArrayAccess support. --- twig.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/twig.c b/twig.c index 832a9b9b8..7c8e7e8ea 100644 --- a/twig.c +++ b/twig.c @@ -151,8 +151,36 @@ int TWIG_INSTANCE_OF_USERLAND(zval *object, char *interface TSRMLS_DC) return instanceof_function(Z_OBJCE_P(object), *pce TSRMLS_CC); } -int TWIG_ISSET_ARRAY_ELEMENT(zval *array, zval *item) +zval *TWIG_GET_ARRAYOBJECT_ELEMENT(zval *object, zval *offset) { + zend_class_entry *ce = Z_OBJCE_P(object); + zval *retval; + + if (Z_TYPE_P(object) == IS_OBJECT) { + SEPARATE_ARG_IF_REF(offset); + zend_call_method_with_1_params(&object, ce, NULL, "offsetget", &retval, offset); + + zval_ptr_dtor(&offset); + + if (UNEXPECTED(!retval)) { + if (UNEXPECTED(!EG(exception))) { + zend_error_noreturn(E_ERROR, "Undefined offset for object of type %s used as array", ce->name); + } + return NULL; + } + + return retval; + } + return NULL; +} + +int TWIG_ISSET_ARRAYOBJECT_ELEMENT(zval *object, zval *offset) +{ + zval *retval = TWIG_GET_ARRAYOBJECT_ELEMENT(object, offset); + if (retval) { + zval_ptr_dtor(&retval); + return 1; + } return 0; } @@ -242,6 +270,10 @@ zval *TWIG_GET_ARRAY_ELEMENT_ZVAL(zval *class, zval *prop_name) char *tmp_name; if (class == NULL || Z_TYPE_P(class) != IS_ARRAY || Z_TYPE_P(prop_name) != IS_STRING) { + if (class != NULL && Z_TYPE_P(class) == IS_OBJECT && TWIG_INSTANCE_OF(class, zend_ce_arrayaccess)) { + // array access object + return TWIG_GET_ARRAYOBJECT_ELEMENT(class, prop_name); + } return NULL; } tmp_name = Z_STRVAL_P(prop_name); @@ -260,6 +292,18 @@ zval *TWIG_GET_ARRAY_ELEMENT(zval *class, char *prop_name, int prop_name_length) return NULL; } + if (class != NULL && Z_TYPE_P(class) == IS_OBJECT && TWIG_INSTANCE_OF(class, zend_ce_arrayaccess)) { + // array access object + zval *tmp_name_zval; + zval *tmp_ret_zval; + + ALLOC_INIT_ZVAL(tmp_name_zval); + ZVAL_STRING(tmp_name_zval, prop_name, 1); + tmp_ret_zval = TWIG_GET_ARRAYOBJECT_ELEMENT(class, tmp_name_zval); + zval_dtor(tmp_name_zval); + return tmp_ret_zval; + } + if (zend_hash_find(HASH_OF(class), prop_name, prop_name_length+1, (void**)&tmp_zval) == SUCCESS) { return *tmp_zval; } @@ -471,7 +515,7 @@ PHP_FUNCTION(twig_template_get_attributes) if (strcmp("method", type) != 0) { // printf("XXXmethod: %s\n", type); if ((TWIG_ARRAY_KEY_EXISTS(object, item)) - || (TWIG_INSTANCE_OF(object, zend_ce_arrayaccess) && TWIG_ISSET_ARRAY_ELEMENT(object, item)) + || (TWIG_INSTANCE_OF(object, zend_ce_arrayaccess) && TWIG_ISSET_ARRAYOBJECT_ELEMENT(object, item)) ) { zval *ret; From 48b0a5f985c3b612a393dc9562bf41a5c4c34685 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Tue, 5 Jul 2011 12:37:26 +0100 Subject: [PATCH 39/73] Fixed compiler warning. --- twig.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/twig.c b/twig.c index 7c8e7e8ea..af19a15bf 100644 --- a/twig.c +++ b/twig.c @@ -278,7 +278,7 @@ zval *TWIG_GET_ARRAY_ELEMENT_ZVAL(zval *class, zval *prop_name) } tmp_name = Z_STRVAL_P(prop_name); - if (zend_hash_find(HASH_OF(class), tmp_name, strlen(tmp_name)+1, &tmp_zval) == SUCCESS) { + if (zend_hash_find(HASH_OF(class), tmp_name, strlen(tmp_name)+1, (void**) &tmp_zval) == SUCCESS) { return tmp_zval; } return NULL; From 9d22fddf081e829e219a53ac5cf598b5fcbb7b24 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 7 Jul 2011 13:04:50 +0100 Subject: [PATCH 40/73] Fixed TWIG_GET_ARRAY_ELEMENT_ZVAL's return value. --- twig.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/twig.c b/twig.c index af19a15bf..224cd9435 100644 --- a/twig.c +++ b/twig.c @@ -266,7 +266,7 @@ zval *TWIG_GET_STATIC_PROPERTY(zval *class, char *prop_name) zval *TWIG_GET_ARRAY_ELEMENT_ZVAL(zval *class, zval *prop_name) { - zval *tmp_zval; + zval **tmp_zval; char *tmp_name; if (class == NULL || Z_TYPE_P(class) != IS_ARRAY || Z_TYPE_P(prop_name) != IS_STRING) { @@ -279,7 +279,7 @@ zval *TWIG_GET_ARRAY_ELEMENT_ZVAL(zval *class, zval *prop_name) tmp_name = Z_STRVAL_P(prop_name); if (zend_hash_find(HASH_OF(class), tmp_name, strlen(tmp_name)+1, (void**) &tmp_zval) == SUCCESS) { - return tmp_zval; + return *tmp_zval; } return NULL; } From 63e1f1279db915d9ab9de47082bbb5c84ceeaca7 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 7 Jul 2011 13:05:12 +0100 Subject: [PATCH 41/73] Check normal property before trying to de-mangle it. --- twig.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/twig.c b/twig.c index 224cd9435..7cf7ad737 100644 --- a/twig.c +++ b/twig.c @@ -314,6 +314,12 @@ zval *TWIG_PROPERTY(zval *object, char *propname) { char *prot_name; int prot_name_length; + zval *tmp; + + tmp = TWIG_GET_ARRAY_ELEMENT(object, propname, strlen(propname)); + if (tmp) { + return tmp; + } zend_mangle_property_name(&prot_name, &prot_name_length, "*", 1, propname, strlen(propname), 0); return TWIG_GET_ARRAY_ELEMENT(object, prot_name, prot_name_length); From 4e899caee4d95a6db5baeb7165c45aea50f8faab Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 7 Jul 2011 13:05:40 +0100 Subject: [PATCH 42/73] Add the properties as assoc keys, so that hash_find actually works on them. --- twig.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/twig.c b/twig.c index 7cf7ad737..02a95c7e5 100644 --- a/twig.c +++ b/twig.c @@ -437,7 +437,7 @@ static int twig_add_property_to_class(zend_property_info *pptr TSRMLS_DC, int nu zend_unmangle_property_name(pptr->name, pptr->name_length, &class_name, &prop_name); - add_next_index_string(retval, prop_name, 1); + add_assoc_string(retval, prop_name, prop_name, 1); return 0; } @@ -456,7 +456,7 @@ static int twig_add_dyn_property_to_class(zval **pptr TSRMLS_DC, int num_args, v ZVAL_STRINGL(&member, hash_key->arKey, hash_key->nKeyLength-1, 0); if (zend_get_property_info(ce, &member, 1 TSRMLS_CC) == &EG(std_property_info)) { zend_unmangle_property_name((&EG(std_property_info))->name, (&EG(std_property_info))->name_length, &class_name, &prop_name); - add_next_index_string(retval, prop_name, 1); + add_assoc_string(retval, prop_name, prop_name, 1); } return 0; } From 508bb66bf7d512a7325a6a1353890aefea8641ec Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 7 Jul 2011 13:06:13 +0100 Subject: [PATCH 43/73] Made properties return properly. --- twig.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/twig.c b/twig.c index 02a95c7e5..ee98590e6 100644 --- a/twig.c +++ b/twig.c @@ -656,7 +656,8 @@ PHP_FUNCTION(twig_template_get_attributes) } convert_to_string(item); - return_value = TWIG_PROPERTY(object, Z_STRVAL_P(item)); // this is obviously wrong + ret = TWIG_PROPERTY(object, Z_STRVAL_P(item)); // this is obviously wrong + RETVAL_ZVAL(ret, 1, 0); } } /* From 7d36defef3b5aade2404e81441b2be20ab518031 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 7 Jul 2011 14:04:53 +0100 Subject: [PATCH 44/73] Split TWIG_PROPERTY into TWIG_PROPERTY and TWIG_PROPERTY_CHAR so the array key can be both a zval or char. --- twig.c | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/twig.c b/twig.c index ee98590e6..e93e5f597 100644 --- a/twig.c +++ b/twig.c @@ -310,19 +310,38 @@ zval *TWIG_GET_ARRAY_ELEMENT(zval *class, char *prop_name, int prop_name_length) return NULL; } -zval *TWIG_PROPERTY(zval *object, char *propname) +zval *TWIG_PROPERTY(zval *object, zval *propname) { char *prot_name; int prot_name_length; - zval *tmp; + zval *tmp = NULL; - tmp = TWIG_GET_ARRAY_ELEMENT(object, propname, strlen(propname)); + tmp = TWIG_GET_ARRAY_ELEMENT(object, Z_STRVAL_P(propname), Z_STRLEN_P(propname)); if (tmp) { return tmp; } - zend_mangle_property_name(&prot_name, &prot_name_length, "*", 1, propname, strlen(propname), 0); - return TWIG_GET_ARRAY_ELEMENT(object, prot_name, prot_name_length); + zend_mangle_property_name(&prot_name, &prot_name_length, "*", 1, Z_STRVAL_P(propname), Z_STRLEN_P(propname), 0); + tmp = TWIG_GET_ARRAY_ELEMENT(object, prot_name, prot_name_length); + if (tmp) { + return tmp; + } + + if (Z_OBJ_HT_P(object)->read_property) { + tmp = Z_OBJ_HT_P(object)->read_property(object, propname, BP_VAR_R TSRMLS_CC); + return tmp; + } +} + +zval *TWIG_PROPERTY_CHAR(zval *object, char *propname) +{ + zval *tmp_name_zval, *tmp; + + ALLOC_INIT_ZVAL(tmp_name_zval); + ZVAL_STRING(tmp_name_zval, propname, 1); + tmp = TWIG_PROPERTY(object, tmp_name_zval); + zval_dtor(tmp_name_zval); + return tmp; } int TWIG_CALL_B_0(zval *object, char *method) @@ -545,7 +564,7 @@ PHP_FUNCTION(twig_template_get_attributes) if (isDefinedTest) { RETURN_FALSE; } - if (!TWIG_CALL_BOOLEAN(TWIG_PROPERTY(template, "env"), "isStrictVariables")) { + if (!TWIG_CALL_BOOLEAN(TWIG_PROPERTY_CHAR(template, "env"), "isStrictVariables")) { return; } /* @@ -585,7 +604,7 @@ PHP_FUNCTION(twig_template_get_attributes) throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, $object)); } */ - if (!TWIG_CALL_BOOLEAN(TWIG_PROPERTY(template, "env"), "isStrictVariables")) { + if (!TWIG_CALL_BOOLEAN(TWIG_PROPERTY_CHAR(template, "env"), "isStrictVariables")) { RETURN_FALSE; } TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Item \"%s\" for \"%s\" does not exist", item, object); @@ -651,8 +670,8 @@ PHP_FUNCTION(twig_template_get_attributes) if (isDefinedTest) { RETURN_TRUE; } - if (TWIG_CALL_S(TWIG_PROPERTY(template, "env"), "hasExtension", "sandbox")) { - TWIG_CALL_ZZ(TWIG_CALL_S(TWIG_PROPERTY(template, "env"), "getExtension", "sandbox"), "checkPropertyAllowed", object, item); + if (TWIG_CALL_S(TWIG_PROPERTY_CHAR(template, "env"), "hasExtension", "sandbox")) { + TWIG_CALL_ZZ(TWIG_CALL_S(TWIG_PROPERTY_CHAR(template, "env"), "getExtension", "sandbox"), "checkPropertyAllowed", object, item); } convert_to_string(item); @@ -711,7 +730,7 @@ PHP_FUNCTION(twig_template_get_attributes) if (isDefinedTest) { RETURN_FALSE; } - zval *env = TWIG_PROPERTY(template, "env"); + zval *env = TWIG_PROPERTY_CHAR(template, "env"); if (TWIG_CALL_B_0(env, "isStrictVariables")) { return; } @@ -728,8 +747,8 @@ PHP_FUNCTION(twig_template_get_attributes) $this->env->getExtension('sandbox')->checkMethodAllowed($object, $method); } */ - if (TWIG_CALL_S(TWIG_PROPERTY(template, "env"), "hasExtension", "sandbox")) { - TWIG_CALL_ZZ(TWIG_CALL_S(TWIG_PROPERTY(template, "env"), "getExtension", "sandbox"), "checkMethodAllowed", object, item); + if (TWIG_CALL_S(TWIG_PROPERTY_CHAR(template, "env"), "hasExtension", "sandbox")) { + TWIG_CALL_ZZ(TWIG_CALL_S(TWIG_PROPERTY_CHAR(template, "env"), "getExtension", "sandbox"), "checkMethodAllowed", object, item); } /* $ret = call_user_func_array(array($object, $method), $arguments); From 3c208ff875ae9fa19d4c8c7a277a8ea53eb69a32 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 7 Jul 2011 14:05:21 +0100 Subject: [PATCH 45/73] We need to use strlen on hash keys. --- twig.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/twig.c b/twig.c index e93e5f597..8b7b84bdb 100644 --- a/twig.c +++ b/twig.c @@ -657,7 +657,7 @@ PHP_FUNCTION(twig_template_get_attributes) zval *tmp_class, *tmp_properties, *tmp_item, *tmp_object_item = NULL; tmp_class = TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name, strlen(class_name)); - tmp_properties = TWIG_GET_ARRAY_ELEMENT(tmp_class, "properties", sizeof("properties")); + tmp_properties = TWIG_GET_ARRAY_ELEMENT(tmp_class, "properties", strlen("properties")); tmp_item = TWIG_GET_ARRAY_ELEMENT_ZVAL(tmp_properties, item); if (tmp_item) { From f7ae76ef4b51ee482b512be9832fa0770bd62d8a Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 7 Jul 2011 14:05:38 +0100 Subject: [PATCH 46/73] Check the property regardless of whether tmp_item is valid/set. --- twig.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/twig.c b/twig.c index 8b7b84bdb..c18b4a688 100644 --- a/twig.c +++ b/twig.c @@ -660,10 +660,7 @@ PHP_FUNCTION(twig_template_get_attributes) tmp_properties = TWIG_GET_ARRAY_ELEMENT(tmp_class, "properties", strlen("properties")); tmp_item = TWIG_GET_ARRAY_ELEMENT_ZVAL(tmp_properties, item); - if (tmp_item) { - convert_to_string(tmp_item); - tmp_object_item = TWIG_PROPERTY(object, Z_STRVAL_P(tmp_item)); - } + tmp_object_item = TWIG_PROPERTY(object, item); if (tmp_item || tmp_object_item || TWIG_ARRAY_KEY_EXISTS(object, item) // FIXME: Array key? is that array access here? ) { From 9f6495f44704beb6ad0ed13bf4aa01e6deef05d4 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 7 Jul 2011 14:06:03 +0100 Subject: [PATCH 47/73] Return the value immediately. --- twig.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/twig.c b/twig.c index c18b4a688..c1239a810 100644 --- a/twig.c +++ b/twig.c @@ -672,8 +672,8 @@ PHP_FUNCTION(twig_template_get_attributes) } convert_to_string(item); - ret = TWIG_PROPERTY(object, Z_STRVAL_P(item)); // this is obviously wrong - RETVAL_ZVAL(ret, 1, 0); + ret = TWIG_PROPERTY(object, item); + RETURN_ZVAL(ret, 1, 0); } } /* From 1e726f436b7d43ff4d0672ec90a41afaf365fe73 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 7 Jul 2011 14:06:18 +0100 Subject: [PATCH 48/73] Those are array elements and not properties. --- twig.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/twig.c b/twig.c index c1239a810..acbae389d 100644 --- a/twig.c +++ b/twig.c @@ -690,24 +690,29 @@ PHP_FUNCTION(twig_template_get_attributes) */ { char *lcItem = TWIG_STRTOLOWER_ZVAL(item); + int lcItem_length; char *method = NULL; - char *tmp_method_name_get = emalloc(4 + strlen(lcItem)); - char *tmp_method_name_is = emalloc(3 + strlen(lcItem)); + char *tmp_method_name_get; + char *tmp_method_name_is; zval *tmp_class, *tmp_methods; + lcItem_length = strlen(lcItem); + tmp_method_name_get = emalloc(4 + lcItem_length); + tmp_method_name_is = emalloc(3 + lcItem_length); + sprintf(tmp_method_name_get, "get%s", lcItem); sprintf(tmp_method_name_is, "is%s", lcItem); tmp_class = TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name, strlen(class_name)); - tmp_methods = TWIG_GET_ARRAY_ELEMENT(tmp_class, "methods", sizeof("methods")); + tmp_methods = TWIG_GET_ARRAY_ELEMENT(tmp_class, "methods", strlen("methods")); - if (TWIG_PROPERTY(tmp_methods, lcItem)) { + if (TWIG_GET_ARRAY_ELEMENT(tmp_methods, lcItem, lcItem_length)) { method = Z_STRVAL_P(item); - } else if (TWIG_PROPERTY(tmp_methods, tmp_method_name_get)) { + } else if (TWIG_GET_ARRAY_ELEMENT(tmp_methods, tmp_method_name_get, lcItem_length + 3)) { method = tmp_method_name_get; - } else if (TWIG_PROPERTY(tmp_methods, tmp_method_name_is)) { + } else if (TWIG_GET_ARRAY_ELEMENT(tmp_methods, tmp_method_name_is, lcItem_length + 2)) { method = tmp_method_name_is; - } else if (TWIG_PROPERTY(tmp_methods, "__call")) { + } else if (TWIG_GET_ARRAY_ELEMENT(tmp_methods, "__call", 6)) { method = Z_STRVAL_P(item); /* } else { From 0b6cefbb6a6a23753fdf2afc7b4711e094c461e5 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 7 Jul 2011 15:43:13 +0100 Subject: [PATCH 49/73] Fixed TWIG_PROPERTY to return a NULL instead of a zval(NULL). --- twig.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/twig.c b/twig.c index acbae389d..26ef899a7 100644 --- a/twig.c +++ b/twig.c @@ -328,9 +328,14 @@ zval *TWIG_PROPERTY(zval *object, zval *propname) } if (Z_OBJ_HT_P(object)->read_property) { - tmp = Z_OBJ_HT_P(object)->read_property(object, propname, BP_VAR_R TSRMLS_CC); - return tmp; + tmp = Z_OBJ_HT_P(object)->read_property(object, propname, BP_VAR_IS TSRMLS_CC); + if (&tmp != &EG(uninitialized_zval_ptr)) { + return tmp; + } else { + return NULL; + } } + return tmp; } zval *TWIG_PROPERTY_CHAR(zval *object, char *propname) From 9096e3f7344b8bb34d01027c3162b19fa5cb5592 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Fri, 8 Jul 2011 12:50:26 +0100 Subject: [PATCH 50/73] Use strtolower on the method names and add them as associative keys so that they are found. This makes method calls work. --- twig.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/twig.c b/twig.c index 26ef899a7..59d2b18b8 100644 --- a/twig.c +++ b/twig.c @@ -445,8 +445,9 @@ char *TWIG_GET_CLASS_NAME(zval *object) static void twig_add_method_to_class(zend_function *mptr TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) { zval *retval = va_arg(args, zval*); + char *item = php_strtolower(mptr->common.function_name, strlen(mptr->common.function_name)); - add_next_index_string(retval, mptr->common.function_name, 1); + add_assoc_string(retval, item, item, 1); } static int twig_add_property_to_class(zend_property_info *pptr TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) From b8b89f61a616a43ae4b816f1c867915c51a1b6a3 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Fri, 8 Jul 2011 13:01:22 +0100 Subject: [PATCH 51/73] Fixed the uninitialized ptr check. This makes all tests in Twig_TemplateTest pass! --- twig.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/twig.c b/twig.c index 59d2b18b8..b3185ca59 100644 --- a/twig.c +++ b/twig.c @@ -329,7 +329,7 @@ zval *TWIG_PROPERTY(zval *object, zval *propname) if (Z_OBJ_HT_P(object)->read_property) { tmp = Z_OBJ_HT_P(object)->read_property(object, propname, BP_VAR_IS TSRMLS_CC); - if (&tmp != &EG(uninitialized_zval_ptr)) { + if (tmp != EG(uninitialized_zval_ptr)) { return tmp; } else { return NULL; From 9facb8d991f74279dd9deb8aa15b251dcb5e3b2a Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Wed, 13 Jul 2011 16:27:10 +0100 Subject: [PATCH 52/73] Split TWIG_CALL_S into TWIG_CALL_S and TWIG_CALL_SB for boolean returning functions. --- twig.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 3 deletions(-) diff --git a/twig.c b/twig.c index b3185ca59..7712d93df 100644 --- a/twig.c +++ b/twig.c @@ -355,10 +355,70 @@ int TWIG_CALL_B_0(zval *object, char *method) zval *TWIG_CALL_S(zval *object, char *method, char *arg0) { + zend_fcall_info fci; + zval **args[1]; + zval *argument; + zval *zfunction; + zval *retval_ptr; + + MAKE_STD_ZVAL(argument); + ZVAL_STRING(argument, arg0, 1); + args[0] = &argument; + + MAKE_STD_ZVAL(zfunction); + ZVAL_STRING(zfunction, method, 0); + fci.size = sizeof(fci); + fci.function_table = EG(function_table); + fci.function_name = zfunction; + fci.symbol_table = NULL; + fci.object_ptr = object; + fci.retval_ptr_ptr = &retval_ptr; + fci.param_count = 1; + fci.params = args; + fci.no_separation = 0; + + if (zend_call_function(&fci, NULL TSRMLS_CC) == FAILURE) { + return 0; + } + efree(argument); + return retval_ptr; } -zval *TWIG_CALL_ZZ(zval *object, char *method, zval *arg1, zval *arg2) +int TWIG_CALL_SB(zval *object, char *method, char *arg0) { + zval *retval_ptr; + + retval_ptr = TWIG_CALL_S(object, method, arg0); + return (retval_ptr && (Z_TYPE_P(retval_ptr) == IS_BOOL) && Z_LVAL_P(retval_ptr)); +} + +int TWIG_CALL_ZZ(zval *object, char *method, zval *arg1, zval *arg2) +{ + zend_fcall_info fci; + zval **args[2]; + zval *zfunction; + zval *retval_ptr; + + args[0] = &arg1; + args[1] = &arg2; + + MAKE_STD_ZVAL(zfunction); + ZVAL_STRING(zfunction, method, 0); + fci.size = sizeof(fci); + fci.function_table = EG(function_table); + fci.function_name = zfunction; + fci.symbol_table = NULL; + fci.object_ptr = object; + fci.retval_ptr_ptr = &retval_ptr; + fci.param_count = 2; + fci.params = args; + fci.no_separation = 0; + + if (zend_call_function(&fci, NULL TSRMLS_CC) == FAILURE) { + return 0; + } + + return (retval_ptr && (Z_TYPE_P(retval_ptr) == IS_BOOL) && Z_LVAL_P(retval_ptr)); } void TWIG_NEW(zval *object, char *class, zval *value) @@ -673,7 +733,7 @@ PHP_FUNCTION(twig_template_get_attributes) if (isDefinedTest) { RETURN_TRUE; } - if (TWIG_CALL_S(TWIG_PROPERTY_CHAR(template, "env"), "hasExtension", "sandbox")) { + if (TWIG_CALL_SB(TWIG_PROPERTY_CHAR(template, "env"), "hasExtension", "sandbox")) { TWIG_CALL_ZZ(TWIG_CALL_S(TWIG_PROPERTY_CHAR(template, "env"), "getExtension", "sandbox"), "checkPropertyAllowed", object, item); } @@ -755,7 +815,7 @@ PHP_FUNCTION(twig_template_get_attributes) $this->env->getExtension('sandbox')->checkMethodAllowed($object, $method); } */ - if (TWIG_CALL_S(TWIG_PROPERTY_CHAR(template, "env"), "hasExtension", "sandbox")) { + if (TWIG_CALL_SB(TWIG_PROPERTY_CHAR(template, "env"), "hasExtension", "sandbox")) { TWIG_CALL_ZZ(TWIG_CALL_S(TWIG_PROPERTY_CHAR(template, "env"), "getExtension", "sandbox"), "checkMethodAllowed", object, item); } /* From 52b9ae44bb05d6e45f93969e81a630dd875acecb Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Wed, 13 Jul 2011 16:27:35 +0100 Subject: [PATCH 53/73] Bailout when we have an exception to defer to PHP's exception handlers. --- twig.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/twig.c b/twig.c index 7712d93df..b641bd0fc 100644 --- a/twig.c +++ b/twig.c @@ -736,6 +736,9 @@ PHP_FUNCTION(twig_template_get_attributes) if (TWIG_CALL_SB(TWIG_PROPERTY_CHAR(template, "env"), "hasExtension", "sandbox")) { TWIG_CALL_ZZ(TWIG_CALL_S(TWIG_PROPERTY_CHAR(template, "env"), "getExtension", "sandbox"), "checkPropertyAllowed", object, item); } + if (EG(exception)) { + return; + } convert_to_string(item); ret = TWIG_PROPERTY(object, item); @@ -818,6 +821,9 @@ PHP_FUNCTION(twig_template_get_attributes) if (TWIG_CALL_SB(TWIG_PROPERTY_CHAR(template, "env"), "hasExtension", "sandbox")) { TWIG_CALL_ZZ(TWIG_CALL_S(TWIG_PROPERTY_CHAR(template, "env"), "getExtension", "sandbox"), "checkMethodAllowed", object, item); } + if (EG(exception)) { + return; + } /* $ret = call_user_func_array(array($object, $method), $arguments); */ From 0ee71a722ef9e2177b426295cd89e202f8ca3014 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Wed, 13 Jul 2011 18:20:21 +0100 Subject: [PATCH 54/73] Added TWIG_CALL_Z (to use for calling a function with one argument). --- twig.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/twig.c b/twig.c index b641bd0fc..a7e2887df 100644 --- a/twig.c +++ b/twig.c @@ -392,6 +392,34 @@ int TWIG_CALL_SB(zval *object, char *method, char *arg0) return (retval_ptr && (Z_TYPE_P(retval_ptr) == IS_BOOL) && Z_LVAL_P(retval_ptr)); } +int TWIG_CALL_Z(zval *object, char *method, zval *arg1) +{ + zend_fcall_info fci; + zval **args[1]; + zval *zfunction; + zval *retval_ptr; + + args[0] = &arg1; + + MAKE_STD_ZVAL(zfunction); + ZVAL_STRING(zfunction, method, 0); + fci.size = sizeof(fci); + fci.function_table = EG(function_table); + fci.function_name = zfunction; + fci.symbol_table = NULL; + fci.object_ptr = object; + fci.retval_ptr_ptr = &retval_ptr; + fci.param_count = 1; + fci.params = args; + fci.no_separation = 0; + + if (zend_call_function(&fci, NULL TSRMLS_CC) == FAILURE) { + return 0; + } + + return (retval_ptr && (Z_TYPE_P(retval_ptr) == IS_BOOL) && Z_LVAL_P(retval_ptr)); +} + int TWIG_CALL_ZZ(zval *object, char *method, zval *arg1, zval *arg2) { zend_fcall_info fci; From 49b885c17034a3fc0d81f047e0ebd8c99ccbfbfb Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Wed, 13 Jul 2011 18:20:47 +0100 Subject: [PATCH 55/73] After instantiating the object, call the constructor and make sure we return from the function. --- twig.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/twig.c b/twig.c index a7e2887df..b43f16b42 100644 --- a/twig.c +++ b/twig.c @@ -461,6 +461,8 @@ void TWIG_NEW(zval *object, char *class, zval *value) object_init_ex(object, *pce); Z_SET_REFCOUNT_P(object, 1); Z_UNSET_ISREF_P(object); + + TWIG_CALL_Z(object, "__construct", value); } static void twig_add_array_key_to_string(zval **zv TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) @@ -869,6 +871,7 @@ PHP_FUNCTION(twig_template_get_attributes) */ if (TWIG_INSTANCE_OF_USERLAND(object, "Twig_TemplateInterface")) { TWIG_NEW(return_value, "Twig_Markup", ret); + return; } /* return $ret; From 7b38766928d124d255fb45c19fdca97352ed6881 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 14 Jul 2011 12:15:50 +0100 Subject: [PATCH 56/73] Handle numeric keys for array elements too. --- twig.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/twig.c b/twig.c index b43f16b42..f62b13389 100644 --- a/twig.c +++ b/twig.c @@ -125,7 +125,7 @@ int TWIG_ARRAY_KEY_EXISTS(zval *array, zval *key) return 0; } convert_to_string(key); - if (zend_hash_find(Z_ARRVAL_P(array), Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, &dummy) == SUCCESS) { + if (zend_symtable_find(Z_ARRVAL_P(array), Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, &dummy) == SUCCESS) { return 1; } return 0; @@ -276,9 +276,10 @@ zval *TWIG_GET_ARRAY_ELEMENT_ZVAL(zval *class, zval *prop_name) } return NULL; } - tmp_name = Z_STRVAL_P(prop_name); - if (zend_hash_find(HASH_OF(class), tmp_name, strlen(tmp_name)+1, (void**) &tmp_zval) == SUCCESS) { + convert_to_string(prop_name); + tmp_name = Z_STRVAL_P(prop_name); + if (zend_symtable_find(HASH_OF(class), tmp_name, strlen(tmp_name)+1, (void**) &tmp_zval) == SUCCESS) { return *tmp_zval; } return NULL; @@ -304,7 +305,7 @@ zval *TWIG_GET_ARRAY_ELEMENT(zval *class, char *prop_name, int prop_name_length) return tmp_ret_zval; } - if (zend_hash_find(HASH_OF(class), prop_name, prop_name_length+1, (void**)&tmp_zval) == SUCCESS) { + if (zend_symtable_find(HASH_OF(class), prop_name, prop_name_length+1, (void**)&tmp_zval) == SUCCESS) { return *tmp_zval; } return NULL; From cbd975ae0cd3da8df6ff480eedc0f57416d71e83 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 14 Jul 2011 12:18:50 +0100 Subject: [PATCH 57/73] Use the proper zend hash function here. --- twig.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/twig.c b/twig.c index f62b13389..7d0b60db0 100644 --- a/twig.c +++ b/twig.c @@ -119,16 +119,11 @@ PHP_MINFO_FUNCTION(twig) int TWIG_ARRAY_KEY_EXISTS(zval *array, zval *key) { - void *dummy; - if (Z_TYPE_P(array) != IS_ARRAY) { return 0; } convert_to_string(key); - if (zend_symtable_find(Z_ARRVAL_P(array), Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, &dummy) == SUCCESS) { - return 1; - } - return 0; + return zend_symtable_exists(Z_ARRVAL_P(array), Z_STRVAL_P(key), Z_STRLEN_P(key) + 1); } int TWIG_INSTANCE_OF(zval *object, zend_class_entry *interface TSRMLS_DC) From a36d11ba0ca60c0c96024ae6db70bf3ec42a5b52 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Fri, 15 Jul 2011 12:29:40 +0100 Subject: [PATCH 58/73] Fixed memory leaks. --- twig.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 74 insertions(+), 10 deletions(-) diff --git a/twig.c b/twig.c index 7d0b60db0..7f2c5c2a3 100644 --- a/twig.c +++ b/twig.c @@ -216,7 +216,7 @@ zval *TWIG_CALL_USER_FUNC_ARRAY(zval *object, char *function, zval *arguments) } MAKE_STD_ZVAL(zfunction); - ZVAL_STRING(zfunction, function, 0); + ZVAL_STRING(zfunction, function, 1); fci.size = sizeof(fci); fci.function_table = EG(function_table); fci.function_name = zfunction; @@ -228,21 +228,28 @@ zval *TWIG_CALL_USER_FUNC_ARRAY(zval *object, char *function, zval *arguments) fci.no_separation = 0; if (zend_call_function(&fci, NULL TSRMLS_CC) == FAILURE) { + zval_dtor(zfunction); + efree(zfunction); zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Could not execute %s::%s()", zend_get_class_entry(object)->name, function); } if (args) { efree(fci.params); } + zval_dtor(zfunction); + efree(zfunction); return retval_ptr; } int TWIG_CALL_BOOLEAN(zval *object, char *functionName) { zval *ret; + int res; ret = TWIG_CALL_USER_FUNC_ARRAY(object, functionName, NULL); - return Z_LVAL_P(ret); + res = Z_LVAL_P(ret); + zval_ptr_dtor(&ret); + return res; } zval *TWIG_GET_STATIC_PROPERTY(zval *class, char *prop_name) @@ -297,6 +304,7 @@ zval *TWIG_GET_ARRAY_ELEMENT(zval *class, char *prop_name, int prop_name_length) ZVAL_STRING(tmp_name_zval, prop_name, 1); tmp_ret_zval = TWIG_GET_ARRAYOBJECT_ELEMENT(class, tmp_name_zval); zval_dtor(tmp_name_zval); + efree(tmp_name_zval); return tmp_ret_zval; } @@ -319,6 +327,7 @@ zval *TWIG_PROPERTY(zval *object, zval *propname) zend_mangle_property_name(&prot_name, &prot_name_length, "*", 1, Z_STRVAL_P(propname), Z_STRLEN_P(propname), 0); tmp = TWIG_GET_ARRAY_ELEMENT(object, prot_name, prot_name_length); + efree(prot_name); if (tmp) { return tmp; } @@ -342,6 +351,7 @@ zval *TWIG_PROPERTY_CHAR(zval *object, char *propname) ZVAL_STRING(tmp_name_zval, propname, 1); tmp = TWIG_PROPERTY(object, tmp_name_zval); zval_dtor(tmp_name_zval); + efree(tmp_name_zval); return tmp; } @@ -362,7 +372,7 @@ zval *TWIG_CALL_S(zval *object, char *method, char *arg0) args[0] = &argument; MAKE_STD_ZVAL(zfunction); - ZVAL_STRING(zfunction, method, 0); + ZVAL_STRING(zfunction, method, 1); fci.size = sizeof(fci); fci.function_table = EG(function_table); fci.function_name = zfunction; @@ -374,8 +384,12 @@ zval *TWIG_CALL_S(zval *object, char *method, char *arg0) fci.no_separation = 0; if (zend_call_function(&fci, NULL TSRMLS_CC) == FAILURE) { + zval_dtor(argument); return 0; } + zval_dtor(zfunction); + efree(zfunction); + zval_dtor(argument); efree(argument); return retval_ptr; } @@ -383,9 +397,16 @@ zval *TWIG_CALL_S(zval *object, char *method, char *arg0) int TWIG_CALL_SB(zval *object, char *method, char *arg0) { zval *retval_ptr; + int success; retval_ptr = TWIG_CALL_S(object, method, arg0); - return (retval_ptr && (Z_TYPE_P(retval_ptr) == IS_BOOL) && Z_LVAL_P(retval_ptr)); + success = (retval_ptr && (Z_TYPE_P(retval_ptr) == IS_BOOL) && Z_LVAL_P(retval_ptr)); + + if (retval_ptr) { + zval_ptr_dtor(&retval_ptr); + } + + return success; } int TWIG_CALL_Z(zval *object, char *method, zval *arg1) @@ -394,11 +415,12 @@ int TWIG_CALL_Z(zval *object, char *method, zval *arg1) zval **args[1]; zval *zfunction; zval *retval_ptr; + int success; args[0] = &arg1; MAKE_STD_ZVAL(zfunction); - ZVAL_STRING(zfunction, method, 0); + ZVAL_STRING(zfunction, method, 1); fci.size = sizeof(fci); fci.function_table = EG(function_table); fci.function_name = zfunction; @@ -410,10 +432,23 @@ int TWIG_CALL_Z(zval *object, char *method, zval *arg1) fci.no_separation = 0; if (zend_call_function(&fci, NULL TSRMLS_CC) == FAILURE) { + zval_dtor(zfunction); + efree(zfunction); + if (retval_ptr) { + zval_ptr_dtor(&retval_ptr); + } return 0; } - return (retval_ptr && (Z_TYPE_P(retval_ptr) == IS_BOOL) && Z_LVAL_P(retval_ptr)); + zval_dtor(zfunction); + efree(zfunction); + + success = (retval_ptr && (Z_TYPE_P(retval_ptr) == IS_BOOL) && Z_LVAL_P(retval_ptr)); + if (retval_ptr) { + zval_ptr_dtor(&retval_ptr); + } + + return success; } int TWIG_CALL_ZZ(zval *object, char *method, zval *arg1, zval *arg2) @@ -422,12 +457,13 @@ int TWIG_CALL_ZZ(zval *object, char *method, zval *arg1, zval *arg2) zval **args[2]; zval *zfunction; zval *retval_ptr; + int success; args[0] = &arg1; args[1] = &arg2; MAKE_STD_ZVAL(zfunction); - ZVAL_STRING(zfunction, method, 0); + ZVAL_STRING(zfunction, method, 1); fci.size = sizeof(fci); fci.function_table = EG(function_table); fci.function_name = zfunction; @@ -439,10 +475,18 @@ int TWIG_CALL_ZZ(zval *object, char *method, zval *arg1, zval *arg2) fci.no_separation = 0; if (zend_call_function(&fci, NULL TSRMLS_CC) == FAILURE) { + zval_dtor(zfunction); return 0; } - return (retval_ptr && (Z_TYPE_P(retval_ptr) == IS_BOOL) && Z_LVAL_P(retval_ptr)); + zval_dtor(zfunction); + + success = (retval_ptr && (Z_TYPE_P(retval_ptr) == IS_BOOL) && Z_LVAL_P(retval_ptr)); + if (retval_ptr) { + zval_ptr_dtor(&retval_ptr); + } + + return success; } void TWIG_NEW(zval *object, char *class, zval *value) @@ -611,6 +655,7 @@ PHP_FUNCTION(twig_template_get_attributes) char *type = NULL; int type_len = 0; zend_bool isDefinedTest = 0; + int free_ret = 0; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ozzasb", &template, &object, &item, &arguments, &type, &type_len, &isDefinedTest) == FAILURE) { return; @@ -641,7 +686,11 @@ PHP_FUNCTION(twig_template_get_attributes) } ret = TWIG_GET_ARRAY_ELEMENT(object, Z_STRVAL_P(item), Z_STRLEN_P(item)); - RETURN_ZVAL(ret, 1, 0); + RETVAL_ZVAL(ret, 1, 0); + if (free_ret) { + zval_ptr_dtor(&ret); + } + return; } /* if (Twig_TemplateInterface::ARRAY_CALL === $type) { @@ -716,16 +765,18 @@ PHP_FUNCTION(twig_template_get_attributes) } } */ - char *class_name = NULL; zval *tmp_self_cache; if (Z_TYPE_P(object) == IS_OBJECT) { + char *class_name = NULL; + class_name = TWIG_GET_CLASS_NAME(object); tmp_self_cache = TWIG_GET_STATIC_PROPERTY(template, "cache"); if (!TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name, strlen(class_name))) { twig_add_class_to_cache(tmp_self_cache, object, class_name); } + efree(class_name); } /* @@ -747,12 +798,15 @@ PHP_FUNCTION(twig_template_get_attributes) */ if (strcmp("method", type) != 0) { zval *tmp_class, *tmp_properties, *tmp_item, *tmp_object_item = NULL; + char *class_name = NULL; + class_name = TWIG_GET_CLASS_NAME(object); tmp_class = TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name, strlen(class_name)); tmp_properties = TWIG_GET_ARRAY_ELEMENT(tmp_class, "properties", strlen("properties")); tmp_item = TWIG_GET_ARRAY_ELEMENT_ZVAL(tmp_properties, item); tmp_object_item = TWIG_PROPERTY(object, item); + efree(class_name); if (tmp_item || tmp_object_item || TWIG_ARRAY_KEY_EXISTS(object, item) // FIXME: Array key? is that array access here? ) { @@ -790,7 +844,9 @@ PHP_FUNCTION(twig_template_get_attributes) char *tmp_method_name_get; char *tmp_method_name_is; zval *tmp_class, *tmp_methods; + char *class_name = NULL; + class_name = TWIG_GET_CLASS_NAME(object); lcItem_length = strlen(lcItem); tmp_method_name_get = emalloc(4 + lcItem_length); tmp_method_name_is = emalloc(3 + lcItem_length); @@ -800,6 +856,7 @@ PHP_FUNCTION(twig_template_get_attributes) tmp_class = TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name, strlen(class_name)); tmp_methods = TWIG_GET_ARRAY_ELEMENT(tmp_class, "methods", strlen("methods")); + efree(class_name); if (TWIG_GET_ARRAY_ELEMENT(tmp_methods, lcItem, lcItem_length)) { method = Z_STRVAL_P(item); @@ -855,6 +912,7 @@ PHP_FUNCTION(twig_template_get_attributes) */ if (Z_TYPE_P(object) == IS_OBJECT) { ret = TWIG_CALL_USER_FUNC_ARRAY(object, method, arguments); + free_ret = 1; } efree(tmp_method_name_get); efree(tmp_method_name_is); @@ -867,6 +925,9 @@ PHP_FUNCTION(twig_template_get_attributes) */ if (TWIG_INSTANCE_OF_USERLAND(object, "Twig_TemplateInterface")) { TWIG_NEW(return_value, "Twig_Markup", ret); + if (ret) { + zval_ptr_dtor(&ret); + } return; } /* @@ -874,5 +935,8 @@ PHP_FUNCTION(twig_template_get_attributes) */ if (ret) { RETVAL_ZVAL(ret, 1, 0); + if (free_ret) { + zval_ptr_dtor(&ret); + } } } From 541f6c84786efdd765a27c705ce3fb674dee0555 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Mon, 18 Jul 2011 18:45:31 +0100 Subject: [PATCH 59/73] Use "isset" logic as fetch always works, even for elements that don't exist and they then just become NULL. --- twig.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/twig.c b/twig.c index 7f2c5c2a3..eefc4cb22 100644 --- a/twig.c +++ b/twig.c @@ -171,10 +171,23 @@ zval *TWIG_GET_ARRAYOBJECT_ELEMENT(zval *object, zval *offset) int TWIG_ISSET_ARRAYOBJECT_ELEMENT(zval *object, zval *offset) { - zval *retval = TWIG_GET_ARRAYOBJECT_ELEMENT(object, offset); - if (retval) { - zval_ptr_dtor(&retval); - return 1; + zend_class_entry *ce = Z_OBJCE_P(object); + zval *retval; + + if (Z_TYPE_P(object) == IS_OBJECT) { + SEPARATE_ARG_IF_REF(offset); + zend_call_method_with_1_params(&object, ce, NULL, "offsetexists", &retval, offset); + + zval_ptr_dtor(&offset); + + if (UNEXPECTED(!retval)) { + if (UNEXPECTED(!EG(exception))) { + zend_error_noreturn(E_ERROR, "Undefined offset for object of type %s used as array", ce->name); + } + return 0; + } + + return (retval && Z_TYPE_P(retval) == IS_BOOL && Z_LVAL_P(retval)); } return 0; } From 53e1652d805c84528ee57a47551ad496be6492ee Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Mon, 18 Jul 2011 18:46:12 +0100 Subject: [PATCH 60/73] Implement HAS_PROPERTY to distinguish between not-set and NULL. --- twig.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/twig.c b/twig.c index eefc4cb22..6fc3973b8 100644 --- a/twig.c +++ b/twig.c @@ -356,6 +356,18 @@ zval *TWIG_PROPERTY(zval *object, zval *propname) return tmp; } +zval *TWIG_HAS_PROPERTY(zval *object, zval *propname) +{ + char *prot_name; + int prot_name_length; + zval *tmp = 1; + + if (Z_OBJ_HT_P(object)->has_property) { + return Z_OBJ_HT_P(object)->has_property(object, propname, 0 TSRMLS_CC); + } + return 0; +} + zval *TWIG_PROPERTY_CHAR(zval *object, char *propname) { zval *tmp_name_zval, *tmp; @@ -818,10 +830,9 @@ PHP_FUNCTION(twig_template_get_attributes) tmp_properties = TWIG_GET_ARRAY_ELEMENT(tmp_class, "properties", strlen("properties")); tmp_item = TWIG_GET_ARRAY_ELEMENT_ZVAL(tmp_properties, item); - tmp_object_item = TWIG_PROPERTY(object, item); efree(class_name); - if (tmp_item || tmp_object_item || TWIG_ARRAY_KEY_EXISTS(object, item) // FIXME: Array key? is that array access here? + if (tmp_item || TWIG_HAS_PROPERTY(object, item) || TWIG_ARRAY_KEY_EXISTS(object, item) // FIXME: Array key? is that array access here? ) { if (isDefinedTest) { RETURN_TRUE; From e9c3eda05def78b46ee78cb147528776954a1550 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Mon, 18 Jul 2011 18:46:39 +0100 Subject: [PATCH 61/73] - Fixed code that was previously never hit. --- twig.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/twig.c b/twig.c index 6fc3973b8..f6f4e673c 100644 --- a/twig.c +++ b/twig.c @@ -908,11 +908,11 @@ PHP_FUNCTION(twig_template_get_attributes) if (isDefinedTest) { RETURN_FALSE; } - zval *env = TWIG_PROPERTY_CHAR(template, "env"); - if (TWIG_CALL_B_0(env, "isStrictVariables")) { + if (!TWIG_CALL_BOOLEAN(TWIG_PROPERTY_CHAR(template, "env"), "isStrictVariables")) { return; } - TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Method \"%s\" for object \"%s\" does not exist", item, TWIG_GET_CLASS(object)); + TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Method \"%s\" for object \"%s\" does not exist", Z_STRVAL_P(item), TWIG_GET_CLASS_NAME(object)); + return; } if (isDefinedTest) { efree(tmp_method_name_get); From 2dffee98e9c72dd90a337ebc6e5c61f1dc73c9ef Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Mon, 25 Jul 2011 16:24:38 +0100 Subject: [PATCH 62/73] Added a return so that the function ends after throwing an exception. This fixes a segfault too. --- twig.c | 1 + 1 file changed, 1 insertion(+) diff --git a/twig.c b/twig.c index f6f4e673c..2b369488e 100644 --- a/twig.c +++ b/twig.c @@ -774,6 +774,7 @@ PHP_FUNCTION(twig_template_get_attributes) RETURN_FALSE; } TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Item \"%s\" for \"%s\" does not exist", item, object); + return; } /* // get some information about the object From 8e11c14a5fa6d098f208755f451a2e1373042491 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Mon, 25 Jul 2011 16:34:13 +0100 Subject: [PATCH 63/73] Sigh, stupid copy and paste error prevented the last tests from passing. All is well now. --- twig.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/twig.c b/twig.c index 2b369488e..4c386583b 100644 --- a/twig.c +++ b/twig.c @@ -919,7 +919,7 @@ PHP_FUNCTION(twig_template_get_attributes) efree(tmp_method_name_get); efree(tmp_method_name_is); efree(lcItem); - RETURN_FALSE; + RETURN_TRUE; } /* if ($this->env->hasExtension('sandbox')) { From 163f80641d60dc1cc3f0bf2e5b7a33d033c2be50 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 29 Sep 2011 14:57:53 +0100 Subject: [PATCH 64/73] PHP 5.2 compatibility. --- twig.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/twig.c b/twig.c index 4c386583b..690ce8039 100644 --- a/twig.c +++ b/twig.c @@ -234,7 +234,11 @@ zval *TWIG_CALL_USER_FUNC_ARRAY(zval *object, char *function, zval *arguments) fci.function_table = EG(function_table); fci.function_name = zfunction; fci.symbol_table = NULL; +#if PHP_VERSIONID >= 50300 fci.object_ptr = object; +#else + fci.object_pp = &object; +#endif fci.retval_ptr_ptr = &retval_ptr; fci.param_count = arg_count; fci.params = args; @@ -402,7 +406,11 @@ zval *TWIG_CALL_S(zval *object, char *method, char *arg0) fci.function_table = EG(function_table); fci.function_name = zfunction; fci.symbol_table = NULL; +#if PHP_VERSIONID >= 50300 fci.object_ptr = object; +#else + fci.object_pp = &object; +#endif fci.retval_ptr_ptr = &retval_ptr; fci.param_count = 1; fci.params = args; @@ -450,7 +458,11 @@ int TWIG_CALL_Z(zval *object, char *method, zval *arg1) fci.function_table = EG(function_table); fci.function_name = zfunction; fci.symbol_table = NULL; +#if PHP_VERSIONID >= 50300 fci.object_ptr = object; +#else + fci.object_pp = &object; +#endif fci.retval_ptr_ptr = &retval_ptr; fci.param_count = 1; fci.params = args; @@ -493,7 +505,11 @@ int TWIG_CALL_ZZ(zval *object, char *method, zval *arg1, zval *arg2) fci.function_table = EG(function_table); fci.function_name = zfunction; fci.symbol_table = NULL; +#if PHP_VERSIONID >= 50300 fci.object_ptr = object; +#else + fci.object_pp = &object; +#endif fci.retval_ptr_ptr = &retval_ptr; fci.param_count = 2; fci.params = args; @@ -514,6 +530,11 @@ int TWIG_CALL_ZZ(zval *object, char *method, zval *arg1, zval *arg2) return success; } +#ifndef Z_SET_REFCOUNT_P +# define Z_SET_REFCOUNT_P(pz, rc) pz->refcount = rc +# define Z_UNSET_ISREF_P(pz) pz->is_ref = 0 +#endif + void TWIG_NEW(zval *object, char *class, zval *value) { zend_class_entry **pce; From 05cb73fe991dadd49eecd2a0bea7dc66dcb80e28 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Thu, 29 Sep 2011 15:24:59 +0100 Subject: [PATCH 65/73] More PHP 5.2 compatibility. --- twig.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/twig.c b/twig.c index 690ce8039..2cd88b37c 100644 --- a/twig.c +++ b/twig.c @@ -157,8 +157,8 @@ zval *TWIG_GET_ARRAYOBJECT_ELEMENT(zval *object, zval *offset) zval_ptr_dtor(&offset); - if (UNEXPECTED(!retval)) { - if (UNEXPECTED(!EG(exception))) { + if (!retval) { + if (!EG(exception)) { zend_error_noreturn(E_ERROR, "Undefined offset for object of type %s used as array", ce->name); } return NULL; @@ -180,8 +180,8 @@ int TWIG_ISSET_ARRAYOBJECT_ELEMENT(zval *object, zval *offset) zval_ptr_dtor(&offset); - if (UNEXPECTED(!retval)) { - if (UNEXPECTED(!EG(exception))) { + if (!retval) { + if (!EG(exception)) { zend_error_noreturn(E_ERROR, "Undefined offset for object of type %s used as array", ce->name); } return 0; From 70778119f00c91279017faabd0cce21007d73af6 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Sat, 1 Oct 2011 16:18:30 +0100 Subject: [PATCH 66/73] PHP 5.4 compatibility. --- twig.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/twig.c b/twig.c index 2cd88b37c..e00c609e4 100644 --- a/twig.c +++ b/twig.c @@ -234,7 +234,7 @@ zval *TWIG_CALL_USER_FUNC_ARRAY(zval *object, char *function, zval *arguments) fci.function_table = EG(function_table); fci.function_name = zfunction; fci.symbol_table = NULL; -#if PHP_VERSIONID >= 50300 +#if PHP_VERSION_ID >= 50300 fci.object_ptr = object; #else fci.object_pp = &object; @@ -406,7 +406,7 @@ zval *TWIG_CALL_S(zval *object, char *method, char *arg0) fci.function_table = EG(function_table); fci.function_name = zfunction; fci.symbol_table = NULL; -#if PHP_VERSIONID >= 50300 +#if PHP_VERSION_ID >= 50300 fci.object_ptr = object; #else fci.object_pp = &object; @@ -458,7 +458,7 @@ int TWIG_CALL_Z(zval *object, char *method, zval *arg1) fci.function_table = EG(function_table); fci.function_name = zfunction; fci.symbol_table = NULL; -#if PHP_VERSIONID >= 50300 +#if PHP_VERSION_ID >= 50300 fci.object_ptr = object; #else fci.object_pp = &object; @@ -505,7 +505,7 @@ int TWIG_CALL_ZZ(zval *object, char *method, zval *arg1, zval *arg2) fci.function_table = EG(function_table); fci.function_name = zfunction; fci.symbol_table = NULL; -#if PHP_VERSIONID >= 50300 +#if PHP_VERSION_ID >= 50300 fci.object_ptr = object; #else fci.object_pp = &object; From 634d8ae9bf837baaba61576f744c3260c0d10ce7 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Fri, 28 Oct 2011 11:34:18 +0200 Subject: [PATCH 67/73] Fixed compiler warnings --- twig.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/twig.c b/twig.c index e00c609e4..11b455fca 100644 --- a/twig.c +++ b/twig.c @@ -24,10 +24,12 @@ #include "php_ini.h" #include "ext/standard/info.h" #include "php_twig.h" +#include "ext/standard/php_string.h" #include "ext/standard/php_smart_str.h" #include "Zend/zend_object_handlers.h" #include "Zend/zend_interfaces.h" +#include "Zend/zend_exceptions.h" ZEND_BEGIN_ARG_INFO_EX(twig_template_get_attribute_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 6) ZEND_ARG_INFO(0, template) @@ -360,12 +362,8 @@ zval *TWIG_PROPERTY(zval *object, zval *propname) return tmp; } -zval *TWIG_HAS_PROPERTY(zval *object, zval *propname) +int TWIG_HAS_PROPERTY(zval *object, zval *propname) { - char *prot_name; - int prot_name_length; - zval *tmp = 1; - if (Z_OBJ_HT_P(object)->has_property) { return Z_OBJ_HT_P(object)->has_property(object, propname, 0 TSRMLS_CC); } @@ -386,6 +384,7 @@ zval *TWIG_PROPERTY_CHAR(zval *object, char *propname) int TWIG_CALL_B_0(zval *object, char *method) { + return 0; } zval *TWIG_CALL_S(zval *object, char *method, char *arg0) @@ -664,7 +663,6 @@ static int twig_add_dyn_property_to_class(zval **pptr TSRMLS_DC, int num_args, v static void twig_add_class_to_cache(zval *cache, zval *object, char *class_name) { - zval *tmp_class, *tmp_properties, *tmp_item, *tmp_object_item = NULL; zval *class_info, *class_methods, *class_properties; zend_class_entry *class_ce; @@ -844,7 +842,7 @@ PHP_FUNCTION(twig_template_get_attributes) } */ if (strcmp("method", type) != 0) { - zval *tmp_class, *tmp_properties, *tmp_item, *tmp_object_item = NULL; + zval *tmp_class, *tmp_properties, *tmp_item; char *class_name = NULL; class_name = TWIG_GET_CLASS_NAME(object); From b7b260bda5d2457a8880e5357d74e0e6cd1c9cd5 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Fri, 28 Oct 2011 12:15:33 +0200 Subject: [PATCH 68/73] Fixed exception message issue with an array element that didn't exist and strict variables checking turned on. --- twig.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/twig.c b/twig.c index 11b455fca..e9e307af4 100644 --- a/twig.c +++ b/twig.c @@ -580,6 +580,7 @@ char *TWIG_IMPLODE_ARRAY_KEYS(char *joiner, zval *array) { smart_str collector = { 0, 0, 0 }; + smart_str_appendl(&collector, "", 0); zend_hash_apply_with_arguments(HASH_OF(array) TSRMLS_CC, (apply_func_args_t) twig_add_array_key_to_string, 2, &collector, joiner); smart_str_0(&collector); @@ -786,13 +787,13 @@ PHP_FUNCTION(twig_template_get_attributes) if (!$this->env->isStrictVariables()) { return null; } - throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, $object)); + throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, implode(', ', array_keys($object)))); } */ if (!TWIG_CALL_BOOLEAN(TWIG_PROPERTY_CHAR(template, "env"), "isStrictVariables")) { RETURN_FALSE; } - TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Item \"%s\" for \"%s\" does not exist", item, object); + TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Item \"%s\" for \"%s\" does not exist", Z_STRVAL_P(item), TWIG_IMPLODE_ARRAY_KEYS(", ", object)); return; } /* From 7cc280810f9c00baf544970164a45f2b0598d648 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Fri, 28 Oct 2011 12:20:22 +0200 Subject: [PATCH 69/73] Added support for the new argument called 'ignoreStrictCheck'. --- twig.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/twig.c b/twig.c index e9e307af4..7e1f990fc 100644 --- a/twig.c +++ b/twig.c @@ -688,7 +688,7 @@ static void twig_add_class_to_cache(zval *cache, zval *object, char *class_name) add_assoc_zval(cache, class_name, class_info); } -/* {{{ proto mixed twig_template_get_attributes(TwigTemplate template, mixed object, mixed item, array arguments, string type, boolean isDefinedTest) +/* {{{ proto mixed twig_template_get_attributes(TwigTemplate template, mixed object, mixed item, array arguments, string type, boolean isDefinedTest, boolean ignoreStrictCheck) A C implementation of TwigTemplate::getAttribute() */ PHP_FUNCTION(twig_template_get_attributes) { @@ -700,9 +700,10 @@ PHP_FUNCTION(twig_template_get_attributes) char *type = NULL; int type_len = 0; zend_bool isDefinedTest = 0; + zend_bool ignoreStrictCheck = 0; int free_ret = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ozzasb", &template, &object, &item, &arguments, &type, &type_len, &isDefinedTest) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ozzasb", &template, &object, &item, &arguments, &type, &type_len, &isDefinedTest, &ignoreStrictCheck) == FAILURE) { return; } @@ -742,7 +743,7 @@ PHP_FUNCTION(twig_template_get_attributes) if ($isDefinedTest) { return false; } - if (!$this->env->isStrictVariables()) { + if ($ignoreStrictCheck || !$this->env->isStrictVariables()) { return null; } */ @@ -750,7 +751,7 @@ PHP_FUNCTION(twig_template_get_attributes) if (isDefinedTest) { RETURN_FALSE; } - if (!TWIG_CALL_BOOLEAN(TWIG_PROPERTY_CHAR(template, "env"), "isStrictVariables")) { + if (ignoreStrictCheck || !TWIG_CALL_BOOLEAN(TWIG_PROPERTY_CHAR(template, "env"), "isStrictVariables")) { return; } /* @@ -784,13 +785,13 @@ PHP_FUNCTION(twig_template_get_attributes) RETURN_FALSE; } /* - if (!$this->env->isStrictVariables()) { + if ($ignoreStrictCheck || !$this->env->isStrictVariables()) { return null; } throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, implode(', ', array_keys($object)))); } */ - if (!TWIG_CALL_BOOLEAN(TWIG_PROPERTY_CHAR(template, "env"), "isStrictVariables")) { + if (ignoreStrictCheck || !TWIG_CALL_BOOLEAN(TWIG_PROPERTY_CHAR(template, "env"), "isStrictVariables")) { RETURN_FALSE; } TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Item \"%s\" for \"%s\" does not exist", Z_STRVAL_P(item), TWIG_IMPLODE_ARRAY_KEYS(", ", object)); @@ -916,7 +917,7 @@ PHP_FUNCTION(twig_template_get_attributes) if ($isDefinedTest) { return false; } - if (!$this->env->isStrictVariables()) { + if ($ignoreStrictCheck || !$this->env->isStrictVariables()) { return null; } throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object))); @@ -929,7 +930,7 @@ PHP_FUNCTION(twig_template_get_attributes) if (isDefinedTest) { RETURN_FALSE; } - if (!TWIG_CALL_BOOLEAN(TWIG_PROPERTY_CHAR(template, "env"), "isStrictVariables")) { + if (ignoreStrictCheck || !TWIG_CALL_BOOLEAN(TWIG_PROPERTY_CHAR(template, "env"), "isStrictVariables")) { return; } TWIG_THROW_EXCEPTION("Twig_Error_Runtime", "Method \"%s\" for object \"%s\" does not exist", Z_STRVAL_P(item), TWIG_GET_CLASS_NAME(object)); From 952fe6f26c23383fa23e24bc7674339250aedb60 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Fri, 28 Oct 2011 12:22:28 +0200 Subject: [PATCH 70/73] Added missing format specifier to parseparameters. --- twig.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/twig.c b/twig.c index 7e1f990fc..8084dac3b 100644 --- a/twig.c +++ b/twig.c @@ -703,7 +703,7 @@ PHP_FUNCTION(twig_template_get_attributes) zend_bool ignoreStrictCheck = 0; int free_ret = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ozzasb", &template, &object, &item, &arguments, &type, &type_len, &isDefinedTest, &ignoreStrictCheck) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ozzasbb", &template, &object, &item, &arguments, &type, &type_len, &isDefinedTest, &ignoreStrictCheck) == FAILURE) { return; } From 699e1232ababcd449df33ac54789a0c67a53643e Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Fri, 28 Oct 2011 12:25:41 +0200 Subject: [PATCH 71/73] Allow any, but the first two arguments to be optional. --- twig.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/twig.c b/twig.c index 8084dac3b..9b5735f00 100644 --- a/twig.c +++ b/twig.c @@ -695,7 +695,7 @@ PHP_FUNCTION(twig_template_get_attributes) zval *template; zval *object; zval *item; - zval *arguments; + zval *arguments = NULL; zval *ret = NULL; char *type = NULL; int type_len = 0; @@ -703,10 +703,14 @@ PHP_FUNCTION(twig_template_get_attributes) zend_bool ignoreStrictCheck = 0; int free_ret = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ozzasbb", &template, &object, &item, &arguments, &type, &type_len, &isDefinedTest, &ignoreStrictCheck) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ozz|asbb", &template, &object, &item, &arguments, &type, &type_len, &isDefinedTest, &ignoreStrictCheck) == FAILURE) { return; } + if (!type) { + type = "any"; + } + /* // array if (Twig_TemplateInterface::METHOD_CALL !== $type) { From 16ca05529c44bdbf42e9f67dcf07e96eccecdef4 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Sun, 20 Nov 2011 21:25:05 +0000 Subject: [PATCH 72/73] Move files into ext/twig. --- .gitignore => ext/twig/.gitignore | 0 config.m4 => ext/twig/config.m4 | 0 php_twig.h => ext/twig/php_twig.h | 0 twig.c => ext/twig/twig.c | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename .gitignore => ext/twig/.gitignore (100%) rename config.m4 => ext/twig/config.m4 (100%) rename php_twig.h => ext/twig/php_twig.h (100%) rename twig.c => ext/twig/twig.c (100%) diff --git a/.gitignore b/ext/twig/.gitignore similarity index 100% rename from .gitignore rename to ext/twig/.gitignore diff --git a/config.m4 b/ext/twig/config.m4 similarity index 100% rename from config.m4 rename to ext/twig/config.m4 diff --git a/php_twig.h b/ext/twig/php_twig.h similarity index 100% rename from php_twig.h rename to ext/twig/php_twig.h diff --git a/twig.c b/ext/twig/twig.c similarity index 100% rename from twig.c rename to ext/twig/twig.c From f7f8346d37d41524834257b180f41fe86a096b91 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Sun, 20 Nov 2011 21:26:21 +0000 Subject: [PATCH 73/73] Switch LICENSE to Revised BSD. --- ext/twig/LICENSE | 22 ++++++++++++++++++++++ ext/twig/php_twig.h | 10 +++------- ext/twig/twig.c | 10 +++------- 3 files changed, 28 insertions(+), 14 deletions(-) create mode 100644 ext/twig/LICENSE diff --git a/ext/twig/LICENSE b/ext/twig/LICENSE new file mode 100644 index 000000000..7874a3668 --- /dev/null +++ b/ext/twig/LICENSE @@ -0,0 +1,22 @@ +Copyright (c) 2007, Michael Wallner . +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/ext/twig/php_twig.h b/ext/twig/php_twig.h index 4958d78f7..abacb57c8 100644 --- a/ext/twig/php_twig.h +++ b/ext/twig/php_twig.h @@ -4,13 +4,9 @@ +----------------------------------------------------------------------+ | Copyright (c) 2011 Derick Rethans | +----------------------------------------------------------------------+ - | This source file is subject to version 2.02 of the PHP license, | - | that is bundled with this package in the file LICENSE, and is | - | available at through the world-wide-web at | - | http://www.php.net/license/2_02.txt. | - | If you did not receive a copy of the PHP license and are unable to | - | obtain it through the world-wide-web, please send a note to | - | license@php.net so we can mail you a copy immediately. | + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met (BSD, revised). | +----------------------------------------------------------------------+ | Author: Derick Rethans | +----------------------------------------------------------------------+ diff --git a/ext/twig/twig.c b/ext/twig/twig.c index 9b5735f00..bc3a5f939 100644 --- a/ext/twig/twig.c +++ b/ext/twig/twig.c @@ -4,13 +4,9 @@ +----------------------------------------------------------------------+ | Copyright (c) 2011 Derick Rethans | +----------------------------------------------------------------------+ - | This source file is subject to version 2.02 of the PHP license, | - | that is bundled with this package in the file LICENSE, and is | - | available at through the world-wide-web at | - | http://www.php.net/license/2_02.txt. | - | If you did not receive a copy of the PHP license and are unable to | - | obtain it through the world-wide-web, please send a note to | - | license@php.net so we can mail you a copy immediately. | + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met (BSD, revised). | +----------------------------------------------------------------------+ | Author: Derick Rethans | +----------------------------------------------------------------------+