Skip to content

Commit 0afe1cb

Browse files
Protect insert from sql injection
1 parent 66b717e commit 0afe1cb

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

src/Eloquent/BaseBuilder.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Grimzy\LaravelMysqlSpatial\Eloquent;
4+
5+
use \Illuminate\Database\Query\Builder;
6+
7+
class BaseBuilder extends Builder
8+
{
9+
/**
10+
* Remove all of the expressions from a list of bindings.
11+
*
12+
* @param array $bindings
13+
* @return array
14+
*/
15+
protected function cleanBindings(array $bindings)
16+
{
17+
$bindings = array_map(function ($binding) {
18+
return $binding instanceof SpatialExpression ? $binding->getSpatialValue() : $binding;
19+
}, $bindings);
20+
21+
return parent::cleanBindings($bindings);
22+
}
23+
}

src/Eloquent/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public function update(array $values)
2020

2121
protected function asWKT(GeometryInterface $geometry)
2222
{
23-
return $this->getQuery()->raw("ST_GeomFromText('".$geometry->toWKT()."')");
23+
return new SpatialExpression($geometry);
2424
}
2525
}

src/Eloquent/SpatialExpression.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Grimzy\LaravelMysqlSpatial\Eloquent;
4+
5+
use Illuminate\Database\Query\Expression;
6+
7+
class SpatialExpression extends Expression
8+
{
9+
public function getValue()
10+
{
11+
return 'ST_GeomFromText(?)';
12+
}
13+
14+
public function getSpatialValue()
15+
{
16+
return $this->value->toWkt();
17+
}
18+
}

src/Eloquent/SpatialTrait.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,21 @@ public function newEloquentBuilder($query)
6161
return new Builder($query);
6262
}
6363

64+
protected function newBaseQueryBuilder()
65+
{
66+
$connection = $this->getConnection();
67+
68+
return new BaseBuilder(
69+
$connection, $connection->getQueryGrammar(), $connection->getPostProcessor()
70+
);
71+
}
72+
6473
protected function performInsert(EloquentBuilder $query, array $options = [])
6574
{
6675
foreach ($this->attributes as $key => $value) {
6776
if ($value instanceof GeometryInterface) {
6877
$this->geometries[$key] = $value; //Preserve the geometry objects prior to the insert
69-
$this->attributes[$key] = $this->getConnection()->raw(sprintf("ST_GeomFromText('%s')", $value->toWKT()));
78+
$this->attributes[$key] = new SpatialExpression($value);
7079
}
7180
}
7281

0 commit comments

Comments
 (0)