aboutsummaryrefslogtreecommitdiffstats
path: root/lib/HTTP/CalDAV/Server.php
blob: 05425a112566bc08cdc54a3de737433ade8e005d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
67
68
69
70
71
72
73
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * CalDav Server class
 *
 * Long description for file (if any)...
 *
 * PHP versions 4 & 5
 *
 * LICENSE: This source file is subject to version 3.0 of the PHP license
 * that is available through the world-wide-web at the following URI:
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
 * the PHP License & are unable to obtain it through the web, please
 * send a note to license@php.net so we can mail you a copy immediately
 *
 * @category HTTP
 * @package HTTP_CalDAV_Server
 * @author Jack Bates <ms419@freezone.co.uk>
 * @copyright 2006 The PHP Group
 * @license PHP License 3.0 http://www.php.net/license/3_0.txt
 * @version CVS: $Id: Server.php,v 1.4 2006/04/13 22:33:13 jablko Exp $
 * @link http://pear.php.net/package/HTTP_CalDAV_Server
 * @see HTTP_WebDAV_Server
 */

require_once 'Tools/ReportParser.php';

/**
 * CalDav Server class
 *
 * Long description
 *
 * @category HTTP
 * @package HTTP_CalDAV_Server
 * @author Jack Bates <ms419@freezone.co.uk>
 * @copyright 2006 The PHP Group
 * @license PHP License 3.0 http://www.php.net/license/3_0.txt
 * @version CVS: $Id: Server.php,v 1.4 2006/04/13 22:33:13 jablko Exp $
 * @link http://pear.php.net/package/HTTP_CalDAV_Server
 * @see HTTP_WebDAV_Server
 */
class HTTP_CalDAV_Server extends HTTP_WebDAV_Server
{
    /**
     * Make a property in the CalDAV namespace
     *
     * @param string property name
     * @param string property value
     * @return array string property namespace
     *               string property name
     *               string property value
     */
    function calDavProp($name, $value=null, $status=null) {
        return $this->mkprop('urn:ietf:params:xml:ns:caldav', $name, $value,
            $status);
    }

    /**
     * REPORT request helper - prepares data-structures from REPORT requests
     *
     * @param options
     * @return void
     * @access public
     */
    function report_request_helper(&$options)
    {
        $options = array();
        $options['path'] = $this->path;

        $options['depth'] = 'infinity';
        if (isset($_SERVER['HTTP_DEPTH'])) {
            $options['depth'] = $_SERVER['HTTP_DEPTH'];
        }

        $parser = new ReportParser('php://input');
        if (!$parser->success) {
            $this->http_status('400 Bad Request');
            return;
        }

        $options['props'] = $parser->props;
        $options['filters'] = $parser->filters;

        return true;
    }

    /**
     * REPORT method wrapper
     *
     * @param void
     * @return void
     * @access public
     */
    function report_wrapper()
    {
        /* Prepare data-structure from REPORT request */
        if (!$this->report_request_helper($options)) {
            return;
        }

        /* Call user handler */
        if (method_exists($this, 'report')) {
            if (!$this->report($options, $files)) {
                return;
            }
        } else {

            /* Empulate REPORT using PROPFIND */
            if (!$this->propfind($options, $files)) {
                return;
            }
        }

        /* Format REPORT response */

        // TODO Make ns_hash a class variable so we can prettify C:
        // Or make getNsName so we can return C:
        $this->propfind_response_helper($options, $files);
    }

    function getProp($reqprop, $file, $options)
    {
        // check if property exists in response
        foreach ($file['props'] as $prop) {
            if ($reqprop['name'] == $prop['name'] &&
                    $reqprop['ns'] == $prop['ns']) {
                return $prop;
            }
        }

        if ($reqprop['name'] == 'lockdiscovery' &&
                $reqprop['ns'] == 'DAV:' &&
                method_exists($this, 'getLocks')) {
            return $this->mkprop('DAV:', 'lockdiscovery',
                $this->getLocks($file['path']));
        }

        if ($reqprop['name'] == 'calendar-data' &&
                $reqprop['ns'] == 'urn:ietf:params:xml:ns:caldav' &&
                method_exists($this, 'get')) {
            $filters = $options['filters'];

            $options = array();
            $options['path'] = $file['path'];

            $status = $this->get($options);
            if (empty($status)) {
                $status = '403 Forbidden';
            }

            if ($status !== true) {
                return $this->calDavProp('calendar-data', null, $status);
            }

            if ($options['mimetype'] != 'text/calendar') {
                return $this->calDavProp('calendar-data', null, '404 Not Found');
            }

            if ($options['stream']) {
                $handle = $options['stream'];
            } else if ($options['data']) {
                // What about data?
            } else {
                return $this->calDavProp('calendar-data', null, '404 Not Found');
            }

            if (!($value = $this->_parseComponent($handle, $reqprop['value'], $filters))) {
                return $this->calDavProp('calendar-data', null, '404 Not Found');
            }

            return HTTP_CalDAV_Server::calDavProp('calendar-data', $value);
        }

        // incase the requested property had a value, like calendar-data
        unset($reqprop['value']);
        $reqprop['status'] = '404 Not Found';
        return $reqprop;
    }

