Skip to content

Commit dfc4ff9

Browse files
author
Jack Bates
committed
* Working filter support for REPORT responses - at least time-range
* TODO Move _parseComponent into separate file
1 parent 36d31ea commit dfc4ff9

File tree

2 files changed

+93
-266
lines changed

2 files changed

+93
-266
lines changed

lib/HTTP/CalDAV/Server.php

+71-223
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* @author Jack Bates <[email protected]>
2121
* @copyright 2006 The PHP Group
2222
* @license PHP License 3.0 http://www.php.net/license/3_0.txt
23-
* @version CVS: $Id: Server.php,v 1.3 2006/04/13 21:14:17 jablko Exp $
23+
* @version CVS: $Id: Server.php,v 1.4 2006/04/13 22:33:13 jablko Exp $
2424
* @link http://pear.php.net/package/HTTP_CalDAV_Server
2525
* @see HTTP_WebDAV_Server
2626
*/
@@ -37,7 +37,7 @@
3737
* @author Jack Bates <[email protected]>
3838
* @copyright 2006 The PHP Group
3939
* @license PHP License 3.0 http://www.php.net/license/3_0.txt
40-
* @version CVS: $Id: Server.php,v 1.3 2006/04/13 21:14:17 jablko Exp $
40+
* @version CVS: $Id: Server.php,v 1.4 2006/04/13 22:33:13 jablko Exp $
4141
* @link http://pear.php.net/package/HTTP_CalDAV_Server
4242
* @see HTTP_WebDAV_Server
4343
*/
@@ -86,177 +86,6 @@ function report_request_helper(&$options)
8686
return true;
8787
}
8888

89-
/**
90-
* REPORT response helper - format REPORT response
91-
*
92-
* @param options
93-
* @return void
94-
* @access public
95-
*/
96-
function report_response_helper($options, $files)
97-
{
98-
$responses = array();
99-
100-
// now loop over all returned files
101-
foreach ($files as $file) {
102-
103-
// collect namespaces here
104-
$ns_hash = array('urn:ietf:params:xml:ns:caldav' => 'C');
105-
106-
$response = array();
107-
108-
$response['href'] = $this->getHref($file['path']);
109-
if (isset($file['href'])) {
110-
$response['href'] = $file['href'];
111-
}
112-
113-
$response['propstat'] = array();
114-
115-
// nothing to do if no properties were returend
116-
if (isset($file['props']) && is_array($file['props'])) {
117-
118-
// now loop over all returned properties
119-
foreach ($file['props'] as $prop) {
120-
$status = '200 OK';
121-
122-
// as a convenience feature we do not require user handlers
123-
// restrict returned properties to the requested ones
124-
// here we ignore unrequested entries
125-
switch ($options['props']) {
126-
case 'propname':
127-
128-
// only names of all existing properties were requested
129-
// so remove values
130-
unset($prop['value']);
131-
132-
case 'allprop':
133-
if (isset($prop['status'])) {
134-
$status = $prop['status'];
135-
}
136-
137-
if (!isset($response['propstat'][$status])) {
138-
$response['propstat'][$status] = array();
139-
}
140-
141-
$response['propstat'][$status][] = $prop;
142-
break;
143-
144-
default:
145-
146-
// search property name in requested properties
147-
foreach($options['props'] as $reqprop) {
148-
if ($reqprop['name'] == $prop['name'] &&
149-
$reqprop['ns'] == $prop['ns']) {
150-
if (isset($prop['status'])) {
151-
$status = $prop['status'];
152-
}
153-
154-
if (!isset($response['propstat'][$status])) {
155-
$response['propstat'][$status] = array();
156-
}
157-
158-
$response['propstat'][$status][] = $prop;
159-
break (2);
160-
}
161-
}
162-
163-
continue (2);
164-
}
165-
166-
// namespace handling
167-
if (empty($prop['ns']) || // empty namespace
168-
$prop['ns'] == 'DAV:' || // default namespace
169-
isset($ns_hash[$prop['ns']])) { // already known
170-
continue;
171-
}
172-
173-
// register namespace
174-
$ns_hash[$prop['ns']] = 'ns' . count($ns_hash);
175-
}
176-
}
177-
178-
// also need empty entries for properties requested
179-
// but for which no values where returned
180-
if (isset($options['props']) && is_array($options['props'])) {
181-
182-
// now loop over all requested properties
183-
foreach ($options['props'] as $reqprop) {
184-
$status = '404 Not Found';
185-
186-
// check if property exists in result
187-
foreach ($file['props'] as $prop) {
188-
if ($reqprop['name'] == $prop['name'] &&
189-
$reqprop['ns'] == $prop['ns']) {
190-
continue (2);
191-
}
192-
}
193-
194-
if ($reqprop['name'] == 'lockdiscovery' &&
195-
$reqprop['ns'] == 'DAV:' &&
196-
method_exists($this, 'getLocks')) {
197-
198-
$status = '200 OK';
199-
if (!isset($response['propstat'][$status])) {
200-
$response['propstat'][$status] = array();
201-
}
202-
203-
$response['propstat'][$status][] =
204-
$this->mkprop('DAV:', 'lockdiscovery',
205-
$this->getLocks($file['path']));
206-
continue;
207-
}
208-
209-
if ($reqprop['name'] == 'calendar-data' &&
210-
$reqprop['ns'] == 'urn:ietf:params:xml:ns:caldav' &&
211-
method_exists($this, 'get')) {
212-
213-
$prop = $this->_calendarData($reqprop, $file, $options);
214-
if (isset($prop)) {
215-
$status = '200 OK';
216-
if (isset($prop['status'])) {
217-
$status = $prop['status'];
218-
}
219-
} else {
220-
$prop = HTTP_CalDAV_Server::calDavProp(
221-
'calendar-data');
222-
}
223-
224-
if (!isset($response['propstat'][$status])) {
225-
$response['propstat'][$status] = array();
226-
}
227-
228-
$response['propstat'][$status][] = $prop;
229-
continue;
230-
}
231-
232-
if (!isset($response['propstat'][$status])) {
233-
$response['propstat'][$status] = array();
234-
}
235-
236-
// add empty value for this property
237-
$response['propstat'][$status][] =
238-
$this->mkprop($reqprop['ns'], $reqprop['name'],
239-
null);
240-
241-
// namespace handling
242-
if (empty($reqprop['ns']) || // empty namespace
243-
$reqprop['ns'] == 'DAV:' || // default namespace
244-
isset($ns_hash[$reqprop['ns']])) { // already known
245-
continue;
246-
}
247-
248-
// register namespace
249-
$ns_hash[$reqprop['ns']] = 'ns' . count($ns_hash);
250-
}
251-
}
252-
253-
$response['ns_hash'] = $ns_hash;
254-
$responses[] = $response;
255-
}
256-
257-
$this->_multistatus($responses);
258-
}
259-
26089
/**
26190
* REPORT method wrapper
26291
*
@@ -285,64 +114,88 @@ function report_wrapper()
285114
}
286115

287116
/* Format REPORT response */
288-
$this->report_response_helper($options, $files);
117+
118+
// TODO Make ns_hash a class variable so we can prettify C:
119+
// Or make getNsName so we can return C:
120+
$this->propfind_response_helper($options, $files);
289121
}
290122

