Commit 67370fc8 authored by Côme Chilliet's avatar Côme Chilliet
Browse files

Optimisation of ldapFilter

parent 1a019950
......@@ -38,8 +38,8 @@ class ldapFilter
{
static $operators = array('!', '&', '|');
var $operator;
var $subparts;
protected $operator;
protected $subparts;
function __construct($operator, $subparts)
{
......@@ -72,6 +72,16 @@ class ldapFilter
}
}
function getOperator()
{
return $this->operator;
}
function getSubparts()
{
return $this->subparts;
}
static function parse($filter)
{
// Remove starting and ending parenthesis
......@@ -110,9 +120,23 @@ class ldapFilterLeaf extends ldapFilter
{
static $operators = array('=','=~','>','>=','<','<=');
protected $pattern;
function __construct($left, $operator, $right)
{
parent::__construct($operator, array($left, $right));
if (($this->operator == '=') || ($this->operator == '=~')) {
$prefix = '';
$suffix = '';
if (preg_match('/^\\*/', $this->subparts[1])) {
$prefix = '.*';
}
if (preg_match('/\\*$/', $this->subparts[1])) {
$suffix = '.*';
}
$search = preg_replace(array('/^\\*/','/\\*$/'), '', $this->subparts[1]);
$this->pattern = '/^'.$prefix.preg_quote($search, '/').$suffix.'$/';
}
}
function __toString()
......@@ -132,16 +156,7 @@ class ldapFilterLeaf extends ldapFilter
case '=~':
trigger_error('Filter apply might not work as expected');
case '=':
$prefix = '';
$suffix = '';
if (preg_match('/^\\*/', $this->subparts[1])) {
$prefix = '.*';
}
if (preg_match('/\\*$/', $this->subparts[1])) {
$suffix = '.*';
}
$search = preg_replace(array('/^\\*/','/\\*$/'), '', $this->subparts[1]);
if (preg_match('/^'.$prefix.preg_quote($search, '/').$suffix.'$/', $value)) {
if (preg_match($this->pattern, $value)) {
return TRUE;
}
break;
......@@ -177,16 +192,19 @@ class ldapFilterLeaf extends ldapFilter
function fdTemplateFilter($filter)
{
if ($filter instanceof ldapFilterLeaf) {
if ($filter->operator == '=') {
$filter->subparts[1] = $filter->subparts[0].':'.$filter->subparts[1];
$filter->subparts[0] = 'fdTemplateField';
if ($filter->getOperator() == '=') {
$subparts = $filter->getSubparts();
return new ldapFilterLeaf('fdTemplateField', '=', $subparts[0].':'.$subparts[1]);
} else {
trigger_error('Not able to adapt this filter for templates');
}
} else {
foreach ($filter->subparts as &$subpart) {
$subparts = $filter->getSubparts();
foreach ($subparts as &$subpart) {
$subpart = fdTemplateFilter($subpart);
}
unset($subpart);
return new ldapFilter($filter->getOperator(), $subparts);
}
return $filter;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment