Exemplo n.º 1
0
 /**
  * Returns true or false depending on if the event falls in the specified
  * time-range. This is used for filtering purposes.
  *
  * The rules used to determine if an event falls within the specified
  * time-range is based on the CalDAV specification.
  *
  * @param \DateTime $start
  * @param \DateTime $end
  * @return bool
  */
 public function isInTimeRange(\DateTime $start, \DateTime $end)
 {
     if ($this->RRULE) {
         $it = new VObject\RecurrenceIterator($this);
         $it->fastForward($start);
         // We fast-forwarded to a spot where the end-time of the
         // recurrence instance exceeded the start of the requested
         // time-range.
         //
         // If the starttime of the recurrence did not exceed the
         // end of the time range as well, we have a match.
         return $it->getDTStart() < $end && $it->getDTEnd() > $start;
     }
     $effectiveStart = $this->DTSTART->getDateTime();
     if (isset($this->DTEND)) {
         // The DTEND property is considered non inclusive. So for a 3 day
         // event in july, dtstart and dtend would have to be July 1st and
         // July 4th respectively.
         //
         // See:
         // http://tools.ietf.org/html/rfc5545#page-54
         $effectiveEnd = $this->DTEND->getDateTime();
     } elseif (isset($this->DURATION)) {
         $effectiveEnd = clone $effectiveStart;
         $effectiveEnd->add(VObject\DateTimeParser::parseDuration($this->DURATION));
     } elseif (!$this->DTSTART->hasTime()) {
         $effectiveEnd = clone $effectiveStart;
         $effectiveEnd->modify('+1 day');
     } else {
         $effectiveEnd = clone $effectiveStart;
     }
     return $start <= $effectiveEnd && $end > $effectiveStart;
 }
Exemplo n.º 2
0
 /**
  * Checks based on the contained FREEBUSY information, if a timeslot is
  * available.
  *
  * @param DateTime $start
  * @param Datetime $end
  * @return bool
  */
 public function isFree(\DateTime $start, \Datetime $end)
 {
     foreach ($this->select('FREEBUSY') as $freebusy) {
         // We are only interested in FBTYPE=BUSY (the default),
         // FBTYPE=BUSY-TENTATIVE or FBTYPE=BUSY-UNAVAILABLE.
         if (isset($freebusy['FBTYPE']) && strtoupper(substr((string) $freebusy['FBTYPE'], 0, 4)) !== 'BUSY') {
             continue;
         }
         // The freebusy component can hold more than 1 value, separated by
         // commas.
         $periods = explode(',', (string) $freebusy);
         foreach ($periods as $period) {
             // Every period is formatted as [start]/[end]. The start is an
             // absolute UTC time, the end may be an absolute UTC time, or
             // duration (relative) value.
             list($busyStart, $busyEnd) = explode('/', $period);
             $busyStart = VObject\DateTimeParser::parse($busyStart);
             $busyEnd = VObject\DateTimeParser::parse($busyEnd);
             if ($busyEnd instanceof \DateInterval) {
                 $tmp = clone $busyStart;
                 $tmp->add($busyEnd);
                 $busyEnd = $tmp;
             }
             if ($start < $busyEnd && $end > $busyStart) {
                 return false;
             }
         }
     }
     return true;
 }
Exemplo n.º 3
0
 /**
  * Returns true or false depending on if the event falls in the specified
  * time-range. This is used for filtering purposes.
  *
  * The rules used to determine if an event falls within the specified
  * time-range is based on the CalDAV specification.
  *
  * @param DateTime $start
  * @param DateTime $end
  * @return bool
  */
 public function isInTimeRange(\DateTime $start, \DateTime $end)
 {
     $dtstart = isset($this->DTSTART) ? $this->DTSTART->getDateTime() : null;
     $duration = isset($this->DURATION) ? VObject\DateTimeParser::parseDuration($this->DURATION) : null;
     $due = isset($this->DUE) ? $this->DUE->getDateTime() : null;
     $completed = isset($this->COMPLETED) ? $this->COMPLETED->getDateTime() : null;
     $created = isset($this->CREATED) ? $this->CREATED->getDateTime() : null;
     if ($dtstart) {
         if ($duration) {
             $effectiveEnd = clone $dtstart;
             $effectiveEnd->add($duration);
             return $start <= $effectiveEnd && $end > $dtstart;
         } elseif ($due) {
             return ($start < $due || $start <= $dtstart) && ($end > $dtstart || $end >= $due);
         } else {
             return $start <= $dtstart && $end > $dtstart;
         }
     }
     if ($due) {
         return $start < $due && $end >= $due;
     }
     if ($completed && $created) {
         return ($start <= $created || $start <= $completed) && ($end >= $created || $end >= $completed);
     }
     if ($completed) {
         return $start <= $completed && $end >= $completed;
     }
     if ($created) {
         return $end > $created;
     }
     return true;
 }
