2012-06-03 14:19:28 -04:00
|
|
|
<?php
|
|
|
|
|
2012-07-08 13:12:58 -04:00
|
|
|
abstract class Sabre_CalDAV_Backend_Common extends Sabre_CalDAV_Backend_Abstract
|
|
|
|
{
|
2012-06-03 14:19:28 -04:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2012-07-08 13:12:58 -04:00
|
|
|
protected $propertyMap = array(
|
|
|
|
'{DAV:}displayname' => 'displayname',
|
2012-06-03 14:19:28 -04:00
|
|
|
'{urn:ietf:params:xml:ns:caldav}calendar-description' => 'description',
|
|
|
|
'{urn:ietf:params:xml:ns:caldav}calendar-timezone' => 'timezone',
|
2012-07-08 13:12:58 -04:00
|
|
|
'{http://apple.com/ns/ical/}calendar-order' => 'calendarorder',
|
|
|
|
'{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor',
|
2012-06-03 14:19:28 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
|
2012-07-08 13:12:58 -04:00
|
|
|
/**
|
|
|
|
* @abstract
|
|
|
|
* @return int
|
|
|
|
*/
|
2012-06-03 14:19:28 -04:00
|
|
|
abstract public function getNamespace();
|
2012-07-08 13:12:58 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $calendarId
|
|
|
|
* @param string $sd
|
|
|
|
* @param string $ed
|
|
|
|
* @param string $base_path
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
abstract public function listItemsByRange($calendarId, $sd, $ed, $base_path);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
static private $calendarCache = array();
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
/**
|
2012-07-08 13:12:58 -04:00
|
|
|
* @var array
|
2012-06-03 14:19:28 -04:00
|
|
|
*/
|
2012-07-08 13:12:58 -04:00
|
|
|
static private $calendarObjectCache = array();
|
2012-06-03 14:19:28 -04:00
|
|
|
|
2012-07-08 13:12:58 -04:00
|
|
|
/**
|
|
|
|
* @static
|
|
|
|
* @param int $calendarId
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
static public function loadCalendarById($calendarId)
|
|
|
|
{
|
|
|
|
if (!isset(self::$calendarCache[$calendarId])) {
|
|
|
|
$c = q("SELECT * FROM %s%scalendars WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendarId));
|
|
|
|
self::$calendarCache[$calendarId] = $c[0];
|
|
|
|
}
|
|
|
|
return self::$calendarCache[$calendarId];
|
2012-06-03 14:19:28 -04:00
|
|
|
}
|
|
|
|
|
2012-07-08 13:12:58 -04:00
|
|
|
/**
|
|
|
|
* @static
|
|
|
|
* @param int $obj_id
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
static public function loadCalendarobjectById($obj_id)
|
|
|
|
{
|
|
|
|
if (!isset(self::$calendarObjectCache[$obj_id])) {
|
|
|
|
$o = q("SELECT * FROM %s%scalendarobjects WHERE `id` = %d",
|
|
|
|
CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($obj_id)
|
|
|
|
);
|
|
|
|
self::$calendarObjectCache[$obj_id] = $o[0];
|
|
|
|
}
|
|
|
|
return self::$calendarObjectCache[$obj_id];
|
|
|
|
}
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2012-07-08 13:12:58 -04:00
|
|
|
* @static
|
|
|
|
* @param Sabre_VObject_Component_VEvent $component
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public static function getDtEndTimeStamp(&$component)
|
|
|
|
{
|
|
|
|
/** @var Sabre_VObject_Property_DateTime $dtstart */
|
|
|
|
$dtstart = $component->__get("DTSTART");
|
|
|
|
if ($component->__get("DTEND")) {
|
|
|
|
/** @var Sabre_VObject_Property_DateTime $dtend */
|
|
|
|
$dtend = $component->__get("DTEND");
|
|
|
|
return $dtend->getDateTime()->getTimeStamp();
|
|
|
|
} elseif ($component->__get("DURATION")) {
|
|
|
|
$endDate = clone $dtstart->getDateTime();
|
|
|
|
$endDate->add(Sabre_VObject_DateTimeParser::parse($component->__get("DURATION")->value));
|
|
|
|
return $endDate->getTimeStamp();
|
|
|
|
} elseif ($dtstart->getDateType() === Sabre_VObject_Property_DateTime::DATE) {
|
|
|
|
$endDate = clone $dtstart->getDateTime();
|
|
|
|
$endDate->modify('+1 day');
|
|
|
|
return $endDate->getTimeStamp();
|
|
|
|
} else {
|
|
|
|
return $dtstart->getDateTime()->getTimeStamp() + 3600;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses some information from calendar objects, used for optimized
|
|
|
|
* calendar-queries.
|
2012-06-03 14:19:28 -04:00
|
|
|
*
|
2012-07-08 13:12:58 -04:00
|
|
|
* Returns an array with the following keys:
|
|
|
|
* * etag
|
|
|
|
* * size
|
|
|
|
* * componentType
|
|
|
|
* * firstOccurence
|
|
|
|
* * lastOccurence
|
2012-06-03 14:19:28 -04:00
|
|
|
*
|
2012-07-08 13:12:58 -04:00
|
|
|
* @param string $calendarData
|
|
|
|
* @throws Sabre_DAV_Exception_BadRequest
|
2012-06-03 14:19:28 -04:00
|
|
|
* @return array
|
|
|
|
*/
|
2012-07-08 13:12:58 -04:00
|
|
|
protected function getDenormalizedData($calendarData)
|
2012-06-03 14:19:28 -04:00
|
|
|
{
|
2012-07-08 13:12:58 -04:00
|
|
|
/** @var Sabre_VObject_Component_VEvent $vObject */
|
|
|
|
$vObject = Sabre_VObject_Reader::read($calendarData);
|
|
|
|
$componentType = null;
|
|
|
|
$component = null;
|
|
|
|
$firstOccurence = null;
|
|
|
|
$lastOccurence = null;
|
|
|
|
foreach ($vObject->getComponents() as $component) {
|
|
|
|
if ($component->name !== 'VTIMEZONE') {
|
|
|
|
$componentType = $component->name;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$componentType) {
|
|
|
|
throw new Sabre_DAV_Exception_BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component');
|
|
|
|
}
|
|
|
|
if ($componentType === 'VEVENT') {
|
|
|
|
/** @var Sabre_VObject_Component_VEvent $component */
|
|
|
|
/** @var Sabre_VObject_Property_DateTime $dtstart */
|
|
|
|
$dtstart = $component->__get("DTSTART");
|
|
|
|
$firstOccurence = $dtstart->getDateTime()->getTimeStamp();
|
|
|
|
// Finding the last occurence is a bit harder
|
|
|
|
if (!$component->__get("RRULE")) {
|
|
|
|
$lastOccurence = self::getDtEndTimeStamp($component);
|
|
|
|
} else {
|
|
|
|
$it = new Sabre_VObject_RecurrenceIterator($vObject, (string)$component->__get("UID"));
|
|
|
|
$maxDate = new DateTime(CALDAV_MAX_YEAR . "-01-01");
|
|
|
|
if ($it->isInfinite()) {
|
|
|
|
$lastOccurence = $maxDate->getTimeStamp();
|
|
|
|
} else {
|
|
|
|
$end = $it->getDtEnd();
|
|
|
|
while ($it->valid() && $end < $maxDate) {
|
|
|
|
$end = $it->getDtEnd();
|
|
|
|
$it->next();
|
|
|
|
|
|
|
|
}
|
|
|
|
$lastOccurence = $end->getTimeStamp();
|
|
|
|
}
|
2012-06-03 14:19:28 -04:00
|
|
|
|
2012-07-08 13:12:58 -04:00
|
|
|
}
|
2012-06-03 14:19:28 -04:00
|
|
|
}
|
|
|
|
|
2012-07-08 13:12:58 -04:00
|
|
|
return array(
|
|
|
|
'etag' => md5($calendarData),
|
|
|
|
'size' => strlen($calendarData),
|
|
|
|
'componentType' => $componentType,
|
|
|
|
'firstOccurence' => $firstOccurence,
|
|
|
|
'lastOccurence' => $lastOccurence,
|
|
|
|
);
|
|
|
|
|
2012-06-03 14:19:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates properties for a calendar.
|
|
|
|
*
|
|
|
|
* The mutations array uses the propertyName in clark-notation as key,
|
|
|
|
* and the array value for the property value. In the case a property
|
|
|
|
* should be deleted, the property value will be null.
|
|
|
|
*
|
|
|
|
* This method must be atomic. If one property cannot be changed, the
|
|
|
|
* entire operation must fail.
|
|
|
|
*
|
|
|
|
* If the operation was successful, true can be returned.
|
|
|
|
* If the operation failed, false can be returned.
|
|
|
|
*
|
|
|
|
* Deletion of a non-existent property is always successful.
|
|
|
|
*
|
|
|
|
* Lastly, it is optional to return detailed information about any
|
|
|
|
* failures. In this case an array should be returned with the following
|
|
|
|
* structure:
|
|
|
|
*
|
|
|
|
* array(
|
|
|
|
* 403 => array(
|
|
|
|
* '{DAV:}displayname' => null,
|
|
|
|
* ),
|
|
|
|
* 424 => array(
|
|
|
|
* '{DAV:}owner' => null,
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* In this example it was forbidden to update {DAV:}displayname.
|
|
|
|
* (403 Forbidden), which in turn also caused {DAV:}owner to fail
|
|
|
|
* (424 Failed Dependency) because the request needs to be atomic.
|
|
|
|
*
|
|
|
|
* @param mixed $calendarId
|
|
|
|
* @param array $mutations
|
|
|
|
* @return bool|array
|
|
|
|
*/
|
2012-07-08 13:12:58 -04:00
|
|
|
public function updateCalendar($calendarId, array $mutations)
|
|
|
|
{
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
$newValues = array();
|
2012-07-08 13:12:58 -04:00
|
|
|
$result = array(
|
2012-06-03 14:19:28 -04:00
|
|
|
200 => array(), // Ok
|
|
|
|
403 => array(), // Forbidden
|
|
|
|
424 => array(), // Failed Dependency
|
|
|
|
);
|
|
|
|
|
|
|
|
$hasError = false;
|
|
|
|
|
2012-07-08 13:12:58 -04:00
|
|
|
foreach ($mutations as $propertyName=> $propertyValue) {
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
// We don't know about this property.
|
|
|
|
if (!isset($this->propertyMap[$propertyName])) {
|
2012-07-08 13:12:58 -04:00
|
|
|
$hasError = true;
|
2012-06-03 14:19:28 -04:00
|
|
|
$result[403][$propertyName] = null;
|
|
|
|
unset($mutations[$propertyName]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-07-08 13:12:58 -04:00
|
|
|
$fieldName = $this->propertyMap[$propertyName];
|
2012-06-03 14:19:28 -04:00
|
|
|
$newValues[$fieldName] = $propertyValue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there were any errors we need to fail the request
|
|
|
|
if ($hasError) {
|
|
|
|
// Properties has the remaining properties
|
2012-07-08 13:12:58 -04:00
|
|
|
foreach ($mutations as $propertyName=> $propertyValue) {
|
2012-06-03 14:19:28 -04:00
|
|
|
$result[424][$propertyName] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removing unused statuscodes for cleanliness
|
2012-07-08 13:12:58 -04:00
|
|
|
foreach ($result as $status=> $properties) {
|
|
|
|
if (is_array($properties) && count($properties) === 0) unset($result[$status]);
|
2012-06-03 14:19:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-07-08 13:12:58 -04:00
|
|
|
$this->increaseCalendarCtag($calendarId);
|
2012-06-03 14:19:28 -04:00
|
|
|
|
|
|
|
$valuesSql = array();
|
2012-07-08 13:12:58 -04:00
|
|
|
foreach ($newValues as $fieldName=> $value) $valuesSql[] = "`" . $fieldName . "` = '" . dbesc($value) . "'";
|
2012-06-03 14:19:28 -04:00
|
|
|
if (count($valuesSql) > 0) {
|
2012-07-08 13:12:58 -04:00
|
|
|
q("UPDATE %s%scalendars SET " . implode(", ", $valuesSql) . " WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendarId));
|
2012-06-03 14:19:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-07-08 13:12:58 -04:00
|
|
|
/**
|
|
|
|
* @param int $calendarId
|
|
|
|
*/
|
|
|
|
protected function increaseCalendarCtag($calendarId)
|
|
|
|
{
|
|
|
|
q("UPDATE %s%scalendars SET `ctag` = `ctag` + 1 WHERE `id` = '%d'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendarId));
|
|
|
|
self::$calendarObjectCache = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @abstract
|
|
|
|
* @param int $calendar_id
|
|
|
|
* @param int $calendarobject_id
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
abstract function getItemDetailRedirect($calendar_id, $calendarobject_id);
|
|
|
|
|
2012-06-03 14:19:28 -04:00
|
|
|
}
|