Skip to content

Commit ca4d96b

Browse files
committed
bug #3237 Replace strtolower() with strtr() when dealing with method names (mpdude)
This PR was squashed before being merged into the 1.x branch (closes #3237). Discussion ---------- Replace strtolower() with strtr() when dealing with method names This is to improve consistency with the implementation of case-insensitivity for method names etc. in the Zend Engine: In fact, the ZE does not apply the internal `strtolower` implementation, but just maps ASCII `A-Z` to `a-z`. This was done in php/php-src@582514d to fix issues like https://bugs.php.net/bug.php?id=18556. Additionally, `strtok` might give a tiny (measurable?) performance gain over `strtolower`, plus it is not locale-dependant. Commits ------- 30fedde Replace strtolower() with strtr() when dealing with method names
2 parents 54be566 + 30fedde commit ca4d96b

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

src/ExpressionParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ public function parseAssignmentExpression()
657657
$stream->expect(Token::NAME_TYPE, null, 'Only variables can be assigned to');
658658
}
659659
$value = $token->getValue();
660-
if (\in_array(strtolower($value), ['true', 'false', 'none', 'null'])) {
660+
if (\in_array(strtr($value, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), ['true', 'false', 'none', 'null'])) {
661661
throw new SyntaxError(sprintf('You cannot assign a value to "%s".', $value), $token->getLine(), $stream->getSourceContext());
662662
}
663663
$targets[] = new AssignNameExpression($value, $token->getLine());

src/Parser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,15 +299,15 @@ public function isReservedMacroName($name)
299299
$this->reservedMacroNames = [];
300300
$r = new \ReflectionClass($this->env->getBaseTemplateClass());
301301
foreach ($r->getMethods() as $method) {
302-
$methodName = strtolower($method->getName());
302+
$methodName = strtr($method->getName(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
303303

304304
if ('get' === substr($methodName, 0, 3) && isset($methodName[3])) {
305305
$this->reservedMacroNames[] = substr($methodName, 3);
306306
}
307307
}
308308
}
309309

310-
return \in_array(strtolower($name), $this->reservedMacroNames);
310+
return \in_array(strtr($name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), $this->reservedMacroNames);
311311
}
312312

313313
public function addTrait($trait)

src/Sandbox/SecurityPolicy.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function setAllowedMethods(array $methods)
5151
{
5252
$this->allowedMethods = [];
5353
foreach ($methods as $class => $m) {
54-
$this->allowedMethods[$class] = array_map('strtolower', \is_array($m) ? $m : [$m]);
54+
$this->allowedMethods[$class] = array_map(function ($value) { return strtr($value, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); }, \is_array($m) ? $m : [$m]);
5555
}
5656
}
5757

@@ -93,7 +93,7 @@ public function checkMethodAllowed($obj, $method)
9393
}
9494

9595
$allowed = false;
96-
$method = strtolower($method);
96+
$method = strtr($method, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
9797
foreach ($this->allowedMethods as $class => $methods) {
9898
if ($obj instanceof $class) {
9999
$allowed = \in_array($method, $methods);

src/Template.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ protected function getAttribute($object, $item, array $arguments = [], $type = s
628628

629629
foreach ($ref->getMethods(\ReflectionMethod::IS_PUBLIC) as $refMethod) {
630630
// Accessing the environment from templates is forbidden to prevent untrusted changes to the environment
631-
if ('getenvironment' !== strtolower($refMethod->name)) {
631+
if ('getenvironment' !== strtr($refMethod->name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')) {
632632
$methods[] = $refMethod->name;
633633
}
634634
}
@@ -642,7 +642,7 @@ protected function getAttribute($object, $item, array $arguments = [], $type = s
642642

643643
foreach ($methods as $method) {
644644
$cache[$method] = $method;
645-
$cache[$lcName = strtolower($method)] = $method;
645+
$cache[$lcName = strtr($method, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')] = $method;
646646

647647
if ('g' === $lcName[0] && 0 === strpos($lcName, 'get')) {
648648
$name = substr($method, 3);
@@ -670,7 +670,7 @@ protected function getAttribute($object, $item, array $arguments = [], $type = s
670670
$call = false;
671671
if (isset(self::$cache[$class][$item])) {
672672
$method = self::$cache[$class][$item];
673-
} elseif (isset(self::$cache[$class][$lcItem = strtolower($item)])) {
673+
} elseif (isset(self::$cache[$class][$lcItem = strtr($item, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')])) {
674674
$method = self::$cache[$class][$lcItem];
675675
} elseif (isset(self::$cache[$class]['__call'])) {
676676
$method = $item;

0 commit comments

Comments
 (0)