]> git.datanom.net - webcal.git/blame - caldav/calendar.class.php
Initial upload
[webcal.git] / caldav / calendar.class.php
CommitLineData
a5eae6b7
MR
1<?php
2/* $Id$ */
3
4 require_once 'caldav-client.php';
5 require_once 'awl/iCalendar.php';
6 require_once 'caldavresource.class.php';
7 require_once 'rruleparser.class.php';
8 require_once 'vevent.class.php';
9 require_once 'icomponent.class.php';
10
11 class CalendarIterator implements Iterator {
12 private $list;
13
14 function __construct(array $list) {
15 $this->list = $list;
16 }
17
18 function current() {
19 return current($this->list);
20 }
21
22 function next() {
23 next($this->list);
24 }
25
26 function key() {
27 return key($this->list);
28 }
29
30 function rewind() {
31 reset($this->list);
32 }
33
34 function valid() {
35 $obj = current($this->list);
36 return ($obj !== FALSE);
37 }
38
39 }
40
41 class Calendar extends CaldavRessource {
42
43 private $calendar;
44
45 function __construct($url, $uid = '', $pwd = '', $cal = '') {
46 //file_put_contents('/tmp/dump', "$url\n$uid\n$pwd\n$cal\n", FILE_APPEND);
47 if (empty($url))
48 throw new Exception("Missing URL");
49 parent::__construct($url, $uid, $pwd, $cal);
50 }
51
52 private function setComponent(VTYPE $type, array $item, $new = FALSE) {
53 switch ($type) {
54 case VTYPE::VEVENT:
55 $ical = new VEvent(
56 $item['etag'], $item['href'],
57 $type, $item['ical'], $new);
58 break;
59 default:
60 throw new Exception(
61 "$thisType: Unsupported iCalendar component");
62 }
63 $this->calendar[$item['etag']] = $ical;
64 //var_dump($this->calendar[$item['etag']]);
65 //print "-------------------------------<br/>";
66 }
67
68 private function setResource($etag, $resource) {
69 if ($resource === NULL)
70 unset($this->calendar[$etag]);
71 else if (isset($this->calendar[$etag]))
72 $this->calendar[$etag]->setResource($resource);
73 else {
74 $type = new VTYPE($this->getType($resource));
75 $this->setComponent($type, array(
76 'etag' => $etag,
77 'href' => NULL,
78 'ical' => $resource),
79 TRUE
80 );
81 }
82 }
83
84 private function getType(iCalendar $iCalendar) {
85 $components = $iCalendar->component->GetComponents();
86 // Find VCalender component
87 foreach($components as $type) {
88 try {
89 $vtype = new VTYPE($type->GetType());
90 if ($vtype->ordinal() != VTYPE::VTIMEZONE)
91 break;
92 }
93 catch (Exception $ex) {}
94 }
95 return $vtype;
96 }
97
98 private function wrapCalendar($component) {
99 $cal = "BEGIN:VCALENDAR\r\n";
100 $cal .= "PRODID:-//datanom.net//NONSGML WEBCAL Calendar//EN\r\n";
101 $cal .= "VERSION:2.0\r\n";
102 $cal .= "CALSCALE:GREGORIAN\r\n";
103 $cal .= $component;
104 $cal .= "END:VCALENDAR\r\n";
105
106 return $cal;
107 }
108
109 function getComponents($start, $end) {
110 $this->calendar = array();
111
112 if (! $this->isDateTime($start) || ! $this->isDateTime($end))
113 throw new Exception("[$start:$end]: Invalid DateTime format");
114 //print "$start:$end<br/>";
115 //file_put_contents('/tmp/dump', "$start, $end\n", FILE_APPEND);
116 $events = $this->callServer('getEvents', array($start, $end));
117 //var_export($events, FALSE);
118 //file_put_contents('/tmp/dump', var_export($events, TRUE), FILE_APPEND);
119 foreach ($events as $k => $event) {
120 $iCalendar = new iCalendar(
121 array('icalendar' => $event['data']));
122 $vtype = $this->getType($iCalendar);
123 $this->setComponent($vtype, array(
124 'etag' => $event['etag'],
125 'href' => $event['href'],
126 'ical' => $iCalendar
127 )
128 );
129 }
130 }
131
132 function newComponent($c_type) {
133 switch (strtoupper($c_type)) {
134 case 'VEVENT': $type = 'VEVENT'; break;
135 default:
136 throw new Exception(
137 "$thisType: Unsupported iCalendar component");
138 }
139 $start = gmdate("Ymd\THm\Z");
140 $end = strtotime($start) + (60*60);
141 $end = gmdate("Ymd\THm\Z", $end);
142 //echo "$start:$end<br/>";
143 $uid = sha1(microtime() . $start . $end);
144 $iCalendar = new iCalendar(array(
145 'type' => $type,
146 'DTSTART' => $start,
147 'DTEND' => $end,
148 'UID' => $uid
149 )
150 );
151 $vtype = $this->getType($iCalendar);
152 $etag = sha1("This is a new component");
153 $this->setComponent($vtype, array(
154 'etag' => $etag,
155 'href' => NULL,
156 'ical' => $iCalendar
157 )
158 );
159 return $this->calendar[$etag];
160 }
161/*
162 function reload($start, $end) {
163 $res = $this->update();
164 if (count($res) < 1) {
165 $this->getComponents($start, $end);
166 }
167 return $res;
168 }
169*/
170 private function updateEvent($url, $etag) {
171 $res = array();
172 $resource = $this->calendar[$etag];
173 if ($resource && $resource->isDirty()) {
174 // update (call put)
175 $component = $resource->getBaseComponent();
176 //print "$etag: update\n";
177 $uid = $component->GetPValue('UID');
178 $ical = $this->wrapCalendar($component->Render());
179 //echo "$uid<br/>".nl2br($ical)."$etag<br/>";
180 $url = $resource->getUrl();
181 if ($url) {
182 $newEtag = $this->callServer('put',
183 array("$uid.ics", $ical, $etag));
184 }
185 else {
186 $newEtag = $this->callServer('put',
187 array("$uid.ics", $ical));
188 }
189 if (is_array($newEtag))
190 array_push($res, $newEtag);
191 else {
192 $resource->setEtag($newEtag);
193 }
194 }
195 return $res;
196 }
197
198 function update($url, $etag = NULL) {
199 //var_dump($this->calendar);
200 if (! $etag) {
201 foreach($this->calendar as $id => $resource) {
202 //var_dump($resource);
203 $thisUrl = $resource->getUrl();
204 if ($thisUrl && strcasecmp($url, $thisUrl) == 0) {
205 $etag = $id;
206 break;
207 }
208 }
209 }
210 if ($etag)
211 $res = $this->updateEvent($url, $etag);
212 else
213 $res = array($url => 'Event does not exist');
214 return $res;
215 }
216
217 function delete($url, $etag = NULL) {
218 if ($etag) {
219 $res = $this->callServer('delete', array($url, $etag));
220 }
221 else {
222 $res = $this->callServer('delete', array($url));
223 }
224 return $res;
225 }
226
227 // inherited abstract methods from parent
228 function offsetExists($etag) {
229 return (is_object($this->calendar[$etag]) &&
230 $this->calendar[$etag] instanceof IComponent);
231 }
232
233 function offsetGet($etag) {
234 if ($this->offsetExists($etag))
235 return $this->calendar[$etag]->getResource();
236 }
237
238 function offsetSet($etag, $ical) {
239 $this->setResource($etag, $ical);
240 }
241
242 function offsetUnset($etag) {
243 $this->setResource($etag, NULL);
244 }
245
246 function getIterator() {
247 return new CalendarIterator($this->calendar);
248 }
249
250 }
251/*
252$cal = new Calendar(
253 'http://calendar.datanom.net/caldav.php/mir/home/',
254 'uid',
255 'pwd'
256);
257$cal->getComponents("20030830T000000Z","20031201T000000Z");
258//print_r($cal);
259$i = 0;
260foreach($cal as $obj) {
261 $i++;
262 print "========= [$i] =========\n";
263 //print_r($obj);
264 //print_r ($obj->getAlarm());
265 print_r($obj->getActiveDates("20031014T000000Z","20031114T000000Z"));
266 //print "{$obj->isUTCTime()}\n";
267 //$obj->getActiveDates();
268}
269print "Found $i event(s)\n";
270
271//print_r ($cal->getUrlByEtag($cal->getEtagFromUid('KOrganizer-1670268771.406')));
272$time = time();
273print "time: $time\n";
274$dt = $cal->timestamp2ICal($time, TRUE);
275print "dt: $dt\n";
276$time = $cal->iCal2Timestamp($dt);
277print "time: $time\n";
278$dt = $cal->timestamp2ICal($time, FALSE);
279print "dt: $dt\n";
280$time = $cal->iCal2Timestamp(substr($dt, 0, strpos($dt, 'T')));
281$dt = $cal->timestamp2ICal($time, TRUE);
282print "dt: $dt\n";
283$r = new RRuleParser(
284 'FREQ=HOURLY;INTERVAL=3;UNTIL=20070101T170000Z',
285 '20070101T090000Z', '20070101T090000Z');
286$r = new RRuleParser(
287 'FREQ=WEEKLY;COUNT=12;INTERVAL=2',
288 '20070101T140000Z', '20070101T120000Z');
289print "$r\n";
290print_r($r->getEventDates('20070301T140000Z','20070501T140000Z'));
291$r = new RRuleParser(
292 'FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1',
293 '20070101T000100Z', '20070101T001100Z');
294//DTSTART;TZID=US-Eastern:19970105T083000
295print "$r\n";
296$r = new RRuleParser(
297 'FREQ=YEARLY;INTERVAL=2;BYMONTH=1;BYDAY=SU;BYHOUR=8,9;BYMINUTE=30',
298 '20070101T000100Z', '20070101T001100Z');
299print "$r\n";
300print_r ($r->getEventDates('20060101T000100Z', '20060101T001100Z'));
301$r = new RRuleParser(
302 'FREQ=DAILY;COUNT=10;INTERVAL=2',
303 '20070101T000100Z', '20070101T001100Z');
304print "$r\n";
305//foreach ($cal as $obj)
306// var_dump($obj->getBaseComponent());
307//$bak = $cal['3ba46312e910765bf7059a53909d149b'];
308//print_r($bak);
309//print_r(new Icalendar(array('SUMMARY' => 'test')));
310//$cal['3ba46312e910765bf7059a53909d149b'] = new Icalendar(array('SUMMARY' => 'test'));
311//print_r($cal['3ba46312e910765bf7059a53909d149b']);
312//unset($cal['3ba46312e910765bf7059a53909d149b']);
313//var_dump($cal['3ba46312e910765bf7059a53909d149b']);
314//$cal['3ba46312e910765bf7059a53909d149b'] = $bak;
315//var_dump($cal['3ba46312e910765bf7059a53909d149b']);
316//$cal->update();
317//print_r($cal['3ba46312e910765bf7059a53909d149b']);*/
This page took 0.067948 seconds and 5 git commands to generate.