Exemplo n.º 4
0
 /**
  * Returns the value, in the format it should be encoded for json.
  *
  * This method must always return an array.
  *
  * @return array
  */
 public function getJsonValue()
 {
     $parts = DateTimeParser::parseVCardDateTime($this->getValue());
     $dateStr = $parts['year'] . '-' . $parts['month'] . '-' . $parts['date'] . 'T' . $parts['hour'] . ':' . $parts['minute'] . ':' . $parts['second'];
     // Timezone
     if (!is_null($parts['timezone'])) {
         $dateStr .= $parts['timezone'];
     }
     return array($dateStr);
 }
Exemplo n.º 5
0
 /**
  * Returns true or false depending on if the event falls in the specified
  * time-range. This is used for filtering purposes.
  *
  * The rules used to determine if an event falls within the specified
  * time-range is based on the CalDAV specification.
  *
  * @param \DateTime $start
  * @param \DateTime $end
  * @return bool
  */
 public function isInTimeRange(\DateTime $start, \DateTime $end)
 {
     $effectiveTrigger = $this->getEffectiveTriggerTime();
     if (isset($this->DURATION)) {
         $duration = VObject\DateTimeParser::parseDuration($this->DURATION);
         $repeat = (string) $this->repeat;
         if (!$repeat) {
             $repeat = 1;
         }
         $period = new \DatePeriod($effectiveTrigger, $duration, (int) $repeat);
         foreach ($period as $occurrence) {
             if ($start <= $occurrence && $end > $occurrence) {
                 return true;
             }
         }
         return false;
     } else {
         return $start <= $effectiveTrigger && $end > $effectiveTrigger;
     }
 }
Exemplo n.º 6
0
 /**
  * Returns the value, in the format it should be encoded for json.
  *
  * This method must always return an array.
  *
  * @return array
  */
 public function getJsonValue()
 {
     $parts = DateTimeParser::parseVCardTime($this->getValue());
     $timeStr = '';
     // Hour
     if (!is_null($parts['hour'])) {
         $timeStr .= $parts['hour'];
         if (!is_null($parts['minute'])) {
             $timeStr .= ':';
         }
     } else {
         // We know either minute or second _must_ be set, so we insert a
         // dash for an empty value.
         $timeStr .= '-';
     }
     // Minute
     if (!is_null($parts['minute'])) {
         $timeStr .= $parts['minute'];
         if (!is_null($parts['second'])) {
             $timeStr .= ':';
         }
     } else {
         if (isset($parts['second'])) {
             // Dash for empty minute
             $timeStr .= '-';
         }
     }
     // Second
     if (!is_null($parts['second'])) {
         $timeStr .= $parts['second'];
     }
     // Timezone
     if (!is_null($parts['timezone'])) {
         $timeStr .= $parts['timezone'];
     }
     return array($timeStr);
 }
Exemplo n.º 7
0
 /**
  * Parses some information from calendar objects, used for optimized
  * calendar-queries.
  *
  * Returns an array with the following keys:
  *   * etag
  *   * size
  *   * componentType
  *   * firstOccurence
  *   * lastOccurence
  *
  * @param string $calendarData
  * @return array
  */
 protected function getDenormalizedData($calendarData)
 {
     $vObject = 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 \SabreForRainLoop\DAV\Exception\BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component');
     }
     if ($componentType === 'VEVENT') {
         $firstOccurence = $component->DTSTART->getDateTime()->getTimeStamp();
         // Finding the last occurence is a bit harder
         if (!isset($component->RRULE)) {
             if (isset($component->DTEND)) {
                 $lastOccurence = $component->DTEND->getDateTime()->getTimeStamp();
             } elseif (isset($component->DURATION)) {
                 $endDate = clone $component->DTSTART->getDateTime();
                 $endDate->add(VObject\DateTimeParser::parse($component->DURATION->getValue()));
                 $lastOccurence = $endDate->getTimeStamp();
             } elseif (!$component->DTSTART->hasTime()) {
                 $endDate = clone $component->DTSTART->getDateTime();
                 $endDate->modify('+1 day');
                 $lastOccurence = $endDate->getTimeStamp();
             } else {
                 $lastOccurence = $firstOccurence;
             }
         } else {
             $it = new VObject\RecurrenceIterator($vObject, (string) $component->UID);
             $maxDate = new \DateTime(self::MAX_DATE);
             if ($it->isInfinite()) {
                 $lastOccurence = $maxDate->getTimeStamp();
             } else {
                 $end = $it->getDtEnd();
                 while ($it->valid() && $end < $maxDate) {
                     $end = $it->getDtEnd();
                     $it->next();
                 }
                 $lastOccurence = $end->getTimeStamp();
             }
         }
     }
     return array('etag' => md5($calendarData), 'size' => strlen($calendarData), 'componentType' => $componentType, 'firstOccurence' => $firstOccurence, 'lastOccurence' => $lastOccurence);
 }
