/** * Unserializes the DOMElement back into a Property class. * * @param DOMElement $node * @return void */ static function unserialize(DOMElement $node) { $components = array(); foreach ($node->childNodes as $childNode) { if (Sabre_DAV_XMLUtil::toClarkNotation($childNode) === '{' . Sabre_CalDAV_Plugin::NS_CALDAV . '}comp') { $components[] = $childNode->getAttribute('name'); } } return new self($components); }
/** * Unserializes this property from a DOM Element * * This method returns an instance of this class. * It will only decode {DAV:}href values. * * @param DOMElement $dom * @return Sabre_DAV_Property_Href */ static function unserialize(DOMElement $dom) { $hrefs = array(); foreach ($dom->childNodes as $child) { if (Sabre_DAV_XMLUtil::toClarkNotation($child) === '{DAV:}href') { $hrefs[] = $child->textContent; } } return new self($hrefs, false); }
/** * This function parses the calendar-query report request body * * The body is quite complicated, so we're turning it into a PHP * array. * * The resulting associative array has xpath expressions as keys. * By default the xpath expressions should simply be checked for existance * The xpath expressions can point to elements or attributes. * * The array values can contain a number of items, which alters the query * filter. * * * time-range. Must also check if the todo or event falls within the * specified timerange. How this is interpreted depends on * the type of object (VTODO, VEVENT, VJOURNAL, etc) * * is-not-defined * Instead of checking if the attribute or element exist, * we must check if it doesn't. * * text-match * Checks if the value of the attribute or element matches * the specified value. This is actually another array with * the 'collation', 'value' and 'negate-condition' items. * * Refer to the CalDAV spec for more information. * * @param DOMNode $domNode * @param string $basePath used for recursive calls. * @param array $filters used for recursive calls. * @return array */ public static function parseCalendarQueryFilters($domNode, $basePath = '/c:iCalendar', &$filters = array()) { foreach ($domNode->childNodes as $child) { switch (Sabre_DAV_XMLUtil::toClarkNotation($child)) { case '{urn:ietf:params:xml:ns:caldav}comp-filter': case '{urn:ietf:params:xml:ns:caldav}prop-filter': $filterName = $basePath . '/' . 'c:' . strtolower($child->getAttribute('name')); $filters[$filterName] = array(); self::parseCalendarQueryFilters($child, $filterName, $filters); break; case '{urn:ietf:params:xml:ns:caldav}time-range': if ($start = $child->getAttribute('start')) { $start = self::parseICalendarDateTime($start); } else { $start = null; } if ($end = $child->getAttribute('end')) { $end = self::parseICalendarDateTime($end); } else { $end = null; } if (!is_null($start) && !is_null($end) && $end <= $start) { throw new Sabre_DAV_Exception_BadRequest('The end-date must be larger than the start-date in the time-range filter'); } $filters[$basePath]['time-range'] = array('start' => $start, 'end' => $end); break; case '{urn:ietf:params:xml:ns:caldav}is-not-defined': $filters[$basePath]['is-not-defined'] = true; break; case '{urn:ietf:params:xml:ns:caldav}param-filter': $filterName = $basePath . '/@' . strtolower($child->getAttribute('name')); $filters[$filterName] = array(); self::parseCalendarQueryFilters($child, $filterName, $filters); break; case '{urn:ietf:params:xml:ns:caldav}text-match': $collation = $child->getAttribute('collation'); if (!$collation) { $collation = 'i;ascii-casemap'; } $filters[$basePath]['text-match'] = array('collation' => $collation == 'default' ? 'i;ascii-casemap' : $collation, 'negate-condition' => $child->getAttribute('negate-condition') === 'yes', 'value' => $child->nodeValue); break; } } return $filters; }
/** * Unserializes the DOMElement back into a Property class. * * @param DOMElement $node * @return Sabre_CalDAV_Property_ScheduleCalendarTransp */ static function unserialize(DOMElement $node) { $value = null; foreach ($node->childNodes as $childNode) { switch (Sabre_DAV_XMLUtil::toClarkNotation($childNode)) { case '{' . Sabre_CalDAV_Plugin::NS_CALDAV . '}opaque': $value = self::OPAQUE; break; case '{' . Sabre_CalDAV_Plugin::NS_CALDAV . '}transparent': $value = self::TRANSPARENT; break; } } if (is_null($value)) { return null; } return new self($value); }
/** * This event is triggered when the server didn't know how to handle a * certain request. * * We intercept this to handle POST requests on calendars. * * @param string $method * @param string $uri * @return null|bool */ public function unknownMethod($method, $uri) { if ($method !== 'POST') { return; } // Only handling xml $contentType = $this->server->httpRequest->getHeader('Content-Type'); if (strpos($contentType, 'application/xml') === false && strpos($contentType, 'text/xml') === false) { return; } // Making sure the node exists try { $node = $this->server->tree->getNodeForPath($uri); } catch (Sabre_DAV_Exception_NotFound $e) { return; } $requestBody = $this->server->httpRequest->getBody(true); // If this request handler could not deal with this POST request, it // will return 'null' and other plugins get a chance to handle the // request. // // However, we already requested the full body. This is a problem, // because a body can only be read once. This is why we preemptively // re-populated the request body with the existing data. $this->server->httpRequest->setBody($requestBody); $dom = Sabre_DAV_XMLUtil::loadDOMDocument($requestBody); $documentType = Sabre_DAV_XMLUtil::toClarkNotation($dom->firstChild); switch ($documentType) { // Dealing with the 'share' document, which modified invitees on a // calendar. case '{' . Sabre_CalDAV_Plugin::NS_CALENDARSERVER . '}share': // We can only deal with IShareableCalendar objects if (!$node instanceof Sabre_CalDAV_IShareableCalendar) { return; } // Getting ACL info $acl = $this->server->getPlugin('acl'); // If there's no ACL support, we allow everything if ($acl) { $acl->checkPrivileges($uri, '{DAV:}write'); } $mutations = $this->parseShareRequest($dom); $node->updateShares($mutations[0], $mutations[1]); $this->server->httpResponse->sendStatus(200); // Adding this because sending a response body may cause issues, // and I wanted some type of indicator the response was handled. $this->server->httpResponse->setHeader('X-Sabre-Status', 'everything-went-well'); // Breaking the event chain return false; // The invite-reply document is sent when the user replies to an // invitation of a calendar share. // The invite-reply document is sent when the user replies to an // invitation of a calendar share. case '{' . Sabre_CalDAV_Plugin::NS_CALENDARSERVER . '}invite-reply': // This only works on the calendar-home-root node. if (!$node instanceof Sabre_CalDAV_UserCalendars) { return; } // Getting ACL info $acl = $this->server->getPlugin('acl'); // If there's no ACL support, we allow everything if ($acl) { $acl->checkPrivileges($uri, '{DAV:}write'); } $message = $this->parseInviteReplyRequest($dom); $url = $node->shareReply($message['href'], $message['status'], $message['calendarUri'], $message['inReplyTo'], $message['summary']); $this->server->httpResponse->sendStatus(200); // Adding this because sending a response body may cause issues, // and I wanted some type of indicator the response was handled. $this->server->httpResponse->setHeader('X-Sabre-Status', 'everything-went-well'); if ($url) { $dom = new DOMDocument('1.0', 'UTF-8'); $dom->formatOutput = true; $root = $dom->createElement('cs:shared-as'); foreach ($this->server->xmlNamespaces as $namespace => $prefix) { $root->setAttribute('xmlns:' . $prefix, $namespace); } $dom->appendChild($root); $href = new Sabre_DAV_Property_Href($url); $href->serialize($this->server, $root); $this->server->httpResponse->setHeader('Content-Type', 'application/xml'); $this->server->httpResponse->sendBody($dom->saveXML()); } // Breaking the event chain return false; case '{' . Sabre_CalDAV_Plugin::NS_CALENDARSERVER . '}publish-calendar': // We can only deal with IShareableCalendar objects if (!$node instanceof Sabre_CalDAV_IShareableCalendar) { return; } // Getting ACL info $acl = $this->server->getPlugin('acl'); // If there's no ACL support, we allow everything if ($acl) { $acl->checkPrivileges($uri, '{DAV:}write'); } $node->setPublishStatus(true); // iCloud sends back the 202, so we will too. $this->server->httpResponse->sendStatus(202); // Adding this because sending a response body may cause issues, // and I wanted some type of indicator the response was handled. $this->server->httpResponse->setHeader('X-Sabre-Status', 'everything-went-well'); // Breaking the event chain return false; case '{' . Sabre_CalDAV_Plugin::NS_CALENDARSERVER . '}unpublish-calendar': // We can only deal with IShareableCalendar objects if (!$node instanceof Sabre_CalDAV_IShareableCalendar) { return; } // Getting ACL info $acl = $this->server->getPlugin('acl'); // If there's no ACL support, we allow everything if ($acl) { $acl->checkPrivileges($uri, '{DAV:}write'); } $node->setPublishStatus(false); $this->server->httpResponse->sendStatus(200); // Adding this because sending a response body may cause issues, // and I wanted some type of indicator the response was handled. $this->server->httpResponse->setHeader('X-Sabre-Status', 'everything-went-well'); // Breaking the event chain return false; } }
/** * This method parses a PropPatch request * * PropPatch changes the properties for a resource. This method * returns a list of properties. * * The keys in the returned array contain the property name (e.g.: {DAV:}displayname, * and the value contains the property value. If a property is to be removed the value * will be null. * * @param string $body xml body * @return array list of properties in need of updating or deletion */ public function parsePropPatchRequest($body) { //We'll need to change the DAV namespace declaration to something else in order to make it parsable $dom = Sabre_DAV_XMLUtil::loadDOMDocument($body); $newProperties = array(); foreach ($dom->firstChild->childNodes as $child) { if ($child->nodeType !== XML_ELEMENT_NODE) { continue; } $operation = Sabre_DAV_XMLUtil::toClarkNotation($child); if ($operation !== '{DAV:}set' && $operation !== '{DAV:}remove') { continue; } $innerProperties = Sabre_DAV_XMLUtil::parseProperties($child, $this->propertyMap); foreach ($innerProperties as $propertyName => $propertyValue) { if ($operation === '{DAV:}remove') { $propertyValue = null; } $newProperties[$propertyName] = $propertyValue; } } return $newProperties; }
/** * Parses all WebDAV properties out of a DOM Element * * Generally WebDAV properties are enclosed in {DAV:}prop elements. This * method helps by going through all these and pulling out the actual * propertynames, making them array keys and making the property values, * well.. the array values. * * If no value was given (self-closing element) null will be used as the * value. This is used in for example PROPFIND requests. * * Complex values are supported through the propertyMap argument. The * propertyMap should have the clark-notation properties as it's keys, and * classnames as values. * * When any of these properties are found, the unserialize() method will be * (statically) called. The result of this method is used as the value. * * @param DOMElement $parentNode * @param array $propertyMap * @return array */ static function parseProperties(DOMElement $parentNode, array $propertyMap = array()) { $propList = array(); foreach ($parentNode->childNodes as $propNode) { if (Sabre_DAV_XMLUtil::toClarkNotation($propNode) !== '{DAV:}prop') { continue; } foreach ($propNode->childNodes as $propNodeData) { /* If there are no elements in here, we actually get 1 text node, this special case is dedicated to netdrive */ if ($propNodeData->nodeType != XML_ELEMENT_NODE) { continue; } $propertyName = Sabre_DAV_XMLUtil::toClarkNotation($propNodeData); if (isset($propertyMap[$propertyName])) { $propList[$propertyName] = call_user_func(array($propertyMap[$propertyName], 'unserialize'), $propNodeData); } else { $propList[$propertyName] = $propNodeData->textContent; } } } return $propList; }
function testToClarkNotationNoElem() { $dom = new DOMDocument(); $dom->loadXML('<?xml version="1.0"?><s:test1 xmlns:s="urn:DAV">Testdoc</s:test1>'); $this->assertNull(Sabre_DAV_XMLUtil::toClarkNotation($dom->firstChild->firstChild)); }
/** * Unserializes this property from a DOM Element * * This method returns an instance of this class. * It will only decode {DAV:}href values. For non-compatible elements null will be returned. * * @param DOMElement $dom * @return Sabre_DAV_Property_Href */ static function unserialize(DOMElement $dom) { if (Sabre_DAV_XMLUtil::toClarkNotation($dom->firstChild) === '{DAV:}href') { return new self($dom->firstChild->textContent, false); } }
/** * 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 = Sabre_DAV_XMLUtil::toClarkNotation($childNode); if ($clark == '{' . self::NS_CALDAV . '}time-range') { $start = $childNode->getAttribute('start'); $end = $childNode->getAttribute('end'); break; } } if ($start) { $start = Sabre_VObject_DateTimeParser::parseDateTime($start); } if ($end) { $end = Sabre_VObject_DateTimeParser::parseDateTime($end); } if (!$start && !$end) { throw new Sabre_DAV_Exception_BadRequest('The freebusy report must have a time-range filter'); } $acl = $this->server->getPlugin('acl'); if (!$acl) { throw new Sabre_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 Sabre_CalDAV_ICalendar) { throw new Sabre_DAV_Exception_NotImplemented('The free-busy-query REPORT is only implemented on calendars'); } $objects = array_map(function ($child) { $obj = $child->get(); if (is_resource($obj)) { $obj = stream_get_contents($obj); } return $obj; }, $calendar->getChildren()); $generator = new Sabre_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); }
/** * parsePrincipalPropertySearchReportRequest * * This method parses the request body from a * {DAV:}principal-property-search report. * * This method returns an array with two elements: * 1. an array with properties to search on, and their values * 2. a list of propertyvalues that should be returned for the request. * * @param DOMDocument $dom * @return array */ protected function parsePrincipalPropertySearchReportRequest($dom) { $httpDepth = $this->server->getHTTPDepth(0); if ($httpDepth !== 0) { throw new Sabre_DAV_Exception_BadRequest('This report is only defined when Depth: 0'); } $searchProperties = array(); $applyToPrincipalCollectionSet = false; // Parsing the search request foreach ($dom->firstChild->childNodes as $searchNode) { if (Sabre_DAV_XMLUtil::toClarkNotation($searchNode) == '{DAV:}apply-to-principal-collection-set') { $applyToPrincipalCollectionSet = true; } if (Sabre_DAV_XMLUtil::toClarkNotation($searchNode) !== '{DAV:}property-search') { continue; } $propertyName = null; $propertyValue = null; foreach ($searchNode->childNodes as $childNode) { switch (Sabre_DAV_XMLUtil::toClarkNotation($childNode)) { case '{DAV:}prop': $property = Sabre_DAV_XMLUtil::parseProperties($searchNode); reset($property); $propertyName = key($property); break; case '{DAV:}match': $propertyValue = $childNode->textContent; break; } } if (is_null($propertyName) || is_null($propertyValue)) { throw new Sabre_DAV_Exception_BadRequest('Invalid search request. propertyname: ' . $propertyName . '. propertvvalue: ' . $propertyValue); } $searchProperties[$propertyName] = $propertyValue; } return array($searchProperties, array_keys(Sabre_DAV_XMLUtil::parseProperties($dom->firstChild)), $applyToPrincipalCollectionSet); }
/** * Unserializes a DOM element into a ResourceType property. * * @param DOMElement $dom * @return Sabre_DAV_Property_ResourceType */ public static function unserialize(DOMElement $dom) { $value = array(); foreach ($dom->childNodes as $child) { $value[] = Sabre_DAV_XMLUtil::toClarkNotation($child); } return new self($value); }
/** * This function handles the MKCALENDAR HTTP method, which creates * a new calendar. * * @param string $uri * @return void */ public function httpMkCalendar($uri) { // Due to unforgivable bugs in iCal, we're completely disabling MKCALENDAR support // for clients matching iCal in the user agent //$ua = $this->server->httpRequest->getHeader('User-Agent'); //if (strpos($ua,'iCal/')!==false) { // throw new Sabre_DAV_Exception_Forbidden('iCal has major bugs in it\'s RFC3744 support. Therefore we are left with no other choice but disabling this feature.'); //} $body = $this->server->httpRequest->getBody(true); $properties = array(); if ($body) { $dom = Sabre_DAV_XMLUtil::loadDOMDocument($body); foreach ($dom->firstChild->childNodes as $child) { if (Sabre_DAV_XMLUtil::toClarkNotation($child) !== '{DAV:}set') { continue; } foreach (Sabre_DAV_XMLUtil::parseProperties($child, $this->server->propertyMap) as $k => $prop) { $properties[$k] = $prop; } } } $resourceType = array('{DAV:}collection', '{urn:ietf:params:xml:ns:caldav}calendar'); $this->server->createCollection($uri, $resourceType, $properties); $this->server->httpResponse->sendStatus(201); $this->server->httpResponse->setHeader('Content-Length', 0); }
/** * Unserializes the {DAV:}acl xml element. * * @param DOMElement $dom * @return Sabre_DAVACL_Property_Acl */ public static function unserialize(DOMElement $dom) { $privileges = array(); $xaces = $dom->getElementsByTagNameNS('urn:DAV', 'ace'); for ($ii = 0; $ii < $xaces->length; $ii++) { $xace = $xaces->item($ii); $principal = $xace->getElementsByTagNameNS('urn:DAV', 'principal'); if ($principal->length !== 1) { throw new Sabre_DAV_Exception_BadRequest('Each {DAV:}ace element must have one {DAV:}principal element'); } $principal = Sabre_DAVACL_Property_Principal::unserialize($principal->item(0)); if ($principal->getType() !== Sabre_DAVACL_Property_Principal::HREF) { throw new Sabre_DAV_Exception_NotImplemented('Currently only uri based principals are support, {DAV:}all, {DAV:}unauthenticated and {DAV:}authenticated are not implemented yet'); } $principal = $principal->getHref(); $protected = false; if ($xace->getElementsByTagNameNS('urn:DAV', 'protected')->length > 0) { $protected = true; } $grants = $xace->getElementsByTagNameNS('urn:DAV', 'grant'); if ($grants->length < 1) { throw new Sabre_DAV_Exception_NotImplemented('Every {DAV:}ace element must have a {DAV:}grant element. {DAV:}deny is not yet supported'); } $grant = $grants->item(0); $xprivs = $grant->getElementsByTagNameNS('urn:DAV', 'privilege'); for ($jj = 0; $jj < $xprivs->length; $jj++) { $xpriv = $xprivs->item($jj); $privilegeName = null; for ($kk = 0; $kk < $xpriv->childNodes->length; $kk++) { $childNode = $xpriv->childNodes->item($kk); if ($t = Sabre_DAV_XMLUtil::toClarkNotation($childNode)) { $privilegeName = $t; break; } } if (is_null($privilegeName)) { throw new Sabre_DAV_Exception_BadRequest('{DAV:}privilege elements must have a privilege element contained within them.'); } $privileges[] = array('principal' => $principal, 'protected' => $protected, 'privilege' => $privilegeName); } } return new self($privileges); }
/** * 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 = Sabre_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 Sabre_DAV_Exception_BadRequest('The freebusy report must have a time-range filter'); } $acl = $this->server->getPlugin('acl'); if (!$acl) { throw new Sabre_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 Sabre_CalDAV_ICalendar) { throw new Sabre_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); }
/** * Deserializes a DOM element into a property object. * * @param DOMElement $dom * @return Sabre_DAV_Property_Principal */ public static function unserialize(DOMElement $dom) { $parent = $dom->firstChild; while (!Sabre_DAV_XMLUtil::toClarkNotation($parent)) { $parent = $parent->nextSibling; } switch (Sabre_DAV_XMLUtil::toClarkNotation($parent)) { case '{DAV:}unauthenticated': return new self(self::UNAUTHENTICATED); case '{DAV:}authenticated': return new self(self::AUTHENTICATED); case '{DAV:}href': return new self(self::HREF, $parent->textContent); default: throw new Sabre_DAV_Exception_BadRequest('Unexpected element (' . Sabre_DAV_XMLUtil::toClarkNotation($parent) . '). Could not deserialize'); } }