diff --git a/application/Espo/Core/Console/Commands/SetPassword.php b/application/Espo/Core/Console/Commands/SetPassword.php index b019a51a90..b43e9ad511 100644 --- a/application/Espo/Core/Console/Commands/SetPassword.php +++ b/application/Espo/Core/Console/Commands/SetPassword.php @@ -58,6 +58,8 @@ class SetPassword extends Base $password = $this->ask(); + $password = trim($password); + if (!$password) { $this->out("Password can not be empty.\n"); die; diff --git a/application/Espo/Core/Formula/Parser.php b/application/Espo/Core/Formula/Parser.php index 34b852afe8..ebcbb97b83 100644 --- a/application/Espo/Core/Formula/Parser.php +++ b/application/Espo/Core/Formula/Parser.php @@ -109,6 +109,8 @@ class Parser { $isString = false; $isSingleQuote = false; + $isComment = false; + $isLineComment = false; $modifiedString = $string; @@ -116,27 +118,31 @@ class Parser for ($i = 0; $i < strlen($string); $i++) { $isStringStart = false; - if ($string[$i] === "'" && ($i === 0 || $string[$i - 1] !== "\\")) { - if (!$isString) { - $isString = true; - $isSingleQuote = true; - $isStringStart = true; - } else { - if ($isSingleQuote) { - $isString = false; + + if (!$isLineComment && !$isComment) { + if ($string[$i] === "'" && ($i === 0 || $string[$i - 1] !== "\\")) { + if (!$isString) { + $isString = true; + $isSingleQuote = true; + $isStringStart = true; + } else { + if ($isSingleQuote) { + $isString = false; + } } - } - } else if ($string[$i] === "\"" && ($i === 0 || $string[$i - 1] !== "\\")) { - if (!$isString) { - $isString = true; - $isStringStart = true; - $isSingleQuote = false; - } else { - if (!$isSingleQuote) { - $isString = false; + } else if ($string[$i] === "\"" && ($i === 0 || $string[$i - 1] !== "\\")) { + if (!$isString) { + $isString = true; + $isStringStart = true; + $isSingleQuote = false; + } else { + if (!$isSingleQuote) { + $isString = false; + } } } } + if ($isString) { if ($string[$i] === '(' || $string[$i] === ')') { $modifiedString[$i] = '_'; @@ -144,24 +150,51 @@ class Parser $modifiedString[$i] = ' '; } } else { - if ($string[$i] === '(') { - $braceCounter++; - } - if ($string[$i] === ')') { - $braceCounter--; - } + if (!$isLineComment && !$isComment) { - if ($braceCounter === 0) { - if (!is_null($splitterIndexList)) { - if ($string[$i] === ';') { - $splitterIndexList[] = $i; + if (!$isComment) { + if ($i && $string[$i] === '/' && $string[$i - 1] === '/') { + $isLineComment = true; } } - if ($intoOneLine) { - if ($string[$i] === "\r" || $string[$i] === "\n" || $string[$i] === "\t") { - $string[$i] = ' '; + + if (!$isLineComment) { + if ($i && $string[$i] === '*' && $string[$i - 1] === '/') { + $isComment = true; } } + + if ($string[$i] === '(') { + $braceCounter++; + } + if ($string[$i] === ')') { + $braceCounter--; + } + + if ($braceCounter === 0) { + if (!is_null($splitterIndexList)) { + if ($string[$i] === ';') { + $splitterIndexList[] = $i; + } + } + if ($intoOneLine) { + if ($string[$i] === "\r" || $string[$i] === "\n" || $string[$i] === "\t") { + $string[$i] = ' '; + } + } + } + } + + if ($isLineComment) { + if ($string[$i] === "\n") { + $isLineComment = false; + } + } + + if ($isComment) { + if ($string[$i - 1] === "*" && $string[$i] === "/") { + $isComment = false; + } } } } @@ -181,6 +214,7 @@ class Parser $this->processStrings($expression, $modifiedExpression, $splitterIndexList, true); + //echo $modifiedExpression; $this->stripComments($expression, $modifiedExpression); foreach ($splitterIndexList as $i => $index) { diff --git a/tests/unit/Espo/Core/Formula/EvaluatorTest.php b/tests/unit/Espo/Core/Formula/EvaluatorTest.php index f84f2417cd..f0a0f655f8 100644 --- a/tests/unit/Espo/Core/Formula/EvaluatorTest.php +++ b/tests/unit/Espo/Core/Formula/EvaluatorTest.php @@ -188,4 +188,100 @@ class EvaluatorTest extends \PHPUnit\Framework\TestCase $this->assertEquals([0, 1, 2], $vars->target); } + + public function testComment1() + { + $expression = " + // test + \$test = '1'; + "; + + $vars = (object) []; + $this->evaluator->process($expression, null, $vars); + $this->assertEquals('1', $vars->test); + } + + public function testComment2() + { + $expression = " + // test'test + \$test = '1'; + "; + + $vars = (object) []; + $this->evaluator->process($expression, null, $vars); + $this->assertEquals('1', $vars->test); + } + + public function testComment3() + { + $expression = " + // test\"test + \$test = '1'; + "; + + $vars = (object) []; + $this->evaluator->process($expression, null, $vars); + $this->assertEquals('1', $vars->test); + } + + public function testComment4() + { + $expression = " + // test)(test + \$test = '1'; + "; + + $vars = (object) []; + $this->evaluator->process($expression, null, $vars); + $this->assertEquals('1', $vars->test); + } + + public function testComment5() + { + $expression = " + /* test'test + */ + \$test = '1'; + "; + + $vars = (object) []; + $this->evaluator->process($expression, null, $vars); + $this->assertEquals('1', $vars->test); + } + + public function testComment6() + { + $expression = " + /* test(test + */ + \$test = '1'; + "; + + $vars = (object) []; + $this->evaluator->process($expression, null, $vars); + $this->assertEquals('1', $vars->test); + } + + public function testComment7() + { + $expression = " + \$test = '/* 1 */'; + "; + + $vars = (object) []; + $this->evaluator->process($expression, null, $vars); + $this->assertEquals('/* 1 */', $vars->test); + } + + public function testComment8() + { + $expression = " + \$test = '// 1 */'; + "; + + $vars = (object) []; + $this->evaluator->process($expression, null, $vars); + $this->assertEquals('// 1 */', $vars->test); + } }