291-
function _calendarData($reqprop, $file, $options)
123+
function getProp($reqprop, $file, $options)
292124
{
293-
$filters = $options['filters'];
294-
295-
$options = array();
296-
$options['path'] = $file['path'];
297-
298-
$status = $this->get($options);
299-
if (empty($status)) {
300-
$status = '403 Forbidden';
125+
// check if property exists in response
126+
foreach ($file['props'] as $prop) {
127+
if ($reqprop['name'] == $prop['name'] &&
128+
$reqprop['ns'] == $prop['ns']) {
129+
return $prop;
130+
}
301131
}
302132

303-
if ($status !== true) {
304-
return HTTP_CalDAV_Server::calDavProp('calendar-data', null,
305-
$status);
133+
if ($reqprop['name'] == 'lockdiscovery' &&
134+
$reqprop['ns'] == 'DAV:' &&
135+
method_exists($this, 'getLocks')) {
136+
return $this->mkprop('DAV:', 'lockdiscovery',
137+
$this->getLocks($file['path']));
306138
}
307139

308-
if ($options['mimetype'] != 'text/calendar') {
309-
return;
310-
}
140+
if ($reqprop['name'] == 'calendar-data' &&
141+
$reqprop['ns'] == 'urn:ietf:params:xml:ns:caldav' &&
142+
method_exists($this, 'get')) {
143+
$filters = $options['filters'];
311144

312-
if ($options['stream']) {
313-
$handle = $options['stream'];
314-
} else if ($options['data']) {
315-
// What about data?
316-
} else {
317-
return;
318-
}
145+
$options = array();
146+
$options['path'] = $file['path'];
319147

320-
if (!($value = HTTP_CalDAV_Server::_parseComponent($handle,
321-
$reqprop['value'], $filters))) {
322-
return;
148+
$status = $this->get($options);
149+
if (empty($status)) {
150+
$status = '403 Forbidden';
151+
}
152+
153+
if ($status !== true) {
154+
return $this->calDavProp('calendar-data', null, $status);
155+
}
156+
157+
if ($options['mimetype'] != 'text/calendar') {
158+
return $this->calDavProp('calendar-data', null, '404 Not Found');
159+
}
160+
161+
if ($options['stream']) {
162+
$handle = $options['stream'];
163+
} else if ($options['data']) {
164+
// What about data?
165+
} else {
166+
return $this->calDavProp('calendar-data', null, '404 Not Found');
167+
}
168+
169+
if (!($value = $this->_parseComponent($handle, $reqprop['value'], $filters))) {
170+
return $this->calDavProp('calendar-data', null, '404 Not Found');
171+
}
172+
173+
return HTTP_CalDAV_Server::calDavProp('calendar-data', $value);
323174
}
324175

325-
return HTTP_CalDAV_Server::calDavProp('calendar-data', $value);
176+
// incase the requested property had a value, like calendar-data
177+
unset($reqprop['value']);
178+
$reqprop['status'] = '404 Not Found';
179+
return $reqprop;
326180
}
327181

328182
function _parseComponent($handle, $value=null, $filters=null)
329183
{
330-
$components = array();
184+
$comps = array();
331185
$compValues = array($value);
332186
$compFilters = array($filters);
333187
while (($line = fgets($handle, 4096)) !== false) {
334188
$line = explode(':', trim($line));
335189

336190
if ($line[0] == 'END') {
337-
if ($line[1] != $components[key($components)]->name) {
191+
if ($line[1] != $comps[count($comps) - 1]->name) {
338192
return;
339193
}
340194

341-
if (is_array($compFilters[key($compFilters)]['filters'])) {
342-
foreach ($compFilters[key($compFilters)]['filters'] as $filter) {
343-
print $filter['name'];
195+
if (is_array($compFilters[count($compFilters) - 1]['filters'])) {
196+
foreach ($compFilters[count($compFilters) - 1]['filters'] as $filter) {
344197
if ($filter['name'] == 'time-range') {
345-
if ($filter['value']['start'] > $components[key($components)]->properties['DTEND'][0]->value || $filter['value']['end'] < $components[key($components)]->properties['DTSTART'][0]->value) {
198+
if ($filter['value']['start'] > $comps[count($comps) - 1]->properties['DTEND'][0]->value || $filter['value']['end'] < $comps[count($comps) - 1]->properties['DTSTART'][0]->value) {
346199
array_pop($compValues);
347200
array_pop($compFilters);
348201
continue;
@@ -353,11 +206,11 @@ function _parseComponent($handle, $value=null, $filters=null)
353206

354207
// If we're about to pop the root component, we ignore the rest
355208
// of our input
356-
if (count($components) == 1) {
357-
return array_pop($components);
209+
if (count($comps) == 1) {
210+
return array_pop($comps);
358211
}
359212

360-
if (!$components[key($components)]->add_component(array_pop($components))) {
213+
if (!$comps[count($comps) - 2]->add_component(array_pop($comps))) {
361214
return;
362215
}
363216

@@ -368,11 +221,8 @@ function _parseComponent($handle, $value=null, $filters=null)
368221

369222
if ($line[0] == 'BEGIN') {
370223
$compName = $line[1];
371-
var_dump($compName);
372-
var_dump($compValues);
373-
var_dump($compValues[key($compValues)]);
374-
if (is_array($compValues[key($compValues)]['comps']) &&
375-
!isset($compValues[key($compValues)]['comps'][$compName])) {
224+
if (is_array($compValues[count($compValues) - 1]['comps']) &&
225+
!isset($compValues[count($compValues) - 1]['comps'][$compName])) {
376226
while (($line = fgets($handle, 4096)) !== false) {
377227
if (trim($line) == "END:$compName") {
378228
continue (2);
@@ -397,27 +247,25 @@ function _parseComponent($handle, $value=null, $filters=null)
397247
return;
398248
}
399249

400-
$components[] = new $className;
401-
end($components);
402-
$compValues[] = $compValues[key($compValues)]['comps'][$compName];
403-
end($components);
404-
$compFilters[] = $compFilters[key($compFilters)]['comps'][$compName];
405-
end($components);
250+
$comps[] = new $className;
251+
$compValues[] = $compValues[count($compValues) - 1]['comps'][$compName];
252+
$compFilters[] = $compFilters[count($compFilters) - 1]['comps'][$compName];
406253
continue;
407254
}
408255

409256
$line[0] = explode(';=', $line[0]);
410257
$propName = array_shift($line[0]);
411-
if (is_array($data['props']) &&
412-
!in_array($propName, $data['props'])) {
258+
if (is_array($compValues[count($compValues) - 1]['props']) &&
259+
!in_array($propName,
260+
$compValues[count($compValues) - 1]['props'])) {
413261
continue;
414262
}
415263

416264
$params = array();
417265
while (!empty($line[0])) {
418266
$params[array_shift($line[0])] = array_shift($line[0]);
419267
}
420-
$components[key($components)]->add_property($propName, $line[1], $params);
268+
$comps[count($comps) - 1]->add_property($propName, $line[1], $params);
421269
}
422270
}
423271

0 commit comments

Comments
 (0)