[Pkg-owncloud-commits] [php-sabredav] 135/220: Set a filename with .ics extension when exporting a calendar

David Prévot taffit at moszumanska.debian.org
Thu May 12 01:21:18 UTC 2016


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository php-sabredav.

commit 3b4306913e448a32be2a2c63def635c7a2638ec3
Author: Robin McCorkell <robin at mccorkell.me.uk>
Date:   Wed Apr 6 21:57:13 2016 +0100

    Set a filename with .ics extension when exporting a calendar
    
    * Set a filename with .ics extension when exporting a calendar
    
    Format is <calendar>-YYYY-MM-DD.ics, or <calendar>-YYYY-MM-DD.json if
    using the jCal export format.
    
    * Filter bad characters for ICS export
    
    * Switch to using calendar name instead of display name for export
---
 lib/CalDAV/ICSExportPlugin.php             | 16 +++++++-
 tests/Sabre/CalDAV/ICSExportPluginTest.php | 60 ++++++++++++++++++++++++++++++
 2 files changed, 74 insertions(+), 2 deletions(-)

diff --git a/lib/CalDAV/ICSExportPlugin.php b/lib/CalDAV/ICSExportPlugin.php
index eb6640f..a3a824c 100644
--- a/lib/CalDAV/ICSExportPlugin.php
+++ b/lib/CalDAV/ICSExportPlugin.php
@@ -170,13 +170,13 @@ class ICSExportPlugin extends DAV\ServerPlugin {
     protected function generateResponse($path, $start, $end, $expand, $componentType, $format, $properties, ResponseInterface $response) {
 
         $calDataProp = '{' . Plugin::NS_CALDAV . '}calendar-data';
+        $calendarNode = $this->server->tree->getNodeForPath($path);
 
         $blobs = [];
         if ($start || $end || $componentType) {
 
             // If there was a start or end filter, we need to enlist
             // calendarQuery for speed.
-            $calendarNode = $this->server->tree->getNodeForPath($path);
             $queryResult = $calendarNode->calendarQuery([
                 'name'         => 'VCALENDAR',
                 'comp-filters' => [
@@ -246,17 +246,29 @@ class ICSExportPlugin extends DAV\ServerPlugin {
             $mergedCalendar = $mergedCalendar->expand($start, $end, $calendarTimeZone);
         }
 
-        $response->setHeader('Content-Type', $format);
+        $filenameExtension = '.ics';
 
         switch ($format) {
             case 'text/calendar' :
                 $mergedCalendar = $mergedCalendar->serialize();
+                $filenameExtension = '.ics';
                 break;
             case 'application/calendar+json' :
                 $mergedCalendar = json_encode($mergedCalendar->jsonSerialize());
+                $filenameExtension = '.json';
                 break;
         }
 
+        $filename = preg_replace(
+            '/[^a-zA-Z0-9-_ ]/um',
+            '',
+            $calendarNode->getName()
+        );
+        $filename .= '-' . date('Y-m-d') . $filenameExtension;
+
+        $response->setHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
+        $response->setHeader('Content-Type', $format);
+
         $response->setStatus(200);
         $response->setBody($mergedCalendar);
 
diff --git a/tests/Sabre/CalDAV/ICSExportPluginTest.php b/tests/Sabre/CalDAV/ICSExportPluginTest.php
index 073403b..46a5bc1 100644
--- a/tests/Sabre/CalDAV/ICSExportPluginTest.php
+++ b/tests/Sabre/CalDAV/ICSExportPluginTest.php
@@ -319,4 +319,64 @@ ICS
         $response = $this->request($request, 400);
 
     }
+
+    function testContentDisposition() {
+
+        $request = new HTTP\Request(
+            'GET',
+            '/calendars/admin/UUID-123467?export'
+        );
+
+        $response = $this->request($request, 200);
+        $this->assertEquals('text/calendar', $response->getHeader('Content-Type'));
+        $this->assertEquals(
+            'attachment; filename="UUID-123467-' . date('Y-m-d') . '.ics"',
+            $response->getHeader('Content-Disposition')
+        );
+
+    }
+
+    function testContentDispositionJson() {
+
+        $request = new HTTP\Request(
+            'GET',
+            '/calendars/admin/UUID-123467?export',
+            ['Accept' => 'application/calendar+json']
+        );
+
+        $response = $this->request($request, 200);
+        $this->assertEquals('application/calendar+json', $response->getHeader('Content-Type'));
+        $this->assertEquals(
+            'attachment; filename="UUID-123467-' . date('Y-m-d') . '.json"',
+            $response->getHeader('Content-Disposition')
+        );
+
+    }
+
+    function testContentDispositionBadChars() {
+
+        $this->caldavBackend->createCalendar(
+            'principals/admin',
+            'UUID-b_ad"(ch)ars',
+            [
+                '{DAV:}displayname'                         => 'Test bad characters',
+                '{http://apple.com/ns/ical/}calendar-color' => '#AA0000FF',
+            ]
+        );
+
+        $request = new HTTP\Request(
+            'GET',
+            '/calendars/admin/UUID-b_ad"(ch)ars?export',
+            ['Accept' => 'application/calendar+json']
+        );
+
+        $response = $this->request($request, 200);
+        $this->assertEquals('application/calendar+json', $response->getHeader('Content-Type'));
+        $this->assertEquals(
+            'attachment; filename="UUID-b_adchars-' . date('Y-m-d') . '.json"',
+            $response->getHeader('Content-Disposition')
+        );
+
+    }
+
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-php/php-sabredav.git



More information about the Pkg-owncloud-commits mailing list