egroupware_official/calendar/inc/class.calendar_ical.inc.php

3201 lines
101 KiB
PHP
Raw Normal View History

2008-06-07 19:45:33 +02:00
<?php
/**
* iCal import and export via Horde iCalendar classes
*
* @link http://www.egroupware.org
* @author Lars Kneschke <lkneschke@egroupware.org>
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
2009-11-16 09:04:18 +01:00
* @author Joerg Lehrke <jlehrke@noc.de>
2008-06-07 19:45:33 +02:00
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package calendar
* @subpackage export
* @version $Id$
*/
2009-11-16 09:04:18 +01:00
require_once EGW_SERVER_ROOT.'/phpgwapi/inc/horde/lib/core.php';
2008-06-07 19:45:33 +02:00
/**
* iCal import and export via Horde iCalendar classes
*/
2008-06-07 19:49:16 +02:00
class calendar_ical extends calendar_boupdate
2008-06-07 19:45:33 +02:00
{
/**
* @var array $supportedFields array containing the supported fields of the importing device
*/
var $supportedFields;
var $recur_days_1_0 = array(
MCAL_M_MONDAY => 'MO',
MCAL_M_TUESDAY => 'TU',
MCAL_M_WEDNESDAY => 'WE',
MCAL_M_THURSDAY => 'TH',
MCAL_M_FRIDAY => 'FR',
MCAL_M_SATURDAY => 'SA',
MCAL_M_SUNDAY => 'SU',
);
/**
* @var array $status_egw2ical conversation of the participant status egw => ical
*/
var $status_egw2ical = array(
'U' => 'NEEDS-ACTION',
'A' => 'ACCEPTED',
'R' => 'DECLINED',
'T' => 'TENTATIVE',
2010-02-23 19:35:43 +01:00
'D' => 'DELEGATED'
2008-06-07 19:45:33 +02:00
);
/**
* @var array conversation of the participant status ical => egw
*/
var $status_ical2egw = array(
'NEEDS-ACTION' => 'U',
2009-11-16 09:04:18 +01:00
'NEEDS ACTION' => 'U',
2008-06-07 19:45:33 +02:00
'ACCEPTED' => 'A',
'DECLINED' => 'R',
'TENTATIVE' => 'T',
2009-11-16 09:04:18 +01:00
'DELEGATED' => 'D',
2010-03-01 22:18:52 +01:00
'X-UNINVITED' => 'G', // removed
2008-06-07 19:45:33 +02:00
);
/**
* @var array $priority_egw2ical conversion of the priority egw => ical
2008-06-07 19:45:33 +02:00
*/
var $priority_egw2ical = array(
0 => 0, // undefined
1 => 9, // low
2 => 5, // normal
3 => 1, // high
);
2009-11-16 09:04:18 +01:00
2008-06-07 19:45:33 +02:00
/**
* @var array $priority_ical2egw conversion of the priority ical => egw
2008-06-07 19:45:33 +02:00
*/
var $priority_ical2egw = array(
0 => 0, // undefined
9 => 1, 8 => 1, 7 => 1, 6 => 1, // low
5 => 2, // normal
2009-11-16 09:04:18 +01:00
4 => 3, 3 => 3, 2 => 3, 1 => 3, // high
2008-06-07 19:45:33 +02:00
);
2010-02-23 19:35:43 +01:00
/**
* @var array $priority_egw2funambol conversion of the priority egw => funambol
*/
var $priority_egw2funambol = array(
0 => 1, // undefined (mapped to normal since undefined does not exist)
1 => 0, // low
2 => 1, // normal
3 => 2, // high
);
/**
* @var array $priority_funambol2egw conversion of the priority funambol => egw
*/
var $priority_funambol2egw = array(
0 => 1, // low
1 => 2, // normal
2 => 3, // high
);
2008-06-07 19:45:33 +02:00
/**
* @var array $recur_egw2ical_2_0 converstaion of egw recur-type => ical FREQ
*/
var $recur_egw2ical_2_0 = array(
MCAL_RECUR_DAILY => 'DAILY',
MCAL_RECUR_WEEKLY => 'WEEKLY',
MCAL_RECUR_MONTHLY_MDAY => 'MONTHLY', // BYMONHTDAY={1..31}
MCAL_RECUR_MONTHLY_WDAY => 'MONTHLY', // BYDAY={1..5}{MO..SO}
MCAL_RECUR_YEARLY => 'YEARLY',
);
/**
* @var array $recur_egw2ical_1_0 converstaion of egw recur-type => ical FREQ
*/
var $recur_egw2ical_1_0 = array(
MCAL_RECUR_DAILY => 'D',
MCAL_RECUR_WEEKLY => 'W',
MCAL_RECUR_MONTHLY_MDAY => 'MD', // BYMONHTDAY={1..31}
MCAL_RECUR_MONTHLY_WDAY => 'MP', // BYDAY={1..5}{MO..SO}
MCAL_RECUR_YEARLY => 'YM',
);
/**
* manufacturer and name of the sync-client
*
* @var string
*/
var $productManufacturer = 'file';
var $productName = '';
2009-11-16 09:04:18 +01:00
/**
* user preference: import all-day events as non blocking
*
* @var boolean
*/
var $nonBlockingAllday = false;
/**
* user preference: attach UID entries to the DESCRIPTION
*
* @var boolean
*/
var $uidExtension = false;
/**
* user preference: calendar to synchronize with
*
* @var int
*/
var $calendarOwner = 0;
/**
* user preference: use server timezone for exports to device
*
* @var boolean
*/
var $useServerTZ = false;
2009-11-16 09:04:18 +01:00
/**
* Device CTCap Properties
*
* @var array
*/
var $clientProperties;
/**
* vCalendar Instance for parsing
*
* @var array
*/
var $vCalendar;
2010-03-06 19:43:44 +01:00
/**
* Addressbook BO instance
*
* @var array
*/
var $addressbook;
2009-11-16 09:04:18 +01:00
/**
* Set Logging
*
* @var boolean
*/
var $log = false;
2009-11-16 09:04:18 +01:00
var $logfile="/tmp/log-vcal";
/**
* Constructor
*
* @param array $_clientProperties client properties
*/
function __construct(&$_clientProperties = array())
{
parent::__construct();
if ($this->log) $this->logfile = $GLOBALS['egw_info']['server']['temp_dir']."/log-vcal";
$this->clientProperties = $_clientProperties;
$this->vCalendar = new Horde_iCalendar;
2010-03-01 22:18:52 +01:00
$this->addressbook = new addressbook_bo;
2009-11-16 09:04:18 +01:00
}
2008-06-07 19:45:33 +02:00
/**
* Exports one calendar event to an iCalendar item
*
2010-03-01 22:18:52 +01:00
* @param int|array $events (array of) cal_id or array of the events
2008-06-07 19:45:33 +02:00
* @param string $version='1.0' could be '2.0' too
* @param string $method='PUBLISH'
2009-11-16 09:04:18 +01:00
* @param int $recur_date=0 if set export the next recurrence at or after the timestamp,
* default 0 => export whole series (or events, if not recurring)
2010-03-01 22:18:52 +01:00
* @param string $principalURL='' Used for CalDAV exports
* @return string|boolean string with iCal or false on error (eg. no permission to read the event)
2008-06-07 19:45:33 +02:00
*/
2010-03-01 22:18:52 +01:00
function &exportVCal($events, $version='1.0', $method='PUBLISH', $recur_date=0, $principalURL='')
2008-06-07 19:45:33 +02:00
{
2010-03-01 22:18:52 +01:00
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__.
"($version, $method, $recur_date, $principalURL)\n",
3, $this->logfile);
}
2008-06-07 19:45:33 +02:00
$egwSupportedFields = array(
2009-11-16 09:04:18 +01:00
'CLASS' => 'public',
'SUMMARY' => 'title',
'DESCRIPTION' => 'description',
'LOCATION' => 'location',
'DTSTART' => 'start',
'DTEND' => 'end',
'ATTENDEE' => 'participants',
2010-02-23 19:35:43 +01:00
'ORGANIZER' => 'owner',
2009-11-16 09:04:18 +01:00
'RRULE' => 'recur_type',
'EXDATE' => 'recur_exception',
'PRIORITY' => 'priority',
'TRANSP' => 'non_blocking',
'CATEGORIES' => 'category',
'UID' => 'uid',
'RECURRENCE-ID' => 'reference',
'SEQUENCE' => 'etag',
2010-02-23 19:35:43 +01:00
'STATUS' => 'status',
2008-06-07 19:45:33 +02:00
);
2009-11-16 09:04:18 +01:00
if (!is_array($this->supportedFields)) $this->setSupportedFields();
2008-06-07 19:45:33 +02:00
2009-11-16 09:04:18 +01:00
if ($this->productManufacturer == '' )
{ // syncevolution is broken
$version = '2.0';
2008-06-07 19:45:33 +02:00
}
2009-11-16 09:04:18 +01:00
$vcal = new Horde_iCalendar;
2008-06-07 19:45:33 +02:00
$vcal->setAttribute('PRODID','-//eGroupWare//NONSGML eGroupWare Calendar '.$GLOBALS['egw_info']['apps']['calendar']['version'].'//'.
strtoupper($GLOBALS['egw_info']['user']['preferences']['common']['lang']));
2009-11-16 09:04:18 +01:00
$vcal->setAttribute('VERSION', $version);
$vcal->setAttribute('METHOD', $method);
$serverTZ = false;
2010-02-24 15:53:15 +01:00
$events_exported = false;
2008-06-07 19:45:33 +02:00
if (!is_array($events)) $events = array($events);
2010-02-23 19:35:43 +01:00
foreach ($events as $event)
2008-06-07 19:45:33 +02:00
{
2010-03-01 22:18:52 +01:00
$organizerURL = '';
2010-02-23 19:35:43 +01:00
$organizerCN = false;
$recurrence = $this->date2usertime($recur_date);
if (!is_array($event)
&& !($event = $this->read($event, $recurrence, false, 'server')))
{
if ($this->read($event, $recurrence, true, 'server'))
{
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__.
'() User does not have the permission to read event ' . $event['id']. "\n",
3,$this->logfile);
}
return -1; // Permission denied
}
else
{
$retval = false; // Entry does not exist
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__.
"() Event $event not found.\n",
3, $this->logfile);
}
}
continue;
}
if ($this->isWholeDay($event)) $event['whole_day'] = true;
2009-11-16 09:04:18 +01:00
if (strpos($this->productName, 'palmos'))
{
$utc = false;
2010-02-23 19:35:43 +01:00
if (isset($event['whole_day']))
{
if (isset($event['reference']))
{
$event['reference'] = mktime(0, 0, 0,
date('m', $event['reference']),
date('d', $event['reference']),
date('Y', $event['reference'])
);
}
foreach((array)$event['recur_exception'] as $n => $date)
{
$event['recur_exception'][$n] = mktime(0, 0, 0,
date('m', $date),
date('d', $date),
date('Y', $date)
);
}
if (isset($event['alarm']) && is_array($event['alarm']))
{
foreach($event['alarm'] as $n => $alarm)
{
$event['alarm'][$n]['time'] = $this->date2usertime($alarm['time']);
}
}
}
else
{
$new_events = array($event);
$this->db2data($new_events, 'ts');
$event = array_shift($new_events);
}
2009-11-16 09:04:18 +01:00
}
else
{
$utc = true;
}
2010-02-23 19:35:43 +01:00
if ($this->log)
2008-06-07 19:45:33 +02:00
{
2010-02-23 19:35:43 +01:00
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__.
'(' . $event['id']. ',' . $recurrence . ")\n" .
array2string($event)."\n",3,$this->logfile);
2008-06-07 19:45:33 +02:00
}
2010-02-23 19:35:43 +01:00
if ($recurrence)
2009-11-16 09:04:18 +01:00
{
2010-02-23 19:35:43 +01:00
if (!isset($this->supportedFields['participants']))
{
// We don't need status only exceptions
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__.
"($_id, $recurrence) Gratuitous pseudo exception, skipped ...\n",
3,$this->logfile);
}
continue; // unsupported status only exception
}
2009-11-16 09:04:18 +01:00
// force single event
foreach (array('recur_enddate','recur_interval','recur_exception','recur_data','recur_date','id','etag') as $name)
{
unset($event[$name]);
}
$event['recur_type'] = MCAL_RECUR_NONE;
}
elseif ($event['recur_enddate'])
{
2010-02-23 19:35:43 +01:00
$event['recur_enddate'] = mktime(23, 59, 59,
date('m', $event['recur_enddate']),
date('d', $event['recur_enddate']),
date('Y', $event['recur_enddate'])
2009-12-04 16:38:36 +01:00
);
2009-11-16 09:04:18 +01:00
}
if (!$serverTZ && date('e', $event['start']) != 'UTC'
&& ($event['recur_type'] != MCAL_RECUR_NONE
|| $this->useServerTZ))
2008-06-07 19:45:33 +02:00
{
if (!$this->useServerTZ &&
$event['recur_type'] != MCAL_RECUR_NONE
&& $event['recur_enddate'])
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
$startDST = date('I', $event['start']);
$finalDST = date('I', $event['recur_enddate']);
// Different DST or more than half a year?
if ($startDST != $finalDST ||
($event['recur_enddate'] - $event['start']) > 15778800)
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
$utc = false;
$serverTZ = true;
}
}
else
{
$utc = false;
$serverTZ = true;
}
if ($serverTZ)
{
$serverTZ = self::generate_vtimezone($event, $vcal);
2009-11-16 09:04:18 +01:00
}
}
if ($this->productManufacturer != 'file' && $this->uidExtension)
{
// Append UID to DESCRIPTION
if (!preg_match('/\[UID:.+\]/m', $event['description'])) {
$event['description'] .= "\n[UID:" . $event['uid'] . "]";
}
}
$vevent = Horde_iCalendar::newComponent('VEVENT', $vcal);
$parameters = $attributes = $values = array();
if ($this->productManufacturer == 'sonyericsson')
{
$eventDST = date('I', $event['start']);
if ($eventDST)
{
$attributes['X-SONYERICSSON-DST'] = 4;
}
}
2010-02-23 19:35:43 +01:00
if ($event['recur_type'] != MCAL_RECUR_NONE)
{
$exceptions = array();
// dont use "virtual" exceptions created by participant status for GroupDAV or file export
2010-02-24 15:53:15 +01:00
if (!in_array($this->productManufacturer,array('file','groupdav')) &&
isset($this->supportedFields['participants']))
2010-02-23 19:35:43 +01:00
{
$exceptions = $this->so->get_recurrence_exceptions($event);
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__."(PSEUDO EXCEPTIONS)\n" .
array2string($exceptions)."\n",3,$this->logfile);
}
}
if (is_array($event['recur_exception']))
{
$exceptions = array_unique(array_merge($exceptions, $event['recur_exception']));
sort($exceptions);
}
$event['recur_exception'] = $exceptions;
}
2009-11-16 09:04:18 +01:00
foreach ($egwSupportedFields as $icalFieldName => $egwFieldName)
{
if (!isset($this->supportedFields[$egwFieldName])) continue;
$values[$icalFieldName] = array();
switch ($icalFieldName)
{
case 'ATTENDEE':
foreach ((array)$event['participants'] as $uid => $status)
{
if (!($info = $this->resource_info($uid))) continue;
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__ .
'()attendee:' . array2string($info) ."\n",3,$this->logfile);
}
$participantCN = '"' . (empty($info['cn']) ? $info['name'] : $info['cn']) . '"';
if ($version == '1.0')
{
$participantURL = trim($participantCN . (empty($info['email']) ? '' : ' <' . $info['email'] .'>'));
}
else
{
$participantURL = empty($info['email']) ? '' : 'MAILTO:' . $info['email'];
}
2010-02-23 19:35:43 +01:00
calendar_so::split_status($status, $quantity, $role);
2010-03-01 22:18:52 +01:00
if ($uid == $event['owner'])
{
$role = 'CHAIR';
}
else
{
$role = 'REQ-PARTICIPANT';
}
$attributes['ATTENDEE'][] = $participantURL;
2009-11-16 09:04:18 +01:00
// RSVP={TRUE|FALSE} // resonse expected, not set in eGW => status=U
$rsvp = $status == 'U' ? 'TRUE' : 'FALSE';
// PARTSTAT={NEEDS-ACTION|ACCEPTED|DECLINED|TENTATIVE|DELEGATED|COMPLETED|IN-PROGRESS} everything from delegated is NOT used by eGW atm.
$status = $this->status_egw2ical[$status];
// CUTYPE={INDIVIDUAL|GROUP|RESOURCE|ROOM|UNKNOWN}
switch ($info['type'])
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
case 'g':
$cutype = 'GROUP';
break;
case 'r':
$cutype = 'RESOURCE';
break;
case 'u': // account
case 'c': // contact
case 'e': // email address
$cutype = 'INDIVIDUAL';
break;
default:
$cutype = 'UNKNOWN';
break;
};
2010-02-23 19:35:43 +01:00
// ROLE={CHAIR|REQ-PARTICIPANT|OPT-PARTICIPANT|NON-PARTICIPANT|X-*}
2009-11-16 09:04:18 +01:00
$parameters['ATTENDEE'][] = array(
2010-02-23 19:35:43 +01:00
'CN' => $participantCN,
2009-11-16 09:04:18 +01:00
'ROLE' => $role,
'PARTSTAT' => $status,
'CUTYPE' => $cutype,
'RSVP' => $rsvp,
)+($info['type'] != 'e' ? array('X-EGROUPWARE-UID' => $uid) : array())+
($quantity > 1 ? array('X-EGROUPWARE-QUANTITY' => $quantity) : array());
}
break;
2008-06-07 19:45:33 +02:00
2009-11-16 09:04:18 +01:00
case 'CLASS':
2010-03-15 18:23:14 +01:00
$attributes['CLASS'] = $event['public'] ? 'PUBLIC' : 'PRIVATE';
2009-11-16 09:04:18 +01:00
break;
2010-02-23 19:35:43 +01:00
case 'ORGANIZER':
2010-03-01 22:18:52 +01:00
$organizerCN = '"' . trim($GLOBALS['egw']->accounts->id2name($event['owner'],'account_firstname')
. ' ' . $GLOBALS['egw']->accounts->id2name($event['owner'],'account_lastname')) . '"';
$organizerURL = $GLOBALS['egw']->accounts->id2name($event['owner'],'account_email');
if ($version == '1.0')
{
$organizerURL = trim($organizerCN . (empty($organizerURL) ? '' : ' <' . $organizerURL .'>'));
}
else
{
$organizerURL = empty($organizerURL) ? '' : 'MAILTO:' . $organizerURL;
}
2010-03-01 22:18:52 +01:00
if (!isset($event['participants'][$event['owner']]))
2009-11-16 09:04:18 +01:00
{
2010-03-01 22:18:52 +01:00
$attributes['ATTENDEE'][] = $organizerURL;
$parameters['ATTENDEE'][] = array(
'CN' => $organizerCN,
'ROLE' => 'CHAIR',
'PARTSTAT' => 'DELEGATED',
'CUTYPE' => 'INDIVIDUAL',
'RSVP' => 'FALSE',
'X-EGROUPWARE-UID' => $event['owner'],
);
2010-02-23 19:35:43 +01:00
}
2010-03-01 22:18:52 +01:00
if ($this->productManufacturer != 'groupdav'
|| !$this->check_perms(EGW_ACL_EDIT,$event['id']))
2010-02-23 19:35:43 +01:00
{
2010-03-01 22:18:52 +01:00
$attributes['ORGANIZER'] = $organizerURL;
2010-02-23 19:35:43 +01:00
$parameters['ORGANIZER']['CN'] = $organizerCN;
2010-03-01 22:18:52 +01:00
$parameters['ORGANIZER']['X-EGROUPWARE-UID'] = $event['owner'];
2009-11-16 09:04:18 +01:00
}
break;
case 'DTSTART':
2010-02-23 19:35:43 +01:00
if (!isset($event['whole_day']))
2009-11-16 09:04:18 +01:00
{
2010-02-23 19:35:43 +01:00
if ($utc)
{
$attributes['DTSTART'] = $event['start'];
}
else
{
$attributes['DTSTART'] = date('Ymd\THis', $event['start']);
if ($serverTZ) $parameters['DTSTART']['TZID'] = $serverTZ;
}
2009-11-16 09:04:18 +01:00
}
break;
2008-06-07 19:45:33 +02:00
2009-11-16 09:04:18 +01:00
case 'DTEND':
// write start + end of whole day events as dates
2010-02-23 19:35:43 +01:00
if (isset($event['whole_day']))
2009-11-16 09:04:18 +01:00
{
$event['end-nextday'] = $event['end'] + 12*3600; // we need the date of the next day, as DTEND is non-inclusive (= exclusive) in rfc2445
foreach (array('start' => 'DTSTART','end-nextday' => 'DTEND') as $f => $t)
{
2009-11-16 09:04:18 +01:00
$arr = $this->date2array($event[$f]);
$vevent->setAttribute($t, array('year' => $arr['year'],'month' => $arr['month'],'mday' => $arr['day']),
array('VALUE' => 'DATE'));
}
unset($attributes['DTSTART']);
}
else
{
if ($utc)
{
$attributes['DTEND'] = $event['end'];
}
else
{
2009-11-16 09:04:18 +01:00
$attributes['DTEND'] = date('Ymd\THis', $event['end']);
if ($serverTZ) $parameters['DTEND']['TZID'] = $serverTZ;
}
2009-11-16 09:04:18 +01:00
}
break;
2008-06-07 19:45:33 +02:00
2009-11-16 09:04:18 +01:00
case 'RRULE':
if ($event['recur_type'] == MCAL_RECUR_NONE) break; // no recuring event
if ($version == '1.0')
{
$interval = ($event['recur_interval'] > 1) ? $event['recur_interval'] : 1;
$rrule = array('FREQ' => $this->recur_egw2ical_1_0[$event['recur_type']].$interval);
switch ($event['recur_type'])
{
case MCAL_RECUR_WEEKLY:
$days = array();
foreach ($this->recur_days_1_0 as $id => $day)
{
if ($event['recur_data'] & $id) $days[] = strtoupper(substr($day,0,2));
}
$rrule['BYDAY'] = implode(' ',$days);
$rrule['FREQ'] = $rrule['FREQ'].' '.$rrule['BYDAY'];
break;
2008-06-07 19:45:33 +02:00
2009-11-16 09:04:18 +01:00
case MCAL_RECUR_MONTHLY_MDAY: // date of the month: BYMONTDAY={1..31}
break;
2008-06-07 19:45:33 +02:00
2009-11-16 09:04:18 +01:00
case MCAL_RECUR_MONTHLY_WDAY: // weekday of the month: BDAY={1..5}{MO..SO}
$rrule['BYDAY'] = (1 + (int) ((date('d',$event['start'])-1) / 7)).'+ '.
strtoupper(substr(date('l',$event['start']),0,2));
$rrule['FREQ'] = $rrule['FREQ'].' '.$rrule['BYDAY'];
break;
}
2008-06-07 19:45:33 +02:00
2009-11-16 09:04:18 +01:00
if ($event['recur_enddate'])
{
2009-12-04 16:38:36 +01:00
$rrule['UNTIL'] = $vcal->_exportDateTime($event['recur_enddate']);
2009-11-16 09:04:18 +01:00
}
else
{
$rrule['UNTIL'] = '#0';
}
$attributes['RRULE'] = $rrule['FREQ'].' '.$rrule['UNTIL'];
}
else // $version == '2.0'
{
$rrule = array('FREQ' => $this->recur_egw2ical_2_0[$event['recur_type']]);
switch ($event['recur_type'])
{
case MCAL_RECUR_WEEKLY:
$days = array();
foreach ($this->recur_days as $id => $day)
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
if ($event['recur_data'] & $id) $days[] = strtoupper(substr($day,0,2));
2008-06-07 19:45:33 +02:00
}
2009-11-16 09:04:18 +01:00
$rrule['BYDAY'] = implode(',',$days);
break;
case MCAL_RECUR_MONTHLY_MDAY: // date of the month: BYMONTDAY={1..31}
$rrule['BYMONTHDAY'] = (int) date('d',$event['start']);
break;
case MCAL_RECUR_MONTHLY_WDAY: // weekday of the month: BDAY={1..5}{MO..SO}
$rrule['BYDAY'] = (1 + (int) ((date('d',$event['start'])-1) / 7)).
strtoupper(substr(date('l',$event['start']),0,2));
break;
}
if ($event['recur_interval'] > 1)
{
$rrule['INTERVAL'] = $event['recur_interval'];
}
if ($event['recur_enddate'])
{
// UNTIL should be a UTC timestamp
2009-12-04 16:38:36 +01:00
$rrule['UNTIL'] = $vcal->_exportDateTime($event['recur_enddate']);
2009-11-16 09:04:18 +01:00
}
$attributes['RRULE'] = '';
$parameters['RRULE'] = $rrule;
}
break;
case 'EXDATE':
if ($event['recur_type'] == MCAL_RECUR_NONE) break;
2010-02-23 19:35:43 +01:00
$days = $event['recur_exception'];
2009-11-16 09:04:18 +01:00
if (!empty($days))
{
// use 'DATE' instead of 'DATE-TIME' on whole day events
2010-02-23 19:35:43 +01:00
if (isset($event['whole_day']))
2009-11-16 09:04:18 +01:00
{
$value_type = 'DATE';
foreach ($days as $id => $timestamp)
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
$arr = $this->date2array($timestamp);
$days[$id] = array(
'year' => $arr['year'],
'month' => $arr['month'],
'mday' => $arr['day'],
);
2008-06-07 19:45:33 +02:00
}
}
2009-11-16 09:04:18 +01:00
else
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
$value_type = 'DATE-TIME';
if (!$utc)
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
foreach ($days as $id => $timestamp)
{
$days[$id] = date('Ymd\THis', $timestamp);
}
2008-06-07 19:45:33 +02:00
}
}
2009-11-16 09:04:18 +01:00
$attributes['EXDATE'] = '';
$values['EXDATE'] = $days;
if ($serverTZ) $parameters['EXDATE']['TZID'] = $serverTZ;
$parameters['EXDATE']['VALUE'] = $value_type;
}
break;
case 'PRIORITY':
2010-02-23 19:35:43 +01:00
if ($this->productManufacturer == 'funambol' &&
(strpos($this->productName, 'outlook') !== false
|| strpos($this->productName, 'pocket pc') !== false))
{
$attributes['PRIORITY'] = (int) $this->priority_egw2funambol[$event['priority']];
}
else
{
2009-11-16 09:04:18 +01:00
$attributes['PRIORITY'] = (int) $this->priority_egw2ical[$event['priority']];
}
break;
2008-06-07 19:45:33 +02:00
2009-11-16 09:04:18 +01:00
case 'TRANSP':
if ($version == '1.0')
{
$attributes['TRANSP'] = ($event['non_blocking'] ? 1 : 0);
}
else
{
$attributes['TRANSP'] = ($event['non_blocking'] ? 'TRANSPARENT' : 'OPAQUE');
}
break;
2008-06-07 19:45:33 +02:00
2010-02-23 19:35:43 +01:00
case 'STATUS':
$attributes['STATUS'] = 'CONFIRMED';
break;
2009-11-16 09:04:18 +01:00
case 'CATEGORIES':
2010-02-23 19:35:43 +01:00
if ($event['category'] && ($values['CATEGORIES'] = $this->get_categories($event['category'])))
2009-11-16 09:04:18 +01:00
{
2010-02-23 19:35:43 +01:00
if (count($values['CATEGORIES']) == 1)
{
$attributes['CATEGORIES'] = array_shift($values['CATEGORIES']);
}
else
{
$attributes['CATEGORIES'] = '';
}
2009-11-16 09:04:18 +01:00
}
break;
2008-06-07 19:45:33 +02:00
2009-11-16 09:04:18 +01:00
case 'RECURRENCE-ID':
if ($version == '1.0')
{
$icalFieldName = 'X-RECURRENCE-ID';
}
if ($recur_date)
{
// We handle a status only exception
2010-02-23 19:35:43 +01:00
if (isset($event['whole_day']))
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
$arr = $this->date2array($recur_date);
$vevent->setAttribute($icalFieldName, array(
'year' => $arr['year'],
'month' => $arr['month'],
'mday' => $arr['day']),
array('VALUE' => 'DATE')
);
2008-06-07 19:45:33 +02:00
}
2009-11-16 09:04:18 +01:00
else
{
if ($utc)
{
$attributes[$icalFieldName] = $recur_date;
}
else
{
$attributes[$icalFieldName] = date('Ymd\THis', $recur_date);
if ($serverTZ) $parameters[$icalFieldName]['TZID'] = $serverTZ;
}
}
}
elseif ($event['reference'])
{
2010-02-23 19:35:43 +01:00
if (isset($event['whole_day']))
2009-11-16 09:04:18 +01:00
{
$arr = $this->date2array($event['reference']);
$vevent->setAttribute($icalFieldName, array(
'year' => $arr['year'],
'month' => $arr['month'],
'mday' => $arr['day']),
array('VALUE' => 'DATE')
);
}
else
{
if ($utc)
{
$attributes[$icalFieldName] = $event['reference'];
}
else
{
$attributes[$icalFieldName] = date('Ymd\THis', $event['reference']);
if ($serverTZ) $parameters[$icalFieldName]['TZID'] = $serverTZ;
}
}
}
break;
2008-06-07 19:45:33 +02:00
2009-11-16 09:04:18 +01:00
default:
if (isset($this->clientProperties[$icalFieldName]['Size']))
{
$size = $this->clientProperties[$icalFieldName]['Size'];
$noTruncate = $this->clientProperties[$icalFieldName]['NoTruncate'];
2010-02-23 19:35:43 +01:00
if ($this->log && $size > 0)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__ .
"() $icalFieldName Size: $size, NoTruncate: " .
($noTruncate ? 'TRUE' : 'FALSE') . "\n",3,$this->logfile);
}
//Horde::logMessage("vCalendar $icalFieldName Size: $size, NoTruncate: " .
// ($noTruncate ? 'TRUE' : 'FALSE'), __FILE__, __LINE__, PEAR_LOG_DEBUG);
2009-11-16 09:04:18 +01:00
}
else
{
$size = -1;
$noTruncate = false;
}
$value = $event[$egwFieldName];
$cursize = strlen($value);
if ($size > 0 && $cursize > $size)
{
if ($noTruncate)
2008-06-07 19:45:33 +02:00
{
2010-02-23 19:35:43 +01:00
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__ .
"() $icalFieldName omitted due to maximum size $size\n",3,$this->logfile);
}
//Horde::logMessage("vCalendar $icalFieldName omitted due to maximum size $size",
// __FILE__, __LINE__, PEAR_LOG_WARNING);
2009-11-16 09:04:18 +01:00
continue; // skip field
2008-06-07 19:45:33 +02:00
}
2009-11-16 09:04:18 +01:00
// truncate the value to size
$value = substr($value, 0, $size - 1);
2010-02-23 19:35:43 +01:00
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__ .
"() $icalFieldName truncated to maximum size $size\n",3,$this->logfile);
}
//Horde::logMessage("vCalendar $icalFieldName truncated to maximum size $size",
// __FILE__, __LINE__, PEAR_LOG_INFO);
2009-11-16 09:04:18 +01:00
}
if (!empty($value) || ($size >= 0 && !$noTruncate))
{
$attributes[$icalFieldName] = $value;
}
2008-06-07 19:45:33 +02:00
}
}
2009-11-16 09:04:18 +01:00
if ($this->productManufacturer == 'nokia')
{
if ($event['special'] == '1')
{
2008-06-07 19:45:33 +02:00
$attributes['X-EPOCAGENDAENTRYTYPE'] = 'ANNIVERSARY';
$attributes['DTEND'] = $attributes['DTSTART'];
}
2009-11-16 09:04:18 +01:00
elseif ($event['special'] == '2')
{
$attributes['X-EPOCAGENDAENTRYTYPE'] = 'EVENT';
2009-11-16 09:04:18 +01:00
}
else
{
2008-06-07 19:45:33 +02:00
$attributes['X-EPOCAGENDAENTRYTYPE'] = 'APPOINTMENT';
}
}
$modified = $GLOBALS['egw']->contenthistory->getTSforAction('calendar',$event['id'],'modify');
$created = $GLOBALS['egw']->contenthistory->getTSforAction('calendar',$event['id'],'add');
2008-06-07 19:45:33 +02:00
if (!$created && !$modified) $created = $event['modified'];
2009-11-16 09:04:18 +01:00
if ($created)
{
$attributes['CREATED'] = $created;
}
2008-06-07 19:45:33 +02:00
if (!$modified) $modified = $event['modified'];
2009-11-16 09:04:18 +01:00
if ($modified)
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
$attributes['LAST-MODIFIED'] = $modified;
}
$attributes['DTSTAMP'] = time();
foreach ($event['alarm'] as $alarmID => $alarmData)
{
2010-02-23 19:35:43 +01:00
// skip over alarms that don't have the minimum required info
if (!$alarmData['offset'] && !$alarmData['time']) continue;
2009-11-16 09:04:18 +01:00
// skip alarms not being set for all users and alarms owned by other users
if ($alarmData['all'] != true && $alarmData['owner'] != $this->user)
{
continue;
}
2010-02-23 19:35:43 +01:00
if ($alarmData['offset'])
{
$alarmData['time'] = $event['start'] - $alarmData['offset'];
}
$description = trim(preg_replace("/\r?\n?\\[[A-Z_]+:.*\\]/i", '', $event['description']));
2008-06-07 19:45:33 +02:00
if ($version == '1.0')
{
2010-02-23 19:35:43 +01:00
if ($event['title']) $description = $event['title'];
if ($description)
{
$values['DALARM']['snooze_time'] = '';
$values['DALARM']['repeat count'] = '';
$values['DALARM']['display text'] = $description;
$values['AALARM']['snooze_time'] = '';
$values['AALARM']['repeat count'] = '';
$values['AALARM']['display text'] = $description;
}
2009-11-16 09:04:18 +01:00
if ($utc)
{
$attributes['DALARM'] = $alarmData['time'];
$attributes['AALARM'] = $alarmData['time'];
}
else
{
$attributes['DALARM'] = date('Ymd\THis', $alarmData['time']);
if ($serverTZ) $parameters['DALARM']['TZID'] = $serverTZ;
$attributes['AALARM'] = date('Ymd\THis', $alarmData['time']);
if ($serverTZ) $parameters['AALARM']['TZID'] = $serverTZ;
}
2008-06-07 19:45:33 +02:00
// lets take only the first alarm
break;
}
else
{
// VCalendar 2.0 / RFC 2445
// RFC requires DESCRIPTION for DISPLAY
2009-11-16 09:04:18 +01:00
if (!$event['title'] && !$description) continue;
2010-02-23 19:35:43 +01:00
if (isset($event['whole_day']) && $alarmData['offset'])
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
$alarmData['time'] = $event['start'] - $alarmData['offset'];
$alarmData['offset'] = false;
2008-06-07 19:45:33 +02:00
}
$valarm = Horde_iCalendar::newComponent('VALARM',$vevent);
if ($alarmData['offset'])
{
$valarm->setAttribute('TRIGGER', -$alarmData['offset'],
array('VALUE' => 'DURATION', 'RELATED' => 'START'));
}
else
{
2009-11-16 09:04:18 +01:00
$params = array('VALUE' => 'DATE-TIME');
if ($utc)
{
$value = $alarmData['time'];
}
else
{
$value = date('Ymd\THis', $alarmData['time']);
if ($serverTZ) $params['TZID'] = $serverTZ;
}
$valarm->setAttribute('TRIGGER', $value, $params);
2008-06-07 19:45:33 +02:00
}
$valarm->setAttribute('ACTION','DISPLAY');
2009-11-16 09:04:18 +01:00
$valarm->setAttribute('DESCRIPTION',$event['title'] ? $event['title'] : $description);
2008-06-07 19:45:33 +02:00
$vevent->addComponent($valarm);
}
}
2009-11-16 09:04:18 +01:00
foreach ($attributes as $key => $value)
2008-06-07 19:45:33 +02:00
{
2010-02-23 19:35:43 +01:00
foreach (is_array($value) && $parameters[$key]['VALUE']!='DATE' ? $value : array($value) as $valueID => $valueData)
2008-06-07 19:45:33 +02:00
{
$valueData = $GLOBALS['egw']->translation->convert($valueData,$GLOBALS['egw']->translation->charset(),'UTF-8');
2009-11-16 09:04:18 +01:00
$paramData = (array) $GLOBALS['egw']->translation->convert(is_array($value) ?
$parameters[$key][$valueID] : $parameters[$key],
$GLOBALS['egw']->translation->charset(),'UTF-8');
$valuesData = (array) $GLOBALS['egw']->translation->convert($values[$key],
$GLOBALS['egw']->translation->charset(),'UTF-8');
2010-02-23 19:35:43 +01:00
$content = $valueData . implode(';', $valuesData);
if (preg_match('/[^\x20-\x7F]/', $content) ||
($paramData['CN'] && preg_match('/[^\x20-\x7F]/', $paramData['CN'])))
2008-06-07 19:45:33 +02:00
{
2010-02-23 19:35:43 +01:00
$paramData['CHARSET'] = 'UTF-8';
switch ($this->productManufacturer)
{
case 'groupdav':
if ($this->productName == 'kde')
{
$paramData['ENCODING'] = 'QUOTED-PRINTABLE';
}
else
{
$paramData['CHARSET'] = '';
if (preg_match('/([\000-\012\015\016\020-\037\075])/', $valueData))
{
$paramData['ENCODING'] = 'QUOTED-PRINTABLE';
}
else
{
$paramData['ENCODING'] = '';
}
}
break;
case 'funambol':
$paramData['ENCODING'] = 'FUNAMBOL-QP';
}
2008-06-07 19:45:33 +02:00
}
2010-02-23 19:35:43 +01:00
/*
2009-11-16 09:04:18 +01:00
if (preg_match('/([\000-\012])/', $valueData))
{
2010-02-23 19:35:43 +01:00
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__ .
"() Has invalid XML data: $valueData",3,$this->logfile);
}
2009-11-16 09:04:18 +01:00
}
2010-02-23 19:35:43 +01:00
*/
$vevent->setAttribute($key, $valueData, $paramData, true, $valuesData);
2008-06-07 19:45:33 +02:00
}
}
$vcal->addComponent($vevent);
2010-02-24 15:53:15 +01:00
$events_exported = true;
2008-06-07 19:45:33 +02:00
}
2010-02-24 15:53:15 +01:00
$retval = $events_exported ? $vcal->exportvCalendar() : false;
2010-02-23 19:35:43 +01:00
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__ .
"() '$this->productManufacturer','$this->productName'\n",3,$this->logfile);
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__ .
"()\n".array2string($retval)."\n",3,$this->logfile);
}
2009-11-16 09:04:18 +01:00
return $retval;
2008-06-07 19:45:33 +02:00
}
/**
* Import an iCal
*
* @param string $_vcalData
2009-11-16 09:04:18 +01:00
* @param int $cal_id=-1 must be -1 for new entries!
2008-06-07 19:45:33 +02:00
* @param string $etag=null if an etag is given, it has to match the current etag or the import will fail
2009-11-16 09:04:18 +01:00
* @param boolean $merge=false merge data with existing entry
* @param int $recur_date=0 if set, import the recurrence at this timestamp,
* default 0 => import whole series (or events, if not recurring)
2010-03-01 22:18:52 +01:00
* @param string $principalURL='' Used for CalDAV imports
* @param int $user=null account_id of owner, default null
2008-06-07 19:45:33 +02:00
* @return int|boolean cal_id > 0 on success, false on failure or 0 for a failed etag
*/
function importVCal($_vcalData, $cal_id=-1, $etag=null, $merge=false, $recur_date=0, $principalURL='', $user=null)
2008-06-07 19:45:33 +02:00
{
2010-02-23 19:35:43 +01:00
if (!is_array($this->supportedFields)) $this->setSupportedFields();
2008-06-07 19:45:33 +02:00
2010-03-01 22:18:52 +01:00
if (!($events = $this->icaltoegw($_vcalData, $principalURL)))
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
return false;
2008-06-07 19:45:33 +02:00
}
2010-02-23 19:35:43 +01:00
if ($cal_id > 0)
{
if (count($events) == 1)
{
$events[0]['id'] = $cal_id;
if (!is_null($etag)) $events[0]['etag'] = (int) $etag;
if ($recur_date) $events[0]['reference'] = $recur_date;
}
elseif (($foundEvent = $this->find_event(array('id' => $cal_id), 'exact')) &&
($eventId = array_shift($foundEvent)) &&
($egwEvent = $this->read($eventId)))
{
foreach ($events as $k => $event)
{
if (!isset($event['uid'])) $events[$k]['uid'] = $egwEvent['uid'];
}
}
}
2008-06-07 19:45:33 +02:00
2010-02-23 19:35:43 +01:00
// check if we are importing an event series with exceptions in CalDAV
// only first event / series master get's cal_id from URL
// other events are exceptions and need to be checked if they are new
// and for real (not status only) exceptions their recurrence-id need
// to be included as recur_exception to the master
if ($this->productManufacturer == 'groupdav' && $cal_id > 0 &&
count($events) > 1 && !$events[1]['id'] &&
$events[0]['recur_type'] != MCAL_RECUR_NONE)
{
calendar_groupdav::fix_series($events);
}
2009-11-16 09:04:18 +01:00
foreach ($events as $event)
2008-06-07 19:45:33 +02:00
{
2010-02-23 19:35:43 +01:00
if ($this->isWholeDay($event, true)) $event['whole_day'] = true;
if (is_array($event['category']))
{
$event['category'] = $this->find_or_add_categories($event['category'],
isset($event['id']) ? $event['id'] : -1);
}
if ($this->log)
{
2010-03-01 22:18:52 +01:00
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__
."($cal_id, $etag, $recur_date, $principalURL, $user)\n"
2010-03-01 22:18:52 +01:00
. array2string($event)."\n",3,$this->logfile);
2010-02-23 19:35:43 +01:00
}
2009-11-16 09:04:18 +01:00
$updated_id = false;
$event_info = $this->get_event_info($event);
2008-06-07 19:45:33 +02:00
2010-02-23 19:35:43 +01:00
// common adjustments for existing events
if (is_array($event_info['stored_event']))
2008-06-07 19:45:33 +02:00
{
2010-02-23 19:35:43 +01:00
if (empty($event['uid']))
2008-06-07 19:45:33 +02:00
{
2010-02-23 19:35:43 +01:00
$event['uid'] = $event_info['stored_event']['uid']; // restore the UID if it was not delivered
2009-11-16 09:04:18 +01:00
}
2010-02-23 19:35:43 +01:00
elseif (empty($event['id']))
2009-11-16 09:04:18 +01:00
{
2010-02-23 19:35:43 +01:00
$event['id'] = $event_info['stored_event']['id']; // CalDAV does only provide UIDs
2009-11-16 09:04:18 +01:00
}
2010-04-14 16:48:17 +02:00
if (is_array($event['participants']))
{
// if the client does not return a status, we restore the original one
foreach ($event['participants'] as $uid => $status)
{
if ($status[0] == 'X')
{
if (isset($event_info['stored_event']['participants'][$uid]))
{
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__.
"() Restore status for $uid\n",3,$this->logfile);
}
$event['participants'][$uid] = $event_info['stored_event']['participants'][$uid];
}
else
{
$event['participants'][$uid] = calendar_so::combine_status('U');
}
}
}
}
2009-11-16 09:04:18 +01:00
if ($merge)
{
2010-02-23 19:35:43 +01:00
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__.
"()[MERGE]\n",3,$this->logfile);
}
2009-11-16 09:04:18 +01:00
// overwrite with server data for merge
foreach ($event_info['stored_event'] as $key => $value)
{
switch ($key)
{
case 'participants_types':
continue;
2008-06-07 19:45:33 +02:00
2010-02-23 19:35:43 +01:00
case 'participants':
2009-11-16 09:04:18 +01:00
foreach ($event_info['stored_event']['participants'] as $uid => $status)
{
// Is a participant and no longer present in the event?
if (!isset($event['participants'][$uid]))
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
// Add it back in
2010-02-24 15:53:15 +01:00
$event['participants'][$uid] = $status;
2008-06-07 19:45:33 +02:00
}
2009-11-16 09:04:18 +01:00
}
break;
2008-06-07 19:45:33 +02:00
2010-02-23 19:35:43 +01:00
default:
if (!empty($value)) $event[$key] = $value;
2009-11-16 09:04:18 +01:00
}
2008-06-07 19:45:33 +02:00
}
}
2009-11-16 09:04:18 +01:00
else
{
2010-02-23 19:35:43 +01:00
// no merge
2010-03-01 22:18:52 +01:00
if(!isset($this->supportedFields['category']) || !isset($event['category']))
2010-02-24 15:53:15 +01:00
{
$event['category'] = $event_info['stored_event']['category'];
}
if (!isset($this->supportedFields['participants'])
|| !$event['participants']
|| !is_array($event['participants'])
|| !count($event['participants']))
2009-11-16 09:04:18 +01:00
{
2010-02-23 19:35:43 +01:00
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__.
"() No participants\n",3,$this->logfile);
}
2009-11-16 09:04:18 +01:00
// If this is an updated meeting, and the client doesn't support
// participants OR the event no longer contains participants, add them back
2010-02-23 19:35:43 +01:00
unset($event['participants']);
}
else
{
2010-02-24 15:53:15 +01:00
foreach ($event_info['stored_event']['participants'] as $uid => $status)
2009-11-16 09:04:18 +01:00
{
2010-02-24 15:53:15 +01:00
// Is it a resource and no longer present in the event?
if ($uid[0] == 'r' && !isset($event['participants'][$uid]))
2010-02-23 19:35:43 +01:00
{
2010-02-24 15:53:15 +01:00
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__.
"() Restore resource $uid to status $status\n",3,$this->logfile);
}
// Add it back in
$event['participants'][$uid] = $status;
2010-02-23 19:35:43 +01:00
}
2009-11-16 09:04:18 +01:00
}
}
2010-02-23 19:35:43 +01:00
2009-11-16 09:04:18 +01:00
// avoid that iCal changes the organizer, which is not allowed
$event['owner'] = $event_info['stored_event']['owner'];
2008-06-07 19:45:33 +02:00
}
2009-11-16 09:04:18 +01:00
}
2010-02-23 19:35:43 +01:00
else // common adjustments for new events
{
unset($event['id']);
// set non blocking all day depending on the user setting
if (isset($event['whole_day']) && $this->nonBlockingAllday)
{
$event['non_blocking'] = 1;
}
if (!is_null($user))
{
if ($this->check_perms(EGW_ACL_ADD, 0, $user))
{
$event['owner'] = $user;
}
else
{
return false; // no permission
}
}
2010-02-23 19:35:43 +01:00
// check if an owner is set and the current user has add rights
// for that owners calendar; if not set the current user
elseif (!isset($event['owner'])
2010-02-23 19:35:43 +01:00
|| !$this->check_perms(EGW_ACL_ADD, 0, $event['owner']))
{
$event['owner'] = $this->user;
}
2010-02-24 15:53:15 +01:00
if (!$event['participants']
|| !is_array($event['participants'])
|| !count($event['participants']))
2010-02-23 19:35:43 +01:00
{
$status = $event['owner'] == $this->user ? 'A' : 'U';
$status = calendar_so::combine_status($status, 1, 'CHAIR');
$event['participants'] = array($event['owner'] => $status);
}
else
{
foreach ($event['participants'] as $uid => $status)
{
2010-02-24 15:53:15 +01:00
// if the client did not give us a proper status => set default
2010-02-23 19:35:43 +01:00
if ($status[0] == 'X')
{
if ($uid == $event['owner'])
{
2010-04-14 16:48:17 +02:00
$event['participants'][$uid] = calendar_so::combine_status('A', 1, 'CHAIR');
2010-02-23 19:35:43 +01:00
}
else
{
2010-04-14 16:48:17 +02:00
$event['participants'][$uid] = calendar_so::combine_status('U');
2010-02-23 19:35:43 +01:00
}
}
}
}
}
2008-06-07 19:45:33 +02:00
2009-11-16 09:04:18 +01:00
// update alarms depending on the given event type
if (count($event['alarm']) > 0 || isset($this->supportedFields['alarm']))
{
switch ($event_info['type'])
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
case 'SINGLE':
case 'SERIES-MASTER':
case 'SERIES-EXCEPTION':
case 'SERIES-EXCEPTION-PROPAGATE':
if (count($event['alarm']) > 0)
{
foreach ($event['alarm'] as $newid => &$alarm)
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
if (!isset($alarm['offset']) && isset($alarm['time']))
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
$alarm['offset'] = $event['start'] - $alarm['time'];
}
if (!isset($alarm['time']) && isset($alarm['offset']))
{
$alarm['time'] = $event['start'] - $alarm['offset'];
}
$alarm['owner'] = $this->user;
$alarm['all'] = false;
if (is_array($event_info['stored_event'])
&& count($event_info['stored_event']['alarm']) > 0)
{
foreach ($event_info['stored_event']['alarm'] as $alarm_id => $alarm_data)
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
if ($alarm['time'] == $alarm_data['time'] &&
($alarm_data['all'] || $alarm_data['owner'] == $this->user))
{
unset($event['alarm'][$newid]);
unset($event_info['stored_event']['alarm'][$alarm_id]);
continue;
}
2008-06-07 19:45:33 +02:00
}
}
}
2009-11-16 09:04:18 +01:00
}
break;
2010-02-23 19:35:43 +01:00
case 'SERIES-PSEUDO-EXCEPTION':
2009-11-16 09:04:18 +01:00
// nothing to do here
break;
}
if (is_array($event_info['stored_event'])
&& count($event_info['stored_event']['alarm']) > 0)
{
foreach ($event_info['stored_event']['alarm'] as $alarm_id => $alarm_data)
{
// only touch own alarms
if ($alarm_data['all'] == false && $alarm_data['owner'] == $this->user)
{
$this->delete_alarm($alarm_id);
}
2008-06-07 19:45:33 +02:00
}
}
2009-11-16 09:04:18 +01:00
}
2008-06-07 19:45:33 +02:00
2009-11-16 09:04:18 +01:00
// save event depending on the given event type
switch ($event_info['type'])
{
case 'SINGLE':
2010-02-23 19:35:43 +01:00
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__.
"(): event SINGLE\n",3,$this->logfile);
}
2008-06-07 19:45:33 +02:00
2009-11-16 09:04:18 +01:00
// update the event
if ($event_info['acl_edit'])
2008-06-07 19:45:33 +02:00
{
2010-02-23 19:35:43 +01:00
// Force SINGLE
$event['reference'] = 0;
$event_to_store = array($event); // prevent $event from being changed by the update method
$this->db2data($event_to_store);
$event_to_store = array_shift($event_to_store);
2009-11-16 09:04:18 +01:00
$updated_id = $this->update($event_to_store, true);
unset($event_to_store);
2008-06-07 19:45:33 +02:00
}
2009-11-16 09:04:18 +01:00
break;
2008-06-07 19:45:33 +02:00
2009-11-16 09:04:18 +01:00
case 'SERIES-MASTER':
2010-02-23 19:35:43 +01:00
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__.
"(): event SERIES-MASTER\n",3,$this->logfile);
}
2009-11-16 09:04:18 +01:00
2010-02-23 19:35:43 +01:00
// remove all known pseudo exceptions and update the event
2009-11-16 09:04:18 +01:00
if ($event_info['acl_edit'])
2008-06-07 19:45:33 +02:00
{
2010-02-23 19:35:43 +01:00
$days = $this->so->get_recurrence_exceptions($event_info['stored_event']);
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__."(PSEUDO EXCEPTIONS):\n" .
array2string($days)."\n",3,$this->logfile);
}
2009-11-16 09:04:18 +01:00
if (is_array($days))
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
$recur_exceptions = array();
2010-02-23 19:35:43 +01:00
2009-11-16 09:04:18 +01:00
foreach ($event['recur_exception'] as $recur_exception)
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
if (!in_array($recur_exception, $days))
{
$recur_exceptions[] = $recur_exception;
}
2008-06-07 19:45:33 +02:00
}
2009-11-16 09:04:18 +01:00
$event['recur_exception'] = $recur_exceptions;
2008-06-07 19:45:33 +02:00
}
2009-11-16 09:04:18 +01:00
2010-02-23 19:35:43 +01:00
$event_to_store = array($event); // prevent $event from being changed by the update method
$this->db2data($event_to_store);
$event_to_store = array_shift($event_to_store);
2009-11-16 09:04:18 +01:00
$updated_id = $this->update($event_to_store, true);
unset($event_to_store);
2008-06-07 19:45:33 +02:00
}
2009-11-16 09:04:18 +01:00
break;
2008-06-07 19:45:33 +02:00
2009-11-16 09:04:18 +01:00
case 'SERIES-EXCEPTION':
case 'SERIES-EXCEPTION-PROPAGATE':
2010-02-23 19:35:43 +01:00
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__.
"(): event SERIES-EXCEPTION\n",3,$this->logfile);
}
2008-06-07 19:45:33 +02:00
2009-11-16 09:04:18 +01:00
// update event
if ($event_info['acl_edit'])
{
if (isset($event_info['stored_event']['id']))
{
// We update an existing exception
$event['id'] = $event_info['stored_event']['id'];
$event['category'] = $event_info['stored_event']['category'];
}
else
{
// We create a new exception
unset($event['id']);
2010-02-23 19:35:43 +01:00
unset($event_info['stored_event']);
$event['recur_type'] = MCAL_RECUR_NONE;
$event_info['master_event']['recur_exception'] =
array_unique(array_merge($event_info['master_event']['recur_exception'],
array($event['reference'])));
2009-11-16 09:04:18 +01:00
$event['category'] = $event_info['master_event']['category'];
$event['owner'] = $event_info['master_event']['owner'];
2010-02-23 19:35:43 +01:00
$event_to_store = array($event_info['master_event']); // prevent the master_event from being changed by the update method
$this->db2data($event_to_store);
$event_to_store = array_shift($event_to_store);
$this->update($event_to_store, true);
unset($event_to_store);
2009-11-16 09:04:18 +01:00
}
2008-06-07 19:45:33 +02:00
2010-02-23 19:35:43 +01:00
$event_to_store = array($event); // prevent $event from being changed by update method
$this->db2data($event_to_store);
$event_to_store = array_shift($event_to_store);
$updated_id = $this->update($event_to_store, true);
2009-11-16 09:04:18 +01:00
unset($event_to_store);
}
break;
2010-02-23 19:35:43 +01:00
case 'SERIES-PSEUDO-EXCEPTION':
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__.
"(): event SERIES-PSEUDO-EXCEPTION\n",3,$this->logfile);
}
//Horde::logMessage('importVCAL event SERIES-PSEUDO-EXCEPTION',
// __FILE__, __LINE__, PEAR_LOG_DEBUG);
2009-11-16 09:04:18 +01:00
if ($event_info['acl_edit'])
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
// truncate the status only exception from the series master
$recur_exceptions = array();
foreach ($event_info['master_event']['recur_exception'] as $recur_exception)
{
if ($recur_exception != $event['reference'])
{
$recur_exceptions[] = $recur_exception;
}
}
$event_info['master_event']['recur_exception'] = $recur_exceptions;
2008-06-07 19:45:33 +02:00
2009-11-16 09:04:18 +01:00
// save the series master with the adjusted exceptions
2010-02-23 19:35:43 +01:00
$event_to_store = array($event_info['master_event']); // prevent the master_event from being changed by the update method
$this->db2data($event_to_store);
$event_to_store = array_shift($event_to_store);
2009-11-16 09:04:18 +01:00
$updated_id = $this->update($event_to_store, true, true, false, false);
unset($event_to_store);
2008-06-07 19:45:33 +02:00
}
2009-11-16 09:04:18 +01:00
break;
}
// read stored event into info array for fresh stored (new) events
if (!is_array($event_info['stored_event']) && $updated_id > 0)
{
2010-02-23 19:35:43 +01:00
$event_info['stored_event'] = $this->read($updated_id, 0, false, 'server');
2009-11-16 09:04:18 +01:00
}
2010-02-24 15:53:15 +01:00
if (isset($event['participants']))
2009-11-16 09:04:18 +01:00
{
2010-02-24 15:53:15 +01:00
// update status depending on the given event type
switch ($event_info['type'])
{
case 'SINGLE':
case 'SERIES-MASTER':
case 'SERIES-EXCEPTION':
case 'SERIES-EXCEPTION-PROPAGATE':
if (is_array($event_info['stored_event'])) // status update requires a stored event
2009-11-16 09:04:18 +01:00
{
2010-02-24 15:53:15 +01:00
if ($event_info['acl_edit'])
{
// update all participants if we have the right to do that
$this->update_status($event, $event_info['stored_event']);
}
elseif (isset($event['participants'][$this->user]) || isset($event_info['stored_event']['participants'][$this->user]))
{
// update the users status only
$this->set_status($event_info['stored_event']['id'], $this->user,
($event['participants'][$this->user] ? $event['participants'][$this->user] : 'R'), 0, true);
}
2008-06-07 19:45:33 +02:00
}
2010-02-24 15:53:15 +01:00
break;
2008-06-07 19:45:33 +02:00
2010-02-24 15:53:15 +01:00
case 'SERIES-PSEUDO-EXCEPTION':
if (is_array($event_info['master_event'])) // status update requires a stored master event
2008-06-07 19:45:33 +02:00
{
2010-02-24 15:53:15 +01:00
$recurrence = $this->date2usertime($event['reference']);
if ($event_info['acl_edit'])
{
// update all participants if we have the right to do that
$this->update_status($event, $event_info['stored_event'], $recurrence);
}
elseif (isset($event['participants'][$this->user]) || isset($event_info['master_event']['participants'][$this->user]))
{
// update the users status only
$this->set_status($event_info['master_event']['id'], $this->user,
($event['participants'][$this->user] ? $event['participants'][$this->user] : 'R'), $recurrence, true);
}
2008-06-07 19:45:33 +02:00
}
2010-02-24 15:53:15 +01:00
break;
}
2009-11-16 09:04:18 +01:00
}
// choose which id to return to the client
switch ($event_info['type'])
{
case 'SINGLE':
case 'SERIES-MASTER':
case 'SERIES-EXCEPTION':
$return_id = is_array($event_info['stored_event']) ? $event_info['stored_event']['id'] : false;
break;
2010-02-23 19:35:43 +01:00
case 'SERIES-PSEUDO-EXCEPTION':
2009-11-16 09:04:18 +01:00
$return_id = is_array($event_info['master_event']) ? $event_info['master_event']['id'] . ':' . $event['reference'] : false;
break;
2008-06-07 19:45:33 +02:00
2009-11-16 09:04:18 +01:00
case 'SERIES-EXCEPTION-PROPAGATE':
if ($event_info['acl_edit'] && is_array($event_info['stored_event']))
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
// we had sufficient rights to propagate the status only exception to a real one
$return_id = $event_info['stored_event']['id'];
2008-06-07 19:45:33 +02:00
}
2009-11-16 09:04:18 +01:00
else
{
// we did not have sufficient rights to propagate the status only exception to a real one
2010-02-23 19:35:43 +01:00
// we have to keep the SERIES-PSEUDO-EXCEPTION id and keep the event untouched
2009-11-16 09:04:18 +01:00
$return_id = $event_info['master_event']['id'] . ':' . $event['reference'];
}
break;
}
if ($this->log)
{
2010-02-23 19:35:43 +01:00
$event_info['stored_event'] = $this->read($event_info['stored_event']['id']);
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__."()[$updated_id]\n" .
array2string($event_info['stored_event'])."\n",3,$this->logfile);
2008-06-07 19:45:33 +02:00
}
}
2009-11-16 09:04:18 +01:00
return $return_id;
2008-06-07 19:45:33 +02:00
}
/**
* get the value of an attribute by its name
*
* @param array $attributes
* @param string $name eg. 'DTSTART'
* @param string $what='value'
* @return mixed
*/
static function _get_attribute($components,$name,$what='value')
{
2009-11-16 09:04:18 +01:00
foreach ($components as $attribute)
2008-06-07 19:45:33 +02:00
{
if ($attribute['name'] == $name)
{
return !$what ? $attribute : $attribute[$what];
}
}
return false;
}
static function valarm2egw(&$alarms, &$valarm)
{
$count = 0;
2009-11-16 09:04:18 +01:00
foreach ($valarm->_attributes as $vattr)
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
switch ($vattr['name'])
2008-06-07 19:45:33 +02:00
{
case 'TRIGGER':
$vtype = (isset($vattr['params']['VALUE']))
? $vattr['params']['VALUE'] : 'DURATION'; //default type
switch ($vtype)
{
case 'DURATION':
if (isset($vattr['params']['RELATED'])
&& $vattr['params']['RELATED'] != 'START')
{
error_log("Unsupported VALARM offset anchor ".$vattr['params']['RELATED']);
}
else
{
$alarms[] = array('offset' => -$vattr['value']);
$count++;
}
break;
case 'DATE-TIME':
$alarms[] = array('time' => $vattr['value']);
$count++;
break;
default:
// we should also do ;RELATED=START|END
error_log('VALARM/TRIGGER: unsupported value type:' . $vtype);
}
break;
2009-11-16 09:04:18 +01:00
case 'ACTION':
case 'DISPLAY':
case 'DESCRIPTION':
case 'SUMMARY':
case 'ATTACH':
case 'ATTENDEE':
// we ignore these fields silently
break;
2008-06-07 19:45:33 +02:00
default:
2009-11-16 09:04:18 +01:00
error_log('VALARM field ' .$vattr['name'] . ': ' . $vattr['value'] . ' HAS NO CONVERSION YET');
2008-06-07 19:45:33 +02:00
}
}
return $count;
}
2009-11-16 09:04:18 +01:00
function setSupportedFields($_productManufacturer='', $_productName='')
2008-06-07 19:45:33 +02:00
{
2010-02-23 19:35:43 +01:00
$state =& $_SESSION['SyncML.state'];
2009-11-16 09:04:18 +01:00
if (isset($state))
{
$deviceInfo = $state->getClientDeviceInfo();
}
2010-02-23 19:35:43 +01:00
// store product manufacturer and name for further usage
2009-11-16 09:04:18 +01:00
if ($_productManufacturer)
{
$this->productManufacturer = strtolower($_productManufacturer);
$this->productName = strtolower($_productName);
}
if (isset($deviceInfo) && is_array($deviceInfo))
{
if (isset($deviceInfo['uidExtension']) &&
$deviceInfo['uidExtension'])
{
$this->uidExtension = true;
}
if (isset($deviceInfo['nonBlockingAllday']) &&
$deviceInfo['nonBlockingAllday'])
{
$this->nonBlockingAllday = true;
}
if (isset($deviceInfo['tzid']) &&
$deviceInfo['tzid'])
{
$this->useServerTZ = ($deviceInfo['tzid'] == 1);
}
elseif (strpos($this->productName, 'palmos') !== false)
{
// for palmos we have to use user-time and NO timezone
$this->useServerTZ = false;
}
if (isset($GLOBALS['egw_info']['user']['preferences']['syncml']['calendar_owner']))
{
$owner = $GLOBALS['egw_info']['user']['preferences']['syncml']['calendar_owner'];
2010-03-24 12:30:38 +01:00
switch ($owner)
2010-02-23 19:35:43 +01:00
{
2010-03-24 12:30:38 +01:00
case 'G':
case 'P':
$owner = $this->user;
break;
2010-02-23 19:35:43 +01:00
}
2010-03-24 12:30:38 +01:00
if (0 < (int)$owner && $this->check_perms(EGW_ACL_EDIT, 0, $owner))
{
$this->calendarOwner = $owner;
}
}
2009-11-16 09:04:18 +01:00
if (!isset($this->productManufacturer) ||
$this->productManufacturer == '' ||
$this->productManufacturer == 'file')
{
$this->productManufacturer = strtolower($deviceInfo['manufacturer']);
}
if (!isset($this->productName) || $this->productName == '')
{
$this->productName = strtolower($deviceInfo['model']);
}
}
2008-06-07 19:45:33 +02:00
$defaultFields['minimal'] = array(
'public' => 'public',
'description' => 'description',
'end' => 'end',
'start' => 'start',
'location' => 'location',
'recur_type' => 'recur_type',
'recur_interval' => 'recur_interval',
'recur_data' => 'recur_data',
'recur_enddate' => 'recur_enddate',
2010-03-15 18:23:14 +01:00
'recur_exception' => 'recur_exception',
2008-06-07 19:45:33 +02:00
'title' => 'title',
2009-11-16 09:04:18 +01:00
'alarm' => 'alarm',
2008-06-07 19:45:33 +02:00
);
$defaultFields['basic'] = $defaultFields['minimal'] + array(
'priority' => 'priority',
);
$defaultFields['nexthaus'] = $defaultFields['basic'] + array(
'participants' => 'participants',
2009-11-16 09:04:18 +01:00
'uid' => 'uid',
);
$defaultFields['s60'] = $defaultFields['basic'] + array(
'category' => 'category',
'uid' => 'uid',
2008-06-07 19:45:33 +02:00
);
$defaultFields['synthesis'] = $defaultFields['basic'] + array(
2009-11-16 09:04:18 +01:00
'participants' => 'participants',
'owner' => 'owner',
2008-06-07 19:45:33 +02:00
'category' => 'category',
2009-11-16 09:04:18 +01:00
'non_blocking' => 'non_blocking',
'uid' => 'uid',
'reference' => 'reference',
'etag' => 'etag',
2008-06-07 19:45:33 +02:00
);
2010-02-23 19:35:43 +01:00
$defaultFields['funambol'] = $defaultFields['basic'] + array(
'participants' => 'participants',
'owner' => 'owner',
'category' => 'category',
'non_blocking' => 'non_blocking',
);
2008-06-07 19:45:33 +02:00
$defaultFields['evolution'] = $defaultFields['basic'] + array(
'participants' => 'participants',
'owner' => 'owner',
'category' => 'category',
2009-11-16 09:04:18 +01:00
'uid' => 'uid',
2008-06-07 19:45:33 +02:00
);
$defaultFields['full'] = $defaultFields['basic'] + array(
'participants' => 'participants',
'owner' => 'owner',
'category' => 'category',
'non_blocking' => 'non_blocking',
2009-11-16 09:04:18 +01:00
'uid' => 'uid',
'reference' => 'reference',
'etag' => 'etag',
2010-03-08 14:54:06 +01:00
'status' => 'status',
2008-06-07 19:45:33 +02:00
);
2009-11-16 09:04:18 +01:00
switch ($this->productManufacturer)
2008-06-07 19:45:33 +02:00
{
case 'nexthaus corporation':
case 'nexthaus corp':
2009-11-16 09:04:18 +01:00
switch ($this->productName)
2008-06-07 19:45:33 +02:00
{
default:
$this->supportedFields = $defaultFields['nexthaus'];
break;
}
break;
// multisync does not provide anymore information then the manufacturer
// we suppose multisync with evolution
case 'the multisync project':
2009-11-16 09:04:18 +01:00
switch ($this->productName)
2008-06-07 19:45:33 +02:00
{
default:
$this->supportedFields = $defaultFields['basic'];
break;
}
break;
2009-11-16 09:04:18 +01:00
case 'siemens':
switch ($this->productName)
{
case 'sx1':
$this->supportedFields = $defaultFields['minimal'];
break;
default:
error_log("Unknown Siemens phone '$_productName', using minimal set");
$this->supportedFields = $defaultFields['minimal'];
break;
}
break;
2008-06-07 19:45:33 +02:00
case 'nokia':
2009-11-16 09:04:18 +01:00
switch ($this->productName)
2008-06-07 19:45:33 +02:00
{
case 'e61':
$this->supportedFields = $defaultFields['minimal'];
break;
case 'e51':
case 'e90':
case 'e71':
2009-11-16 09:04:18 +01:00
case 'e66':
case '6120c':
case 'nokia 6131':
2010-03-15 18:23:14 +01:00
case 'n97 mini':
2009-11-16 09:04:18 +01:00
$this->supportedFields = $defaultFields['s60'];
break;
2008-06-07 19:45:33 +02:00
default:
error_log("Unknown Nokia phone '$_productName', assuming E61");
$this->supportedFields = $defaultFields['minimal'];
break;
}
break;
case 'sonyericsson':
case 'sony ericsson':
2009-11-16 09:04:18 +01:00
switch ($this->productName)
2008-06-07 19:45:33 +02:00
{
case 'd750i':
case 'p910i':
2009-11-16 09:04:18 +01:00
case 'g705i':
2010-02-24 15:53:15 +01:00
case 'w890i':
2008-06-07 19:45:33 +02:00
$this->supportedFields = $defaultFields['basic'];
break;
default:
2009-11-16 09:04:18 +01:00
error_log("Unknown Sony Ericsson phone '$this->productName' assuming d750i");
2008-06-07 19:45:33 +02:00
$this->supportedFields = $defaultFields['basic'];
break;
}
break;
case 'synthesis ag':
2009-11-16 09:04:18 +01:00
switch ($this->productName)
2008-06-07 19:45:33 +02:00
{
case 'sysync client pocketpc std':
case 'sysync client pocketpc pro':
2009-11-16 09:04:18 +01:00
case 'sysync client iphone contacts':
case 'sysync client iphone contacts+todoz':
2008-06-07 19:45:33 +02:00
default:
$this->supportedFields = $defaultFields['synthesis'];
break;
}
break;
//Syncevolution compatibility
case 'patrick ohly':
$this->supportedFields = $defaultFields['evolution'];
break;
case '': // seems syncevolution 0.5 doesn't send a manufacturer
error_log("No vendor name, assuming syncevolution 0.5");
$this->supportedFields = $defaultFields['evolution'];
break;
case 'file': // used outside of SyncML, eg. by the calendar itself ==> all possible fields
$this->useServerTZ = true;
2008-06-07 19:45:33 +02:00
$this->supportedFields = $defaultFields['full'];
break;
case 'groupdav': // all GroupDAV access goes through here
$this->useServerTZ = true;
2009-11-16 09:04:18 +01:00
switch ($this->productName)
{
default:
$this->supportedFields = $defaultFields['full'];
}
break;
case 'funambol':
2010-02-23 19:35:43 +01:00
$this->supportedFields = $defaultFields['funambol'];
break;
2008-06-07 19:45:33 +02:00
// the fallback for SyncML
default:
2009-11-16 09:04:18 +01:00
error_log("Unknown calendar SyncML client: manufacturer='$this->productManufacturer' product='$this->productName'");
$this->supportedFields = $defaultFields['synthesis'];
2008-06-07 19:45:33 +02:00
}
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__.
'(' . $this->productManufacturer .
', '. $this->productName .', ' .
($this->useServerTZ ? 'SERVERTIME' : 'USERTIME') .
2010-03-24 12:30:38 +01:00
', ' . $this->calendarOwner . ")\n" , 3, $this->logfile);
}
//Horde::logMessage('setSupportedFields(' . $this->productManufacturer . ', '
// . $this->productName .', ' .
// ($this->useServerTZ ? 'TRUE' : 'FALSE') .')',
// __FILE__, __LINE__, PEAR_LOG_DEBUG);
2008-06-07 19:45:33 +02:00
}
2010-03-01 22:18:52 +01:00
/**
* Convert vCalendar data in EGw events
*
* @param string $_vcalData
* @param string $principalURL='' Used for CalDAV imports
* @return array|boolean events on success, false on failure
*/
function icaltoegw($_vcalData, $principalURL='')
2008-06-07 19:45:33 +02:00
{
2010-02-23 19:35:43 +01:00
if ($this->log)
{
2010-03-01 22:18:52 +01:00
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__."($principalURL)\n" .
2010-02-23 19:35:43 +01:00
array2string($_vcalData)."\n",3,$this->logfile);
}
2009-11-16 09:04:18 +01:00
$events = array();
2008-06-07 19:45:33 +02:00
2009-11-16 09:04:18 +01:00
$vcal = new Horde_iCalendar;
2010-02-23 19:35:43 +01:00
if (!$vcal->parsevCalendar($_vcalData))
{
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__.
"(): No vCalendar Container found!\n",3,$this->logfile);
}
return false;
}
2009-11-16 09:04:18 +01:00
$version = $vcal->getAttribute('VERSION');
if (!is_array($this->supportedFields)) $this->setSupportedFields();
2008-06-07 19:45:33 +02:00
2009-11-16 09:04:18 +01:00
foreach ($vcal->getComponents() as $component)
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
if (is_a($component, 'Horde_iCalendar_vevent'))
2008-06-07 19:45:33 +02:00
{
2010-03-01 22:18:52 +01:00
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__.'()' .
get_class($component)." found\n",3,$this->logfile);
}
if ($event = $this->vevent2egw($component, $version, $this->supportedFields, $principalURL))
2008-06-07 19:45:33 +02:00
{
2010-02-23 19:35:43 +01:00
if ($this->isWholeDay($event)) $event['whole_day'] = true;
2009-11-16 09:04:18 +01:00
//common adjustments
if ($this->productManufacturer == '' && $this->productName == ''
&& !empty($event['recur_enddate']))
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
// syncevolution needs an adjusted recur_enddate
$event['recur_enddate'] = (int)$event['recur_enddate'] + 86400;
}
2010-02-23 19:35:43 +01:00
2009-11-16 09:04:18 +01:00
if ($event['recur_type'] != MCAL_RECUR_NONE)
{
// No reference or RECURRENCE-ID for the series master
$event['reference'] = 0;
}
// handle the alarms
$alarms = $event['alarm'];
foreach ($component->getComponents() as $valarm)
{
if (is_a($valarm, 'Horde_iCalendar_valarm'))
{
$this->valarm2egw($alarms, $valarm);
}
}
$event['alarm'] = $alarms;
2010-02-23 19:35:43 +01:00
if (strpos($this->productName, 'palmos'))
{
if (isset($event['whole_day']))
{
$event['start'] = mktime(0, 0, 0,
date('m', $event['start']),
date('d', $event['start']),
date('Y', $event['start'])
);
$event['end'] = mktime(23, 59, 59,
date('m', $event['end']),
date('d', $event['end']),
date('Y', $event['end'])
);
if (isset($event['reference']))
{
$event['reference'] = mktime(0, 0, 0,
date('m', $event['reference']),
date('d', $event['reference']),
date('Y', $event['reference'])
);
}
foreach($event['recur_exception'] as $n => $date)
{
$event['recur_exception'][$n] = mktime(0, 0, 0,
date('m', $date),
date('d', $date),
date('Y', $date)
);
}
}
else
{
foreach(array('start','end','recur_enddate','reference') as $ts)
{
// we convert here from user-time to timestamps in server-time!
if (isset($event[$ts])) $event[$ts] = $event[$ts] ? $this->date2ts($event[$ts],true) : 0;
}
// same with the recur exceptions
if (isset($event['recur_exception']) && is_array($event['recur_exception']))
{
foreach($event['recur_exception'] as $n => $date)
{
$event['recur_exception'][$n] = $this->date2ts($date,true);
}
}
}
// same with the alarms
if (isset($event['alarm']) && is_array($event['alarm']))
{
foreach($event['alarm'] as &$alarm)
{
$alarm['time'] = $this->date2ts($alarm['time'],true);
}
}
}
if (!empty($event['recur_enddate']))
{
// reset recure_enddate to 00:00:00
$event['recur_enddate'] = mktime(0, 0, 0,
date('m', $event['recur_enddate']),
date('d', $event['recur_enddate']),
date('Y', $event['recur_enddate'])
);
}
2009-11-16 09:04:18 +01:00
$events[] = $event;
}
}
2010-03-01 22:18:52 +01:00
else
{
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__.'()' .
get_class($component)." found\n",3,$this->logfile);
}
}
2009-11-16 09:04:18 +01:00
}
2010-02-23 19:35:43 +01:00
return $events;
2009-11-16 09:04:18 +01:00
}
/**
* Parse a VEVENT
*
* @param array $component VEVENT
* @param string $version vCal version (1.0/2.0)
* @param array $supportedFields supported fields of the device
2010-03-01 22:18:52 +01:00
* @param string $principalURL='' Used for CalDAV imports
2010-02-23 19:35:43 +01:00
*
2009-11-16 09:04:18 +01:00
* @return array|boolean event on success, false on failure
*/
2010-03-01 22:18:52 +01:00
function vevent2egw(&$component, $version, $supportedFields, $principalURL='')
2009-11-16 09:04:18 +01:00
{
2010-03-01 22:18:52 +01:00
if (!is_a($component, 'Horde_iCalendar_vevent'))
{
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__.'()' .
get_class($component)." found\n",3,$this->logfile);
}
return false;
}
2009-11-16 09:04:18 +01:00
if (isset($GLOBALS['egw_info']['user']['preferences']['syncml']['minimum_uid_length'])) {
$minimum_uid_length = $GLOBALS['egw_info']['user']['preferences']['syncml']['minimum_uid_length'];
}
else
{
$minimum_uid_length = 8;
}
$isDate = false;
$event = array();
$alarms = array();
$vcardData = array(
'recur_type' => MCAL_RECUR_NONE,
'recur_exception' => array(),
);
// we parse DTSTART and DTEND first
foreach ($component->_attributes as $attributes)
{
switch ($attributes['name'])
{
case 'DTSTART':
if (isset($attributes['params']['VALUE'])
&& $attributes['params']['VALUE'] == 'DATE')
{
$isDate = true;
}
$dtstart_ts = is_numeric($attributes['value']) ? $attributes['value'] : $this->date2ts($attributes['value']);
$vcardData['start'] = $dtstart_ts;
break;
case 'DTEND':
$dtend_ts = is_numeric($attributes['value']) ? $attributes['value'] : $this->date2ts($attributes['value']);
if (date('H:i:s',$dtend_ts) == '00:00:00')
{
2010-02-23 19:35:43 +01:00
$dtend_ts -= 1;
2009-11-16 09:04:18 +01:00
}
$vcardData['end'] = $dtend_ts;
}
}
2010-03-01 22:18:52 +01:00
if (!isset($vcardData['start']))
{
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__
. "() DTSTART missing!\n",3,$this->logfile);
}
return false; // not a valid entry
}
2009-11-16 09:04:18 +01:00
// lets see what we can get from the vcard
foreach ($component->_attributes as $attributes)
{
2010-03-01 22:18:52 +01:00
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__
. '(): ' . $attributes['name'] . ' => '
. $attributes['value'] . "\n",3,$this->logfile);
}
2009-11-16 09:04:18 +01:00
switch ($attributes['name'])
{
case 'AALARM':
case 'DALARM':
$alarmTime = $attributes['value'];
$alarms[$alarmTime] = array(
'time' => $alarmTime
);
break;
case 'CLASS':
$vcardData['public'] = (int)(strtolower($attributes['value']) == 'public');
break;
case 'DESCRIPTION':
2010-02-23 19:35:43 +01:00
$vcardData['description'] = str_replace("\r\n", "\n", $attributes['value']);
2009-11-16 09:04:18 +01:00
if (preg_match('/\s*\[UID:(.+)?\]/Usm', $attributes['value'], $matches))
{
if (!isset($vCardData['uid'])
&& strlen($matches[1]) >= $minimum_uid_length)
{
$vcardData['uid'] = $matches[1];
}
}
break;
case 'RECURRENCE-ID':
case 'X-RECURRENCE-ID':
$vcardData['reference'] = $attributes['value'];
break;
case 'LOCATION':
2010-02-23 19:35:43 +01:00
$vcardData['location'] = str_replace("\r\n", "\n", $attributes['value']);
2009-11-16 09:04:18 +01:00
break;
case 'RRULE':
$recurence = $attributes['value'];
2010-02-23 19:35:43 +01:00
$vcardData['recur_interval'] = 1;
2009-11-16 09:04:18 +01:00
$type = preg_match('/FREQ=([^;: ]+)/i',$recurence,$matches) ? $matches[1] : $recurence[0];
// vCard 2.0 values for all types
if (preg_match('/UNTIL=([0-9TZ]+)/',$recurence,$matches))
2009-11-16 09:04:18 +01:00
{
$vcardData['recur_enddate'] = $this->vCalendar->_parseDateTime($matches[1]);
}
elseif (preg_match('/COUNT=([0-9]+)/',$recurence,$matches))
{
$vcardData['recur_count'] = (int)$matches[1];
}
if (preg_match('/INTERVAL=([0-9]+)/',$recurence,$matches))
{
2010-02-23 19:35:43 +01:00
$vcardData['recur_interval'] = (int) $matches[1] ? (int) $matches[1] : 1;
2009-11-16 09:04:18 +01:00
}
$vcardData['recur_data'] = 0;
switch($type)
{
case 'W':
case 'WEEKLY':
$days = array();
2010-02-23 19:35:43 +01:00
if (preg_match('/W(\d+) *((?i: [AEFHMORSTUW]{2})+)?( +([^ ]*))$/',$recurence, $recurenceMatches)) // 1.0
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
$vcardData['recur_interval'] = $recurenceMatches[1];
2010-02-23 19:35:43 +01:00
if (empty($recurenceMatches[2]))
2009-11-16 09:04:18 +01:00
{
$days[0] = strtoupper(substr(date('D', $vcardData['start']),0,2));
}
else
{
$days = explode(' ',trim($recurenceMatches[2]));
2010-02-23 19:35:43 +01:00
}
2008-06-07 19:45:33 +02:00
2010-02-23 19:35:43 +01:00
if (preg_match('/#(\d+)/',$recurenceMatches[4],$repeatMatches))
{
if ($repeatMatches[1]) $vcardData['recur_count'] = $repeatMatches[1];
2009-11-16 09:04:18 +01:00
}
2010-02-23 19:35:43 +01:00
else
{
$vcardData['recur_enddate'] = $this->vCalendar->_parseDateTime($recurenceMatches[4]);
}
2009-11-16 09:04:18 +01:00
$recur_days = $this->recur_days_1_0;
}
elseif (preg_match('/BYDAY=([^;: ]+)/',$recurence,$recurenceMatches)) // 2.0
{
$days = explode(',',$recurenceMatches[1]);
$recur_days = $this->recur_days;
}
else // no day given, use the day of dtstart
{
$vcardData['recur_data'] |= 1 << (int)date('w',$vcardData['start']);
$vcardData['recur_type'] = MCAL_RECUR_WEEKLY;
}
if ($days)
{
foreach ($recur_days as $id => $day)
{
if (in_array(strtoupper(substr($day,0,2)),$days))
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
$vcardData['recur_data'] |= $id;
2008-06-07 19:45:33 +02:00
}
2009-11-16 09:04:18 +01:00
}
$vcardData['recur_type'] = MCAL_RECUR_WEEKLY;
}
2008-06-07 19:45:33 +02:00
2009-11-16 09:04:18 +01:00
if (!empty($vcardData['recur_count']))
{
2009-12-04 16:38:36 +01:00
$vcardData['recur_enddate'] = mktime(
date('H', $vcardData['end']),
date('i', $vcardData['end']),
date('s', $vcardData['end']),
date('m', $vcardData['start']),
date('d', $vcardData['start']) + ($vcardData['recur_interval']*($vcardData['recur_count']-1)*7),
date('Y', $vcardData['start']));
2008-06-07 19:45:33 +02:00
}
break;
2009-11-16 09:04:18 +01:00
case 'D': // 1.0
2010-02-23 19:35:43 +01:00
if (preg_match('/D(\d+) #(\d+)/', $recurence, $recurenceMatches))
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
$vcardData['recur_interval'] = $recurenceMatches[1];
if ($recurenceMatches[2] > 0 && $vcardData['end'])
{
$vcardData['recur_enddate'] = mktime(
date('H', $vcardData['end']),
date('i', $vcardData['end']),
date('s', $vcardData['end']),
date('m', $vcardData['end']),
2010-02-23 19:35:43 +01:00
date('d', $vcardData['end']) + ($vcardData['recur_interval']*($recurenceMatches[2]-1)),
2009-11-16 09:04:18 +01:00
date('Y', $vcardData['end'])
);
}
2008-06-07 19:45:33 +02:00
}
2009-11-16 09:04:18 +01:00
elseif (preg_match('/D(\d+) (.*)/', $recurence, $recurenceMatches))
{
2009-11-16 09:04:18 +01:00
$vcardData['recur_interval'] = $recurenceMatches[1];
2010-02-23 19:35:43 +01:00
$vcardData['recur_enddate'] = $this->vCalendar->_parseDateTime(trim($recurenceMatches[2]));
}
2009-11-16 09:04:18 +01:00
else break;
// fall-through
case 'DAILY': // 2.0
$vcardData['recur_type'] = MCAL_RECUR_DAILY;
if (!empty($vcardData['recur_count']))
2008-06-07 19:45:33 +02:00
{
2009-12-04 16:38:36 +01:00
$vcardData['recur_enddate'] = mktime(
date('H', $vcardData['end']),
date('i', $vcardData['end']),
date('s', $vcardData['end']),
date('m', $vcardData['start']),
date('d', $vcardData['start']) + ($vcardData['recur_interval']*($vcardData['recur_count']-1)),
date('Y', $vcardData['start']));
2008-06-07 19:45:33 +02:00
}
2009-11-16 09:04:18 +01:00
break;
case 'M':
2010-02-23 19:35:43 +01:00
if (preg_match('/MD(\d+)(?: [^ ])? #(\d+)/', $recurence, $recurenceMatches))
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
$vcardData['recur_type'] = MCAL_RECUR_MONTHLY_MDAY;
$vcardData['recur_interval'] = $recurenceMatches[1];
if ($recurenceMatches[2] > 0 && $vcardData['end'])
{
$vcardData['recur_enddate'] = mktime(
date('H', $vcardData['end']),
date('i', $vcardData['end']),
date('s', $vcardData['end']),
2010-02-23 19:35:43 +01:00
date('m', $vcardData['end']) + ($vcardData['recur_interval']*($recurenceMatches[2]-1)),
2009-11-16 09:04:18 +01:00
date('d', $vcardData['end']),
date('Y', $vcardData['end'])
);
}
2008-06-07 19:45:33 +02:00
}
2009-11-16 09:04:18 +01:00
elseif (preg_match('/MD(\d+) (.*)/',$recurence, $recurenceMatches))
{
$vcardData['recur_type'] = MCAL_RECUR_MONTHLY_MDAY;
if ($recurenceMatches[1] > 1)
{
$vcardData['recur_interval'] = $recurenceMatches[1];
}
2010-02-23 19:35:43 +01:00
$vcardData['recur_enddate'] = $this->vCalendar->_parseDateTime(trim($recurenceMatches[2]));
2008-06-07 19:45:33 +02:00
}
2009-11-16 09:04:18 +01:00
elseif (preg_match('/MP(\d+) (.*) (.*) (.*)/',$recurence, $recurenceMatches))
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
$vcardData['recur_type'] = MCAL_RECUR_MONTHLY_WDAY;
2010-02-23 19:35:43 +01:00
$vcardData['recur_interval'] = $recurenceMatches[1];
if (preg_match('/#(\d+)/',$recurenceMatches[4],$recurenceMatches))
2009-11-16 09:04:18 +01:00
{
2010-02-23 19:35:43 +01:00
if ($recurenceMatches[1])
{
$vcardData['recur_enddate'] = mktime(
date('H', $vcardData['end']),
date('i', $vcardData['end']),
date('s', $vcardData['end']),
date('m', $vcardData['start']) + ($vcardData['recur_interval']*($recurenceMatches[1]-1)),
date('d', $vcardData['start']),
date('Y', $vcardData['start']));
}
2009-11-16 09:04:18 +01:00
}
2010-02-23 19:35:43 +01:00
else
2009-11-16 09:04:18 +01:00
{
2010-02-23 19:35:43 +01:00
$vcardData['recur_enddate'] = $this->vCalendar->_parseDateTime(trim($recurenceMatches[4]));
2009-11-16 09:04:18 +01:00
}
2008-06-07 19:45:33 +02:00
}
break;
2009-11-16 09:04:18 +01:00
case 'MONTHLY':
$vcardData['recur_type'] = strpos($recurence,'BYDAY') !== false ?
MCAL_RECUR_MONTHLY_WDAY : MCAL_RECUR_MONTHLY_MDAY;
2008-06-07 19:45:33 +02:00
2009-11-16 09:04:18 +01:00
if (!empty($vcardData['recur_count']))
{
2009-12-04 16:38:36 +01:00
$vcardData['recur_enddate'] = mktime(
date('H', $vcardData['end']),
date('i', $vcardData['end']),
date('s', $vcardData['end']),
date('m', $vcardData['start']) + ($vcardData['recur_interval']*($vcardData['recur_count']-1)),
date('d', $vcardData['start']),
date('Y', $vcardData['start']));
2009-11-16 09:04:18 +01:00
}
2008-06-07 19:45:33 +02:00
break;
2009-11-16 09:04:18 +01:00
case 'Y': // 1.0
if (preg_match('/YM(\d+)[^#]*#(\d+)/', $recurence, $recurenceMatches))
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
$vcardData['recur_interval'] = $recurenceMatches[1];
if ($recurenceMatches[2] > 0 && $vcardData['end'])
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
$vcardData['recur_enddate'] = mktime(
date('H', $vcardData['end']),
date('i', $vcardData['end']),
date('s', $vcardData['end']),
date('m', $vcardData['end']),
date('d', $vcardData['end']),
date('Y', $vcardData['end']) + ($recurenceMatches[2] * $vcardData['recur_interval'])
);
2008-06-07 19:45:33 +02:00
}
}
2009-11-16 09:04:18 +01:00
elseif (preg_match('/YM(\d+) (.*)/',$recurence, $recurenceMatches))
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
$vcardData['recur_interval'] = $recurenceMatches[1];
if ($recurenceMatches[2] != '#0')
{
2010-02-23 19:35:43 +01:00
$vcardData['recur_enddate'] = $this->vCalendar->_parseDateTime(trim($recurenceMatches[2]));
2009-11-16 09:04:18 +01:00
}
} else break;
// fall-through
case 'YEARLY': // 2.0
$vcardData['recur_type'] = MCAL_RECUR_YEARLY;
if (!empty($vcardData['recur_count']))
{
2009-12-04 16:38:36 +01:00
$vcardData['recur_enddate'] = mktime(
date('H', $vcardData['end']),
date('i', $vcardData['end']),
date('s', $vcardData['end']),
date('m', $vcardData['start']),
date('d', $vcardData['start']),
date('Y', $vcardData['start']) + ($vcardData['recur_interval']*($vcardData['recur_count']-1)));
2008-06-07 19:45:33 +02:00
}
break;
}
2009-11-16 09:04:18 +01:00
break;
case 'EXDATE':
2010-02-23 19:35:43 +01:00
if (!$attributes['value']) break;
2009-11-16 09:04:18 +01:00
if ((isset($attributes['params']['VALUE'])
&& $attributes['params']['VALUE'] == 'DATE') ||
(!isset($attributes['params']['VALUE']) && $isDate))
{
$days = array();
$hour = date('H', $vcardData['start']);
$minutes = date('i', $vcardData['start']);
$seconds = date('s', $vcardData['start']);
foreach ($attributes['values'] as $day)
{
$days[] = mktime(
$hour,
$minutes,
$seconds,
$day['month'],
$day['mday'],
$day['year']);
}
$vcardData['recur_exception'] = array_merge($vcardData['recur_exception'], $days);
}
else
{
$vcardData['recur_exception'] = array_merge($vcardData['recur_exception'], $attributes['values']);
}
break;
case 'SUMMARY':
2010-02-23 19:35:43 +01:00
$vcardData['title'] = str_replace("\r\n", "\n", $attributes['value']);
2009-11-16 09:04:18 +01:00
break;
case 'UID':
if (strlen($attributes['value']) >= $minimum_uid_length)
{
2010-02-24 15:53:15 +01:00
$vcardData['uid'] = $attributes['value'];
2009-11-16 09:04:18 +01:00
}
break;
case 'TRANSP':
if ($version == '1.0')
{
$vcardData['non_blocking'] = ($attributes['value'] == 1);
}
else
{
$vcardData['non_blocking'] = ($attributes['value'] == 'TRANSPARENT');
}
break;
case 'PRIORITY':
2010-02-23 19:35:43 +01:00
if ($this->productManufacturer == 'funambol' &&
(strpos($this->productName, 'outlook') !== false
|| strpos($this->productName, 'pocket pc') !== false))
{
$vcardData['priority'] = (int) $this->priority_funambol2egw[$attributes['value']];
}
else
{
$vcardData['priority'] = (int) $this->priority_ical2egw[$attributes['value']];
}
2009-11-16 09:04:18 +01:00
break;
case 'CATEGORIES':
if ($attributes['value'])
{
2010-02-23 19:35:43 +01:00
$vcardData['category'] = explode(',', $attributes['value']);
2009-11-16 09:04:18 +01:00
}
else
{
$vcardData['category'] = array();
}
break;
case 'ATTENDEE':
2010-02-23 19:35:43 +01:00
case 'ORGANIZER': // will be written direct to the event
2009-11-16 09:04:18 +01:00
if (isset($attributes['params']['PARTSTAT']))
{
$attributes['params']['STATUS'] = $attributes['params']['PARTSTAT'];
}
if (isset($attributes['params']['STATUS']))
{
$status = $this->status_ical2egw[strtoupper($attributes['params']['STATUS'])];
2010-02-23 19:35:43 +01:00
if (empty($status)) $status = 'X';
2009-11-16 09:04:18 +01:00
}
else
{
2010-02-23 19:35:43 +01:00
$status = 'X'; // client did not return the status
2009-11-16 09:04:18 +01:00
}
2010-03-01 22:18:52 +01:00
$uid = $email = $cn = '';
$quantity = 1;
$role = 'REQ-PARTICIPANT';
if (!empty($attributes['params']['ROLE']))
{
$role = $attributes['params']['ROLE'];
}
// try pricipal url from CalDAV
if (strpos($attributes['value'], 'http') === 0)
{
if (!empty($principalURL) && strstr($attributes['value'], $principalURL) !== false)
{
$uid = $this->user;
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__
. "(): Found myself: '$uid'\n",3,$this->logfile);
}
}
else
{
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__
. '(): Unknown URI: ' . $attributes['value']
. "\n",3,$this->logfile);
}
$attributes['value'] = '';
}
}
// try X-EGROUPWARE-UID
if (!$uid && !empty($attributes['params']['X-EGROUPWARE-UID']))
{
$uid = $attributes['params']['X-EGROUPWARE-UID'];
if (!empty($attributes['params']['X-EGROUPWARE-QUANTITY']))
{
$quantity = $attributes['params']['X-EGROUPWARE-QUANTITY'];
}
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__
. "(): Found X-EGROUPWARE-UID: '$uid'\n",3,$this->logfile);
}
}
elseif ($attributes['value'] == 'Unknown')
{
// we use the current user
$uid = $this->user;
}
// try to find an email address
elseif (preg_match('/MAILTO:([@.a-z0-9_-]+)|MAILTO:"?([.a-z0-9_ -]*)"?[ ]*<([@.a-z0-9_-]*)>/i',
2009-11-16 09:04:18 +01:00
$attributes['value'],$matches))
{
$email = $matches[1] ? $matches[1] : $matches[3];
$cn = isset($matches[2]) ? $matches[2]: '';
}
2010-03-01 22:18:52 +01:00
elseif (!empty($attributes['value']) &&
preg_match('/"?([.a-z0-9_ -]*)"?[ ]*<([@.a-z0-9_-]*)>/i',
2009-11-16 09:04:18 +01:00
$attributes['value'],$matches))
{
$cn = $matches[1];
$email = $matches[2];
}
elseif (strpos($attributes['value'],'@') !== false)
{
$email = $attributes['value'];
}
2010-03-01 22:18:52 +01:00
if (!$uid && $email && ($uid = $GLOBALS['egw']->accounts->name2id($email, 'account_email')))
2009-11-16 09:04:18 +01:00
{
2010-03-01 22:18:52 +01:00
// we use the account we found
if ($this->log)
2009-11-16 09:04:18 +01:00
{
2010-03-01 22:18:52 +01:00
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__
. "() Found account: '$uid', '$cn', '$email'\n",3,$this->logfile);
2009-11-16 09:04:18 +01:00
}
}
2010-03-01 22:18:52 +01:00
if (!$uid)
2009-11-16 09:04:18 +01:00
{
2010-03-01 22:18:52 +01:00
$searcharray = array();
// search for provided email address ...
if ($email)
{
$searcharray = array('email' => $email, 'email_home' => $email);
}
// ... and for provided CN
if (!empty($attributes['params']['CN']))
{
2010-04-12 09:19:25 +02:00
$cn = $attributes['params']['CN'];
if ($cn[0] == '"' && substr($cn,-1) == '"')
2010-03-01 22:18:52 +01:00
{
2010-04-12 09:19:25 +02:00
$cn = substr($cn,1,-1);
2010-03-01 22:18:52 +01:00
}
$searcharray['n_fn'] = $cn;
}
elseif ($cn)
{
$searcharray['n_fn'] = $cn;
}
2008-06-07 19:45:33 +02:00
2010-04-12 09:19:25 +02:00
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__
. "() Search participant: '$cn', '$email'\n",3,$this->logfile);
}
2010-03-01 22:18:52 +01:00
//elseif (//$attributes['params']['CUTYPE'] == 'GROUP'
if (preg_match('/(.*) ' . lang('Group') . '/', $cn, $matches))
2009-11-16 09:04:18 +01:00
{
2010-04-12 09:19:25 +02:00
// we found a group
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__
. "() Found group: '$matches[1]', '$cn', '$email'\n",3,$this->logfile);
}
2010-03-01 22:18:52 +01:00
if (($uid = $GLOBALS['egw']->accounts->name2id($matches[1], 'account_lid', 'g')))
2009-11-16 09:04:18 +01:00
{
2010-03-01 22:18:52 +01:00
//Horde::logMessage("vevent2egw: group participant $uid",
// __FILE__, __LINE__, PEAR_LOG_DEBUG);
if (!isset($vcardData['participants'][$this->user]) &&
$status != 'X' && $status != 'U')
{
2010-03-01 22:18:52 +01:00
// User tries to reply to the group invitiation
$members = $GLOBALS['egw']->accounts->members($uid, true);
if (in_array($this->user, $members))
{
//Horde::logMessage("vevent2egw: set status to " . $status,
// __FILE__, __LINE__, PEAR_LOG_DEBUG);
$vcardData['participants'][$this->user] =
calendar_so::combine_status($status,$quantity,$role);
}
}
$status = 'U'; // keep the group
2009-11-16 09:04:18 +01:00
}
2010-03-01 22:18:52 +01:00
else continue; // can't find this group
2009-11-16 09:04:18 +01:00
}
2010-03-01 22:18:52 +01:00
elseif (empty($searcharray))
{
continue; // participants without email AND CN --> ignore it
}
elseif ((list($data) = $this->addressbook->search($searcharray,
array('id','egw_addressbook.account_id as account_id','n_fn'),
'egw_addressbook.account_id IS NOT NULL DESC, n_fn IS NOT NULL DESC',
'','',false,'OR')))
2009-11-16 09:04:18 +01:00
{
2010-03-01 22:18:52 +01:00
// found an addressbook entry
$uid = $data['account_id'] ? (int)$data['account_id'] : 'c'.$data['id'];
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__
. "() Found addressbook entry: '$uid', '$cn', '$email'\n",3,$this->logfile);
}
}
else
{
if (!$email)
{
$email = 'no-email@egroupware.org'; // set dummy email to store the CN
}
$uid = 'e'. ($cn ? '"' . $cn . '" <' . $email . '>' : $email);
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__
. "() Not Found, create dummy: '$uid', '$cn', '$email'\n",3,$this->logfile);
}
2009-11-16 09:04:18 +01:00
}
}
switch($attributes['name'])
{
case 'ATTENDEE':
2010-02-24 15:53:15 +01:00
if (!isset($vcardData['participants'][$uid]) ||
$vcardData['participants'][$uid][0] != 'A')
2009-11-16 09:04:18 +01:00
{
2010-02-23 19:35:43 +01:00
// for multiple entries the ACCEPT wins
2009-11-16 09:04:18 +01:00
// add quantity and role
2010-02-24 15:53:15 +01:00
$vcardData['participants'][$uid] =
2010-03-01 22:18:52 +01:00
calendar_so::combine_status($status, $quantity, $role);
if (!$this->calendarOwner && is_numeric($uid) &&
$role == 'CHAIR' &&
is_a($component->getAttribute('ORGANIZER'), 'PEAR_Error'))
{
// we can store the ORGANIZER as event owner
$event['owner'] = $uid;
}
2009-11-16 09:04:18 +01:00
}
2010-02-23 19:35:43 +01:00
break;
2009-11-16 09:04:18 +01:00
case 'ORGANIZER':
2010-02-24 15:53:15 +01:00
if (isset($vcardData['participants'][$uid]))
2010-02-23 19:35:43 +01:00
{
2010-02-24 15:53:15 +01:00
$status = $vcardData['participants'][$uid];
2010-02-23 19:35:43 +01:00
calendar_so::split_status($status, $quantity, $role);
2010-02-24 15:53:15 +01:00
$vcardData['participants'][$uid] =
2010-02-23 19:35:43 +01:00
calendar_so::combine_status($status, $quantity, 'CHAIR');
}
2010-03-01 22:18:52 +01:00
if (!$this->calendarOwner && is_numeric($uid))
2009-11-16 09:04:18 +01:00
{
// we can store the ORGANIZER as event owner
2009-11-16 09:04:18 +01:00
$event['owner'] = $uid;
}
else
{
2010-02-23 19:35:43 +01:00
// we must insert a CHAIR participant to keep the ORGANIZER
2009-11-16 09:04:18 +01:00
$event['owner'] = $this->user;
2010-02-24 15:53:15 +01:00
if (!isset($vcardData['participants'][$uid]))
2010-02-23 19:35:43 +01:00
{
// save the ORGANIZER as event CHAIR
2010-02-24 15:53:15 +01:00
$vcardData['participants'][$uid] =
2010-03-01 22:18:52 +01:00
calendar_so::combine_status('D', 1, 'CHAIR');
2010-02-23 19:35:43 +01:00
}
}
2009-11-16 09:04:18 +01:00
}
break;
case 'CREATED': // will be written direct to the event
if ($event['modified']) break;
// fall through
case 'LAST-MODIFIED': // will be written direct to the event
$event['modified'] = $attributes['value'];
2008-06-07 19:45:33 +02:00
}
}
2009-11-16 09:04:18 +01:00
// check if the entry is a birthday
// this field is only set from NOKIA clients
$agendaEntryType = $component->getAttribute('X-EPOCAGENDAENTRYTYPE');
if (!is_a($agendaEntryType, 'PEAR_Error'))
{
if (strtolower($agendaEntryType) == 'anniversary')
{
$event['special'] = '1';
$event['non_blocking'] = 1;
// make it a whole day event for eGW
$vcardData['end'] = $vcardData['start'] + 86399;
}
elseif (strtolower($agendaEntryType) == 'event')
{
$event['special'] = '2';
$event['non_blocking'] = 1;
}
2008-06-07 19:45:33 +02:00
}
2009-11-16 09:04:18 +01:00
$event['priority'] = 2; // default
$event['alarm'] = $alarms;
2008-06-07 19:45:33 +02:00
2009-11-16 09:04:18 +01:00
// now that we know what the vard provides,
// we merge that data with the information we have about the device
while (($fieldName = array_shift($supportedFields)))
{
switch ($fieldName)
{
case 'recur_interval':
case 'recur_enddate':
case 'recur_data':
case 'recur_exception':
// not handled here
break;
2009-11-16 09:04:18 +01:00
case 'recur_type':
$event['recur_type'] = $vcardData['recur_type'];
if ($event['recur_type'] != MCAL_RECUR_NONE)
{
$event['reference'] = 0;
foreach (array('recur_interval','recur_enddate','recur_data','recur_exception') as $r)
{
if (isset($vcardData[$r]))
{
$event[$r] = $vcardData[$r];
}
}
}
break;
default:
if (isset($vcardData[$fieldName]))
{
$event[$fieldName] = $vcardData[$fieldName];
}
break;
}
2008-06-07 19:45:33 +02:00
}
if ($this->calendarOwner) $event['owner'] = $this->calendarOwner;
2010-02-23 19:35:43 +01:00
if ($this->log)
{
2010-03-01 22:18:52 +01:00
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__."($principalURL)\n" .
2010-02-23 19:35:43 +01:00
array2string($event)."\n",3,$this->logfile);
}
2010-02-23 19:35:43 +01:00
//Horde::logMessage("vevent2egw:\n" . print_r($event, true),
// __FILE__, __LINE__, PEAR_LOG_DEBUG);
2009-11-16 09:04:18 +01:00
return $event;
}
2008-06-07 19:45:33 +02:00
2009-11-16 09:04:18 +01:00
function search($_vcalData, $contentID=null, $relax=false)
{
2010-02-23 19:35:43 +01:00
if (($events = $this->icaltoegw($_vcalData)))
2009-11-16 09:04:18 +01:00
{
// this function only supports searching a single event
if (count($events) == 1)
{
2010-02-23 19:35:43 +01:00
$filter = $relax ? 'relax' : 'check';
2009-11-16 09:04:18 +01:00
$event = array_shift($events);
$eventId = -1;
2010-02-23 19:35:43 +01:00
if ($this->isWholeDay($event, true)) $event['whole_day'] = true;
if ($contentID)
{
$parts = preg_split('/:/', $contentID);
$event['id'] = $eventId = $parts[0];
}
2010-02-23 19:35:43 +01:00
$event['category'] = $this->find_or_add_categories($event['category'], $eventId);
return $this->find_event($event, $filter);
}
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__."() found:\n" .
array2string($events)."\n",3,$this->logfile);
2008-06-07 19:45:33 +02:00
}
}
2010-02-23 19:35:43 +01:00
return array();
2008-06-07 19:45:33 +02:00
}
/**
* Create a freebusy vCal for the given user(s)
*
* @param int $user account_id
* @param mixed $end=null end-date, default now+1 month
2009-11-16 09:04:18 +01:00
* @param boolean $utc=true if false, use servertime for dates
2008-06-07 19:45:33 +02:00
* @return string
*/
2009-11-16 09:04:18 +01:00
function freebusy($user,$end=null,$utc=true)
2008-06-07 19:45:33 +02:00
{
if (!$end) $end = $this->now_su + 100*DAY_s; // default next 100 days
2009-11-16 09:04:18 +01:00
$vcal = new Horde_iCalendar;
2008-06-07 19:45:33 +02:00
$vcal->setAttribute('PRODID','-//eGroupWare//NONSGML eGroupWare Calendar '.$GLOBALS['egw_info']['apps']['calendar']['version'].'//'.
strtoupper($GLOBALS['egw_info']['user']['preferences']['common']['lang']));
$vcal->setAttribute('VERSION','2.0');
$vfreebusy = Horde_iCalendar::newComponent('VFREEBUSY',$vcal);
$parameters = array(
'ORGANIZER' => $GLOBALS['egw']->translation->convert(
$GLOBALS['egw']->accounts->id2name($user,'account_firstname').' '.
$GLOBALS['egw']->accounts->id2name($user,'account_lastname'),
$GLOBALS['egw']->translation->charset(),'utf-8'),
);
2009-11-16 09:04:18 +01:00
if ($utc)
2008-06-07 19:45:33 +02:00
{
2009-11-16 09:04:18 +01:00
foreach (array(
'URL' => $this->freebusy_url($user),
'DTSTART' => $this->date2ts($this->now_su,true), // true = server-time
'DTEND' => $this->date2ts($end,true), // true = server-time
'ORGANIZER' => $GLOBALS['egw']->accounts->id2name($user,'account_email'),
'DTSTAMP' => time(),
) as $attr => $value)
{
$vfreebusy->setAttribute($attr, $value);
}
}
else
{
foreach (array(
'URL' => $this->freebusy_url($user),
'DTSTART' => date('Ymd\THis',$this->date2ts($this->now_su,true)), // true = server-time
'DTEND' => date('Ymd\THis',$this->date2ts($end,true)), // true = server-time
'ORGANIZER' => $GLOBALS['egw']->accounts->id2name($user,'account_email'),
'DTSTAMP' => date('Ymd\THis',time()),
) as $attr => $value)
{
$vfreebusy->setAttribute($attr, $value);
}
2008-06-07 19:45:33 +02:00
}
$fbdata = parent::search(array(
2009-11-16 09:04:18 +01:00
'start' => $this->now_su,
'end' => $end,
'users' => $user,
'date_format' => 'server',
'show_rejected' => false,
));
2008-06-07 19:45:33 +02:00
if (is_array($fbdata))
{
foreach ($fbdata as $event)
{
if ($event['non_blocking']) continue;
2009-11-16 09:04:18 +01:00
if ($utc)
{
$vfreebusy->setAttribute('FREEBUSY',array(array(
'start' => $event['start'],
'end' => $event['end'],
)));
}
else
{
$vfreebusy->setAttribute('FREEBUSY',array(array(
'start' => date('Ymd\THis',$event['start']),
'end' => date('Ymd\THis',$event['end']),
)));
}
2008-06-07 19:45:33 +02:00
}
}
$vcal->addComponent($vfreebusy);
return $vcal->exportvCalendar();
}
2009-11-16 09:04:18 +01:00
/**
* generate and insert a VTIMEZONE entry to a vcalendar
*
* @param array $event
* @param array $vcal VCALENDAR entry
* @return string local timezone name (e.g. 'CET/CEST')
*/
static function generate_vtimezone($event, &$vcal)
2009-11-16 09:04:18 +01:00
{
$dayofweek = array('SU','MO','TU','WE','TH','FR','SA');
$tbl_tz = array(
array("Afghanistan","Asia/Kabul",270,0,"",0,0,0,0,0,0,0,0,0,0),
array("AKST/AKDT","America/Anchorage",-540,60,"",3,0,2,2,0,11,0,1,2,0),
array("AKST/AKDT","America/Anchorage",-540,60,"2006",4,0,1,2,0,10,0,5,2,0),
array("AKST/AKDT","America/Anchorage",-540,60,"2007",3,0,2,2,0,11,0,1,2,0),
array("HNY/NAY","America/Anchorage",-540,60,"",3,0,2,2,0,11,0,1,2,0),
array("HNY/NAY","America/Anchorage",-540,60,"2006",4,0,1,2,0,10,0,5,2,0),
array("HNY/NAY","America/Anchorage",-540,60,"2007",3,0,2,2,0,11,0,1,2,0),
array("Alaskan","America/Anchorage",-540,60,"",3,0,2,2,0,11,0,1,2,0),
array("Alaskan","America/Anchorage",-540,60,"2006",4,0,1,2,0,10,0,5,2,0),
array("Alaskan","America/Anchorage",-540,60,"2007",3,0,2,2,0,11,0,1,2,0),
array("Arab","Asia/Riyadh",180,0,"",0,0,0,0,0,0,0,0,0,0),
array("Arabian","Asia/Dubai",240,0,"",0,0,0,0,0,0,0,0,0,0),
array("Arabic","Asia/Baghdad",180,60,"",4,0,1,3,0,10,0,1,4,0),
array("AST/ADT","America/Halifax",-240,60,"",3,0,2,2,0,11,0,1,2,0),
array("AST/ADT","America/Halifax",-240,60,"2006",4,0,1,2,0,10,0,5,2,0),
array("AST/ADT","America/Halifax",-240,60,"2007",3,0,2,2,0,11,0,1,2,0),
array("HNA/HAA","America/Halifax",-240,60,"",3,0,2,2,0,11,0,1,2,0),
array("HNA/HAA","America/Halifax",-240,60,"2006",4,0,1,2,0,10,0,5,2,0),
array("HNA/HAA","America/Halifax",-240,60,"2007",3,0,2,2,0,11,0,1,2,0),
array("Atlantic","America/Halifax",-240,60,"",3,0,2,2,0,11,0,1,2,0),
array("Atlantic","America/Halifax",-240,60,"2006",4,0,1,2,0,10,0,5,2,0),
array("Atlantic","America/Halifax",-240,60,"2007",3,0,2,2,0,11,0,1,2,0),
array("AUS_Central","Australia/Darwin",570,0,"",0,0,0,0,0,0,0,0,0,0),
array("AUS_Eastern","Australia/Sydney",600,60,"",10,0,5,2,0,3,0,5,3,0),
array("Azerbaijan","Asia/Baku",240,60,"",3,0,5,4,0,10,0,5,5,0),
array("Azores","Atlantic/Azores",-60,60,"",3,0,5,2,0,10,0,5,3,0),
array("Canada_Central","America/Regina",-360,0,"",0,0,0,0,0,0,0,0,0,0),
array("Cape_Verde","Atlantic/Cape_Verde",-60,0,"",0,0,0,0,0,0,0,0,0,0),
array("Caucasus","Asia/Tbilisi",240,60,"",3,0,5,2,0,10,0,5,3,0),
array("ACST/ACDT","Australia/Adelaide",570,60,"",10,0,5,2,0,3,0,5,3,0),
array("Central_Australia","Australia/Adelaide",570,60,"",10,0,5,2,0,3,0,5,3,0),
array("Central_America","America/Guatemala",-360,0,"",0,0,0,0,0,0,0,0,0,0),
array("Central_Asia","Asia/Dhaka",360,0,"",0,0,0,0,0,0,0,0,0,0),
array("Central_Brazilian","America/Manaus",-240,60,"",11,0,1,0,0,2,0,5,0,0),
array("Central_Brazilian","America/Manaus",-240,60,"2006",11,0,1,0,0,2,0,2,2,0),
array("Central_Brazilian","America/Manaus",-240,60,"2007",11,0,1,0,0,2,0,5,0,0),
array("CET/CEST","Europe/Zurich",60,60,"",3,0,5,2,0,10,0,5,3,0),
array("CET/CEST","Europe/Vienna",60,60,"",3,0,5,2,0,10,0,5,3,0),
array("MEZ/MESZ","Europe/Berlin",60,60,"",3,0,5,2,0,10,0,5,3,0),
array("Central_Europe","Europe/Budapest",60,60,"",3,0,5,2,0,10,0,5,3,0),
array("Central_European","Europe/Warsaw",60,60,"",3,0,5,2,0,10,0,5,3,0),
array("Central_Pacific","Pacific/Guadalcanal",660,0,"",0,0,0,0,0,0,0,0,0,0),
array("CST/CDT","America/Chicago",-360,60,"",3,0,2,2,0,11,0,1,2,0),
array("CST/CDT","America/Chicago",-360,60,"2006",4,0,1,2,0,10,0,5,2,0),
array("CST/CDT","America/Chicago",-360,60,"2007",3,0,2,2,0,11,0,1,2,0),
array("HNC/HAC","America/Chicago",-360,60,"",3,0,2,2,0,11,0,1,2,0),
array("HNC/HAC","America/Chicago",-360,60,"2006",4,0,1,2,0,10,0,5,2,0),
array("HNC/HAC","America/Chicago",-360,60,"2007",3,0,2,2,0,11,0,1,2,0),
array("Central","America/Chicago",-360,60,"",3,0,2,2,0,11,0,1,2,0),
array("Central","America/Chicago",-360,60,"2006",4,0,1,2,0,10,0,5,2,0),
array("Central","America/Chicago",-360,60,"2007",3,0,2,2,0,11,0,1,2,0),
array("Central_Mexico","America/Mexico_City",-360,60,"",4,0,1,2,0,10,0,5,2,0),
array("China","Asia/Shanghai",480,0,"",0,0,0,0,0,0,0,0,0,0),
array("Dateline","Etc/GMT+12",-720,0,"",0,0,0,0,0,0,0,0,0,0),
array("East_Africa","Africa/Nairobi",180,0,"",0,0,0,0,0,0,0,0,0,0),
array("AEST/AEDT","Australia/Brisbane",600,0,"",0,0,0,0,0,0,0,0,0,0),
array("East_Australia","Australia/Brisbane",600,0,"",0,0,0,0,0,0,0,0,0,0),
array("EET/EEST","Europe/Minsk",120,60,"",3,0,5,2,0,10,0,5,3,0),
array("EET/EEST","Europe/Helsinki",120,60,"",3,0,5,2,0,10,0,5,3,0),
array("East_Europe","Europe/Minsk",120,60,"",3,0,5,2,0,10,0,5,3,0),
array("East_South_America","America/Sao_Paulo",-180,60,"",11,0,1,0,0,2,0,5,0,0),
array("East_South_America","America/Sao_Paulo",-180,60,"2006",11,0,1,0,0,2,0,2,2,0),
array("East_South_America","America/Sao_Paulo",-180,60,"2007",11,0,1,0,0,2,0,5,0,0),
array("EST/EDT","America/New_York",-300,60,"",3,0,2,2,0,11,0,1,2,0),
array("EST/EDT","America/New_York",-300,60,"2006",4,0,1,2,0,10,0,5,2,0),
array("EST/EDT","America/New_York",-300,60,"2007",3,0,2,2,0,11,0,1,2,0),
array("HNE/HAE","America/New_York",-300,60,"",3,0,2,2,0,11,0,1,2,0),
array("HNE/HAE","America/New_York",-300,60,"2006",4,0,1,2,0,10,0,5,2,0),
array("HNE/HAE","America/New_York",-300,60,"2007",3,0,2,2,0,11,0,1,2,0),
array("Eastern","America/New_York",-300,60,"",3,0,2,2,0,11,0,1,2,0),
array("Eastern","America/New_York",-300,60,"2006",4,0,1,2,0,10,0,5,2,0),
array("Eastern","America/New_York",-300,60,"2007",3,0,2,2,0,11,0,1,2,0),
array("Egypt","Africa/Cairo",120,60,"",4,4,5,23,59,9,4,5,23,59),
array("Ekaterinburg","Asia/Yekaterinburg",300,60,"",3,0,5,2,0,10,0,5,3,0),
array("Fiji","Pacific/Fiji",720,0,"",0,0,0,0,0,0,0,0,0,0),
array("FLE","Europe/Kiev",120,60,"",3,0,5,3,0,10,0,5,4,0),
array("Georgian","Etc/GMT-3",180,0,"",0,0,0,0,0,0,0,0,0,0),
array("GMT","Europe/London",0,60,"",3,0,5,1,0,10,0,5,2,0),
array("Greenland","America/Godthab",-180,60,"",4,0,1,2,0,10,0,5,2,0),
array("Greenwich","Africa/Casablanca",0,0,"",0,0,0,0,0,0,0,0,0,0),
array("GTB","Europe/Istanbul",120,60,"",3,0,5,3,0,10,0,5,4,0),
array("HAST/HADT","Pacific/Honolulu",-600,0,"",0,0,0,0,0,0,0,0,0,0),
array("Hawaiian","Pacific/Honolulu",-600,0,"",0,0,0,0,0,0,0,0,0,0),
array("India","Asia/Calcutta",330,0,"",0,0,0,0,0,0,0,0,0,0),
array("Iran","Asia/Tehran",210,0,"",0,0,0,0,0,0,0,0,0,0),
array("Iran","Asia/Tehran",210,60,"2005",3,0,1,2,0,9,2,4,2,0),
array("Iran","Asia/Tehran",210,0,"2006",0,0,0,0,0,0,0,0,0,0),
array("Israel","Asia/Jerusalem",120,60,"",3,5,5,2,0,9,0,3,2,0),
array("Israel","Asia/Jerusalem",120,0,"2004",0,0,0,0,0,0,0,0,0,0),
array("Israel","Asia/Jerusalem",120,60,"2005",4,-1,1,2,0,10,-1,9,2,0),
array("Israel","Asia/Jerusalem",120,60,"2006",3,-1,31,2,0,10,-1,1,2,0),
array("Israel","Asia/Jerusalem",120,60,"2007",3,-1,30,2,0,9,-1,16,2,0),
array("Israel","Asia/Jerusalem",120,60,"2008",3,-1,28,2,0,10,-1,5,2,0),
array("Israel","Asia/Jerusalem",120,60,"2009",3,-1,27,2,0,9,-1,27,2,0),
array("Israel","Asia/Jerusalem",120,60,"2010",3,-1,26,2,0,9,-1,12,2,0),
array("Israel","Asia/Jerusalem",120,60,"2011",4,-1,1,2,0,10,-1,2,2,0),
array("Israel","Asia/Jerusalem",120,60,"2012",3,-1,30,2,0,9,-1,23,2,0),
array("Israel","Asia/Jerusalem",120,60,"2013",3,-1,29,2,0,9,-1,8,2,0),
array("Israel","Asia/Jerusalem",120,60,"2014",3,-1,28,2,0,9,-1,28,2,0),
array("Israel","Asia/Jerusalem",120,60,"2015",3,-1,27,2,0,9,-1,20,2,0),
array("Israel","Asia/Jerusalem",120,60,"2016",4,-1,1,2,0,10,-1,9,2,0),
array("Israel","Asia/Jerusalem",120,60,"2017",3,-1,31,2,0,9,-1,24,2,0),
array("Israel","Asia/Jerusalem",120,60,"2018",3,-1,30,2,0,9,-1,16,2,0),
array("Israel","Asia/Jerusalem",120,60,"2019",3,-1,29,2,0,10,-1,6,2,0),
array("Israel","Asia/Jerusalem",120,60,"2020",3,-1,27,2,0,9,-1,27,2,0),
array("Israel","Asia/Jerusalem",120,60,"2021",3,-1,26,2,0,9,-1,12,2,0),
array("Israel","Asia/Jerusalem",120,60,"2022",4,-1,1,2,0,10,-1,2,2,0),
array("Israel","Asia/Jerusalem",120,0,"2023",0,0,0,0,0,0,0,0,0,0),
array("Jordan","Asia/Amman",120,60,"",3,4,5,0,0,9,5,5,1,0),
array("Korea","Asia/Seoul",540,0,"",0,0,0,0,0,0,0,0,0,0),
array("Mexico","America/Mexico_City",-360,60,"",4,0,1,2,0,10,0,5,2,0),
array("Mexico_2","America/Chihuahua",-420,60,"",4,0,1,2,0,10,0,5,2,0),
array("Mid_Atlantic","Atlantic/South_Georgia",-120,60,"",3,0,5,2,0,9,0,5,2,0),
array("Middle_East","Asia/Beirut",120,60,"",3,0,5,0,0,10,6,5,23,59),
array("Montevideo","America/Montevideo",-180,60,"",10,0,1,2,0,3,0,2,2,0),
array("MST/MDT","America/Denver",-420,60,"",3,0,2,2,0,11,0,1,2,0),
array("MST/MDT","America/Denver",-420,60,"2006",4,0,1,2,0,10,0,5,2,0),
array("MST/MDT","America/Denver",-420,60,"2007",3,0,2,2,0,11,0,1,2,0),
array("HNR/HAR","America/Denver",-420,60,"",3,0,2,2,0,11,0,1,2,0),
array("HNR/HAR","America/Denver",-420,60,"2006",4,0,1,2,0,10,0,5,2,0),
array("HNR/HAR","America/Denver",-420,60,"2007",3,0,2,2,0,11,0,1,2,0),
array("Mountain","America/Denver",-420,60,"",3,0,2,2,0,11,0,1,2,0),
array("Mountain","America/Denver",-420,60,"2006",4,0,1,2,0,10,0,5,2,0),
array("Mountain","America/Denver",-420,60,"2007",3,0,2,2,0,11,0,1,2,0),
array("Mountain_Mexico","America/Chihuahua",-420,60,"",4,0,1,2,0,10,0,5,2,0),
array("Myanmar","Asia/Rangoon",390,0,"",0,0,0,0,0,0,0,0,0,0),
array("North_Central_Asia","Asia/Novosibirsk",360,60,"",3,0,5,2,0,10,0,5,3,0),
array("Namibia","Africa/Windhoek",120,-60,"",4,0,1,2,0,9,0,1,2,0),
array("Nepal","Asia/Katmandu",345,0,"",0,0,0,0,0,0,0,0,0,0),
array("New_Zealand","Pacific/Auckland",720,60,"",10,0,1,2,0,3,0,3,3,0),
array("NST/NDT","America/St_Johns",-210,60,"",3,0,2,0,1,11,0,1,0,1),
array("NST/NDT","America/St_Johns",-210,60,"2006",4,0,1,0,1,10,0,5,0,0),
array("NST/NDT","America/St_Johns",-210,60,"2007",3,0,2,0,1,11,0,1,0,0),
array("HNT/HAT","America/St_Johns",-210,60,"",3,0,2,0,1,11,0,1,0,1),
array("HNT/HAT","America/St_Johns",-210,60,"2006",4,0,1,0,1,10,0,5,0,0),
array("HNT/HAT","America/St_Johns",-210,60,"2007",3,0,2,0,1,11,0,1,0,0),
array("Newfoundland","America/St_Johns",-210,60,"",3,0,2,0,1,11,0,1,0,1),
array("Newfoundland","America/St_Johns",-210,60,"2006",4,0,1,0,1,10,0,5,0,0),
array("Newfoundland","America/St_Johns",-210,60,"2007",3,0,2,0,1,11,0,1,0,0),
array("North_Asia_East","Asia/Irkutsk",480,60,"",3,0,5,2,0,10,0,5,3,0),
array("North_Asia","Asia/Krasnoyarsk",420,60,"",3,0,5,2,0,10,0,5,3,0),
array("Pacific_SA","America/Santiago",-240,60,"",10,6,2,23,59,3,6,2,23,59),
array("PST/PDT","America/Los_Angeles",-480,60,"",3,0,2,2,0,11,0,1,2,0),
array("PST/PDT","America/Los_Angeles",-480,60,"2006",4,0,1,2,0,10,0,5,2,0),
array("PST/PDT","America/Los_Angeles",-480,60,"2007",3,0,2,2,0,11,0,1,2,0),
array("HNP/HAP","America/Los_Angeles",-480,60,"",3,0,2,2,0,11,0,1,2,0),
array("HNP/HAP","America/Los_Angeles",-480,60,"2006",4,0,1,2,0,10,0,5,2,0),
array("HNP/HAP","America/Los_Angeles",-480,60,"2007",3,0,2,2,0,11,0,1,2,0),
array("Pacific","America/Los_Angeles",-480,60,"",3,0,2,2,0,11,0,1,2,0),
array("Pacific","America/Los_Angeles",-480,60,"2006",4,0,1,2,0,10,0,5,2,0),
array("Pacific","America/Los_Angeles",-480,60,"2007",3,0,2,2,0,11,0,1,2,0),
array("Pacific_Mexico","America/Tijuana",-480,60,"",4,0,1,2,0,10,0,5,2,0),
array("Romance","Europe/Paris",60,60,"",3,0,5,2,0,10,0,5,3,0),
array("Russian","Europe/Moscow",180,60,"",3,0,5,2,0,10,0,5,3,0),
array("SA_Eastern","Etc/GMT+3",-180,0,"",0,0,0,0,0,0,0,0,0,0),
array("SA_Pacific","America/Bogota",-300,0,"",0,0,0,0,0,0,0,0,0,0),
array("SA_Western","America/La_Paz",-240,0,"",0,0,0,0,0,0,0,0,0,0),
array("Samoa","Pacific/Apia",-660,0,"",0,0,0,0,0,0,0,0,0,0),
array("SE_Asia","Asia/Bangkok",420,0,"",0,0,0,0,0,0,0,0,0,0),
array("Singapore","Asia/Singapore",480,0,"",0,0,0,0,0,0,0,0,0,0),
array("South_Africa","Africa/Johannesburg",120,0,"",0,0,0,0,0,0,0,0,0,0),
array("Sri_Lanka","Asia/Colombo",330,0,"",0,0,0,0,0,0,0,0,0,0),
array("Taipei","Asia/Taipei",480,0,"",0,0,0,0,0,0,0,0,0,0),
array("Tasmania","Australia/Hobart",600,60,"",10,0,1,2,0,3,0,5,3,0),
array("Tokyo","Asia/Tokyo",540,0,"",0,0,0,0,0,0,0,0,0,0),
array("Tonga","Pacific/Tongatapu",780,0,"",0,0,0,0,0,0,0,0,0,0),
array("US_Eastern","Etc/GMT+5",-300,0,"",0,0,0,0,0,0,0,0,0,0),
array("US_Mountain","America/Phoenix",-420,0,"",0,0,0,0,0,0,0,0,0,0),
array("Vladivostok","Asia/Vladivostok",600,60,"",3,0,5,2,0,10,0,5,3,0),
array("West_Australia","Australia/Perth",480,60,"",10,0,5,2,0,3,0,5,3,0),
array("West_Australia","Australia/Perth",480,0,"2005",0,0,0,0,0,0,0,0,0,0),
array("West_Australia","Australia/Perth",480,60,"2006",12,-1,1,2,0,1,-1,1,0,0),
array("West_Australia","Australia/Perth",480,60,"2007",10,0,5,2,0,3,0,5,3,0),
array("West_Central_Africa","Africa/Lagos",60,0,"",0,0,0,0,0,0,0,0,0,0),
array("WET/WEST","Europe/Berlin",60,60,"",3,0,5,2,0,10,0,5,3,0),
array("West_Europe","Europe/Berlin",60,60,"",3,0,5,2,0,10,0,5,3,0),
array("West_Asia","Asia/Karachi",300,0,"",0,0,0,0,0,0,0,0,0,0),
array("West_Pacific","Pacific/Port_Moresby",600,0,"",0,0,0,0,0,0,0,0,0,0),
array("Yakutsk","Asia/Yakutsk",540,60,"",3,0,5,2,0,10,0,5,3,0),
);
$serverTZ = date('e', $event['start']);
$year = date('Y', $event['start']);
$i = 0;
$row = false;
do
{
$tbl_tz_row =& $tbl_tz[$i];
if ($tbl_tz_row[1] == $serverTZ)
{
if ($tbl_tz_row[4] == '' || $year >= (int) $tbl_tz_row[4])
{
$row = $i;
}
elseif ($row !== false) break;
elseif ($tbl_tz_row[4] != '' && $year < (int) $tbl_tz_row[4])
{
// First DST starts at this year
$year = (int) $tbl_tz_row[4];
if (empty($event['recur_enddate'])
|| $year <= date('Y', $event['recur_enddate']))
{
$row = $i;
}
break;
}
}
elseif ($row !== false) break;
}
while (is_array($tbl_tz[++$i]));
if ($row === false) return $serverTZ; // UTC or unkown TZ
$tbl_tz_row =& $tbl_tz[$row];
if (preg_match('/(.+)\/(.+)/', $tbl_tz_row[0], $matches))
{
$stdname = $matches[1];
$dstname = $matches[2];
}
else
{
$stdname = $dstname = $tbl_tz_row[0];
}
$container = false;
$vtimezone = Horde_iCalendar::newComponent('VTIMEZONE', $container);
$vtimezone->setAttribute('TZID', $tbl_tz_row[1]);
do {
if (is_array($tbl_tz[++$row]))
{
$tbl_next_row =& $tbl_tz[$row];
if ($tbl_next_row[1] != $serverTZ) $tbl_next_row = false;
}
else $tbl_next_row = false;
$minutes = $tbl_tz_row[2] + $tbl_tz_row[3];
$value1['ahead'] = ($minutes >= 0);
$minutes = abs($minutes);
$value1['hour'] = (int)$minutes / 60;
$value1['minute'] = $minutes % 60;
$minutes = $tbl_tz_row[2];
$value2['ahead'] = ($minutes > 0);
$minutes = abs($minutes);
$value2['hour'] = (int)$minutes / 60;
$value2['minute'] = $minutes % 60;
$daylight = Horde_iCalendar::newComponent('DAYLIGHT', $container);
$dtstart = self::calc_dtstart($year, $tbl_tz_row[5],
2009-11-16 09:04:18 +01:00
$tbl_tz_row[6], $tbl_tz_row[7], $tbl_tz_row[8], $tbl_tz_row[9]);
$daylight->setAttribute('DTSTART', $dtstart);
$byday = ($tbl_tz_row[7] == 5 ? '-1' : $tbl_tz_row[7]) . $dayofweek[$tbl_tz_row[6]];
$rrule = array('FREQ' => 'YEARLY', 'BYMONTH' => $tbl_tz_row[5], 'BYDAY' => $byday);
if ($tbl_next_row)
{
$rrule['UNTIL'] = $tbl_next_row[4] . '0101T000000Z';
}
$daylight->setAttribute('RRULE', '', $rrule);
$daylight->setAttribute('TZNAME', $dstname);
$daylight->setAttribute('TZOFFSETFROM', $value2);
$daylight->setAttribute('TZOFFSETTO', $value1);
$standard = Horde_iCalendar::newComponent('STANDARD', $container);
$dtstart = self::calc_dtstart($year, $tbl_tz_row[10], $tbl_tz_row[11],
2009-11-16 09:04:18 +01:00
$tbl_tz_row[12], $tbl_tz_row[13], $tbl_tz_row[14]);
$standard->setAttribute('DTSTART', $dtstart);
$byday = ($tbl_tz_row[12] == 5 ? '-1' : $tbl_tz_row[12]) . $dayofweek[$tbl_tz_row[11]];
$rrule['BYMONTH'] = $tbl_tz_row[10];
$rrule['BYDAY'] = $byday;
$standard->setAttribute('RRULE', '', $rrule);
$standard->setAttribute('TZNAME', $stdname);
$standard->setAttribute('TZOFFSETFROM', $value1);
$standard->setAttribute('TZOFFSETTO', $value2);
$vtimezone->addComponent($daylight);
$vtimezone->addComponent($standard);
if ($tbl_next_row)
{
$tbl_tz_row = $tbl_next_row;
$year = (int) $tbl_tz_row[4];
}
} while ($tbl_next_row && (empty($event['recur_enddate']) ||
$year <= date('Y', $event['recur_enddate'])));
$vcal->addComponent($vtimezone);
return $serverTZ;
}
/**
* calculate the DTSTART value for a given timezone switch occurrence
*
* @param int $wYear
* @param int $wMonth
* @param int $wDayOfWeek
* @param int $wNth n-th day of week (5 last occurrence)
* @param int $wHour
* @param int $wMinute
* @return string DTSTART entry
*/
static function calc_dtstart($wYear, $wMonth, $wDayOfWeek, $wNth, $wHour, $wMinute)
2009-11-16 09:04:18 +01:00
{
if (!$wYear) $wYear = 1981;
if ($wNth < 5)
{
$ts = mktime($wHour, $wMinute, 0, $wMonth, 1, $wYear);
$day = $wDayOfWeek - date('w', $ts);
if ($day < 0) $day += 7;
$day += 7 * ($wNth - 1) + 1;
$ts = mktime($wHour, $wMinute, 0, $wMonth, $day, $wYear);
}
else
{
$ts = mktime($wHour, $wMinute, 0, $wMonth, 31, $wYear);
$day = $wDayOfWeek - date('w', $ts);
if ($day > 0) $day -= 7;
$day += 31;
do
{
$ts = mktime($wHour, $wMinute, 0, $wMonth, $day, $wYear);
$day -= 7;
} while ($wMonth < date('n', $ts));
}
$dtstart = date('Ymd\T', $ts) . sprintf("%'02u%'02u00", $wHour, $wMinute);
return $dtstart;
}
2008-06-07 19:45:33 +02:00
}