bug #23351 [Dotenv] parse concatenated variable values (xabbuh)

This PR was merged into the 3.3 branch.

Discussion
----------

[Dotenv] parse concatenated variable values

| Q             | A
| ------------- | ---
| Branch?       | 3.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #23306
| License       | MIT
| Doc PR        |

Commits
-------

aaaf64ddde [Dotenv] parse concatenated variable values
This commit is contained in:
Fabien Potencier 2017-07-03 12:28:08 +03:00
commit 3169e12bbf
2 changed files with 93 additions and 53 deletions

View File

@ -168,73 +168,110 @@ final class Dotenv
throw $this->createFormatException('Whitespace are not supported before the value');
}
$value = '';
$singleQuoted = false;
$notQuoted = false;
if ("'" === $this->data[$this->cursor]) {
$singleQuoted = true;
++$this->cursor;
while ("\n" !== $this->data[$this->cursor]) {
if ("'" === $this->data[$this->cursor]) {
if ($this->cursor + 1 === $this->end) {
$v = '';
do {
if ("'" === $this->data[$this->cursor]) {
$value = '';
++$this->cursor;
while ("\n" !== $this->data[$this->cursor]) {
if ("'" === $this->data[$this->cursor]) {
break;
}
if ("'" !== $this->data[$this->cursor + 1]) {
break;
$value .= $this->data[$this->cursor];
++$this->cursor;
if ($this->cursor === $this->end) {
throw $this->createFormatException('Missing quote to end the value');
}
}
if ("\n" === $this->data[$this->cursor]) {
throw $this->createFormatException('Missing quote to end the value');
}
++$this->cursor;
$v .= $value;
} elseif ('"' === $this->data[$this->cursor]) {
$value = '';
++$this->cursor;
while ('"' !== $this->data[$this->cursor] || ('\\' === $this->data[$this->cursor - 1] && '\\' !== $this->data[$this->cursor - 2])) {
$value .= $this->data[$this->cursor];
++$this->cursor;
if ($this->cursor === $this->end) {
throw $this->createFormatException('Missing quote to end the value');
}
}
if ("\n" === $this->data[$this->cursor]) {
throw $this->createFormatException('Missing quote to end the value');
}
++$this->cursor;
$value = str_replace(array('\\\\', '\\"', '\r', '\n'), array('\\', '"', "\r", "\n"), $value);
$resolvedValue = $value;
$resolvedValue = $this->resolveVariables($resolvedValue);
$resolvedValue = $this->resolveCommands($resolvedValue);
$v .= $resolvedValue;
} else {
$value = '';
$prevChr = $this->data[$this->cursor - 1];
while ($this->cursor < $this->end && !in_array($this->data[$this->cursor], array("\n", '"', "'"), true) && !((' ' === $prevChr || "\t" === $prevChr) && '#' === $this->data[$this->cursor])) {
if ('\\' === $this->data[$this->cursor] && isset($this->data[$this->cursor + 1]) && ('"' === $this->data[$this->cursor + 1] || "'" === $this->data[$this->cursor + 1])) {
++$this->cursor;
}
$value .= $prevChr = $this->data[$this->cursor];
if ('$' === $this->data[$this->cursor] && isset($this->data[$this->cursor + 1]) && '(' === $this->data[$this->cursor + 1]) {
++$this->cursor;
$value .= '('.$this->lexNestedExpression().')';
}
++$this->cursor;
}
$value .= $this->data[$this->cursor];
++$this->cursor;
$value = rtrim($value);
$resolvedValue = $value;
$resolvedValue = $this->resolveVariables($resolvedValue);
$resolvedValue = $this->resolveCommands($resolvedValue);
if ($this->cursor === $this->end) {
throw $this->createFormatException('Missing quote to end the value');
}
}
if ("\n" === $this->data[$this->cursor]) {
throw $this->createFormatException('Missing quote to end the value');
}
++$this->cursor;
} elseif ('"' === $this->data[$this->cursor]) {
++$this->cursor;
while ('"' !== $this->data[$this->cursor] || ('\\' === $this->data[$this->cursor - 1] && '\\' !== $this->data[$this->cursor - 2])) {
$value .= $this->data[$this->cursor];
++$this->cursor;
if ($this->cursor === $this->end) {
throw $this->createFormatException('Missing quote to end the value');
}
}
if ("\n" === $this->data[$this->cursor]) {
throw $this->createFormatException('Missing quote to end the value');
}
++$this->cursor;
$value = str_replace(array('\\\\', '\\"', '\r', '\n'), array('\\', '"', "\r", "\n"), $value);
} else {
$notQuoted = true;
$prevChr = $this->data[$this->cursor - 1];
while ($this->cursor < $this->end && "\n" !== $this->data[$this->cursor] && !((' ' === $prevChr || "\t" === $prevChr) && '#' === $this->data[$this->cursor])) {
if ('\\' === $this->data[$this->cursor] && isset($this->data[$this->cursor + 1]) && ('"' === $this->data[$this->cursor + 1] || "'" === $this->data[$this->cursor + 1])) {
++$this->cursor;
if ($resolvedValue === $value && preg_match('/\s+/', $value)) {
throw $this->createFormatException('A value containing spaces must be surrounded by quotes');
}
$value .= $prevChr = $this->data[$this->cursor];
++$this->cursor;
$v .= $resolvedValue;
if ($this->cursor < $this->end && '#' === $this->data[$this->cursor]) {
break;
}
}
$value = rtrim($value);
}
} while ($this->cursor < $this->end && "\n" !== $this->data[$this->cursor]);
$this->skipEmptyLines();
$currentValue = $value;
if (!$singleQuoted) {
$value = $this->resolveVariables($value);
$value = $this->resolveCommands($value);
return $v;
}
private function lexNestedExpression()
{
++$this->cursor;
$value = '';
while ("\n" !== $this->data[$this->cursor] && ')' !== $this->data[$this->cursor]) {
$value .= $this->data[$this->cursor];
if ('(' === $this->data[$this->cursor]) {
$value .= $this->lexNestedExpression().')';
}
++$this->cursor;
if ($this->cursor === $this->end) {
throw $this->createFormatException('Missing closing parenthesis.');
}
}
if ($notQuoted && $currentValue == $value && preg_match('/\s+/', $value)) {
throw $this->createFormatException('A value containing spaces must be surrounded by quotes');
if ("\n" === $this->data[$this->cursor]) {
throw $this->createFormatException('Missing closing parenthesis.');
}
return $value;

View File

@ -85,7 +85,6 @@ class DotenvTest extends TestCase
array("FOO='bar'\n", array('FOO' => 'bar')),
array("FOO='bar\"foo'\n", array('FOO' => 'bar"foo')),
array("FOO=\"bar\\\"foo\"\n", array('FOO' => 'bar"foo')),
array("FOO='bar''foo'\n", array('FOO' => 'bar\'foo')),
array('FOO="bar\nfoo"', array('FOO' => "bar\nfoo")),
array('FOO="bar\rfoo"', array('FOO' => "bar\rfoo")),
array('FOO=\'bar\nfoo\'', array('FOO' => 'bar\nfoo')),
@ -99,6 +98,10 @@ class DotenvTest extends TestCase
array('FOO=\\"BAR', array('FOO' => '"BAR')),
// concatenated values
array("FOO='bar''foo'\n", array('FOO' => 'barfoo')),
array("FOO='bar '' baz'", array('FOO' => 'bar baz')),
array("FOO=bar\nBAR='baz'\"\$FOO\"", array('FOO' => 'bar', 'BAR' => 'bazbar')),
array("FOO='bar '\\'' baz'", array('FOO' => "bar ' baz")),
// comments
array("#FOO=bar\nBAR=foo", array('BAR' => 'foo')),