]>
git.datanom.net - webcal.git/blob - caldav/ical.class.php
6 PRODID:-//davical.org//NONSGML AWL Calendar//EN
10 DTSTAMP:20070417T195323Z
14 CREATED:20050814T160951Z
15 UID:libkcal-1123041440.757
17 LAST-MODIFIED:20070124T213448Z
18 SUMMARY:møde carl christian\, hovedopgave
21 RRULE:FREQ=DAILY;UNTIL=20020528T153000Z;INTERVAL=14
22 EXDATE;VALUE=DATE:20020402
23 DTSTART:20020219T163000Z
24 DTEND:20020219T180000Z
29 TRIGGER;VALUE=DURATION:-P1D
34 PRODID:-//davical.org//NONSGML AWL Calendar//EN
38 DTSTAMP:20070417T195323Z
42 CREATED:20050814T160951Z
43 UID:libkcal-1123041440.757
45 LAST-MODIFIED:20070124T213448Z
46 SUMMARY:event number 2
49 RRULE:FREQ=DAILY;UNTIL=20020528T153000Z;INTERVAL=14
50 EXDATE;VALUE=DATE:20020402
51 DTSTART:20020219T163000Z
52 DTEND:20020219T180000Z
57 TRIGGER;VALUE=DURATION:-P1D
63 require_once 'caldav-client.php';
66 function getProperty($name);
67 function setProperty($name, $value);
68 function getAllProperties();
69 function setAllProperties(array $assoc_array);
73 class VEvent
implements ICalendar
{
83 private $lastmodified;
93 private $xprop = array();
94 // Properties must not contain hypens in their name
95 private static $translate = array(
96 'lastmodified' => 'last-modified',
101 function __construct($values = array()) {
102 if (! is_array($values))
103 throw new Exception("Attribute to constructor must be an array");
104 $this->setProperties($values);
105 $this->changed
= FALSE;
108 private function setProperties(array $props) {
109 foreach ($props as $k => $v) {
110 switch (strtoupper($k)) {
115 $this->organizer
= $v;
124 $this->sequence
= $v;
126 case 'LAST-MODIFIED':
127 $this->lastmodified
= $v;
136 $this->priority
= $v;
154 $this->attendee
= $v;
157 $this->duration
= $v;
160 $this->location
= $v;
166 $this->description
= $v;
169 if (($k[0] == 'x' ||
$k[0] == 'X') && $k[1] == '-')
170 $this->xprop
[$k] = $v;
172 throw new Exception("[$k,$v]: Unknown attribute");
177 public function getProperty($name) {
178 $prop = strtolower($name);
179 if (($trans = array_search($prop, self
::$translate)) !== false)
181 if (! property_exists($this, $prop)) {
182 // Support for PHP 5 < 5.3
183 $obj = new ReflectionClass(get_class($this));
184 if (! $obj->hasProperty($prop))
185 throw new Exception("$name: Unknown property");
190 public function setProperty($name, $value) {
191 $prop = strtolower($name);
192 if (($trans = array_search($prop, self
::$translate)) !== false)
194 if (! property_exists($this, $prop)) {
195 // Support for PHP 5 < 5.3
196 $obj = new ReflectionClass(get_class($this));
197 if (! $obj->hasProperty($prop))
198 throw new Exception("$name: Unknown property");
200 $this->$prop = $value;
201 $this->changed
= TRUE;
204 function getAllProperties() {
206 $p = get_object_vars($this);
207 foreach ($p as $k => $v){
210 if (array_key_exists($k, self
::$translate))
211 $k = strtoupper(self
::$translate[$k]);
219 function setAllProperties(array $assoc_array) {
221 foreach ($assoc_array as $k => $v){
224 foreach ($v as $k1 => $v1)
230 $this->setProperties($props);
231 $this->changed
= TRUE;
234 function isChanged() {
235 return $this->changed
;
240 class VAlarm
implements ICalendar
{
242 private $description;
246 // Properties must not contain hypens in their name
247 private static $translate = array();
250 function __construct($values = array()) {
251 if (! is_array($values))
252 throw new Exception("Attribute to constructor must be an array");
253 $this->setProperties($values);
254 $this->changed
= FALSE;
257 private function setProperties(array $props) {
258 foreach ($props as $k => $v) {
259 switch (strtoupper($k)) {
264 $this->description
= $v;
267 $this->duration
= $v;
276 throw new Exception("$k: Unknown attribute");
281 public function getProperty($name) {
282 $prop = strtolower($name);
283 if (($trans = array_search($prop, self
::$translate)) !== false)
285 if (! property_exists($this, $prop)) {
286 // Support for PHP 5 < 5.3
287 $obj = new ReflectionClass(get_class($this));
288 if (! $obj->hasProperty($prop))
289 throw new Exception("$name: Unknown property");
294 public function setProperty($name, $value) {
295 $prop = strtolower($name);
296 if (($trans = array_search($prop, self
::$translate)) !== false)
298 if (! property_exists($this, $prop)) {
299 // Support for PHP 5 < 5.3
300 $obj = new ReflectionClass(get_class($this));
301 if (! $obj->hasProperty($prop))
302 throw new Exception("$name: Unknown property");
304 $this->$prop = $value;
305 $this->changed
= TRUE;
308 function getAllProperties() {
310 $p = get_object_vars($this);
311 foreach ($p as $k => $v){
314 if (array_key_exists($k, self
::$translate))
315 $k = strtoupper(self
::$translate[$k]);
323 function setAllProperties(array $assoc_array) {
325 foreach ($assoc_array as $k => $v){
328 foreach ($v as $k1 => $v1)
334 $this->setProperties($props);
335 $this->changed
= TRUE;
338 function isChanged() {
339 return $this->changed
;
344 class VTimezone
implements ICalendar
{
351 // Properties must not contain hypens in their name
352 private static $translate = array();
355 function __construct($values = array()) {
356 if (! is_array($values))
357 throw new Exception("Attribute to constructor must be an array");
358 $this->setProperties($values);
359 $this->changed
= FALSE;
362 private function setProperties(array $props) {
363 foreach ($props as $k => $v) {
364 switch (strtoupper($k)) {
369 if (($k[0] == 'x' ||
$k[0] == 'X') && $k[1] == '-')
370 $this->xprop
[$k] = $v;
372 throw new Exception("[$k,$v]: Unknown attribute");
377 function getProperty($name){}
378 function setProperty($name, $value) {
379 $prop = strtolower($name);
380 if (($trans = array_search($prop, self
::$translate)) !== false)
382 if (! property_exists($this, $prop)) {
383 // Support for PHP 5 < 5.3
384 $obj = new ReflectionClass(get_class($this));
385 if (! $obj->hasProperty($prop))
386 throw new Exception("$name: Unknown property");
388 $this->$prop = $value;
390 $this->$prop == 'standard' && ! isset($this->standard
) ||
391 $this->$prop == 'daylight' && ! isset($this->daylight
)
393 // we are constructing the object
396 $this->changed
= TRUE;
398 function getAllProperties(){}
399 function setAllProperties(array $assoc_array){}
400 function isChanged(){}
409 function __construct($values = array()) {
410 if (! is_array($values))
411 throw new Exception("Attribute to constructor must be an array");
412 $this->objects
= array();
414 foreach ($values as $k => $v) {
415 switch (strtoupper($k)) {
417 $this->calscale
= $v;
426 throw new Exception("$k: Unknown attribute");
431 public function addObject(ICalendar
$object) {
432 $this->objects
[get_class($object)] = $object;
435 public function getObject($name) {
436 if (isset($this->objects
[$name]))
437 return $this->objects
[$name];
442 public function getObjects() {
443 return $this->objects
;
448 class IcalParserIterator
implements Iterator
{
452 function __construct(IcalParser
$icalParser) {
453 if (! $icalParser instanceof IcalParser
)
454 throw new Exception(get_class($icalParser) . ': Can only handle instances of class IcalParser');
455 $this->icalParser
= $icalParser;
459 public function current() {
460 return $this->icalParser
->getIcal($this->pos
);
463 public function key() {
467 public function next() {
471 public function rewind() {
475 public function valid() {
476 return $this->icalParser
->peek($this->pos
);
481 class IcalParser
implements IteratorAggregate
{
482 // private $elements = array('VCALENDAR', 'VEVENT', 'VALARM', 'VTIMEZONE');
483 // private $subelem = array('VTIMEZONE' => array('STANDARD', 'DAYLIGHT'));
486 function __construct($message = NULL) {
487 $this->items
= array();
489 $this->setMessage($message);
492 function setMessage($message) {
493 $this->split_message($message);
496 private function remove_value(array &$array, $key) {
500 foreach ($array as $k => $v) {
510 private function newObject($name, $props = array()) {
512 $obj = new ReflectionClass($name);
514 catch (ReflectionException
$ex) {
515 print $ex->getMessage();
516 throw new Exception("$name: Class not found");
519 return new $name($props);
522 private function split_message($message) {
524 $elem_data = array();
525 $data_list = array();
526 $elem_list = array();
530 //print "$message\n";
531 $lines = explode("\n", $message);
532 foreach ($lines as $line) {
533 $a = explode(":", $line);
535 $item = strtoupper($a[0]);
538 for ($i = 1; $i < count($a); $i++
) {
543 //print "elem: $elem\n";
549 /*if (! in_array($elem, $this->elements)) {
551 if (($sub = end($elem_list)) !== FALSE) {
552 if (! array_key_exists($sub, $this->subelem))
561 if (count($data) > 0) {
562 if (count($elem_list) > 0) {
563 /*$name = array_pop($elem_list);
564 if (array_key_exists($name, $this->subelem)) {
565 print "BEGIN: $name\n";
568 $elem_data[array_pop($elem_list)] = $data;
571 $elem_data[$elem] = $data;
573 array_push($elem_list, $elem);
577 if (count($elem_list) > 0) {
578 /*$name = array_pop($elem_list);
579 if (array_key_exists($name, $this->subelem)) {
580 print "END: $name\n";
583 $elem_data[array_pop($elem_list)] = $data;
586 if ($elem == 'VCALENDAR' && count($elem_data) > 1) {
587 array_push($data_list, $elem_data);
588 $elem_data = array();
589 $elem_list = array();
593 if (! is_array($data))
594 throw new Exception("Message is not valid [missing 'BEGIN']");
595 if (($pos = strpos($item, ';')) !== false) {
596 $head = substr($item, 0, $pos);
597 $elem = substr($item, $pos +
1) . ';' . $elem;
599 //print "Ny elem: $item:$elem\n";
601 $data[$item] = $elem;
604 //print_r($data_list);
605 foreach ($data_list as $item) {
606 $c = $this->remove_value($item, 'VCALENDAR');
607 $calendar = new VCalendar($c);
608 foreach ($item as $k => $v) {
609 if ($k == 'DAYLIGHT' ||
$k == 'STANDARD') {
610 $object = $calendar->getObject('VTimezone');
611 if (! is_object($object)) {
612 $object = $this->newObject('VTimezone');
613 $calendar->addObject($object);
615 $object->setProperty($k, $v);
618 if (($object = $calendar->getObject($k)) == NULL) {
619 $object = $this->newObject($k, $v);
620 $calendar->addObject($object);
623 $object->setAllProperties($v);
627 array_push($this->items
, $calendar);
631 public function getIterator() {
632 return new IcalParserIterator($this);
635 public function getIcal($id) {
636 if ($this->peek($id))
637 return $this->items
[$id];
639 throw new Exception("Index out of bounds");
642 public function peek($id) {
643 return isset($this->items
[$id]);
This page took 0.119978 seconds and 6 git commands to generate.