]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/lib/Sabre/CalDAV/ICSExportPlugin.php
ec42b406b2f172c7289a00ba7975fc35f59fa877
[friendica-addons.git] / dav / SabreDAV / lib / Sabre / CalDAV / ICSExportPlugin.php
1 <?php
2
3 /**
4  * ICS Exporter
5  *
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
8  * support ics files.
9  *
10  * @package Sabre
11  * @subpackage CalDAV
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
15  */
16 class Sabre_CalDAV_ICSExportPlugin extends Sabre_DAV_ServerPlugin {
17
18     /**
19      * Reference to Server class
20      *
21      * @var Sabre_DAV_Server
22      */
23     private $server;
24
25     /**
26      * Initializes the plugin and registers event handlers
27      *
28      * @param Sabre_DAV_Server $server
29      * @return void
30      */
31     public function initialize(Sabre_DAV_Server $server) {
32
33         $this->server = $server;
34         $this->server->subscribeEvent('beforeMethod',array($this,'beforeMethod'), 90);
35
36     }
37
38     /**
39      * 'beforeMethod' event handles. This event handles intercepts GET requests ending
40      * with ?export
41      *
42      * @param string $method
43      * @param string $uri
44      * @return bool
45      */
46     public function beforeMethod($method, $uri) {
47
48         if ($method!='GET') return;
49         if ($this->server->httpRequest->getQueryString()!='export') return;
50
51         // splitting uri
52         list($uri) = explode('?',$uri,2);
53
54         $node = $this->server->tree->getNodeForPath($uri);
55
56         if (!($node instanceof Sabre_CalDAV_Calendar)) return;
57
58         // Checking ACL, if available.
59         if ($aclPlugin = $this->server->getPlugin('acl')) {
60             $aclPlugin->checkPrivileges($uri, '{DAV:}read');
61         }
62
63         $this->server->httpResponse->setHeader('Content-Type','text/calendar');
64         $this->server->httpResponse->sendStatus(200);
65
66         $nodes = $this->server->getPropertiesForPath($uri, array(
67             '{' . Sabre_CalDAV_Plugin::NS_CALDAV . '}calendar-data',
68         ),1);
69
70         $this->server->httpResponse->sendBody($this->generateICS($nodes));
71
72         // Returning false to break the event chain
73         return false;
74
75     }
76
77     /**
78      * Merges all calendar objects, and builds one big ics export
79      *
80      * @param array $nodes
81      * @return string
82      */
83     public function generateICS(array $nodes) {
84
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';
89         } else {
90             $calendar->prodid = '-//SabreDAV//SabreDAV//EN';
91         }
92         $calendar->calscale = 'GREGORIAN';
93
94         $collectedTimezones = array();
95
96         $timezones = array();
97         $objects = array();
98
99         foreach($nodes as $node) {
100
101             if (!isset($node[200]['{' . Sabre_CalDAV_Plugin::NS_CALDAV . '}calendar-data'])) {
102                 continue;
103             }
104             $nodeData = $node[200]['{' . Sabre_CalDAV_Plugin::NS_CALDAV . '}calendar-data'];
105
106             $nodeComp = Sabre_VObject_Reader::read($nodeData);
107
108             foreach($nodeComp->children() as $child) {
109
110                 switch($child->name) {
111                     case 'VEVENT' :
112                     case 'VTODO' :
113                     case 'VJOURNAL' :
114                         $objects[] = $child;
115                         break;
116
117                     // VTIMEZONE is special, because we need to filter out the duplicates
118                     case 'VTIMEZONE' :
119                         // Naively just checking tzid.
120                         if (in_array((string)$child->TZID, $collectedTimezones)) continue;
121
122                         $timezones[] = $child;
123                         $collectedTimezones[] = $child->TZID;
124                         break;
125
126                 }
127
128             }
129
130         }
131
132         foreach($timezones as $tz) $calendar->add($tz);
133         foreach($objects as $obj) $calendar->add($obj);
134
135         return $calendar->serialize();
136
137     }
138
139 }