Exemplo n.º 8
0
 /**
  * This method is responsible for parsing the request and generating the
  * response for the CALDAV:free-busy-query REPORT.
  *
  * @param \DOMNode $dom
  * @return void
  */
 protected function freeBusyQueryReport(\DOMNode $dom)
 {
     $start = null;
     $end = null;
     foreach ($dom->firstChild->childNodes as $childNode) {
         $clark = DAV\XMLUtil::toClarkNotation($childNode);
         if ($clark == '{' . self::NS_CALDAV . '}time-range') {
             $start = $childNode->getAttribute('start');
             $end = $childNode->getAttribute('end');
             break;
         }
     }
     if ($start) {
         $start = VObject\DateTimeParser::parseDateTime($start);
     }
     if ($end) {
         $end = VObject\DateTimeParser::parseDateTime($end);
     }
     if (!$start && !$end) {
         throw new DAV\Exception\BadRequest('The freebusy report must have a time-range filter');
     }
     $acl = $this->server->getPlugin('acl');
     if (!$acl) {
         throw new DAV\Exception('The ACL plugin must be loaded for free-busy queries to work');
     }
     $uri = $this->server->getRequestUri();
     $acl->checkPrivileges($uri, '{' . self::NS_CALDAV . '}read-free-busy');
     $calendar = $this->server->tree->getNodeForPath($uri);
     if (!$calendar instanceof ICalendar) {
         throw new DAV\Exception\NotImplemented('The free-busy-query REPORT is only implemented on calendars');
     }
     // Doing a calendar-query first, to make sure we get the most
     // performance.
     $urls = $calendar->calendarQuery(array('name' => 'VCALENDAR', 'comp-filters' => array(array('name' => 'VEVENT', 'comp-filters' => array(), 'prop-filters' => array(), 'is-not-defined' => false, 'time-range' => array('start' => $start, 'end' => $end))), 'prop-filters' => array(), 'is-not-defined' => false, 'time-range' => null));
     $objects = array_map(function ($url) use($calendar) {
         $obj = $calendar->getChild($url)->get();
         return $obj;
     }, $urls);
     $generator = new VObject\FreeBusyGenerator();
     $generator->setObjects($objects);
     $generator->setTimeRange($start, $end);
     $result = $generator->getResult();
     $result = $result->serialize();
     $this->server->httpResponse->sendStatus(200);
     $this->server->httpResponse->setHeader('Content-Type', 'text/calendar');
     $this->server->httpResponse->setHeader('Content-Length', strlen($result));
     $this->server->httpResponse->sendBody($result);
 }
 /**
  * Parses the CALDAV:expand element
  *
  * @param \DOMElement $parentNode
  * @return void
  */
 protected function parseExpand(\DOMElement $parentNode)
 {
     $start = $parentNode->getAttribute('start');
     if (!$start) {
         throw new \SabreForRainLoop\DAV\Exception\BadRequest('The "start" attribute is required for the CALDAV:expand element');
     }
     $start = VObject\DateTimeParser::parseDateTime($start);
     $end = $parentNode->getAttribute('end');
     if (!$end) {
         throw new \SabreForRainLoop\DAV\Exception\BadRequest('The "end" attribute is required for the CALDAV:expand element');
     }
     $end = VObject\DateTimeParser::parseDateTime($end);
     if ($end <= $start) {
         throw new \SabreForRainLoop\DAV\Exception\BadRequest('The end-date must be larger than the start-date in the expand element.');
     }
     return array('start' => $start, 'end' => $end);
 }
