aboutsummaryrefslogtreecommitdiffstats
path: root/lib/HTTP/CalDAV/Tools/ICalendarParser.php
blob: 0e3ffe3038d62522782de4d660199a9ee8bde6b0 (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
<?php

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

/**
 * Helper for parsing iCalendar format
 *
 * 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: ICalendarParser.php,v 1.1 2006/04/18 03:22:12 jablko Exp $
 * @link http://pear.php.net/package/HTTP_CalDAV_Server
 * @see HTTP_WebDAV_Server
 */

/**
 * Helper for parsing iCalendar format
 *
 * 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: ICalendarParser.php,v 1.1 2006/04/18 03:22:12 jablko Exp $
 * @link http://pear.php.net/package/HTTP_CalDAV_Server
 * @see HTTP_WebDAV_Server
 */
class ICalendarParser
{
    /**
     * Success state flag
     *
     * @var bool
     * @access public
     */
    var $success = false;

    /**
     * Parsed components are collected here in post-order
     *
     * @var array
     * @access public
     */
    var $comps = array();

    /**
     * Parsed components' offsets are collected here in post-order
     *
     * @var array
     * @access public
     */
    var $offsets = array();

    /**
     * Constructor
     *
     * @param resource input stream handle
     * @access public
     */
    function ICalendarParser($handle, $beginOffset=null, $endOffset=null,
        $value=null, $filters=null)
    {
        $comps = array();
        $beginOffsets = array();
        $compValues = array($value);
        $compFilters = array($filters);
        $this->success = true;
        while (($offset = ftell($handle)) !== false &&
                ($line = fgets($handle, 4096)) !== false) {
            if (isset($endOffset) && $offset > $endOffset) {
                return;
            }

            $line = explode(':', trim($line));

            if ($line[0] == 'END') {
                if ($line[1] != $comps[count($comps) - 1]->name) {
                    $this->success = false;
                    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 (count($comps) > 1 &&
                        !$comps[count($comps) - 2]->add_component($comps[count(
                        $comps) - 1])) {
                    $this->success = false;
                    return;
                }

                if (($offset = ftell($handle)) === false) {
                    $this->success = false;
                    return;
                }

                $this->comps[] = array_pop($comps);
                $this->offsets[] = array(array_pop($beginOffsets), $offset);
                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);
                        }
                    }

                    $this->success = false;
                    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);
                        }
                    }

                    $this->success = false;
                    return;
                }

                $comps[] = new $className;
                $beginOffsets[] = $offset;
                $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);
        }

        if (!feof($handle)) {
            $this->success = false;
        }
    }
}

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

?>

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