Andrew's Web Libraries (AWL)
/build/awl-Fcy7wi/awl-0.59/inc/iCalendar.php

A Class for handling iCalendar data.

When parsed the underlying structure is roughly as follows:

iCalendar( array(iCalComponent), array(iCalProp) )

each iCalComponent is similarly structured:

iCalComponent( array(iCalComponent), array(iCalProp) )

Once parsed, $ical->component will point to the wrapping VCALENDAR component of the iCalendar. This will be fine for simple iCalendar usage as sampled below, but more complex iCalendar such as a VEVENT with RRULE which has repeat overrides will need quite a bit more thought to process correctly.

To create a new iCalendar from several data values: $ical = new iCalendar( array('DTSTART' => $dtstart, 'SUMMARY' => $summary, 'DURATION' => $duration ) );

<?php
require_once('XMLElement.php');
require_once('AwlQuery.php');
class iCalProp {
var $name;
var $parameters;
var $content;
var $rendered;
function __construct( $propstring = null ) {
$this->name = "";
$this->content = "";
$this->parameters = array();
unset($this->rendered);
if ( $propstring != null && gettype($propstring) == 'string' ) {
$this->ParseFrom($propstring);
}
}
function ParseFrom( $propstring ) {
$this->rendered = (strlen($propstring) < 72 ? $propstring : null); // Only pre-rendered if we didn't unescape it
// Unescape newlines
$unescaped = preg_replace('{\\\\[nN]}', "\n", $propstring);
/*
* Split propname with params from propvalue. Searches for the first unquoted COLON.
*
* RFC5545 3.2
*
* Property parameter values that contain the COLON, SEMICOLON, or COMMA
* character separators MUST be specified as quoted-string text values.
* Property parameter values MUST NOT contain the DQUOTE character.
*/
$split = $this->SplitQuoted($unescaped, ':', 2);
if (count($split) != 2) {
// Bad things happended...
dbg_error_log('ERROR', "iCalendar::ParseFrom(): Couldn't parse property from string: `%s`, skipping", $unescaped);
return;
}
list($prop, $value) = $split;
// Unescape ESCAPED-CHAR
$this->content = preg_replace( "/\\\\([,;:\"\\\\])/", '$1', $value);
// Split property name and parameters
$parameters = $this->SplitQuoted($prop, ';');
$this->name = array_shift($parameters);
$this->parameters = array();
foreach ($parameters AS $k => $v) {
$pos = strpos($v, '=');
$name = substr($v, 0, $pos);
$value = substr($v, $pos + 1);
$this->parameters[$name] = preg_replace('/^"(.+)"$/', '$1', $value); // Removes DQUOTE on demand
}
// dbg_error_log('iCalendar', " iCalProp::ParseFrom found '%s' = '%s' with %d parameters", $this->name, substr($this->content,0,200), count($this->parameters) );
}
function SplitQuoted($str, $sep = ',', $limit = 0) {
$result = array();
$cursor = 0;
$inquote = false;
$num = 0;
for($i = 0, $len = strlen($str); $i < $len; ++$i) {
$ch = $str[$i];
if ($ch == '"') {
$inquote = !$inquote;
}
if (!$inquote && $ch == $sep) {
//var_dump("Found sep `$sep` - Splitting from $cursor to $i from $len.");
// If we reached the maximal number of splits, we cut till the end and stop here.
++$num;
if ($limit > 0 && $num == $limit) {
$result[] = substr($str, $cursor);
break;
}
$result[] = substr($str, $cursor, $i - $cursor);
$cursor = $i + 1;
}
// Add rest of string on end reached
if ($i + 1 == $len) {
//var_dump("Reached end - Splitting from $cursor to $len.");
$result[] = substr($str, $cursor);
}
}
return $result;
}
function Name( $newname = null ) {
if ( $newname != null ) {
$this->name = $newname;
if ( isset($this->rendered) ) unset($this->rendered);
// dbg_error_log('iCalendar', " iCalProp::Name(%s)", $this->name );
}
return $this->name;
}
function Value( $newvalue = null ) {
if ( $newvalue != null ) {
$this->content = $newvalue;
if ( isset($this->rendered) ) unset($this->rendered);
}
return $this->content;
}
function Parameters( $newparams = null ) {
if ( $newparams != null ) {
$this->parameters = $newparams;
if ( isset($this->rendered) ) unset($this->rendered);
}
return $this->parameters;
}
function TextMatch( $search ) {
if ( isset($this->content) ) {
return (stristr( $this->content, $search ) !== false);
}
return false;
}
function GetParameterValue( $name ) {
if ( isset($this->parameters[$name]) ) return $this->parameters[$name];
}
function SetParameterValue( $name, $value ) {
if ( isset($this->rendered) ) unset($this->rendered);
$this->parameters[$name] = $value;
}
function RenderParameters() {
$rendered = "";
foreach( $this->parameters AS $k => $v ) {
$escaped = preg_replace( "/([;:])/", '\\\\$1', $v);
$rendered .= sprintf( ";%s=%s", $k, $escaped );
}
return $rendered;
}
function Render() {
// If we still have the string it was parsed in from, it hasn't been screwed with
// and we can just return that without modification.
if ( isset($this->rendered) ) return $this->rendered;
$property = preg_replace( '/[;].*$/', '', $this->name );
$escaped = $this->content;
switch( $property ) {
case 'ATTACH': case 'GEO': case 'PERCENT-COMPLETE': case 'PRIORITY':
case 'DURATION': case 'FREEBUSY': case 'TZOFFSETFROM': case 'TZOFFSETTO':
case 'TZURL': case 'ATTENDEE': case 'ORGANIZER': case 'RECURRENCE-ID':
case 'URL': case 'EXRULE': case 'SEQUENCE': case 'CREATED':
case 'RRULE': case 'REPEAT': case 'TRIGGER':
break;
case 'COMPLETED': case 'DTEND':
case 'DUE': case 'DTSTART':
case 'DTSTAMP': case 'LAST-MODIFIED':
case 'CREATED': case 'EXDATE':
case 'RDATE':
if ( isset($this->parameters['VALUE']) && $this->parameters['VALUE'] == 'DATE' ) {
$escaped = substr( $escaped, 0, 8);
}
break;
default:
$escaped = str_replace( '\\', '\\\\', $escaped);
$escaped = preg_replace( '/\r?\n/', '\\n', $escaped);
$escaped = preg_replace( "/([,;])/", '\\\\$1', $escaped);
}
$property = sprintf( "%s%s:", $this->name, $this->RenderParameters() );
if ( (strlen($property) + strlen($escaped)) <= 72 ) {
$this->rendered = $property . $escaped;
}
else if ( (strlen($property) + strlen($escaped)) > 72 && (strlen($property) < 72) && (strlen($escaped) < 72) ) {
$this->rendered = $property . "\r\n " . $escaped;
}
else {
$this->rendered = preg_replace( '/(.{72})/u', '$1'."\r\n ", $property . $escaped );
}
return $this->rendered;
}
}
var $type;
var $properties;
var $components;
var $rendered;
function __construct( $content = null ) {
$this->type = "";
$this->properties = array();
$this->components = array();
$this->rendered = "";
if ( $content != null && (gettype($content) == 'string' || gettype($content) == 'array') ) {
$this->ParseFrom($content);
}
}
function VCalendar( $extra_properties = null ) {
$this->SetType('VCALENDAR');
$this->AddProperty('PRODID', '-//davical.org//NONSGML AWL Calendar//EN');
$this->AddProperty('VERSION', '2.0');
$this->AddProperty('CALSCALE', 'GREGORIAN');
if ( is_array($extra_properties) ) {
foreach( $extra_properties AS $k => $v ) {
$this->AddProperty($k,$v);
}
}
}
function CollectParameterValues( $parameter_name ) {
$values = array();
foreach( $this->components AS $k => $v ) {
$also = $v->CollectParameterValues($parameter_name);
$values = array_merge( $values, $also );
}
foreach( $this->properties AS $k => $v ) {
$also = $v->GetParameterValue($parameter_name);
if ( isset($also) && $also != "" ) {
// dbg_error_log( 'iCalendar', "::CollectParameterValues(%s) : Found '%s'", $parameter_name, $also);
$values[$also] = 1;
}
}
return $values;
}
function ParseFrom( $content ) {
$this->rendered = $content;
$content = $this->UnwrapComponent($content);
$type = false;
$subtype = false;
$finish = null;
$subfinish = null;
$length = strlen($content);
$linefrom = 0;
while( $linefrom < $length ) {
$lineto = strpos( $content, "\n", $linefrom );
if ( $lineto === false ) {
$lineto = strpos( $content, "\r", $linefrom );
}
if ( $lineto > 0 ) {
$line = substr( $content, $linefrom, $lineto - $linefrom);
$linefrom = $lineto + 1;
}
else {
$line = substr( $content, $linefrom );
$linefrom = $length;
}
if ( preg_match('/^\s*$/', $line ) ) continue;
$line = rtrim( $line, "\r\n" );
// dbg_error_log( 'iCalendar', "::ParseFrom: Parsing line: $line");
if ( $type === false ) {
if ( preg_match( '/^BEGIN:(.+)$/', $line, $matches ) ) {
// We have found the start of the main component
$type = $matches[1];
$finish = "END:$type";
$this->type = $type;
dbg_error_log( 'iCalendar', "::ParseFrom: Start component of type '%s'", $type);
}
else {
dbg_error_log( 'iCalendar', "::ParseFrom: Ignoring crap before start of component: $line");
// unset($lines[$k]); // The content has crap before the start
if ( $line != "" ) $this->rendered = null;
}
}
else if ( $type == null ) {
dbg_error_log( 'iCalendar', "::ParseFrom: Ignoring crap after end of component");
if ( $line != "" ) $this->rendered = null;
}
else if ( $line == $finish ) {
dbg_error_log( 'iCalendar', "::ParseFrom: End of component");
$type = null; // We have reached the end of our component
}
else {
if ( $subtype === false && preg_match( '/^BEGIN:(.+)$/', $line, $matches ) ) {
// We have found the start of a sub-component
$subtype = $matches[1];
$subfinish = "END:$subtype";
$subcomponent = $line . "\r\n";
dbg_error_log( 'iCalendar', "::ParseFrom: Found a subcomponent '%s'", $subtype);
}
else if ( $subtype ) {
// We are inside a sub-component
$subcomponent .= $this->WrapComponent($line);
if ( $line == $subfinish ) {
dbg_error_log( 'iCalendar', "::ParseFrom: End of subcomponent '%s'", $subtype);
// We have found the end of a sub-component
$this->components[] = new iCalComponent($subcomponent);
$subtype = false;
}
// else
// dbg_error_log( 'iCalendar', "::ParseFrom: Inside a subcomponent '%s'", $subtype );
}
else {
// dbg_error_log( 'iCalendar', "::ParseFrom: Parse property of component");
// It must be a normal property line within a component.
$this->properties[] = new iCalProp($line);
}
}
}
}
function UnwrapComponent( $content ) {
return preg_replace('/\r?\n[ \t]/', '', $content );
}
function WrapComponent( $content ) {
$strs = preg_split( "/\r?\n/", $content );
$wrapped = "";
foreach ($strs as $str) {
$wrapped .= preg_replace( '/(.{72})/u', '$1'."\r\n ", $str ) ."\r\n";
}
return $wrapped;
}
function GetType() {
return $this->type;
}
function SetType( $type ) {
if ( isset($this->rendered) ) unset($this->rendered);
$this->type = $type;
return $this->type;
}
function GetProperties( $type = null ) {
$properties = array();
foreach( $this->properties AS $k => $v ) {
if ( $type == null || $v->Name() == $type ) {
$properties[$k] = $v;
}
}
return $properties;
}
function GetPValue( $type ) {
foreach( $this->properties AS $k => $v ) {
if ( $v->Name() == $type ) return $v->Value();
}
return null;
}
function GetPParamValue( $type, $parameter_name ) {
foreach( $this->properties AS $k => $v ) {
if ( $v->Name() == $type ) return $v->GetParameterValue($parameter_name);
}
return null;
}
function ClearProperties( $type = null ) {
if ( $type != null ) {
// First remove all the existing ones of that type
foreach( $this->properties AS $k => $v ) {
if ( $v->Name() == $type ) {
unset($this->properties[$k]);
if ( isset($this->rendered) ) unset($this->rendered);
}
}
$this->properties = array_values($this->properties);
}
else {
if ( isset($this->rendered) ) unset($this->rendered);
$this->properties = array();
}
}
function SetProperties( $new_properties, $type = null ) {
if ( isset($this->rendered) && count($new_properties) > 0 ) unset($this->rendered);
$this->ClearProperties($type);
foreach( $new_properties AS $k => $v ) {
$this->AddProperty($v);
}
}
function AddProperty( $new_property, $value = null, $parameters = null ) {
if ( isset($this->rendered) ) unset($this->rendered);
if ( isset($value) && gettype($new_property) == 'string' ) {
$new_prop = new iCalProp();
$new_prop->Name($new_property);
$new_prop->Value($value);
if ( $parameters != null ) $new_prop->Parameters($parameters);
dbg_error_log('iCalendar'," Adding new property '%s'", $new_prop->Render() );
$this->properties[] = $new_prop;
}
else if ( gettype($new_property) ) {
$this->properties[] = $new_property;
}
}
function &FirstNonTimezone( $type = null ) {
foreach( $this->components AS $k => $v ) {
if ( $v->GetType() != 'VTIMEZONE' ) return $this->components[$k];
}
$result = false;
return $result;
}
function IsOrganizer( $email ) {
if ( !preg_match( '#^mailto:#', $email ) ) $email = 'mailto:'.$email;
$props = $this->GetPropertiesByPath('!VTIMEZONE/ORGANIZER');
foreach( $props AS $k => $prop ) {
if ( $prop->Value() == $email ) return true;
}
return false;
}
function IsAttendee( $email ) {
if ( !preg_match( '#^mailto:#', $email ) ) $email = 'mailto:'.$email;
if ( $this->IsOrganizer($email) ) return true;
$props = $this->GetPropertiesByPath('!VTIMEZONE/ATTENDEE');
foreach( $props AS $k => $prop ) {
if ( $prop->Value() == $email ) return true;
}
return false;
}
function GetComponents( $type = null, $normal_match = true ) {
$components = $this->components;
if ( $type != null ) {
foreach( $components AS $k => $v ) {
if ( ($v->GetType() != $type) === $normal_match ) {
unset($components[$k]);
}
}
$components = array_values($components);
}
return $components;
}
function ClearComponents( $type = null ) {
if ( $type != null ) {
// First remove all the existing ones of that type
foreach( $this->components AS $k => $v ) {
if ( $v->GetType() == $type ) {
unset($this->components[$k]);
if ( isset($this->rendered) ) unset($this->rendered);
}
else {
if ( ! $this->components[$k]->ClearComponents($type) ) {
if ( isset($this->rendered) ) unset($this->rendered);
}
}
}
return isset($this->rendered);
}
else {
if ( isset($this->rendered) ) unset($this->rendered);
$this->components = array();
}
}
function SetComponents( $new_component, $type = null ) {
if ( isset($this->rendered) ) unset($this->rendered);
if ( count($new_component) > 0 ) $this->ClearComponents($type);
foreach( $new_component AS $k => $v ) {
$this->components[] = $v;
}
}
function AddComponent( $new_component ) {
if ( is_array($new_component) && count($new_component) == 0 ) return;
if ( isset($this->rendered) ) unset($this->rendered);
if ( is_array($new_component) ) {
foreach( $new_component AS $k => $v ) {
$this->components[] = $v;
}
}
else {
$this->components[] = $new_component;
}
}
function MaskComponents( $keep ) {
foreach( $this->components AS $k => $v ) {
if ( ! in_array( $v->GetType(), $keep ) ) {
unset($this->components[$k]);
if ( isset($this->rendered) ) unset($this->rendered);
}
else {
$v->MaskComponents($keep);
}
}
}
function MaskProperties( $keep, $component_list=null ) {
foreach( $this->components AS $k => $v ) {
$v->MaskProperties($keep, $component_list);
}
if ( !isset($component_list) || in_array($this->GetType(), $component_list) ) {
foreach( $this->properties AS $k => $v ) {
if ( ! in_array( $v->name, $keep ) ) {
unset($this->properties[$k]);
if ( isset($this->rendered) ) unset($this->rendered);
}
}
}
}
function CloneConfidential() {
$confidential = clone($this);
$keep_properties = array( 'DTSTAMP', 'DTSTART', 'RRULE', 'DURATION', 'DTEND', 'DUE', 'UID', 'CLASS', 'TRANSP', 'CREATED', 'LAST-MODIFIED' );
$resource_components = array( 'VEVENT', 'VTODO', 'VJOURNAL' );
$confidential->MaskComponents(array( 'VTIMEZONE', 'STANDARD', 'DAYLIGHT', 'VEVENT', 'VTODO', 'VJOURNAL' ));
$confidential->MaskProperties($keep_properties, $resource_components );
if ( isset($confidential->rendered) )
unset($confidential->rendered); // we need to re-render the whole object
if ( in_array( $confidential->GetType(), $resource_components ) ) {
$confidential->AddProperty( 'SUMMARY', translate('Busy') );
}
foreach( $confidential->components AS $k => $v ) {
if ( in_array( $v->GetType(), $resource_components ) ) {
$v->AddProperty( 'SUMMARY', translate('Busy') );
}
}
return $confidential;
}
function RenderWithoutWrap($restricted_properties = null){
// substr - remove new line of end, because new line
// are handled in vComponent::RenderWithoutWrap
return substr($this->Render($restricted_properties), 0 , -2);
}
function Render( $restricted_properties = null) {
$unrestricted = (!isset($restricted_properties) || count($restricted_properties) == 0);
if ( isset($this->rendered) && $unrestricted )
return $this->rendered;
$rendered = "BEGIN:$this->type\r\n";
foreach( $this->properties AS $k => $v ) {
if ( method_exists($v, 'Render') ) {
if ( $unrestricted || isset($restricted_properties[$v]) ) $rendered .= $v->Render() . "\r\n";
}
}
foreach( $this->components AS $v ) { $rendered .= $v->Render(); }
$rendered .= "END:$this->type\r\n";
$rendered = preg_replace('{(?<!\r)\n}', "\r\n", $rendered);
if ( $unrestricted ) $this->rendered = $rendered;
return $rendered;
}
function GetPropertiesByPath( $path ) {
$properties = array();
dbg_error_log( 'iCalendar', "GetPropertiesByPath: Querying within '%s' for path '%s'", $this->type, $path );
if ( !preg_match( '#(/?)(!?)([^/]+)(/?.*)$#', $path, $matches ) ) return $properties;
$adrift = ($matches[1] == '');
$normal = ($matches[2] == '');
$ourtest = $matches[3];
$therest = $matches[4];
dbg_error_log( 'iCalendar', "GetPropertiesByPath: Matches: %s -- %s -- %s -- %s\n", $matches[1], $matches[2], $matches[3], $matches[4] );
if ( $ourtest == '*' || (($ourtest == $this->type) === $normal) && $therest != '' ) {
if ( preg_match( '#^/(!?)([^/]+)$#', $therest, $matches ) ) {
$normmatch = ($matches[1] =='');
$proptest = $matches[2];
foreach( $this->properties AS $k => $v ) {
if ( $proptest == '*' || (($v->Name() == $proptest) === $normmatch ) ) {
$properties[] = $v;
}
}
}
else {
foreach( $this->components AS $k => $v ) {
$properties = array_merge( $properties, $v->GetPropertiesByPath($therest) );
}
}
}
if ( $adrift ) {
foreach( $this->components AS $k => $v ) {
$properties = array_merge( $properties, $v->GetPropertiesByPath($path) );
}
}
dbg_error_log('iCalendar', "GetPropertiesByPath: Found %d within '%s' for path '%s'\n", count($properties), $this->type, $path );
return $properties;
}
}