Exemplo n.º 10
0
 /**
  * Returns the value, in the format it should be encoded for json.
  *
  * This method must always return an array.
  *
  * @return array
  */
 public function getJsonValue()
 {
     $parts = DateTimeParser::parseVCardDateTime($this->getValue());
     $dateStr = '';
     // Year
     if (!is_null($parts['year'])) {
         $dateStr .= $parts['year'];
         if (!is_null($parts['month'])) {
             // If a year and a month is set, we need to insert a separator
             // dash.
             $dateStr .= '-';
         }
     } else {
         if (!is_null($parts['month']) || !is_null($parts['date'])) {
             // Inserting two dashes
             $dateStr .= '--';
         }
     }
     // Month
     if (!is_null($parts['month'])) {
         $dateStr .= $parts['month'];
         if (isset($parts['date'])) {
             // If month and date are set, we need the separator dash.
             $dateStr .= '-';
         }
     } else {
         if (isset($parts['date'])) {
             // If the month is empty, and a date is set, we need a 'empty
             // dash'
             $dateStr .= '-';
         }
     }
     // Date
     if (!is_null($parts['date'])) {
         $dateStr .= $parts['date'];
     }
     // Early exit if we don't have a time string.
     if (is_null($parts['hour']) && is_null($parts['minute']) && is_null($parts['second'])) {
         return array($dateStr);
     }
     $dateStr .= 'T';
     // Hour
     if (!is_null($parts['hour'])) {
         $dateStr .= $parts['hour'];
         if (!is_null($parts['minute'])) {
             $dateStr .= ':';
         }
     } else {
         // We know either minute or second _must_ be set, so we insert a
         // dash for an empty value.
         $dateStr .= '-';
     }
     // Minute
     if (!is_null($parts['minute'])) {
         $dateStr .= $parts['minute'];
         if (!is_null($parts['second'])) {
             $dateStr .= ':';
         }
     } else {
         if (isset($parts['second'])) {
             // Dash for empty minute
             $dateStr .= '-';
         }
     }
     // Second
     if (!is_null($parts['second'])) {
         $dateStr .= $parts['second'];
     }
     // Timezone
     if (!is_null($parts['timezone'])) {
         $dateStr .= $parts['timezone'];
     }
     return array($dateStr);
 }
Exemplo n.º 11
0
 /**
  * Returns the value, in the format it should be encoded for json.
  *
  * This method must always return an array.
  *
  * @return array
  */
 public function getJsonValue()
 {
     $return = array();
     foreach ($this->getParts() as $item) {
         list($start, $end) = explode('/', $item, 2);
         $start = DateTimeParser::parseDateTime($start);
         // This is a duration value.
         if ($end[0] === 'P') {
             $return[] = array($start->format('Y-m-d\\TH:i:s'), $end);
         } else {
             $end = DateTimeParser::parseDateTime($end);
             $return[] = array($start->format('Y-m-d\\TH:i:s'), $end->format('Y-m-d\\TH:i:s'));
         }
     }
     return $return;
 }
