From 5a935caa5db5511cf010b54e702982dff4fc369d Mon Sep 17 00:00:00 2001 From: Jared Hancock <jared@osticket.com> Date: Tue, 18 Aug 2015 09:16:20 -0500 Subject: [PATCH] orm: Implement regular expression matching --- include/class.forms.php | 3 ++- include/class.orm.php | 10 +++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/include/class.forms.php b/include/class.forms.php index 74bb0a64d..bc3ac862f 100644 --- a/include/class.forms.php +++ b/include/class.forms.php @@ -907,7 +907,8 @@ class FormField { 'placeholder' => __('Valid regular expression'), 'configuration' => array('size'=>30), 'validators' => function($self, $v) { - if (false === @preg_match($v, ' ')) + if (false === @preg_match($v, ' ') + && false === @preg_match("/$v/", ' ')) $self->addError(__('Cannot compile this regular expression')); })), ); diff --git a/include/class.orm.php b/include/class.orm.php index 7246dc1b1..a774fc65e 100644 --- a/include/class.orm.php +++ b/include/class.orm.php @@ -1807,7 +1807,7 @@ class SqlCompiler { static $operators = array( 'exact' => 1, 'isnull' => 1, 'gt' => 1, 'lt' => 1, 'gte' => 1, 'lte' => 1, - 'contains' => 1, 'like' => 1, 'startswith' => 1, 'endswith' => 1, + 'contains' => 1, 'like' => 1, 'startswith' => 1, 'endswith' => 1, 'regex' => 1, 'in' => 1, 'intersect' => 1, 'hasbit' => 1, ); @@ -2204,6 +2204,7 @@ class MySqlCompiler extends SqlCompiler { 'hasbit' => '%1$s & %2$s != 0', 'in' => array('self', '__in'), 'intersect' => array('self', '__find_in_set'), + 'regex' => array('self', '__regex'), ); // Thanks, http://stackoverflow.com/a/3683868 @@ -2265,6 +2266,13 @@ class MySqlCompiler extends SqlCompiler { return sprintf('FIND_IN_SET(%s, %s)', $b, $a); } + function __regex($a, $b) { + // Strip slashes and options + if ($b[0] == '/') + $b = preg_replace('`/[^/]*$`', '', substr($b, 1)); + return sprintf('%s REGEXP %s', $a, $this->input($b)); + } + function compileJoin($tip, $model, $alias, $info, $extra=false) { $constraints = array(); $join = ' JOIN '; -- GitLab