From 12cddf20e3bbd002532354435bc93527cfe62204 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Thu, 5 Jul 2012 21:52:40 +0200 Subject: [PATCH] [DDC-1900] Throw exception when overwriting internal function. --- lib/Doctrine/ORM/Configuration.php | 24 +++++++++++++++++++++--- lib/Doctrine/ORM/ORMException.php | 5 +++++ lib/Doctrine/ORM/Query/Parser.php | 16 ++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/Configuration.php b/lib/Doctrine/ORM/Configuration.php index 554f77f66..4e8fba4ea 100644 --- a/lib/Doctrine/ORM/Configuration.php +++ b/lib/Doctrine/ORM/Configuration.php @@ -380,6 +380,10 @@ class Configuration extends \Doctrine\DBAL\Configuration */ public function addCustomStringFunction($name, $className) { + if (Query\Parser::isInternalFunction($name)) { + throw ORMException::overwriteInternalDQLFunctionNotAllowed($name); + } + $this->_attributes['customStringFunctions'][strtolower($name)] = $className; } @@ -410,7 +414,9 @@ class Configuration extends \Doctrine\DBAL\Configuration */ public function setCustomStringFunctions(array $functions) { - $this->_attributes['customStringFunctions'] = array_change_key_case($functions); + foreach ($functions as $name => $className) { + $this->addCustomStringFunction($name, $className); + } } /** @@ -425,6 +431,10 @@ class Configuration extends \Doctrine\DBAL\Configuration */ public function addCustomNumericFunction($name, $className) { + if (Query\Parser::isInternalFunction($name)) { + throw ORMException::overwriteInternalDQLFunctionNotAllowed($name); + } + $this->_attributes['customNumericFunctions'][strtolower($name)] = $className; } @@ -455,7 +465,9 @@ class Configuration extends \Doctrine\DBAL\Configuration */ public function setCustomNumericFunctions(array $functions) { - $this->_attributes['customNumericFunctions'] = array_change_key_case($functions); + foreach ($functions as $name => $className) { + $this->addCustomNumericFunction($name, $className); + } } /** @@ -470,6 +482,10 @@ class Configuration extends \Doctrine\DBAL\Configuration */ public function addCustomDatetimeFunction($name, $className) { + if (Query\Parser::isInternalFunction($name)) { + throw ORMException::overwriteInternalDQLFunctionNotAllowed($name); + } + $this->_attributes['customDatetimeFunctions'][strtolower($name)] = $className; } @@ -500,7 +516,9 @@ class Configuration extends \Doctrine\DBAL\Configuration */ public function setCustomDatetimeFunctions(array $functions) { - $this->_attributes['customDatetimeFunctions'] = array_change_key_case($functions); + foreach ($functions as $name => $className) { + $this->addCustomDatetimeFunction($name, $className); + } } /** diff --git a/lib/Doctrine/ORM/ORMException.php b/lib/Doctrine/ORM/ORMException.php index cc181973f..15ffb638c 100644 --- a/lib/Doctrine/ORM/ORMException.php +++ b/lib/Doctrine/ORM/ORMException.php @@ -149,4 +149,9 @@ class ORMException extends Exception { return new self("The identifier $fieldName is missing for a query of " . $className); } + + public static function overwriteInternalDQLFunctionNotAllowed($functionName) + { + return new self("It is not allowed to overwrite internal function '$functionName' in the DQL parser through user-defined functions."); + } } diff --git a/lib/Doctrine/ORM/Query/Parser.php b/lib/Doctrine/ORM/Query/Parser.php index fcf021cc0..1e1919d54 100644 --- a/lib/Doctrine/ORM/Query/Parser.php +++ b/lib/Doctrine/ORM/Query/Parser.php @@ -136,6 +136,22 @@ class Parser */ private $_identVariableExpressions = array(); + /** + * Check if a function is internally defined. Used to prevent overwriting + * of built-in functions through user-defined functions. + * + * @param string $functionName + * @return bool + */ + static public function isInternalFunction($functionName) + { + $functionName = strtolower($functionName); + + return isset(self::$_STRING_FUNCTIONS[$functionName]) + || isset(self::$_DATETIME_FUNCTIONS[$functionName]) + || isset(self::$_NUMERIC_FUNCTIONS[$functionName]); + } + /** * Creates a new query parser object. *