    function _parseComponent($handle, $value=null, $filters=null)
    {
        $comps = array();
        $compValues = array($value);
        $compFilters = array($filters);
        while (($line = fgets($handle, 4096)) !== false) {
            $line = explode(':', trim($line));

            if ($line[0] == 'END') {
                if ($line[1] != $comps[count($comps) - 1]->name) {
                    return;
                }

                if (is_array($compFilters[count($compFilters) - 1]['filters'])) {
                    foreach ($compFilters[count($compFilters) - 1]['filters'] as $filter) {
                        if ($filter['name'] == 'time-range') {
                            if ($filter['value']['start'] > $comps[count($comps) - 1]->properties['DTEND'][0]->value || $filter['value']['end'] < $comps[count($comps) - 1]->properties['DTSTART'][0]->value) {
                                array_pop($compValues);
                                array_pop($compFilters);
                                continue;
                            }
                        }
                    }
                }

                // If we're about to pop the root component, we ignore the rest
                // of our input
                if (count($comps) == 1) {
                    return array_pop($comps);
                }

                if (!$comps[count($comps) - 2]->add_component(array_pop($comps))) {
                    return;
                }

                array_pop($compValues);
                array_pop($compFilters);
                continue;
            }

            if ($line[0] == 'BEGIN') {
                $compName = $line[1];
                if (is_array($compValues[count($compValues) - 1]['comps']) &&
                        !isset($compValues[count($compValues) - 1]['comps'][$compName])) {
                    while (($line = fgets($handle, 4096)) !== false) {
                        if (trim($line) == "END:$compName") {
                            continue (2);
                        }
                    }

                    return;
                }

                $className = 'iCalendar_' . ltrim(strtolower($compName), 'v');
                if ($line[1] == 'VCALENDAR') {
                    $className = 'iCalendar';
                }

                if (!class_exists($className)) {
                    while (($line = fgets($handle, 4096)) !== false) {
                        if (trim($line) == "END:$compName") {
                            continue (2);
                        }
                    }

                    return;
                }

                $comps[] = new $className;
                $compValues[] = $compValues[count($compValues) - 1]['comps'][$compName];
                $compFilters[] = $compFilters[count($compFilters) - 1]['comps'][$compName];
                continue;
            }

            $line[0] = explode(';=', $line[0]);
            $propName = array_shift($line[0]);
            if (is_array($compValues[count($compValues) - 1]['props']) &&
                    !in_array($propName,
                    $compValues[count($compValues) - 1]['props'])) {
                continue;
            }

            $params = array();
            while (!empty($line[0])) {
                $params[array_shift($line[0])] = array_shift($line[0]);
            }
            $comps[count($comps) - 1]->add_property($propName, $line[1], $params);
        }
    }

