Skip to content

Commit

Permalink
Merge pull request #16 from noahheck/BackReferenceFix
Browse files Browse the repository at this point in the history
Fix back reference syntax in replacement value
  • Loading branch information
noahheck authored Apr 12, 2018
2 parents 2cb0ce2 + d9dd2da commit 11c8d4b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/EPDOStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,22 @@ private function replaceMarker($queryString, $marker, $replValue)
$marker = (preg_match("/^:/", $marker)) ? $marker : ":" . $marker;
}

$testParam = "/({$marker}(?!\w))(?=(?:[^\"']|[\"'][^\"']*[\"'])*$)/";

$this->debug("Replacing marker {marker} with value {value}",
array(
"marker" => $marker,
"value" => $replValue,
));

return preg_replace($testParam, $replValue, $queryString, 1);
$testParam = "/({$marker}(?!\w))(?=(?:[^\"']|[\"'][^\"']*[\"'])*$)/";

// Back references may be replaced in the resultant interpolatedQuery, so we need to sanitize that syntax
$cleanBackRefCharMap = ['%'=>'%%', '$'=>'$%', '\\'=>'\\%'];

$backReferenceSafeReplValue = strtr($replValue, $cleanBackRefCharMap);

$interpolatedString = preg_replace($testParam, $backReferenceSafeReplValue, $queryString, 1);

return strtr($interpolatedString, array_flip($cleanBackRefCharMap));
}

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/src/EPDOStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,4 +414,21 @@ public function testQueryIsNotChangedIfNoParametersUsedInQuery()

$this->assertEquals($query, $stmt->interpolateQuery());
}

public function testDollarSignBackReferenceSyntaxGetsOutputCorrectlyInFullQuery()
{
$pdo = $this->getPdo();

$hashedPassword = '$2y$10$yOwqQMxRo0AveSZ6I6Yhn.aMqbtGYrKQvcLGEtanplhdboUM1ffGi';

$query = "UPDATE users SET password = :password";

$stmt = $pdo->prepare($query);

$stmt->bindParam(":password", $hashedPassword);

$result = $stmt->interpolateQuery();

$this->assertEquals("UPDATE users SET password = '$hashedPassword'", $result);
}
}

0 comments on commit 11c8d4b

Please sign in to comment.