8 * This addon adds the ability to export entire calendars as .ics files.
9 * This is useful for clients that don't support CalDAV yet. They often do
14 * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
15 * @author Evert Pot (http://www.rooftopsolutions.nl/)
16 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
18 class Sabre_CalDAV_ICSExportPlugin extends Sabre_DAV_ServerPlugin {
21 * Reference to Server class
23 * @var Sabre_DAV_Server
28 * Initializes the addon and registers event handlers
30 * @param Sabre_DAV_Server $server
33 public function initialize(Sabre_DAV_Server $server) {
35 $this->server = $server;
36 $this->server->subscribeEvent('beforeMethod',array($this,'beforeMethod'), 90);
41 * 'beforeMethod' event handles. This event handles intercepts GET requests ending
44 * @param string $method
48 public function beforeMethod($method, $uri) {
50 if ($method!='GET') return;
51 if ($this->server->httpRequest->getQueryString()!='export') return;
54 list($uri) = explode('?',$uri,2);
56 $node = $this->server->tree->getNodeForPath($uri);
58 if (!($node instanceof Sabre_CalDAV_Calendar)) return;
60 // Checking ACL, if available.
61 if ($aclPlugin = $this->server->getPlugin('acl')) {
62 $aclPlugin->checkPrivileges($uri, '{DAV:}read');
65 $this->server->httpResponse->setHeader('Content-Type','text/calendar');
66 $this->server->httpResponse->sendStatus(200);
68 $nodes = $this->server->getPropertiesForPath($uri, array(
69 '{' . Sabre_CalDAV_Plugin::NS_CALDAV . '}calendar-data',
72 $this->server->httpResponse->sendBody($this->generateICS($nodes));
74 // Returning false to break the event chain
80 * Merges all calendar objects, and builds one big ics export
85 public function generateICS(array $nodes) {
87 $calendar = new VObject\Component('vcalendar');
88 $calendar->version = '2.0';
89 if (Sabre_DAV_Server::$exposeVersion) {
90 $calendar->prodid = '-//SabreDAV//SabreDAV ' . Sabre_DAV_Version::VERSION . '//EN';
92 $calendar->prodid = '-//SabreDAV//SabreDAV//EN';
94 $calendar->calscale = 'GREGORIAN';
96 $collectedTimezones = array();
101 foreach($nodes as $node) {
103 if (!isset($node[200]['{' . Sabre_CalDAV_Plugin::NS_CALDAV . '}calendar-data'])) {
106 $nodeData = $node[200]['{' . Sabre_CalDAV_Plugin::NS_CALDAV . '}calendar-data'];
108 $nodeComp = VObject\Reader::read($nodeData);
110 foreach($nodeComp->children() as $child) {
112 switch($child->name) {
119 // VTIMEZONE is special, because we need to filter out the duplicates
121 // Naively just checking tzid.
122 if (in_array((string)$child->TZID, $collectedTimezones)) continue;
124 $timezones[] = $child;
125 $collectedTimezones[] = $child->TZID;
134 foreach($timezones as $tz) $calendar->add($tz);
135 foreach($objects as $obj) $calendar->add($obj);
137 return $calendar->serialize();