    function _multistatus($responses)
    {
        // now we generate the response header...
        $this->http_status('207 Multi-Status');
        header('Content-Type: text/xml; charset="utf-8"');

        // ...and payload
        echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
        echo "<D:multistatus xmlns:D=\"DAV:\">\n";

        foreach ($responses as $response) {

            // ignore empty or incomplete entries
            if (!is_array($response) || empty($response)) {
                continue;
            }

            $ns_defs = array();
            foreach ($response['ns_hash'] as $name => $prefix) {
                $ns_defs[] = "xmlns:$prefix=\"$name\"";
            }
            echo ' <D:response ' . implode(' ', $ns_defs) . ">\n";
            echo "  <D:href>$response[href]</D:href>\n";

            // report all found properties and their values (if any)
            // nothing to do if no properties were returend for a file
            if (isset($response['propstat']) &&
                    is_array($response['propstat'])) {

                foreach ($response['propstat'] as $status => $props) {
                    echo "  <D:propstat>\n";
                    echo "   <D:prop>\n";

                    foreach ($props as $prop) {
                        if (!is_array($prop) || !isset($prop['name'])) {
                            continue;
                        }

                        // empty properties (cannot use empty for check as '0'
                        // is a legal value here)
                        if (!isset($prop['value']) || empty($prop['value']) &&
                                $prop['value'] !== 0) {
                            if ($prop['ns'] == 'DAV:') {
                                echo "    <D:$prop[name]/>\n";
                                continue;
                            }

                            if (!empty($prop['ns'])) {
                                echo '    <' .
                                    $response['ns_hash'][$prop['ns']] .
                                    ":$prop[name]/>\n";
                                continue;
                            }

                            echo "    <$prop[name] xmlns=\"\"/>";
                            continue;
                        }

                        // some WebDAV properties need special treatment
                        if ($prop['ns'] == 'DAV:') {

                            switch ($prop['name']) {
                            case 'creationdate':
                                echo "    <D:creationdate ns0:dt=\"dateTime.tz\">\n";
                                echo '     ' . gmdate('Y-m-d\TH:i:s\Z', $prop['value']) . "\n";
                                echo "    </D:creationdate>\n";
                                break;

                            case 'getlastmodified':
                                echo "    <D:getlastmodified ns0:dt=\"dateTime.rfc1123\">\n";
                                echo '     ' . gmdate('D, d M Y H:i:s', $prop['value']) . " GMT\n";
                                echo "    </D:getlastmodified>\n";
                                break;

                            case 'resourcetype':
                                echo "    <D:resourcetype>\n";
                                echo "     <D:$prop[value]/>\n";
                                echo "    </D:resourcetype>\n";
                                break;

                            case 'supportedlock':

                                if (is_array($prop[value])) {
                                    $prop[value] = $this->_lockentries($prop[value]);
                                }
                                echo "    <D:supportedlock>\n";
                                echo "     $prop[value]\n";
                                echo "    </D:supportedlock>\n";
                                break;

                            case 'lockdiscovery':

                                if (is_array($prop[value])) {
                                    $prop[value] = $this->_activelocks($prop[value]);
                                }
                                echo "    <D:lockdiscovery>\n";
                                echo "     $prop[value]\n";
                                echo "    </D:lockdiscovery>\n";
                                break;

                            default:
                                echo "    <D:$prop[name]>\n";
                                echo '     ' . $this->_prop_encode(htmlspecialchars($prop['value'])) . "\n";
                                echo "    </D:$prop[name]>\n";
                            }

                            continue;
                        }

                        if ($prop['name'] == 'calendar-data' &&
                                is_object($prop['value'])) {
                            $prop['value'] = $prop['value']->serialize();
                        }

                        if (!empty($prop['ns'])) {
                            echo '    <' . $response['ns_hash'][$prop['ns']] . ":$prop[name]>\n";
                            echo '     ' . $this->_prop_encode(htmlspecialchars($prop['value'])) . "\n";
                            echo '    </' . $response['ns_hash'][$prop['ns']] . ":$prop[name]>\n";

                            continue;
                        }

                        echo "    <$prop[name] xmlns=\"\">\n";
                        echo '     ' . $this->_prop_encode(htmlspecialchars($prop['value'])) . "\n";
                        echo "    </$prop[name]>\n";
                    }

                    echo "   </D:prop>\n";
                    echo "   <D:status>HTTP/1.1 $status</D:status>\n";
                    echo "  </D:propstat>\n";
                }
            }

            if (isset($response['status'])) {
                echo "  <D:status>HTTP/1.1 $status</D:status>\n";
            }

            if (isset($response['responsedescription'])) {
                echo "  <D:responsedescription>\n";
                echo '   ' . $this->_prop_encode(htmlspecialchars($response['responsedescription'])) . "\n";
                echo "  </D:responsedescription>\n";
            }

            echo " </D:response>\n";
        }

        echo "</D:multistatus>\n";
    }
}

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * c-handling-comment-ender-p: nil
 * End:
 */

?>

© 2014-2024 Faster IT GmbH | imprint | privacy policy