Skip to content

Commit 012d643

Browse files
authored
Merge pull request #1322 from mstilkerich/fix_addressbook_query_paramfilter
Fixes for addressbook-query filters
2 parents 82ea0e9 + b5792d3 commit 012d643

File tree

2 files changed

+42
-15
lines changed

2 files changed

+42
-15
lines changed

lib/CardDAV/Plugin.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -587,14 +587,21 @@ protected function validateParamFilters(array $vProperties, array $filters, $tes
587587
foreach ($vProperties as $vProperty) {
588588
// If we got all the way here, we'll need to validate the
589589
// text-match filter.
590-
$success = DAV\StringUtil::textMatch($vProperty[$filter['name']]->getValue(), $filter['text-match']['value'], $filter['text-match']['collation'], $filter['text-match']['match-type']);
590+
if (isset($vProperty[$filter['name']])) {
591+
$success = DAV\StringUtil::textMatch(
592+
$vProperty[$filter['name']]->getValue(),
593+
$filter['text-match']['value'],
594+
$filter['text-match']['collation'],
595+
$filter['text-match']['match-type']
596+
);
597+
if ($filter['text-match']['negate-condition']) {
598+
$success = !$success;
599+
}
600+
}
591601
if ($success) {
592602
break;
593603
}
594604
}
595-
if ($filter['text-match']['negate-condition']) {
596-
$success = !$success;
597-
}
598605
} // else
599606

600607
// There are two conditions where we can already determine whether
@@ -628,15 +635,15 @@ protected function validateTextMatches(array $texts, array $filters, $test)
628635
$success = false;
629636
foreach ($texts as $haystack) {
630637
$success = DAV\StringUtil::textMatch($haystack, $filter['value'], $filter['collation'], $filter['match-type']);
638+
if ($filter['negate-condition']) {
639+
$success = !$success;
640+
}
631641

632642
// Breaking on the first match
633643
if ($success) {
634644
break;
635645
}
636646
}
637-
if ($filter['negate-condition']) {
638-
$success = !$success;
639-
}
640647

641648
if ($success && 'anyof' === $test) {
642649
return true;

tests/Sabre/CardDAV/ValidateFilterTest.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ public function data()
3030
VERSION:3.0
3131
ORG:Company;
3232
TITLE:Title
33+
ITEM4.TEL:(111) 11 11 11
34+
ITEM5.TEL:(6) 66 66 66 66
35+
ITEM6.TEL:(77) 777 77 77
3336
TEL;TYPE=IPHONE;TYPE=pref:(222) 22 22 22
3437
TEL;TYPE=HOME:(33) 333 66 66
3538
TEL;TYPE=WORK:(444) 44 44 44
3639
TEL;TYPE=MAIN:(55) 555 55 55
37-
ITEM4.TEL:(111) 11 11 11
38-
ITEM5.TEL:(6) 66 66 66 66
39-
ITEM6.TEL:(77) 777 77 77
4040
UID:3151DE6A-BC35-4612-B340-B53A034A2B27
4141
ITEM1.EMAIL:1111@111.com
4242
ITEM2.EMAIL:bbbbb@bbbb.com
@@ -133,19 +133,36 @@ public function data()
133133
$filter14['text-matches'][0]['value'] = 'bing';
134134
$filter14['text-matches'][0]['negate-condition'] = true;
135135

136+
// Check if there is an EMAIL address that does not have the 111.com domain
137+
$filterEmailWithoutSpecificDomain = $filter11;
138+
$filterEmailWithoutSpecificDomain['name'] = 'email';
139+
$filterEmailWithoutSpecificDomain['text-matches'][0]['value'] = '@111.com';
140+
$filterEmailWithoutSpecificDomain['text-matches'][0]['negate-condition'] = true;
141+
136142
// Param filter with text
143+
// Check there is a TEL;TYPE that contains WORK
137144
$filter15 = $filter5;
138145
$filter15['param-filters'][0]['text-match'] = [
139146
'match-type' => 'contains',
140147
'value' => 'WORK',
141148
'collation' => 'i;octet',
142149
'negate-condition' => false,
143150
];
151+
152+
// Check if there is a TEL;TYPE that does not contain WORK
144153
$filter16 = $filter15;
145154
$filter16['param-filters'][0]['text-match']['negate-condition'] = true;
146155

156+
// Check there is a TEL;TYPE that does not contain OTHER
157+
// All TEL properties with a TYPE parameter match this
158+
$filterNoTelWithTypeOther = $filter16;
159+
$filterNoTelWithTypeOther['param-filters'][0]['text-match']['value'] = 'OTHER';
160+
147161
// Param filter + text filter
148162
$filter17 = $filter5;
163+
164+
// Matches if the VCard contains a TEL property that either has a TYPE property defined (-> true),
165+
// or that has a value containing 444 (true)
149166
$filter17['test'] = 'anyof';
150167
$filter17['text-matches'][] = [
151168
'match-type' => 'contains',
@@ -154,6 +171,8 @@ public function data()
154171
'negate-condition' => false,
155172
];
156173

174+
// Matches if the VCard contains a TEL property that has a TYPE property defined
175+
// AND that has a value NOT containing 444 -> there is 3 properties matching these criteria
157176
$filter18 = $filter17;
158177
$filter18['text-matches'][0]['negate-condition'] = true;
159178

@@ -179,7 +198,7 @@ public function data()
179198
[$body1, [$filter6], 'anyof', false, 'TEL;FOO is not defined, so this should return false'],
180199

181200
[$body1, [$filter7], 'anyof', false, 'TEL;TYPE is defined, so this should return false'],
182-
[$body1, [$filter8], 'anyof', true, 'TEL;TYPE is not defined, so this should return true'],
201+
[$body1, [$filter8], 'anyof', true, 'TEL;FOO is not defined, so this should return true'],
183202

184203
// Combined parameters
185204
[$body1, [$filter9], 'anyof', true],
@@ -190,15 +209,16 @@ public function data()
190209
[$body1, [$filter12], 'anyof', false],
191210
[$body1, [$filter13], 'anyof', false],
192211
[$body1, [$filter14], 'anyof', true],
212+
[$body1, [$filterEmailWithoutSpecificDomain], 'anyof', true, 'EMAIL properties with other domain exists, so this should return true'],
193213

194214
// Param filter with text-match
195-
[$body1, [$filter15], 'anyof', true],
196-
[$body1, [$filter16], 'anyof', false],
215+
[$body1, [$filter15], 'anyof', true, 'TEL;TYPE with value WORK exists, so this should return true'],
216+
[$body1, [$filter16], 'anyof', true, 'Some TEL;TYPE that do not match WORK exist. Match result is inverted, so this should return true'],
217+
[$body1, [$filterNoTelWithTypeOther], 'anyof', true, 'No TEL;TYPE contains OTHER. Match result is inverted, so this should return true'],
197218

198219
// Param filter + text filter
199220
[$body1, [$filter17], 'anyof', true],
200-
[$body1, [$filter18], 'anyof', false],
201-
[$body1, [$filter18], 'anyof', false],
221+
[$body1, [$filter18], 'anyof', true],
202222
];
203223
}
204224
}

0 commit comments

Comments
 (0)