diff --git a/src/Latte/Compiler/Compiler.php b/src/Latte/Compiler/Compiler.php index 8da2d5947..7c2a36036 100644 --- a/src/Latte/Compiler/Compiler.php +++ b/src/Latte/Compiler/Compiler.php @@ -426,7 +426,7 @@ private function processHtmlTagEnd(Token $token) private function processHtmlAttributeBegin(Token $token) { - if (strncmp($token->name, Parser::N_PREFIX, strlen(Parser::N_PREFIX)) === 0) { + if (Helpers::startsWith($token->name, Parser::N_PREFIX)) { $name = substr($token->name, strlen(Parser::N_PREFIX)); if (isset($this->htmlNode->macroAttrs[$name])) { throw new CompileException("Found multiple attributes $token->name."); @@ -452,7 +452,7 @@ private function processHtmlAttributeBegin(Token $token) $context = NULL; if (in_array($this->contentType, [self::CONTENT_HTML, self::CONTENT_XHTML], TRUE)) { $lower = strtolower($token->name); - if (substr($lower, 0, 2) === 'on') { + if (Helpers::startsWith($lower, 'on')) { $context = self::CONTENT_JS; } elseif ($lower === 'style') { $context = self::CONTENT_CSS; @@ -543,7 +543,7 @@ public function closeMacro($name, $args = NULL, $modifiers = NULL, $isRightmost $node = $this->macroNode; if (!$node || ($node->name !== $name && '' !== $name) || $modifiers - || ($args && $node->args && strncmp("$node->args ", "$args ", strlen($args) + 1)) + || ($args && $node->args && !Helpers::startsWith("$node->args ", "$args ")) || $nPrefix !== $node->prefix ) { $name = $nPrefix diff --git a/src/Latte/Compiler/Parser.php b/src/Latte/Compiler/Parser.php index 4ad78843a..b38c511c3 100644 --- a/src/Latte/Compiler/Parser.php +++ b/src/Latte/Compiler/Parser.php @@ -86,7 +86,7 @@ class Parser */ public function parse($input) { - if (substr($input, 0, 3) === "\xEF\xBB\xBF") { // BOM + if (Helpers::startsWith($input, "\xEF\xBB\xBF")) { // BOM $input = substr($input, 3); } @@ -195,7 +195,7 @@ private function contextHtmlTag() $token->value = isset($matches['value']) ? $matches['value'] : ''; if ($token->value === '"' || $token->value === "'") { // attribute = "' - if (strncmp($token->name, self::N_PREFIX, strlen(self::N_PREFIX)) === 0) { + if (Helpers::startsWith($token->name, self::N_PREFIX)) { $token->value = ''; if ($m = $this->match('~(.*?)' . $matches['value'] . '~xsi')) { $token->value = $m[1]; diff --git a/src/Latte/Helpers.php b/src/Latte/Helpers.php index fc9a11ded..f35156206 100644 --- a/src/Latte/Helpers.php +++ b/src/Latte/Helpers.php @@ -62,4 +62,14 @@ public static function removeFilter(& $modifier, $filter) return (bool) $found; } + + /** + * Starts the $haystack string with the prefix $needle? + * @return bool + */ + public static function startsWith($haystack, $needle) + { + return strncmp($haystack, $needle, strlen($needle)) === 0; + } + } diff --git a/src/Latte/Loaders/FileLoader.php b/src/Latte/Loaders/FileLoader.php index 37677ad00..4e171b793 100644 --- a/src/Latte/Loaders/FileLoader.php +++ b/src/Latte/Loaders/FileLoader.php @@ -34,7 +34,7 @@ public function __construct($baseDir = NULL) public function getContent($file) { $file = $this->baseDir . $file; - if ($this->baseDir && strncmp($this->normalizePath($file), $this->baseDir, strlen($this->baseDir))) { + if ($this->baseDir && !Latte\Helpers::startsWith($this->normalizePath($file), $this->baseDir)) { throw new \RuntimeException("Template '$file' is not within the allowed path '$this->baseDir'."); } elseif (!is_file($file)) { diff --git a/src/Latte/Macros/CoreMacros.php b/src/Latte/Macros/CoreMacros.php index da9c18743..4dd58815b 100644 --- a/src/Latte/Macros/CoreMacros.php +++ b/src/Latte/Macros/CoreMacros.php @@ -164,7 +164,7 @@ public function macroElse(MacroNode $node, PhpWriter $writer) if ($node->modifiers) { throw new CompileException('Modifiers are not allowed in ' . $node->getNotation()); } elseif ($node->args) { - $hint = substr($node->args, 0, 2) === 'if' ? ', did you mean {elseif}?' : ''; + $hint = Helpers::startsWith($node->args, 'if') ? ', did you mean {elseif}?' : ''; throw new CompileException('Arguments are not allowed in ' . $node->getNotation() . $hint); } $ifNode = $node->parentNode; @@ -261,7 +261,7 @@ public function macroUse(MacroNode $node, PhpWriter $writer) public function macroCapture(MacroNode $node, PhpWriter $writer) { $variable = $node->tokenizer->fetchWord(); - if (substr($variable, 0, 1) !== '$') { + if (!Helpers::startsWith($variable, '$')) { throw new CompileException("Invalid capture block variable '$variable'"); } $this->checkExtraArgs($node);