Blame view

sources/3rdparty/sabre/dav/lib/Sabre/CardDAV/AddressBookQueryParser.php 5.9 KB
03e52840d   Kload   Init
1
  <?php
6d9380f96   Cédric Dupont   Update sources OC...
2
3
4
  namespace Sabre\CardDAV;
  
  use Sabre\DAV;
03e52840d   Kload   Init
5
6
7
8
9
10
  /**
   * Parses the addressbook-query report request body.
   *
   * Whoever designed this format, and the CalDAV equivalent even more so,
   * has no feel for design.
   *
6d9380f96   Cédric Dupont   Update sources OC...
11
12
13
   * @copyright Copyright (C) 2007-2014 fruux GmbH (https://fruux.com/).
   * @author Evert Pot (http://evertpot.com/)
   * @license http://sabre.io/license/ Modified BSD License
03e52840d   Kload   Init
14
   */
6d9380f96   Cédric Dupont   Update sources OC...
15
  class AddressBookQueryParser {
03e52840d   Kload   Init
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  
      const TEST_ANYOF = 'anyof';
      const TEST_ALLOF = 'allof';
  
      /**
       * List of requested properties the client wanted
       *
       * @var array
       */
      public $requestedProperties;
  
      /**
       * The number of results the client wants
       *
       * null means it wasn't specified, which in most cases means 'all results'.
       *
       * @var int|null
       */
      public $limit;
  
      /**
       * List of property filters.
       *
       * @var array
       */
      public $filters;
  
      /**
       * Either TEST_ANYOF or TEST_ALLOF
       *
       * @var string
       */
      public $test;
  
      /**
       * DOM Document
       *
       * @var DOMDocument
       */
      protected $dom;
  
      /**
       * DOM XPath object
       *
       * @var DOMXPath
       */
      protected $xpath;
  
      /**
       * Creates the parser
       *
6d9380f96   Cédric Dupont   Update sources OC...
67
       * @param \DOMDocument $dom
03e52840d   Kload   Init
68
       */
6d9380f96   Cédric Dupont   Update sources OC...
69
      public function __construct(\DOMDocument $dom) {
03e52840d   Kload   Init
70
71
  
          $this->dom = $dom;
6d9380f96   Cédric Dupont   Update sources OC...
72
73
          $this->xpath = new \DOMXPath($dom);
          $this->xpath->registerNameSpace('card',Plugin::NS_CARDDAV);
03e52840d   Kload   Init
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  
      }
  
      /**
       * Parses the request.
       *
       * @return void
       */
      public function parse() {
  
          $filterNode = null;
  
          $limit = $this->xpath->evaluate('number(/card:addressbook-query/card:limit/card:nresults)');
          if (is_nan($limit)) $limit = null;
  
          $filter = $this->xpath->query('/card:addressbook-query/card:filter');
  
          // According to the CardDAV spec there needs to be exactly 1 filter
          // element. However, KDE 4.8.2 contains a bug that will encode 0 filter
          // elements, so this is a workaround for that.
          //
          // See: https://bugs.kde.org/show_bug.cgi?id=300047
          if ($filter->length === 0) {
              $test = null;
              $filter = null;
          } elseif ($filter->length === 1) {
              $filter = $filter->item(0);
              $test = $this->xpath->evaluate('string(@test)', $filter);
          } else {
6d9380f96   Cédric Dupont   Update sources OC...
103
              throw new DAV\Exception\BadRequest('Only one filter element is allowed');
03e52840d   Kload   Init
104
105
106
107
          }
  
          if (!$test) $test = self::TEST_ANYOF;
          if ($test !== self::TEST_ANYOF && $test !== self::TEST_ALLOF) {
6d9380f96   Cédric Dupont   Update sources OC...
108
              throw new DAV\Exception\BadRequest('The test attribute must either hold "anyof" or "allof"');
03e52840d   Kload   Init
109
110
111
112
113
114
115
116
117
118
119
120
121
122
          }
  
          $propFilters = array();
  
          $propFilterNodes = $this->xpath->query('card:prop-filter', $filter);
          for($ii=0; $ii < $propFilterNodes->length; $ii++) {
  
              $propFilters[] = $this->parsePropFilterNode($propFilterNodes->item($ii));
  
  
          }
  
          $this->filters = $propFilters;
          $this->limit = $limit;
6d9380f96   Cédric Dupont   Update sources OC...
123
          $this->requestedProperties = array_keys(DAV\XMLUtil::parseProperties($this->dom->firstChild));
03e52840d   Kload   Init
124
125
126
127
128
129
130
          $this->test = $test;
  
      }
  
      /**
       * Parses the prop-filter xml element
       *
6d9380f96   Cédric Dupont   Update sources OC...
131
       * @param \DOMElement $propFilterNode
03e52840d   Kload   Init
132
133
       * @return array
       */
6d9380f96   Cédric Dupont   Update sources OC...
134
      protected function parsePropFilterNode(\DOMElement $propFilterNode) {
03e52840d   Kload   Init
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  
          $propFilter = array();
          $propFilter['name'] = $propFilterNode->getAttribute('name');
          $propFilter['test'] = $propFilterNode->getAttribute('test');
          if (!$propFilter['test']) $propFilter['test'] = 'anyof';
  
          $propFilter['is-not-defined'] = $this->xpath->query('card:is-not-defined', $propFilterNode)->length>0;
  
          $paramFilterNodes = $this->xpath->query('card:param-filter', $propFilterNode);
  
          $propFilter['param-filters'] = array();
  
  
          for($ii=0;$ii<$paramFilterNodes->length;$ii++) {
  
              $propFilter['param-filters'][] = $this->parseParamFilterNode($paramFilterNodes->item($ii));
  
          }
          $propFilter['text-matches'] = array();
          $textMatchNodes = $this->xpath->query('card:text-match', $propFilterNode);
  
          for($ii=0;$ii<$textMatchNodes->length;$ii++) {
  
              $propFilter['text-matches'][] = $this->parseTextMatchNode($textMatchNodes->item($ii));
  
          }
  
          return $propFilter;
  
      }
  
      /**
       * Parses the param-filter element
       *
6d9380f96   Cédric Dupont   Update sources OC...
169
       * @param \DOMElement $paramFilterNode
03e52840d   Kload   Init
170
171
       * @return array
       */
6d9380f96   Cédric Dupont   Update sources OC...
172
      public function parseParamFilterNode(\DOMElement $paramFilterNode) {
03e52840d   Kload   Init
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  
          $paramFilter = array();
          $paramFilter['name'] = $paramFilterNode->getAttribute('name');
          $paramFilter['is-not-defined'] = $this->xpath->query('card:is-not-defined', $paramFilterNode)->length>0;
          $paramFilter['text-match'] = null;
  
          $textMatch = $this->xpath->query('card:text-match', $paramFilterNode);
          if ($textMatch->length>0) {
              $paramFilter['text-match'] = $this->parseTextMatchNode($textMatch->item(0));
          }
  
          return $paramFilter;
  
      }
  
      /**
       * Text match
       *
6d9380f96   Cédric Dupont   Update sources OC...
191
       * @param \DOMElement $textMatchNode
03e52840d   Kload   Init
192
193
       * @return array
       */
6d9380f96   Cédric Dupont   Update sources OC...
194
      public function parseTextMatchNode(\DOMElement $textMatchNode) {
03e52840d   Kload   Init
195
196
197
198
199
  
          $matchType = $textMatchNode->getAttribute('match-type');
          if (!$matchType) $matchType = 'contains';
  
          if (!in_array($matchType, array('contains', 'equals', 'starts-with', 'ends-with'))) {
6d9380f96   Cédric Dupont   Update sources OC...
200
              throw new DAV\Exception\BadRequest('Unknown match-type: ' . $matchType);
03e52840d   Kload   Init
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
          }
  
          $negateCondition = $textMatchNode->getAttribute('negate-condition');
          $negateCondition = $negateCondition==='yes';
          $collation = $textMatchNode->getAttribute('collation');
          if (!$collation) $collation = 'i;unicode-casemap';
  
          return array(
              'negate-condition' => $negateCondition,
              'collation' => $collation,
              'match-type' => $matchType,
              'value' => $textMatchNode->nodeValue
          );
  
  
      }
  
  }