6 * This plugin adds the ability to export entire calendars as .ics files.
7 * This is useful for clients that don't support CalDAV yet. They often do
12 * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
13 * @author Evert Pot (http://www.rooftopsolutions.nl/)
14 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
16 class Sabre_CalDAV_ICSExportPlugin extends Sabre_DAV_ServerPlugin {
19 * Reference to Server class
21 * @var Sabre_DAV_Server
26 * Initializes the plugin and registers event handlers
28 * @param Sabre_DAV_Server $server
31 public function initialize(Sabre_DAV_Server $server) {
33 $this->server = $server;
34 $this->server->subscribeEvent('beforeMethod',array($this,'beforeMethod'), 90);
39 * 'beforeMethod' event handles. This event handles intercepts GET requests ending
42 * @param string $method
46 public function beforeMethod($method, $uri) {
48 if ($method!='GET') return;
49 if ($this->server->httpRequest->getQueryString()!='export') return;
52 list($uri) = explode('?',$uri,2);
54 $node = $this->server->tree->getNodeForPath($uri);
56 if (!($node instanceof Sabre_CalDAV_Calendar)) return;
58 // Checking ACL, if available.
59 if ($aclPlugin = $this->server->getPlugin('acl')) {
60 $aclPlugin->checkPrivileges($uri, '{DAV:}read');
63 $this->server->httpResponse->setHeader('Content-Type','text/calendar');
64 $this->server->httpResponse->sendStatus(200);
66 $nodes = $this->server->getPropertiesForPath($uri, array(
67 '{' . Sabre_CalDAV_Plugin::NS_CALDAV . '}calendar-data',
70 $this->server->httpResponse->sendBody($this->generateICS($nodes));
72 // Returning false to break the event chain
78 * Merges all calendar objects, and builds one big ics export
83 public function generateICS(array $nodes) {
85 $calendar = new Sabre_VObject_Component('vcalendar');
86 $calendar->version = '2.0';
87 if (Sabre_DAV_Server::$exposeVersion) {
88 $calendar->prodid = '-//SabreDAV//SabreDAV ' . Sabre_DAV_Version::VERSION . '//EN';
90 $calendar->prodid = '-//SabreDAV//SabreDAV//EN';
92 $calendar->calscale = 'GREGORIAN';
94 $collectedTimezones = array();
99 foreach($nodes as $node) {
101 if (!isset($node[200]['{' . Sabre_CalDAV_Plugin::NS_CALDAV . '}calendar-data'])) {
104 $nodeData = $node[200]['{' . Sabre_CalDAV_Plugin::NS_CALDAV . '}calendar-data'];
106 $nodeComp = Sabre_VObject_Reader::read($nodeData);
108 foreach($nodeComp->children() as $child) {
110 switch($child->name) {
117 // VTIMEZONE is special, because we need to filter out the duplicates
119 // Naively just checking tzid.
120 if (in_array((string)$child->TZID, $collectedTimezones)) continue;
122 $timezones[] = $child;
123 $collectedTimezones[] = $child->TZID;
132 foreach($timezones as $tz) $calendar->add($tz);
133 foreach($objects as $obj) $calendar->add($obj);
135 return $calendar->serialize();