4 * BENNU - PHP iCalendar library
5 * (c) 2005-2006 Ioannis Papaioannou (pj@moodle.org). All rights reserved.
7 * Released under the LGPL.
9 * See http://bennu.sourceforge.net/ for more information and downloads.
11 * @author Ioannis Papaioannou
12 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
15 class iCalendar_component
{
17 var $properties = NULL;
18 var $components = NULL;
19 var $valid_properties = NULL;
20 var $valid_components = NULL;
22 * Added to hold errors from last run of unserialize
23 * @var $parser_errors array
25 var $parser_errors = NULL;
27 function __construct() {
28 // Initialize the components array
29 if(empty($this->components
)) {
30 $this->components
= array();
31 foreach($this->valid_components
as $name) {
32 $this->components
[$name] = array();
41 function add_property($name, $value = NULL, $parameters = NULL) {
43 // Uppercase first of all
44 $name = strtoupper($name);
46 // Are we trying to add a valid property?
48 if(!isset($this->valid_properties
[$name])) {
49 // If not, is it an x-name as per RFC 2445?
50 if(!rfc2445_is_xname($name)) {
53 // Since this is an xname, all components are supposed to allow this property
57 // Create a property object of the correct class
59 $property = new iCalendar_property_x
;
60 $property->set_name($name);
63 $classname = 'iCalendar_property_'.strtolower(str_replace('-', '_', $name));
64 $property = new $classname;
67 // If $value is NULL, then this property must define a default value.
69 $value = $property->default_value();
75 // Set this property's parent component to ourselves, because some
76 // properties behave differently according to what component they apply to.
77 $property->set_parent_component($this->name
);
79 // Set parameters before value; this helps with some properties which
80 // accept a VALUE parameter, and thus change their default value type.
82 // The parameters must be valid according to property specifications
83 if(!empty($parameters)) {
84 foreach($parameters as $paramname => $paramvalue) {
85 if(!$property->set_parameter($paramname, $paramvalue)) {
90 // Some parameters interact among themselves (e.g. ENCODING and VALUE)
91 // so make sure that after the dust settles, these invariants hold true
92 if(!$property->invariant_holds()) {
97 // $value MUST be valid according to the property data type
98 if(!$property->set_value($value)) {
102 // Check if the property already exists, and is limited to one occurrance,
103 // DON'T overwrite the value - this can be done explicity with set_value() instead.
104 if(!$xname && $this->valid_properties
[$name] & RFC2445_ONCE
&& isset($this->properties
[$name])) {
108 // Otherwise add it to the instance array for this property
109 $this->properties
[$name][] = $property;
112 // Finally: after all these, does the component invariant hold?
113 if(!$this->invariant_holds()) {
114 // If not, completely undo the property addition
115 array_pop($this->properties
[$name]);
116 if(empty($this->properties
[$name])) {
117 unset($this->properties
[$name]);
126 function add_component($component) {
128 // With the detailed interface, you can add only components with this function
129 if(!is_object($component) ||
!is_subclass_of($component, 'iCalendar_component')) {
133 $name = $component->get_name();
135 // Only valid components as specified by this component are allowed
136 if(!in_array($name, $this->valid_components
)) {
141 $this->components
[$name][] = $component;
146 function get_property_list($name) {
149 function invariant_holds() {
153 function is_valid() {
154 // If we have any child components, check that they are all valid
155 if(!empty($this->components
)) {
156 foreach($this->components
as $component => $instances) {
157 foreach($instances as $number => $instance) {
158 if(!$instance->is_valid()) {
165 // Finally, check the valid property list for any mandatory properties
166 // that have not been set and do not have a default value
167 foreach($this->valid_properties
as $property => $propdata) {
168 if(($propdata & RFC2445_REQUIRED
) && empty($this->properties
[$property])) {
169 $classname = 'iCalendar_property_'.strtolower(str_replace('-', '_', $property));
170 $object = new $classname;
171 if($object->default_value() === NULL) {
181 function serialize() {
182 // Check for validity of the object
183 if(!$this->is_valid()) {
187 // Maybe the object is valid, but there are some required properties that
188 // have not been given explicit values. In that case, set them to defaults.
189 foreach($this->valid_properties
as $property => $propdata) {
190 if(($propdata & RFC2445_REQUIRED
) && empty($this->properties
[$property])) {
191 $this->add_property($property);
196 $string = rfc2445_fold('BEGIN:'.$this->name
) . RFC2445_CRLF
;
198 // List of properties
199 if(!empty($this->properties
)) {
200 foreach($this->properties
as $name => $properties) {
201 foreach($properties as $property) {
202 $string .= $property->serialize();
207 // List of components
208 if(!empty($this->components
)) {
209 foreach($this->components
as $name => $components) {
210 foreach($components as $component) {
211 $string .= $component->serialize();
217 $string .= rfc2445_fold('END:'.$this->name
) . RFC2445_CRLF
;
225 * I needed a way to convert an iCalendar component back to a Bennu object so I could
226 * easily access and modify it after it had been stored; if this functionality is already
227 * present somewhere in the library, I apologize for adding it here unnecessarily; however,
228 * I couldn't find it so I added it myself.
229 * @param string $string the iCalendar object to load in to this iCalendar_component
230 * @return bool true if the file parsed with no errors. False if there were errors.
233 function unserialize($string) {
234 $string = rfc2445_unfold($string); // Unfold any long lines
235 $lines = explode(RFC2445_CRLF
, $string); // Create an array of lines
237 $components = array(); // Initialise a stack of components
238 $this->clear_errors();
239 foreach ($lines as $key => $line) {
240 // ignore empty lines
241 if (trim($line) == '') {
245 // Divide the line up into label, parameters and data fields.
246 if (!preg_match('#^(?P<label>[-[:alnum:]]+)(?P<params>(?:;(?:(?:[-[:alnum:]]+)=(?:[^[:cntrl:]";:,]+|"[^[:cntrl:]"]+")))*):(?P<data>.*)$#', $line, $match)) {
247 $this->parser_error('Invalid line: '.$key.', ignoring');
253 if (preg_match_all('#;(?P<param>[-[:alnum:]]+)=(?P<value>[^[:cntrl:]";:,]+|"[^[:cntrl:]"]+")#', $match['params'], $pmatch)) {
254 $params = array_combine($pmatch['param'], $pmatch['value']);
256 $label = $match['label'];
257 $data = $match['data'];
258 unset($match, $pmatch);
260 if ($label == 'BEGIN') {
261 // This is the start of a component.
262 $current_component = array_pop($components); // Get the current component off the stack so we can check its valid components
263 if ($current_component == null) { // If there's nothing on the stack
264 $current_component = $this; // use the iCalendar
266 if (in_array($data, $current_component->valid_components
)) { // Check that the new component is a valid subcomponent of the current one
267 if($current_component != $this) {
268 array_push($components, $current_component); // We're done with the current component, put it back on the stack.
270 if(strpos($data, 'V') === 0) {
271 $data = substr($data, 1);
273 $cname = 'iCalendar_' . strtolower($data);
274 $new_component = new $cname;
275 array_push($components, $new_component); // Push a new component onto the stack
277 if($current_component != $this) {
278 array_push($components, $current_component);
279 $this->parser_error('Invalid component type on line '.$key);
282 unset($current_component, $new_component);
283 } else if ($label == 'END') {
284 // It's the END of a component.
285 $component = array_pop($components); // Pop the top component off the stack - we're now done with it
286 $parent_component = array_pop($components); // Pop the component's conatining component off the stack so we can add this component to it.
287 if($parent_component == null) {
288 $parent_component = $this; // If there's no components on the stack, use the iCalendar object
290 if ($parent_component->add_component($component) === false) {
291 $this->parser_error("Failed to add component on line $key");
293 if ($parent_component != $this) { // If we're not using the iCalendar
294 array_push($components, $parent_component); // Put the component back on the stack
296 unset($parent_component, $component);
299 $component = array_pop($components); // Get the component off the stack so we can add properties to it
300 if ($component == null) { // If there's nothing on the stack
301 $component = $this; // use the iCalendar
304 if ($component->add_property($label, $data, $params) === false) {
305 $this->parser_error("Failed to add property '$label' on line $key");
308 if($component != $this) { // If we're not using the iCalendar
309 array_push($components, $component); // Put the component back on the stack
318 function clear_errors() {
319 $this->parser_errors
= array();
322 function parser_error($error) {
323 $this->parser_errors
[] = $error;
328 class iCalendar
extends iCalendar_component
{
329 var $name = 'VCALENDAR';
331 function __construct() {
332 $this->valid_properties
= array(
333 'CALSCALE' => RFC2445_OPTIONAL | RFC2445_ONCE
,
334 'METHOD' => RFC2445_OPTIONAL | RFC2445_ONCE
,
335 'PRODID' => RFC2445_REQUIRED | RFC2445_ONCE
,
336 'VERSION' => RFC2445_REQUIRED | RFC2445_ONCE
,
337 RFC2445_XNAME
=> RFC2445_OPTIONAL
340 $this->valid_components
= array(
341 'VEVENT', 'VTODO', 'VJOURNAL', 'VFREEBUSY', 'VTIMEZONE', 'VALARM'
343 parent
::__construct();
348 class iCalendar_event
extends iCalendar_component
{
350 var $name = 'VEVENT';
353 function __construct() {
355 $this->valid_components
= array('VALARM');
357 $this->valid_properties
= array(
358 'CLASS' => RFC2445_OPTIONAL | RFC2445_ONCE
,
359 'CREATED' => RFC2445_OPTIONAL | RFC2445_ONCE
,
360 'DESCRIPTION' => RFC2445_OPTIONAL | RFC2445_ONCE
,
361 // Standard ambiguous here: in 4.6.1 it says that DTSTAMP in optional,
362 // while in 4.8.7.2 it says it's REQUIRED. Go with REQUIRED.
363 'DTSTAMP' => RFC2445_REQUIRED | RFC2445_ONCE
,
364 // Standard ambiguous here: in 4.6.1 it says that DTSTART in optional,
365 // while in 4.8.2.4 it says it's REQUIRED. Go with REQUIRED.
366 'DTSTART' => RFC2445_REQUIRED | RFC2445_ONCE
,
367 'GEO' => RFC2445_OPTIONAL | RFC2445_ONCE
,
368 'LAST-MODIFIED' => RFC2445_OPTIONAL | RFC2445_ONCE
,
369 'LOCATION' => RFC2445_OPTIONAL | RFC2445_ONCE
,
370 'ORGANIZER' => RFC2445_OPTIONAL | RFC2445_ONCE
,
371 'PRIORITY' => RFC2445_OPTIONAL | RFC2445_ONCE
,
372 'SEQUENCE' => RFC2445_OPTIONAL | RFC2445_ONCE
,
373 'STATUS' => RFC2445_OPTIONAL | RFC2445_ONCE
,
374 'SUMMARY' => RFC2445_OPTIONAL | RFC2445_ONCE
,
375 'TRANSP' => RFC2445_OPTIONAL | RFC2445_ONCE
,
376 // Standard ambiguous here: in 4.6.1 it says that UID in optional,
377 // while in 4.8.4.7 it says it's REQUIRED. Go with REQUIRED.
378 'UID' => RFC2445_REQUIRED | RFC2445_ONCE
,
379 'URL' => RFC2445_OPTIONAL | RFC2445_ONCE
,
380 'RECURRENCE-ID' => RFC2445_OPTIONAL | RFC2445_ONCE
,
381 'DTEND' => RFC2445_OPTIONAL | RFC2445_ONCE
,
382 'DURATION' => RFC2445_OPTIONAL | RFC2445_ONCE
,
383 'ATTACH' => RFC2445_OPTIONAL
,
384 'ATTENDEE' => RFC2445_OPTIONAL
,
385 'CATEGORIES' => RFC2445_OPTIONAL
,
386 'COMMENT' => RFC2445_OPTIONAL
,
387 'CONTACT' => RFC2445_OPTIONAL
,
388 'EXDATE' => RFC2445_OPTIONAL
,
389 'EXRULE' => RFC2445_OPTIONAL
,
390 'REQUEST-STATUS' => RFC2445_OPTIONAL
,
391 'RELATED-TO' => RFC2445_OPTIONAL
,
392 'RESOURCES' => RFC2445_OPTIONAL
,
393 'RDATE' => RFC2445_OPTIONAL
,
394 'RRULE' => RFC2445_OPTIONAL
,
395 RFC2445_XNAME
=> RFC2445_OPTIONAL
398 parent
::__construct();
401 function invariant_holds() {
402 // DTEND and DURATION must not appear together
403 if(isset($this->properties
['DTEND']) && isset($this->properties
['DURATION'])) {
408 if(isset($this->properties
['DTEND']) && isset($this->properties
['DTSTART'])) {
409 // DTEND must be later than DTSTART
410 // The standard is not clear on how to hande different value types though
411 // TODO: handle this correctly even if the value types are different
412 if($this->properties
['DTEND'][0]->value
<= $this->properties
['DTSTART'][0]->value
) {
416 // DTEND and DTSTART must have the same value type
417 if($this->properties
['DTEND'][0]->val_type
!= $this->properties
['DTSTART'][0]->val_type
) {
427 class iCalendar_todo
extends iCalendar_component
{
431 function __construct() {
433 $this->valid_components
= array('VALARM');
435 $this->valid_properties
= array(
436 'CLASS' => RFC2445_OPTIONAL | RFC2445_ONCE
,
437 'COMPLETED' => RFC2445_OPTIONAL | RFC2445_ONCE
,
438 'CREATED' => RFC2445_OPTIONAL | RFC2445_ONCE
,
439 'DESCRIPTION' => RFC2445_OPTIONAL | RFC2445_ONCE
,
440 'DTSTAMP' => RFC2445_OPTIONAL | RFC2445_ONCE
,
441 'DTSTAP' => RFC2445_OPTIONAL | RFC2445_ONCE
,
442 'GEO' => RFC2445_OPTIONAL | RFC2445_ONCE
,
443 'LAST-MODIFIED' => RFC2445_OPTIONAL | RFC2445_ONCE
,
444 'LOCATION' => RFC2445_OPTIONAL | RFC2445_ONCE
,
445 'ORGANIZER' => RFC2445_OPTIONAL | RFC2445_ONCE
,
446 'PERCENT' => RFC2445_OPTIONAL | RFC2445_ONCE
,
447 'PRIORITY' => RFC2445_OPTIONAL | RFC2445_ONCE
,
448 'RECURID' => RFC2445_OPTIONAL | RFC2445_ONCE
,
449 'SEQUENCE' => RFC2445_OPTIONAL | RFC2445_ONCE
,
450 'STATUS' => RFC2445_OPTIONAL | RFC2445_ONCE
,
451 'SUMMARY' => RFC2445_OPTIONAL | RFC2445_ONCE
,
452 'UID' => RFC2445_OPTIONAL | RFC2445_ONCE
,
453 'URL' => RFC2445_OPTIONAL | RFC2445_ONCE
,
454 'DUE' => RFC2445_OPTIONAL | RFC2445_ONCE
,
455 'DURATION' => RFC2445_OPTIONAL | RFC2445_ONCE
,
456 'ATTACH' => RFC2445_OPTIONAL
,
457 'ATTENDEE' => RFC2445_OPTIONAL
,
458 'CATEGORIES' => RFC2445_OPTIONAL
,
459 'COMMENT' => RFC2445_OPTIONAL
,
460 'CONTACT' => RFC2445_OPTIONAL
,
461 'EXDATE' => RFC2445_OPTIONAL
,
462 'EXRULE' => RFC2445_OPTIONAL
,
463 'RSTATUS' => RFC2445_OPTIONAL
,
464 'RELATED' => RFC2445_OPTIONAL
,
465 'RESOURCES' => RFC2445_OPTIONAL
,
466 'RDATE' => RFC2445_OPTIONAL
,
467 'RRULE' => RFC2445_OPTIONAL
,
468 RFC2445_XNAME
=> RFC2445_OPTIONAL
471 parent
::__construct();
474 function invariant_holds() {
475 // DTEND and DURATION must not appear together
476 if(isset($this->properties
['DTEND']) && isset($this->properties
['DURATION'])) {
481 if(isset($this->properties
['DTEND']) && isset($this->properties
['DTSTART'])) {
482 // DTEND must be later than DTSTART
483 // The standard is not clear on how to hande different value types though
484 // TODO: handle this correctly even if the value types are different
485 if($this->properties
['DTEND'][0]->value
<= $this->properties
['DTSTART'][0]->value
) {
489 // DTEND and DTSTART must have the same value type
490 if($this->properties
['DTEND'][0]->val_type
!= $this->properties
['DTSTART'][0]->val_type
) {
496 if(isset($this->properties
['DUE']) && isset($this->properties
['DTSTART'])) {
497 if($this->properties
['DUE'][0]->value
<= $this->properties
['DTSTART'][0]->value
) {
507 class iCalendar_journal
extends iCalendar_component
{
508 var $name = 'VJOURNAL';
511 function __construct() {
513 $this->valid_properties
= array(
514 'CLASS' => RFC2445_OPTIONAL | RFC2445_ONCE
,
515 'CREATED' => RFC2445_OPTIONAL | RFC2445_ONCE
,
516 'DESCRIPTION' => RFC2445_OPTIONAL | RFC2445_ONCE
,
517 'DTSTART' => RFC2445_OPTIONAL | RFC2445_ONCE
,
518 'DTSTAMP' => RFC2445_OPTIONAL | RFC2445_ONCE
,
519 'LAST-MODIFIED' => RFC2445_OPTIONAL | RFC2445_ONCE
,
520 'ORGANIZER' => RFC2445_OPTIONAL | RFC2445_ONCE
,
521 'RECURRANCE-ID' => RFC2445_OPTIONAL | RFC2445_ONCE
,
522 'SEQUENCE' => RFC2445_OPTIONAL | RFC2445_ONCE
,
523 'STATUS' => RFC2445_OPTIONAL | RFC2445_ONCE
,
524 'SUMMARY' => RFC2445_OPTIONAL | RFC2445_ONCE
,
525 'UID' => RFC2445_OPTIONAL | RFC2445_ONCE
,
526 'URL' => RFC2445_OPTIONAL | RFC2445_ONCE
,
527 'ATTACH' => RFC2445_OPTIONAL
,
528 'ATTENDEE' => RFC2445_OPTIONAL
,
529 'CATEGORIES' => RFC2445_OPTIONAL
,
530 'COMMENT' => RFC2445_OPTIONAL
,
531 'CONTACT' => RFC2445_OPTIONAL
,
532 'EXDATE' => RFC2445_OPTIONAL
,
533 'EXRULE' => RFC2445_OPTIONAL
,
534 'RELATED-TO' => RFC2445_OPTIONAL
,
535 'RDATE' => RFC2445_OPTIONAL
,
536 'RRULE' => RFC2445_OPTIONAL
,
537 RFC2445_XNAME
=> RFC2445_OPTIONAL
540 parent
::__construct();
545 class iCalendar_freebusy
extends iCalendar_component
{
546 var $name = 'VFREEBUSY';
549 function __construct() {
550 $this->valid_components
= array();
551 $this->valid_properties
= array(
552 'CONTACT' => RFC2445_OPTIONAL | RFC2445_ONCE
,
553 'DTSTART' => RFC2445_OPTIONAL | RFC2445_ONCE
,
554 'DTEND' => RFC2445_OPTIONAL | RFC2445_ONCE
,
555 'DURATION' => RFC2445_OPTIONAL | RFC2445_ONCE
,
556 'DTSTAMP' => RFC2445_OPTIONAL | RFC2445_ONCE
,
557 'ORGANIZER' => RFC2445_OPTIONAL | RFC2445_ONCE
,
558 'UID' => RFC2445_OPTIONAL | RFC2445_ONCE
,
559 'URL' => RFC2445_OPTIONAL | RFC2445_ONCE
,
560 // TODO: the next two are components of their own!
561 'ATTENDEE' => RFC2445_OPTIONAL
,
562 'COMMENT' => RFC2445_OPTIONAL
,
563 'FREEBUSY' => RFC2445_OPTIONAL
,
564 'RSTATUS' => RFC2445_OPTIONAL
,
565 RFC2445_XNAME
=> RFC2445_OPTIONAL
568 parent
::__construct();
571 function invariant_holds() {
572 // DTEND and DURATION must not appear together
573 if(isset($this->properties
['DTEND']) && isset($this->properties
['DURATION'])) {
578 if(isset($this->properties
['DTEND']) && isset($this->properties
['DTSTART'])) {
579 // DTEND must be later than DTSTART
580 // The standard is not clear on how to hande different value types though
581 // TODO: handle this correctly even if the value types are different
582 if($this->properties
['DTEND'][0]->value
<= $this->properties
['DTSTART'][0]->value
) {
586 // DTEND and DTSTART must have the same value type
587 if($this->properties
['DTEND'][0]->val_type
!= $this->properties
['DTSTART'][0]->val_type
) {
596 class iCalendar_alarm
extends iCalendar_component
{
597 var $name = 'VALARM';
600 function __construct() {
601 $this->valid_components
= array();
602 $this->valid_properties
= array(
603 'ACTION' => RFC2445_REQUIRED | RFC2445_ONCE
,
604 'TRIGGER' => RFC2445_REQUIRED | RFC2445_ONCE
,
605 // If one of these 2 occurs, so must the other.
606 'DURATION' => RFC2445_OPTIONAL | RFC2445_ONCE
,
607 'REPEAT' => RFC2445_OPTIONAL | RFC2445_ONCE
,
608 // The following is required if action == "PROCEDURE" | "AUDIO"
609 'ATTACH' => RFC2445_OPTIONAL
,
610 // The following is required if trigger == "EMAIL" | "DISPLAY"
611 'DESCRIPTION' => RFC2445_OPTIONAL | RFC2445_ONCE
,
612 // The following are required if action == "EMAIL"
613 'SUMMARY' => RFC2445_OPTIONAL | RFC2445_ONCE
,
614 'ATTENDEE' => RFC2445_OPTIONAL
,
615 RFC2445_XNAME
=> RFC2445_OPTIONAL
618 parent
::__construct();
621 function invariant_holds() {
622 // DTEND and DURATION must not appear together
623 if(isset($this->properties
['ACTION'])) {
624 switch ($this->properties
['ACTION'][0]->value
) {
626 if (!isset($this->properties
['ATTACH'])) {
631 if (!isset($this->properties
['DESCRIPTION'])) {
636 if (!isset($this->properties
['DESCRIPTION']) ||
!isset($this->properties
['SUMMARY']) ||
!isset($this->properties
['ATTACH'])) {
641 if (!isset($this->properties
['ATTACH']) ||
count($this->properties
['ATTACH']) > 1) {
653 class iCalendar_timezone
extends iCalendar_component
{
654 var $name = 'VTIMEZONE';
657 function __construct() {
659 $this->valid_components
= array('STANDARD', 'DAYLIGHT');
661 $this->valid_properties
= array(
662 'TZID' => RFC2445_REQUIRED | RFC2445_ONCE
,
663 'LAST-MODIFIED' => RFC2445_OPTIONAL | RFC2445_ONCE
,
664 'TZURL' => RFC2445_OPTIONAL | RFC2445_ONCE
,
665 RFC2445_XNAME
=> RFC2445_OPTIONAL
668 parent
::__construct();
673 class iCalendar_standard
extends iCalendar_component
{
674 var $name = 'STANDARD';
677 function __construct() {
678 $this->valid_components
= array();
679 $this->valid_properties
= array(
680 'DTSTART' => RFC2445_REQUIRED | RFC2445_ONCE
,
681 'TZOFFSETTO' => RFC2445_REQUIRED | RFC2445_ONCE
,
682 'TZOFFSETFROM' => RFC2445_REQUIRED | RFC2445_ONCE
,
683 'COMMENT' => RFC2445_OPTIONAL
,
684 'RDATE' => RFC2445_OPTIONAL
,
685 'RRULE' => RFC2445_OPTIONAL
,
686 'TZNAME' => RFC2445_OPTIONAL
,
687 RFC2445_XNAME
=> RFC2445_OPTIONAL
,
689 parent
::__construct();
693 class iCalendar_daylight
extends iCalendar_standard
{
694 var $name = 'DAYLIGHT';
697 // REMINDER: DTEND must be later than DTSTART for all components which support both
698 // REMINDER: DUE must be later than DTSTART for all components which support both