Skip to content

Commit 3aacbb6

Browse files
committed
Better field sanitation
1 parent 0423c8d commit 3aacbb6

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/PHPFUI/ORM/Table.php

+19-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function addFind(array $parameters) : \PHPFUI\ORM\DataObjectCursor
6464

6565
foreach ($parameters as $field => $value)
6666
{
67-
$baseField = $field;
67+
$baseField = $this->cleanField($field);
6868
$parts = \explode(':', $field);
6969
$direction = '';
7070

@@ -229,6 +229,11 @@ public function addSelect(string | object $field, string $as = '') : static
229229
else
230230
{
231231
$parts = \explode('.', $field);
232+
233+
foreach ($parts as $index => $part)
234+
{
235+
$parts[$index] = $this->cleanField($part);
236+
}
232237
$field = \implode('`.`', $parts);
233238
$this->selects['`' . $field . '`'] = $as;
234239
}
@@ -296,7 +301,19 @@ public static function capitalSplit(string $key) : string
296301

297302
public function cleanField(string $fieldName) : string
298303
{
299-
return \preg_replace('/[^[a-zA-Z_][a-zA-Z0-9_.$@-]{0,63}$]/', '', $fieldName); // string invalid characters since we can't use a placeholder in order and group by
304+
// Remove invalid characters (replace with underscore)
305+
$sanitized = \preg_replace('/[^a-zA-Z0-9_$]/', '', $fieldName);
306+
307+
// Remove leading/trailing underscores
308+
$sanitized = \trim($sanitized, '_');
309+
310+
// If the string is empty after sanitization, use field
311+
if (! \strlen($sanitized))
312+
{
313+
$sanitized = 'field';
314+
}
315+
316+
return $sanitized;
300317
}
301318

302319
/**

0 commit comments

Comments
 (0)