Exemplo n.º 12
0
 /**
  * Parses the input data and returns a correct VFREEBUSY object, wrapped in
  * a VCALENDAR.
  *
  * @return Component
  */
 public function getResult()
 {
     $busyTimes = array();
     foreach ($this->objects as $object) {
         foreach ($object->getBaseComponents() as $component) {
             switch ($component->name) {
                 case 'VEVENT':
                     $FBTYPE = 'BUSY';
                     if (isset($component->TRANSP) && strtoupper($component->TRANSP) === 'TRANSPARENT') {
                         break;
                     }
                     if (isset($component->STATUS)) {
                         $status = strtoupper($component->STATUS);
                         if ($status === 'CANCELLED') {
                             break;
                         }
                         if ($status === 'TENTATIVE') {
                             $FBTYPE = 'BUSY-TENTATIVE';
                         }
                     }
                     $times = array();
                     if ($component->RRULE) {
                         $iterator = new RecurrenceIterator($object, (string) $component->uid);
                         if ($this->start) {
                             $iterator->fastForward($this->start);
                         }
                         $maxRecurrences = 200;
                         while ($iterator->valid() && --$maxRecurrences) {
                             $startTime = $iterator->getDTStart();
                             if ($this->end && $startTime > $this->end) {
                                 break;
                             }
                             $times[] = array($iterator->getDTStart(), $iterator->getDTEnd());
                             $iterator->next();
                         }
                     } else {
                         $startTime = $component->DTSTART->getDateTime();
                         if ($this->end && $startTime > $this->end) {
                             break;
                         }
                         $endTime = null;
                         if (isset($component->DTEND)) {
                             $endTime = $component->DTEND->getDateTime();
                         } elseif (isset($component->DURATION)) {
                             $duration = DateTimeParser::parseDuration((string) $component->DURATION);
                             $endTime = clone $startTime;
                             $endTime->add($duration);
                         } elseif (!$component->DTSTART->hasTime()) {
                             $endTime = clone $startTime;
                             $endTime->modify('+1 day');
                         } else {
                             // The event had no duration (0 seconds)
                             break;
                         }
                         $times[] = array($startTime, $endTime);
                     }
                     foreach ($times as $time) {
                         if ($this->end && $time[0] > $this->end) {
                             break;
                         }
                         if ($this->start && $time[1] < $this->start) {
                             break;
                         }
                         $busyTimes[] = array($time[0], $time[1], $FBTYPE);
                     }
                     break;
                 case 'VFREEBUSY':
                     foreach ($component->FREEBUSY as $freebusy) {
                         $fbType = isset($freebusy['FBTYPE']) ? strtoupper($freebusy['FBTYPE']) : 'BUSY';
                         // Skipping intervals marked as 'free'
                         if ($fbType === 'FREE') {
                             continue;
                         }
                         $values = explode(',', $freebusy);
                         foreach ($values as $value) {
                             list($startTime, $endTime) = explode('/', $value);
                             $startTime = DateTimeParser::parseDateTime($startTime);
                             if (substr($endTime, 0, 1) === 'P' || substr($endTime, 0, 2) === '-P') {
                                 $duration = DateTimeParser::parseDuration($endTime);
                                 $endTime = clone $startTime;
                                 $endTime->add($duration);
                             } else {
                                 $endTime = DateTimeParser::parseDateTime($endTime);
                             }
                             if ($this->start && $this->start > $endTime) {
                                 continue;
                             }
                             if ($this->end && $this->end < $startTime) {
                                 continue;
                             }
                             $busyTimes[] = array($startTime, $endTime, $fbType);
                         }
                     }
                     break;
             }
         }
     }
     if ($this->baseObject) {
         $calendar = $this->baseObject;
     } else {
         $calendar = new VCalendar();
     }
     $vfreebusy = $calendar->createComponent('VFREEBUSY');
     $calendar->add($vfreebusy);
     if ($this->start) {
         $dtstart = $calendar->createProperty('DTSTART');
         $dtstart->setDateTime($this->start);
         $vfreebusy->add($dtstart);
     }
     if ($this->end) {
         $dtend = $calendar->createProperty('DTEND');
         $dtend->setDateTime($this->end);
         $vfreebusy->add($dtend);
     }
     $dtstamp = $calendar->createProperty('DTSTAMP');
     $dtstamp->setDateTime(new \DateTime('now', new \DateTimeZone('UTC')));
     $vfreebusy->add($dtstamp);
     foreach ($busyTimes as $busyTime) {
         $busyTime[0]->setTimeZone(new \DateTimeZone('UTC'));
         $busyTime[1]->setTimeZone(new \DateTimeZone('UTC'));
         $prop = $calendar->createProperty('FREEBUSY', $busyTime[0]->format('Ymd\\THis\\Z') . '/' . $busyTime[1]->format('Ymd\\THis\\Z'));
         $prop['FBTYPE'] = $busyTime[2];
         $vfreebusy->add($prop);
     }
     return $calendar;
 }
Exemplo n.º 13
0
 /**
  * Returns multiple date-time values.
  *
  * @return \DateTime[]
  */
 public function getDateTimes()
 {
     // Finding the timezone.
     $tz = $this['TZID'];
     if ($tz) {
         $tz = TimeZoneUtil::getTimeZone((string) $tz, $this->root);
     }
     $dts = array();
     foreach ($this->getParts() as $part) {
         $dts[] = DateTimeParser::parse($part, $tz);
     }
     return $dts;
 }
Exemplo n.º 14
0
 /**
  * Returns a DateInterval representation of the Duration property.
  *
  * If the property has more than one value, only the first is returned.
  *
  * @return \DateInterval
  */
 public function getDateInterval()
 {
     $parts = $this->getParts();
     $value = $parts[0];
     return DateTimeParser::parseDuration($value);
 }