diff options
Diffstat (limited to 'vendor/sabre/dav/lib/CalDAV/ICSExportPlugin.php')
-rw-r--r-- | vendor/sabre/dav/lib/CalDAV/ICSExportPlugin.php | 154 |
1 files changed, 79 insertions, 75 deletions
diff --git a/vendor/sabre/dav/lib/CalDAV/ICSExportPlugin.php b/vendor/sabre/dav/lib/CalDAV/ICSExportPlugin.php index fc8b971f3..717d3a9c9 100644 --- a/vendor/sabre/dav/lib/CalDAV/ICSExportPlugin.php +++ b/vendor/sabre/dav/lib/CalDAV/ICSExportPlugin.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + namespace Sabre\CalDAV; use DateTime; @@ -11,7 +13,7 @@ use Sabre\HTTP\ResponseInterface; use Sabre\VObject; /** - * ICS Exporter + * ICS Exporter. * * This plugin adds the ability to export entire calendars as .ics files. * This is useful for clients that don't support CalDAV yet. They often do @@ -44,44 +46,49 @@ use Sabre\VObject; * @author Evert Pot (http://evertpot.com/) * @license http://sabre.io/license/ Modified BSD License */ -class ICSExportPlugin extends DAV\ServerPlugin { - +class ICSExportPlugin extends DAV\ServerPlugin +{ /** - * Reference to Server class + * Reference to Server class. * * @var \Sabre\DAV\Server */ protected $server; /** - * Initializes the plugin and registers event handlers + * Initializes the plugin and registers event handlers. * * @param \Sabre\DAV\Server $server - * @return void */ - function initialize(DAV\Server $server) { - + public function initialize(DAV\Server $server) + { $this->server = $server; $server->on('method:GET', [$this, 'httpGet'], 90); - $server->on('browserButtonActions', function($path, $node, &$actions) { + $server->on('browserButtonActions', function ($path, $node, &$actions) { if ($node instanceof ICalendar) { - $actions .= '<a href="' . htmlspecialchars($path, ENT_QUOTES, 'UTF-8') . '?export"><span class="oi" data-glyph="calendar"></span></a>'; + $actions .= '<a href="'.htmlspecialchars($path, ENT_QUOTES, 'UTF-8').'?export"><span class="oi" data-glyph="calendar"></span></a>'; } }); - } /** * Intercepts GET requests on calendar urls ending with ?export. * - * @param RequestInterface $request + * @param RequestInterface $request * @param ResponseInterface $response + * + * @throws BadRequest + * @throws DAV\Exception\NotFound + * @throws VObject\InvalidDataException + * * @return bool */ - function httpGet(RequestInterface $request, ResponseInterface $response) { - + public function httpGet(RequestInterface $request, ResponseInterface $response) + { $queryParams = $request->getQueryParameters(); - if (!array_key_exists('export', $queryParams)) return; + if (!array_key_exists('export', $queryParams)) { + return; + } $path = $request->getPath(); @@ -93,7 +100,7 @@ class ICSExportPlugin extends DAV\ServerPlugin { '{http://apple.com/ns/ical/}calendar-color', ]); - if (!isset($node['{DAV:}resourcetype']) || !$node['{DAV:}resourcetype']->is('{' . Plugin::NS_CALDAV . '}calendar')) { + if (!isset($node['{DAV:}resourcetype']) || !$node['{DAV:}resourcetype']->is('{'.Plugin::NS_CALDAV.'}calendar')) { return; } // Marking the transactionType, for logging purposes. @@ -117,7 +124,7 @@ class ICSExportPlugin extends DAV\ServerPlugin { } $end = DateTime::createFromFormat('U', $queryParams['end']); } - if (isset($queryParams['expand']) && !!$queryParams['expand']) { + if (isset($queryParams['expand']) && (bool) $queryParams['expand']) { if (!$start || !$end) { throw new BadRequest('If you\'d like to expand recurrences, you must specify both a start= and end= parameter.'); } @@ -126,12 +133,12 @@ class ICSExportPlugin extends DAV\ServerPlugin { } if (isset($queryParams['componentType'])) { if (!in_array($queryParams['componentType'], ['VEVENT', 'VTODO', 'VJOURNAL'])) { - throw new BadRequest('You are not allowed to search for components of type: ' . $queryParams['componentType'] . ' here'); + throw new BadRequest('You are not allowed to search for components of type: '.$queryParams['componentType'].' here'); } $componentType = $queryParams['componentType']; } - $format = \Sabre\HTTP\Util::Negotiate( + $format = \Sabre\HTTP\negotiateContentType( $request->getHeader('Accept'), [ 'text/calendar', @@ -140,7 +147,7 @@ class ICSExportPlugin extends DAV\ServerPlugin { ); if (isset($queryParams['accept'])) { - if ($queryParams['accept'] === 'application/calendar+json' || $queryParams['accept'] === 'jcal') { + if ('application/calendar+json' === $queryParams['accept'] || 'jcal' === $queryParams['accept']) { $format = 'application/calendar+json'; } } @@ -152,61 +159,61 @@ class ICSExportPlugin extends DAV\ServerPlugin { // Returning false to break the event chain return false; - } /** * This method is responsible for generating the actual, full response. * - * @param string $path - * @param DateTime|null $start - * @param DateTime|null $end - * @param bool $expand - * @param string $componentType - * @param string $format - * @param array $properties + * @param string $path + * @param DateTime|null $start + * @param DateTime|null $end + * @param bool $expand + * @param string $componentType + * @param string $format + * @param array $properties * @param ResponseInterface $response + * + * @throws DAV\Exception\NotFound + * @throws VObject\InvalidDataException */ - protected function generateResponse($path, $start, $end, $expand, $componentType, $format, $properties, ResponseInterface $response) { - - $calDataProp = '{' . Plugin::NS_CALDAV . '}calendar-data'; + 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. $queryResult = $calendarNode->calendarQuery([ - 'name' => 'VCALENDAR', + 'name' => 'VCALENDAR', 'comp-filters' => [ [ - 'name' => $componentType, - 'comp-filters' => [], - 'prop-filters' => [], + 'name' => $componentType, + 'comp-filters' => [], + 'prop-filters' => [], 'is-not-defined' => false, - 'time-range' => [ + 'time-range' => [ 'start' => $start, - 'end' => $end, + 'end' => $end, ], ], ], - 'prop-filters' => [], + 'prop-filters' => [], 'is-not-defined' => false, - 'time-range' => null, + 'time-range' => null, ]); // queryResult is just a list of base urls. We need to prefix the // calendar path. $queryResult = array_map( - function($item) use ($path) { - return $path . '/' . $item; + function ($item) use ($path) { + return $path.'/'.$item; }, $queryResult ); $nodes = $this->server->getPropertiesForMultiplePaths($queryResult, [$calDataProp]); unset($queryResult); - } else { $nodes = $this->server->getPropertiesForPath($path, [$calDataProp], 1); } @@ -228,7 +235,7 @@ class ICSExportPlugin extends DAV\ServerPlugin { $calendarTimeZone = null; // We're expanding, and for that we need to figure out the // calendar's timezone. - $tzProp = '{' . Plugin::NS_CALDAV . '}calendar-timezone'; + $tzProp = '{'.Plugin::NS_CALDAV.'}calendar-timezone'; $tzResult = $this->server->getProperties($path, [$tzProp]); if (isset($tzResult[$tzProp])) { // This property contains a VCALENDAR with a single @@ -249,11 +256,11 @@ class ICSExportPlugin extends DAV\ServerPlugin { $filenameExtension = '.ics'; switch ($format) { - case 'text/calendar' : + case 'text/calendar': $mergedCalendar = $mergedCalendar->serialize(); $filenameExtension = '.ics'; break; - case 'application/calendar+json' : + case 'application/calendar+json': $mergedCalendar = json_encode($mergedCalendar->jsonSerialize()); $filenameExtension = '.json'; break; @@ -264,29 +271,29 @@ class ICSExportPlugin extends DAV\ServerPlugin { '', $calendarNode->getName() ); - $filename .= '-' . date('Y-m-d') . $filenameExtension; + $filename .= '-'.date('Y-m-d').$filenameExtension; - $response->setHeader('Content-Disposition', 'attachment; filename="' . $filename . '"'); + $response->setHeader('Content-Disposition', 'attachment; filename="'.$filename.'"'); $response->setHeader('Content-Type', $format); $response->setStatus(200); $response->setBody($mergedCalendar); - } /** * Merges all calendar objects, and builds one big iCalendar blob. * - * @param array $properties Some CalDAV properties + * @param array $properties Some CalDAV properties * @param array $inputObjects + * * @return VObject\Component\VCalendar */ - function mergeObjects(array $properties, array $inputObjects) { - + public function mergeObjects(array $properties, array $inputObjects) + { $calendar = new VObject\Component\VCalendar(); $calendar->VERSION = '2.0'; if (DAV\Server::$exposeVersion) { - $calendar->PRODID = '-//SabreDAV//SabreDAV ' . DAV\Version::VERSION . '//EN'; + $calendar->PRODID = '-//SabreDAV//SabreDAV '.DAV\Version::VERSION.'//EN'; } else { $calendar->PRODID = '-//SabreDAV//SabreDAV//EN'; } @@ -303,41 +310,41 @@ class ICSExportPlugin extends DAV\ServerPlugin { $objects = []; foreach ($inputObjects as $href => $inputObject) { - $nodeComp = VObject\Reader::read($inputObject); foreach ($nodeComp->children() as $child) { - switch ($child->name) { - case 'VEVENT' : - case 'VTODO' : - case 'VJOURNAL' : + case 'VEVENT': + case 'VTODO': + case 'VJOURNAL': $objects[] = clone $child; break; // VTIMEZONE is special, because we need to filter out the duplicates - case 'VTIMEZONE' : + case 'VTIMEZONE': // Naively just checking tzid. - if (in_array((string)$child->TZID, $collectedTimezones)) continue; + if (in_array((string) $child->TZID, $collectedTimezones)) { + break; + } $timezones[] = clone $child; $collectedTimezones[] = $child->TZID; break; - } - } // Destroy circular references to PHP will GC the object. $nodeComp->destroy(); unset($nodeComp); - } - foreach ($timezones as $tz) $calendar->add($tz); - foreach ($objects as $obj) $calendar->add($obj); + foreach ($timezones as $tz) { + $calendar->add($tz); + } + foreach ($objects as $obj) { + $calendar->add($obj); + } return $calendar; - } /** @@ -348,10 +355,9 @@ class ICSExportPlugin extends DAV\ServerPlugin { * * @return string */ - function getPluginName() { - + public function getPluginName() + { return 'ics-export'; - } /** @@ -365,14 +371,12 @@ class ICSExportPlugin extends DAV\ServerPlugin { * * @return array */ - function getPluginInfo() { - + public function getPluginInfo() + { return [ - 'name' => $this->getPluginName(), + 'name' => $this->getPluginName(), 'description' => 'Adds the ability to export CalDAV calendars as a single iCalendar file.', - 'link' => 'http://sabre.io/dav/ics-export-plugin/', + 'link' => 'http://sabre.io/dav/ics-export-plugin/', ]; - } - } |